import java.util.ArrayList; import java.util.Scanner; /* 学生管理系统 */ public class studentManager { 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": //System.out.println("添加学生"); addstudent(array); break; case "2": //System.out.println("删除学生"); deletestudent(array); break; case "3": //System.out.println("修改学生"); updatestudent(array); break; case "4": //System.out.println("查看所有学生"); findAllstudent(array); break; case "5": System.out.println("谢谢使用!"); System.exit(0);//JVM退出 } } } //定义方法,用于添加学生信息 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\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 ss = array.get(i); if (ss.getSid().equals(sid)) { //键盘录入要修改的学生新信息 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.set(i,s); System.out.println("修改学生信息成功!"); index = i; break; } } if (array.size() == 0 || index == -1) { 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; } }
———————————————————————————————————————————
import com.Cx330.Student; public class student { private String sid;//学号 private String name;//姓名 private String age;//年龄 private String address;//居住地 public student(){} public student(String sid,String name,String age,String address){ this.sid = sid; this.name = name; this.age = age; this.address = address; } public String getSid(){return sid;} public void setSid(String sid){this.sid = sid;} public String getName(){return name;} public void setName(String name){this.name = name;} public String getAge(){return age;} public void setAge(String age){this.age = age;} public String getAddress(){return address;} public void setAddress(String address){this.address = address;} }
———————————————————————————————————————————