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
|
import java.util.ArrayList; import java.util.Scanner;
public class StudentSystem {
static ArrayList<Student> list = new ArrayList<>(); static{ Student s1 = new Student("01","张三","23","q"); Student s2 = new Student("02","李四","24","w"); Student s3 = new Student("03","王五","25","e"); list.add(s1); list.add(s2); list.add(s3); }
private static final String ADD_STUDENT = "1"; private static final String DELETE_STUDENT = "2"; private static final String UPDATE_STUDENT = "3"; private static final String QUERY_STUDENT = "4"; private static final String EXIT = "5";
public static void startStudentSystem() { loop: 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 choose = sc.next(); switch (choose) { case ADD_STUDENT -> addStudent(list); case DELETE_STUDENT -> deleteStudent(list); case UPDATE_STUDENT -> updateStudent(list); case QUERY_STUDENT -> queryStudent(list); case EXIT -> { System.out.println("退出"); break loop;
} default -> System.out.println("请输入已有的选项"); } } }
public static void addStudent(ArrayList<Student> list){ Scanner sc = new Scanner(System.in); Student s = new Student(); System.out.println("添加学生的信息"); System.out.println("请输入id"); while (true) { String sid = sc.next(); boolean flag = contains(list,sid); if(flag){ System.out.println("id已存在,添加失败,请重新录入"); }else{ s.setId(sid); break; } } System.out.println("请输入姓名"); String sname = sc.next(); s.setName(sname); System.out.println("请输入年龄"); while (true) { String sage = sc.next(); boolean flag = isNum(sage); if(flag){ s.setAge(sage); break; }else { System.out.println("年龄只能为数字,请重新输入年龄。"); } } System.out.println("请输入家庭住址"); String saddress = sc.next(); s.setAddress(saddress); list.add(s); System.out.println("添加成功"); }
public static void deleteStudent(ArrayList<Student> list){ System.out.println("请输入要删除的学生的id"); for(int i = 0;i<list.size();i++){ Scanner sc = new Scanner(System.in); String sid = sc.next(); boolean flag = contains(list,sid); if(flag){ list.remove(getIndex(list,sid)); System.out.println("删除成功"); break; }else{ System.out.println("该用户不存在,删除失败。"); return; } } }
public static void updateStudent(ArrayList<Student> list){ System.out.println("请输入需要修改的学生的id"); Scanner sc = new Scanner(System.in); String sid = sc.next(); boolean flag = contains(list,sid); int index = getIndex(list,sid); if(flag){ System.out.println("请输入需要修改的信息"); System.out.println("修改学生姓名"); String sname = sc.next(); list.get(index).setName(sname); System.out.println("修改学生年龄"); while (true) { String sage = sc.next(); boolean flag2 = isNum(sage); if(flag2){ list.get(index).setAge(sage); break; }else { System.out.println("年龄只能为数字,请重新输入年龄。"); } } System.out.println("修改学生家庭住址"); String saddress = sc.next(); list.get(index).setAddress(saddress); System.out.println("修改完成"); }else{ System.out.println("该学生id不存在"); } }
public static void queryStudent(ArrayList<Student> list){ System.out.println("查询学生"); if(list.size() == 0){ System.out.println("当前无学生信息,请添加后查询。"); return; } System.out.println("id\t姓名\t年龄\t家庭住址"); for (int i = 0;i<list.size();i++){ Student stu = list.get(i); String sid = stu.getId(); String sname = stu.getName(); String sage = stu.getAge(); String saddress = stu.getAddress(); System.out.println(sid+"\t"+sname+"\t"+sage+"\t"+saddress); } }
public static boolean contains(ArrayList<Student> list,String id){
return getIndex(list,id) >= 0; }
public static int getIndex(ArrayList<Student> list,String id) { for (int i = 0; i < list.size(); i++) { if (id.equals(list.get(i).getId())) { return i; } } return -1; }
public static boolean isNum(String age){ for(int i = 0;i<age.length();i++){ char c = age.charAt(i); if((c>='0')&&(c<='9')){ return true; } } return false; } }
|