史上最强学生管理系统之ArrayList版

  其实不管是网上或者培训班,都会有学生管理系统的最基础版本,本人也不过是照猫画虎,在某些细节方面进行了一些渲染,使这个最基本的小程序更加人性化和便于利于操作一点,个人愚见,大牛勿喷,欢迎转载(请注明出处,谢谢合作!)。

  个人写的小东西,欢迎大家指出其中的不足之处,本人一定虚心接受并改正,入门小白,写些小东西为了自己能印象更深刻,纪念一下自己的劳动成果,也希望能对其中一些对于IT行业有憧憬的新人有些许帮助。

 

这是程序主类:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 
  5 public class BestStuManagerArrayList {
  6     public static void main(String[] args) {
  7         Scanner scanner = new Scanner(System.in);
  8         ArrayList<Student> array = new ArrayList<Student>();
  9 
 10         while (true) {// 为的是每次操作之后都能回到这个页面
 11             System.out.println("********欢迎使用学生管理系统********");
 12             System.out.println("请选择您要进行的操作:1.查询 2.添加 3.修改 4.删除 5.退出");
 13             String choiceNum = scanner.nextLine();
 14             switch (choiceNum) {
 15             case "1":
 16                 // 查询学生信息
 17                 findStudent(array);
 18                 break;
 19             case "2":
 20                 // 添加学生信息
 21                 addStudent(array);
 22                 break;
 23             case "3":
 24                 // 修改学生信息
 25                 updateStudent(array);
 26                 break;
 27             case "4":
 28                 // 删除学生信息
 29                 deleteStudent(array);
 30                 break;
 31             case "5":
 32                 // 退出
 33                 // 因为选择退出跟选择除了1234是一样的,所以用case穿透
 34             default:
 35                 System.out.println("谢谢您的使用,再见!");
 36                 System.exit(0);// 退出系统,同时也退出了整个while无限循环
 37                 break;
 38             }
 39         }
 40     }
 41 
 42     /**
 43      * 这是删除学生的方法
 44      * @param array
 45      */
 46     public static void deleteStudent(ArrayList<Student> array) {
 47         Scanner scanner = new Scanner(System.in);
 48         boolean flag = true;
 49 
 50         while (flag) {
 51             System.out.println("请输入你想删除信息学生的学号");
 52             String id = scanner.nextLine();
 53 
 54             // 这里需要讨论到底有没有输入的这个学号的这个学生
 55 
 56             int index = -1;// 定义变量index为-1
 57             for (int i = 0; i < array.size(); i++) {
 58                 Student stu = array.get(i);
 59                 if (id.equals(stu.getSid())) {// 如果输入的id跟集合中学生的id有一样的,那么把这个学生的id赋值给index
 60                     index = i;
 61                     break;
 62                 }
 63             }
 64             if (index != -1) {// 如果不等于-1,说明进去过上面的判断条件,也就是有输入的id的学生,所以我们修改这个学生的信息就行了
 65                 System.out.println("有你需要的学生信息,请问是否确认删除? 1.(是) 2.(否,重新选择操作) 其他.(退出)");
 66                 String choice = scanner.nextLine();
 67                 if (choice.equals("1")) {
 68                     array.remove(index);
 69                     System.out.println("学生信息删除成功");
 70                 } else if (choice.equals("2")) {
 71                     return;
 72                 } else {
 73                     System.out.println("谢谢使用,再见");
 74                     System.exit(0);
 75                 }
 76                 flag = false;
 77             } else {
 78                 System.out.println("您想删除信息的学生不存在,您可以选择:1.(重新输入) 2.(重新选择操作) 其他:(退出系统)");
 79                 String choice = scanner.nextLine();
 80                 if (choice.equals("1")) {
 81 
 82                 } else if (choice.equals("2")) {
 83                     return;
 84                 } else {
 85                     System.out.println("谢谢使用,再见");
 86                     System.exit(0);
 87                 }
 88             }
 89         }
 90 
 91     }
 92 
 93     /**
 94      * 这是修改集合内学生信息的方法
 95      * 
 96      * @param array
 97      */
 98     public static void updateStudent(ArrayList<Student> array) {
 99         Scanner scanner = new Scanner(System.in);
100         boolean flag = true;
101 
102         while (flag) {
103             System.out.println("请输入你想修改信息学生的学号");
104             String id = scanner.nextLine();
105 
106             // 这里需要讨论到底有没有输入的这个学号的这个学生
107 
108             int index = -1;// 定义变量index为-1
109             for (int i = 0; i < array.size(); i++) {
110                 Student stu = array.get(i);
111                 if (id.equals(stu.getSid())) {// 如果输入的id跟集合中学生的id有一样的,那么把这个学生的id赋值给index
112                     index = i;
113                     break;
114                 }
115             }
116             if (index != -1) {// 如果不等于-1,说明进去过上面的判断条件,也就是有输入的id的学生,所以我们修改这个学生的信息就行了
117                 System.out.println("有你查找的学生信息,请问需要修改哪一项:1.(全部) 2.(姓名) 3.(年龄)  4.(居住地) 5.(放弃修改,重新选择操作) 6.(退出)");
118                 String choice = scanner.nextLine();
119                 if (choice.equals("1")) {
120                     Student sNew = new Student();
121                     System.out.println("请输入学生姓名");
122                     String name = scanner.nextLine();
123                     System.out.println("请输入学生年龄");
124                     String age = scanner.nextLine();
125                     System.out.println("请输入学生居住地 ");
126                     String address = scanner.nextLine();
127                     sNew.setSid(id);
128                     sNew.setName(name);
129                     sNew.setAge(age);
130                     sNew.setAddress(address);
131                     array.set(index, sNew);
132                     System.out.println("学生信息全部更新成功");
133                 } else if (choice.equals("2")) {
134                     Student s = array.get(index);
135                     System.out.println("请输入修改后的姓名");
136                     String name = scanner.nextLine();
137                     s.setName(name);
138                     System.out.println("学生姓名信息更新成功");
139                 } else if (choice.equals("3")) {
140                     Student s = array.get(index);
141                     System.out.println("请输入修改后的年龄");
142                     String age = scanner.nextLine();
143                     s.setAge(age);
144                     System.out.println("学生年龄信息更新成功");
145                 } else if (choice.equals("4")) {
146                     Student s = array.get(index);
147                     System.out.println("请输入修改后的居住地");
148                     String address = scanner.nextLine();
149                     s.setAddress(address);
150                     System.out.println("学生居住地信息更新成功");
151                 } else if (choice.equals("5")) {
152                     return;
153                 } else {
154                     System.out.println("谢谢使用,再见");
155                     System.exit(0);
156                 }
157                 flag = false;
158             } else {
159                 System.out.println("您想修改的学生不存在,您可以选择:1.(重新输入) 2.(重新选择操作) 其他:(退出系统)");
160                 String choice = scanner.nextLine();
161                 if (choice.equals("1")) {
162 
163                 } else if (choice.equals("2")) {
164                     return;
165                 } else {
166                     System.out.println("谢谢使用,再见");
167                     System.exit(0);
168                 }
169             }
170         }
171     }
172 
173     /**
174      * 这是查询所有学生信息的方法
175      * 
176      * @param array
177      *            需要被查询的学生信息的集合
178      */
179     public static void findStudent(ArrayList<Student> array) {
180         Scanner scanner = new Scanner(System.in);
181         // 如果现在没有录入学生信息
182         if (array.size() <= 0) {
183             System.out.println("现在还没有学生信息,您可以选择:1.(重新选择操作) 其他.(退出系统)");
184             String choice = scanner.nextLine();
185             if (choice.equals("1")) {
186                 return;
187             } else {
188                 System.out.println("谢谢使用,再见");
189                 System.exit(0);
190             }
191         }
192 
193         System.out.println("学号\t姓名\t年龄\t居住地");
194         for (int i = 0; i < array.size(); i++) {
195             Student stu = array.get(i);
196             System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
197 
198         }
199     }
200 
201     /**
202      * 这是向ArrayList集合中添加学生的方法
203      * 
204      * @param array
205      *            需要被操作的ArrayList集合
206      */
207     public static void addStudent(ArrayList<Student> array) {
208         Student stu = new Student();
209         Scanner scanner = new Scanner(System.in);
210         String id;
211         while (true) {
212             boolean flag = false;// 定义flag标记学号是否冲突
213             System.out.println("请输入学生学号:");
214             id = scanner.nextLine();
215             for (int i = 0; i < array.size(); i++) {
216                 if (id.equals(array.get(i).getSid())) {
217                     System.out.println("您添加的学号已经存在,您可以选择:1.(重新输入学号) 2.(重新选择操作) 其他.(退出系统)");
218                     String choice = scanner.nextLine();
219                     if (choice.equals("1")) {
220                         flag = true;
221                         break;
222                     } else if (choice.equals("2")) {
223                         return;
224                     } else {
225                         System.out.println("谢谢使用,再见");
226                         System.exit(0);
227                     }
228                 }
229             }
230             if (flag == false) {
231                 break;
232             }
233         }
234         System.out.println("请输入学生姓名:");
235         String name = scanner.nextLine();
236         System.out.println("请输入学生年龄:");
237         String age = scanner.nextLine();
238         System.out.println("请输入学生居住地:");
239         String address = scanner.nextLine();
240         stu.setSid(id);
241         stu.setName(name);
242         stu.setAge(age);
243         stu.setAddress(address);
244         array.add(stu);
245         System.out.println("添加学生信息成功");
246     }
247 }

 

学生类(其实自己可以往里面添加很多字段,本人只是做个演示而已):

 1 public class Student {
 2     private String sid;
 3     private String name;
 4     private String age;
 5     private String address;
 6 
 7     public Student() {
 8         super();
 9     }
10 
11     public Student(String sid, String name, String age, String address) {
12         super();
13         this.sid = sid;
14         this.name = name;
15         this.age = age;
16         this.address = address;
17     }
18 
19     public String getSid() {
20         return sid;
21     }
22 
23     public void setSid(String sid) {
24         this.sid = sid;
25     }
26 
27     public String getName() {
28         return name;
29     }
30 
31     public void setName(String name) {
32         this.name = name;
33     }
34 
35     public String getAge() {
36         return age;
37     }
38 
39     public void setAge(String age) {
40         this.age = age;
41     }
42 
43     public String getAddress() {
44         return address;
45     }
46 
47     public void setAddress(String address) {
48         this.address = address;
49     }
50     
51 
52 }

 

posted on 2017-11-20 22:58  程序猿伊桑  阅读(995)  评论(0编辑  收藏  举报

导航