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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| import java.util.ArrayList; import java.util.Scanner;
public class StudentDemo {
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 sc = new Scanner(System.in); String line = sc.nextLine();
switch (line) { case "1":
addStudent(array); break; case "2":
deleteStudent(array); break; case "3":
updateStudent(array); break; case "4":
findAllStudent(array); break; case "5": System.out.println("谢谢使用");
System.exit(0); } } }
public static void addStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in);
String sid;
while(true) { System.out.println("请输入学生学号"); sid = sc.nextLine();
boolean flag = isUsed(array, sid); if (flag) { System.out.println("你输入的学号已经被使用,请重新输入"); }else{ break; } } System.out.println("请输入学生姓名"); String name = sc.nextLine(); System.out.println("请输入学生年龄"); String age = sc.nextLine(); System.out.println("请输入学生居住地"); String address = sc.nextLine();
Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
public static void findAllStudent(ArrayList<Student> array) { if (array.size() == 0) { System.out.println("无信息,请先添加信息再查询"); return; } System.out.println("学号\t\t姓名\t\t年龄\t\t居住地");
for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress()); } }
public static void deleteStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除的学生的学号"); String sid = sc.nextLine();
int index = -1; for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getSid().equals(sid)) { index = i; break; } } if (index == -1) { System.out.println("该信息不存在,请重新输入"); } else { array.remove(index); System.out.println("删除学生成功"); } }
public static void updateStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改的学生的学号:"); String sid = sc.nextLine();
int index = -1; for (int i = 0; i < array.size(); i++) { Student student = array.get(i); if (student.getSid().equals(sid)) {
index = i; break; } } if (index == -1) { System.out.println("该信息不存在,请重新输入"); } else {
System.out.println("请输入学生的新姓名"); String name = sc.nextLine(); System.out.println("请输入学生的新年龄"); String age = sc.nextLine(); System.out.println("请输入学生的新居住地"); String adress = sc.nextLine();
Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(adress);
if (index == -1) { System.out.println("该信息不存在,请重新输入"); } else { array.set(index, s); System.out.println("修改学生成功"); } } }
public static boolean isUsed(ArrayList<Student> array,String sid){ boolean flag = false;
for (int i=0;i< array.size();i++){ Student s = array.get(i); if (s.getSid().equals(sid)){ flag = true; break; } } return flag; } }
|