-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek07
173 lines (148 loc) · 3.21 KB
/
week07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package week7;
public class Student {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "\t" + id + "\t" + name + "\n";
}
public Student(String id,String name){
this.id=id;
this.name=name;
}
}
package week7;
public class Subject {
Student list[];
String id;
String name;
int maxsize;
public Subject(String id,String name,int max){
this.id=id;
this.name=name;
list=new Student[max];
maxsize=0;
}
public boolean add(Student student){
if (student ==null){
return false;
}
if(list.length<=maxsize){
return false;
}
list[maxsize]=student;
maxsize++;
return true;
}
public boolean remove(int index){
int n=maxsize-1;
if(index>=maxsize||index<0){
return false;
}
for(int i=index;i<n;i++){
list[i]=list[i+1];
}
list[n]=null;
return true;
}
public int indexOf(String id){
for(int i=0;i<list.length && list[i]!=null;i++){
if(list[i].getId().equals(id))
return i;
}
return -1;
}
public Student get(int index){
return list[index];
}
public int size(){
return maxsize;
}
public String tostring(){
String info="";
info+=id+"\t"+name+"\t\n";
for(int i=0;i<list.length&&list[i]!=null;i++){
info+=list[i].toString()+"\n";
}
return info;
}
}
package week7;
import java.util.Scanner;
public class Driver {
private static Subject S1;
public static int meau(){
System.out.println("check class system");
System.out.println("1.creat new subject");
System.out.println("2.add a student");
System.out.println("3.quit the subject");
System.out.println("4.print subject details");
System.out.println("5.exit");
System.out.println("please choose your choice(1~5)");
Scanner scan=new Scanner(System.in);
int t=scan.nextInt();
return t;
}
public static void creatsubject(){
System.out.println("enter the subject id");
Scanner scan=new Scanner(System.in);
String subid=scan.next();
System.out.println("enter the subject name");
String name =scan.next();
System.out.println("enter the maxsize of the subject");
int maxsize=scan.nextInt();
S1=new Subject(subid,name,maxsize);
}
public static void addstudent(){
System.out.println("enter the student id");
Scanner scan=new Scanner(System.in);
String stuid=scan.next();
System.out.println("enter the student name");
String stuname =scan.next();
Student s1=new Student(stuid,stuname);
S1.add(s1);
}
public static void quit(){
System.out.println("enter the student id");
Scanner scan=new Scanner(System.in);
String stuid=scan.next();
System.out.println("enter the student name");
String stuname =scan.next();
S1.indexOf(stuid);
S1.remove(S1.indexOf(stuid));
}
public static void display(){
System.out.println(S1.tostring());
}
public static void main(String[] args) {
int o;
while(true){
o=meau();
if(o==5){
System.out.println("bye");
break;
}
switch(o){
case 1:creatsubject();
break;
case 2:addstudent();
break;
case 3:quit();
break;
case 4:display();
break;
}
}
}
}