ArrayList练习-学生管理系统

学生类

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
package Student;

public class Student {
private String name;
private String sex;
private String id;
private String address;

public Student(){}

public Student(String name, String id,String sex, String address) {
super();
this.name = name;
this.sex = sex;
this.id = id;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

}

测试类

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
package Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentMan {
public static void main(String[] args) {
ArrayList<Student> array=new ArrayList<Student>();
while(true){
System.out.println("----------欢迎进入学生管理系统----------");
System.out.println(" 1.添加学生 ");
System.out.println(" 2.删除学生 ");
System.out.println(" 3.修改学生 ");
System.out.println(" 4.查看所有学生 ");
System.out.println(" 5.退出 ");
System.out.println(" 请输入你的选择: ");
Scanner s=new Scanner(System.in);
int num=s.nextInt();
switch(num){
case 1:
addStudent(array);
break;
case 2:
deleteStu(array);
break;
case 3:
alert(array);
break;
case 4:
selectStu(array);
break;
case 5:
System.out.println("感谢使用");
System.exit(0);//JVM退出

}
}
}
public static void addStudent(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
System.out.println("请输入姓名");
String name=sc.nextLine();
System.out.println("请输入学号");
String id=sc.nextLine();
System.out.println("请输入性别");
String sex=sc.nextLine();
System.out.println("请输入居住地");
String address=sc.nextLine();

if(isUsed(array,id)){
System.out.println("该学号已经存在,添加失败");
return;
}
Student stu=new Student(name,id,sex,address);

System.out.println(array.add(stu)?"添加成功!":"添加失败");
}
public static boolean isUsed(ArrayList<Student> array,String id){
boolean flag=false;
for(int i=0;i<array.size();i++){
Student stu=array.get(i);
if(stu.getId().equals(id)){
flag=true;
break;
}
}
return flag;
}
public static void selectStu(ArrayList<Student> array){
if(array.size()==0){
System.out.println("无信息,请先添加信息");
return;
}
System.out.println("学号\t姓名\t性别\t居住地");
Student stu=new Student();
for(int i=0;i<array.size();i++){
Student s=array.get(i);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getSex()+"\t"+s.getAddress());
}

}
public static void deleteStu(ArrayList<Student> array){
Scanner s=new Scanner(System.in);
System.out.println("请输入要删除的学生id");
String id =s.nextLine();
for(int i=0;i<array.size();i++){
Student stu=array.get(i);
if(stu.getId().equals(id)){
array.remove(i);
System.out.println("删除成功!");
}else{
System.out.println("您的输入有误");
}
}



}
public static void alert(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String stuid=sc.nextLine();
for(int i=0;i<array.size();i++){
Student student=array.get(i);
if(student.getId().equals(stuid)){

}else{
System.out.println("该学生不存在");
return;


}
}
System.out.println("姓名:");
String name=sc.nextLine();
System.out.println("学号:");
String id=sc.nextLine();
System.out.println("性别:");
String sex=sc.nextLine();
System.out.println("居住地:");
String address=sc.nextLine();
Student stu=new Student(name,id,sex,address);

for(int i=0;i<array.size();i++){

array.set(i, stu);
System.out.println("修改成功!");
}

}
}

请我喝杯咖啡吧~

支付宝
微信