java 学生管理系统(案例)25
package com.demo.studer; public class stu { private String id; private String name; private int age; private String birthday; public stu() { } public stu(String id, String name, int age, String birthday) { this.id = id; this.name = name; this.age = age; this.birthday = birthday; } /** * 获取 * @return id */ public String getId() { return id; } /** * 设置 * @param id */ public void setId(String id) { this.id = id; } /** * 获取 * @return name */ public String getName() { return name; } /** * 设置 * @param name */ public void setName(String name) { this.name = name; } /** * 获取 * @return age */ public int getAge() { return age; } /** * 设置 * @param age */ public void setAge(int age) { this.age = age; } /** * 获取 * @return birthday */ public String getBirthday() { return birthday; } /** * 设置 * @param birthday */ public void setBirthday(String birthday) { this.birthday = birthday; } public String toString() { return "stu{id = " + id + ", name = " + name + ", age = " + age + ", birthday = " + birthday + "}"; } }
package com.demo.studer; import java.util.ArrayList; public class dame { public static void main(String[] args) { ArrayList<stu> list = new ArrayList<>(); stu list1 = new stu("23", "张三", 26, "1999"); stu list2 = new stu("24", "李四", 26, "1999"); stu list3 = new stu("25", "王五", 26, "1999"); list.add(list1); list.add(list2); list.add(list3); String id = "25"; int index = getIndex(id, list); System.out.println(index); } private static int getIndex(String id, ArrayList<stu> list) { for (int i = 0; i < list.size(); i++) { stu stu = list.get(i); if (stu.getId().equals(id)){ return i; } } return -1; } }
package com.demo.studer; import java.util.ArrayList; import java.util.Scanner; public class SysTem { public static void main(String[] args) { ArrayList<stu> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); stu list1 = new stu("1", "张三", 26, "1999"); stu list2 = new stu("2", "李四", 26, "1999"); stu list3 = new stu("3", "王五", 26, "1999"); list.add(list1); list.add(list2); list.add(list3); 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.退出"); int modify = sc.nextInt(); switch (modify) { case 1: Addlist(list); break; case 2: setList(list); break; case 3: removeList(list); break; case 4: getList(list); break; case 5: System.out.println("退出"); System.exit(0); break; default: System.out.println("您的输入有误"); break; } } } //删除学生信息 private static void removeList(ArrayList<stu> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入您要删除的学生学号:"); String id = sc.nextLine(); int index = getIndex(id, list); if (index == -1) { System.out.println("没有该学生信息"); return; } else { list.remove(index); System.out.println("删除成功"); } } private static void setList(ArrayList<stu> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入您要修改的学生学号:"); String updateId = sc.nextLine(); int index = getIndex(updateId, list); if (index == -1) { System.out.println("没有该学生信息"); return; } else { System.out.println("请输入新的学生名字:"); String name = sc.next(); System.out.println("请输入新的学生年龄:"); int age = sc.nextInt(); System.out.println("请输入新的学生生日:"); String birthday = sc.next(); stu stu = new stu(updateId, name, age, birthday); list.set(index, stu); } } // 查询学生 private static void getList(ArrayList<stu> list) { if (list.size() == 0) { System.out.println("查无信息!"); } else { System.out.println("学号\t\t\t姓名\t年龄\t生日"); for (int i = 0; i < list.size(); i++) { stu s = list.get(i); System.out.println("学号" + s.getId() + "姓名" + s.getName() + "年龄" + s.getAge() + "生日" + s.getBirthday()); } } } //添加学生信息 private static void Addlist(ArrayList<stu> list) { Scanner sc = new Scanner(System.in); System.out.println("请输入新的学生编号:"); String id = ""; while (true) { id = sc.next(); int index = getIndex(id, list); if (index == -1) { break; } else { System.out.println("您输入的学号已被占用,请重新输入:"); } } System.out.println("请输入新的学生名字:"); String name = sc.next(); System.out.println("请输入新的学生年龄:"); int age = sc.nextInt(); System.out.println("请输入新的学生生日:"); String birthday = sc.next(); stu stu = new stu(id, name, age, birthday); list.add(stu); System.out.println("添加成功"); } // 根据索引查询学生信息 private static int getIndex(String id, ArrayList<stu> list) { for (int i = 0; i < list.size(); i++) { stu stu = list.get(i); if (stu.getId().equals(id)) { return i; } } return -1; } }
代码改变了我们,也改变了世界