1 package day9.student_management_system;
2
3 /*
4 快捷键:alt+insert,快速生成构造方法、set&get等
5 */
6
7 public class Student {
8 private String sid;
9 private String name;
10 private String age;
11 private String address;
12
13 public Student() {
14 }
15
16 public Student(String sid, String name, String age, String address) {
17 this.sid = sid;
18 this.name = name;
19 this.age = age;
20 this.address = address;
21 }
22
23 public void setSid(String sid) {
24 this.sid = sid;
25 }
26
27 public void setName(String name) {
28 this.name = name;
29 }
30
31 public void setAge(String age) {
32 this.age = age;
33 }
34
35 public void setAddress(String address) {
36 this.address = address;
37 }
38
39 public String getSid() {
40 return sid;
41 }
42
43 public String getName() {
44 return name;
45 }
46
47 public String getAge() {
48 return age;
49 }
50
51 public String getAddress() {
52 return address;
53 }
54 }
1 package day9.student_management_system;
2
3 import java.util.ArrayList;
4 import java.util.Scanner;
5
6 public class StudentManager {
7
8 public static void main(String[] args) {
9 ArrayList<Student> arrayList = new ArrayList<Student>();
10
11 while (true){
12 System.out.println("-----欢迎来到学生管理系统-----");
13 System.out.println("1 添加学生");
14 System.out.println("2 删除学生");
15 System.out.println("3 修改学生");
16 System.out.println("4 查看学生");
17 System.out.println("5 退出");
18
19 System.out.println("请输入你的选择:");
20 Scanner sc = new Scanner(System.in);
21
22 String line = sc.nextLine();
23 switch (line){
24 case "1":
25 addStudent(arrayList);
26 break;
27 case "2":
28 deleteStudent(arrayList);
29 break;
30 case "3":
31 updateStudent(arrayList);
32 break;
33 case "4":
34 showAllStudent(arrayList);
35 break;
36 case "5":
37 System.exit(0); //JVM退出
38 default:
39 System.out.println("输入错误");
40 break;
41 }
42 }
43 }
44
45 public static boolean isUsed(ArrayList<Student> arrayList, String sid){
46 boolean flag = false;
47
48 for (int i=0; i<arrayList.size(); i++){
49 Student s = arrayList.get(i);
50 if(s.getSid().equals(sid)){
51 flag = true;
52 break;
53 }
54 }
55
56 return flag;
57 }
58
59 /*public static void addStudent(ArrayList<Student> arrayList){
60 Scanner sc = new Scanner(System.in);
61
62 System.out.println("请输入学生学号:");
63 String sid = sc.nextLine();
64 System.out.println("请输入学生姓名:");
65 String name = sc.nextLine();
66 System.out.println("请输入学生年龄:");
67 String age = sc.nextLine();
68 System.out.println("请输入学生地址:");
69 String address = sc.nextLine();
70
71 Student s = new Student();
72 s.setSid(sid);
73 s.setName(name);
74 s.setAge(age);
75 s.setAddress(address);
76
77 arrayList.add(s);
78 System.out.println("添加学生成功");
79 }*/
80
81 public static void addStudent(ArrayList<Student> arrayList){
82 Scanner sc = new Scanner(System.in);
83
84 String sid;
85 while (true){
86 System.out.println("请输入学生学号:");
87 sid = sc.nextLine();
88
89 boolean flag = isUsed(arrayList, sid);
90 if(flag){
91 System.out.println("该学号已存在");
92 }else {
93 break;
94 }
95 }
96 System.out.println("请输入学生姓名:");
97 String name = sc.nextLine();
98 System.out.println("请输入学生年龄:");
99 String age = sc.nextLine();
100 System.out.println("请输入学生地址:");
101 String address = sc.nextLine();
102
103 Student s = new Student();
104 s.setSid(sid);
105 s.setName(name);
106 s.setAge(age);
107 s.setAddress(address);
108
109 arrayList.add(s);
110 System.out.println("添加学生成功");
111 }
112
113 public static void showAllStudent(ArrayList<Student> arrayList){
114 if(arrayList.size() == 0){
115 System.out.println("暂无记录");
116 return;
117 }
118
119 System.out.println("学号\t\t姓名\t\t年龄\t\t地址");
120 for (int i=0; i<arrayList.size(); i++){
121 Student s = arrayList.get(i);
122 System.out.println(s.getSid() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "\t\t" + s.getAddress());
123 }
124 }
125
126 /*public static void deleteStudent(ArrayList<Student> arrayList){
127 Scanner sc = new Scanner(System.in);
128 System.out.println("请输入待删除学生的学号:");
129 String sid = sc.nextLine();
130
131 for (int i=0; i<arrayList.size(); i++){
132 Student s = arrayList.get(i);
133 if(s.getSid().equals(sid)){
134 arrayList.remove(i);
135 break;
136 }
137 }
138
139 System.out.println("删除成功");
140 }*/
141
142 public static void deleteStudent(ArrayList<Student> arrayList){
143 Scanner sc = new Scanner(System.in);
144 System.out.println("请输入待删除学生的学号:");
145 String sid = sc.nextLine();
146
147 int index = -1;
148 for (int i=0; i<arrayList.size(); i++){
149 Student s = arrayList.get(i);
150 if(s.getSid().equals(sid)){
151 index = i;
152 break;
153 }
154 }
155
156 if(index == -1){
157 System.out.println("该信息不存在");
158 }else {
159 arrayList.remove(index);
160 System.out.println("删除成功");
161 }
162 }
163
164 /*public static void updateStudent(ArrayList<Student> arrayList){
165 Scanner sc = new Scanner(System.in);
166 System.out.println("请输入待修改学生的学号:");
167 String sid = sc.nextLine();
168
169 System.out.println("请输入姓名:");
170 String name = sc.nextLine();
171 System.out.println("请输入年龄:");
172 String age = sc.nextLine();
173 System.out.println("请输入地址:");
174 String address = sc.nextLine();
175
176 Student s = new Student();
177 s.setSid(sid);
178 s.setName(name);
179 s.setAge(age);
180 s.setAddress(address);
181
182 for (int i=0; i<arrayList.size(); i++){
183 Student ss = arrayList.get(i);
184 if(ss.getSid().equals(sid)){
185 arrayList.set(i, s);
186 break;
187 }
188 }
189
190 System.out.println("修改成功");
191 }*/
192
193 public static void updateStudent(ArrayList<Student> arrayList){
194 Scanner sc = new Scanner(System.in);
195 System.out.println("请输入待修改学生的学号:");
196 String sid = sc.nextLine();
197
198 int index = -1;
199 for (int i=0; i<arrayList.size(); i++){
200 Student s = arrayList.get(i);
201 if(s.getSid().equals(sid)){
202 index = i;
203 break;
204 }
205 }
206
207 if(index == -1){
208 System.out.println("该信息不存在");
209 }else {
210 System.out.println("请输入姓名:");
211 String name = sc.nextLine();
212 System.out.println("请输入年龄:");
213 String age = sc.nextLine();
214 System.out.println("请输入地址:");
215 String address = sc.nextLine();
216
217 Student student = new Student();
218 student.setSid(sid);
219 student.setName(name);
220 student.setAge(age);
221 student.setAddress(address);
222
223 arrayList.set(index, student);
224 System.out.println("修改成功");
225 }
226 }
227
228 }