import java.io.PrintStream; import java.util.*; public class main { public static void main(String[] args) { ArrayList<student> Stu=new ArrayList<>();/*创建学生集合 */ for( ; ; )/*死循环 */{ System.out.println("学生信息处"); System.out.println("1录入学生信息"); System.out.println("2删除学生信息"); System.out.println("3修改学生信息"); System.out.println("4查询学生信息"); System.out.println("0结束"); Scanner s=new Scanner(System.in); int n=s.nextInt(); switch(n)/*数字选择变方法 */{ case 1: add(Stu); break; case 2: delete(Stu); break; case 3: modify(Stu);break; case 4:find(Stu);break; case 0: System.out.println("结束"); System.exit(0);/*结束死循环 */ } } } public static void add(ArrayList<student> Stu)/*添加学生的方法 */{ Scanner sc=new Scanner(System.in); System.out.println("学号"); String ID=sc.nextLine(); System.out.println("名字"); String name=sc.nextLine(); System.out.println("性别"); String sex=sc.nextLine(); System.out.println("出生日期"); String born=sc.nextLine(); System.out.println("政治地位"); String status=sc.nextLine(); System.out.println("家庭住址"); String address=sc.nextLine(); System.out.println("电话"); String number=sc.nextLine(); System.out.println("宿舍号"); String dormitory=sc.nextLine();/*录入学生信息 */ student jia=new student();/*创建学生对象 */ jia.setID(ID); jia.setName(name); jia.setSex(sex); jia.setBorn(born); jia.setStatus(status); jia.setAddress(address); jia.setNumber(number); jia.setDormitory(dormitory); Stu.add(jia); System.out.println("完成"); return; } public static void delete(ArrayList<student> Stu)/*删除学生的方法 */{ Scanner sc = new Scanner(System.in); System.out.println("学号"); String ID = sc.nextLine(); for(int i = 0; i < Stu.size(); i++) { student s = Stu.get(i); Stu.remove(i);/*删除i位置的学生 */ System.out.println("完成"); } } public static void modify(ArrayList<student> Stu)/*更改学生 */{ Scanner sc = new Scanner(System.in); System.out.println("学号"); String ID = sc.nextLine(); System.out.println("名字"); String name=sc.nextLine(); System.out.println("性别"); String sex=sc.nextLine(); System.out.println("出生日期"); String born=sc.nextLine(); System.out.println("政治地位"); String status=sc.nextLine(); System.out.println("家庭住址"); String address=sc.nextLine(); System.out.println("电话"); String number=sc.nextLine(); System.out.println("宿舍号"); String dormitory=sc.nextLine(); student jia=new student(); jia.setID(ID); jia.setName(name); jia.setSex(sex); jia.setBorn(born); jia.setStatus(status); jia.setAddress(address); jia.setNumber(number); jia.setDormitory(dormitory); int i=0,a=0; for( i = 0; i < Stu.size(); i++) { student stu = Stu.get(i); if (stu.getID().equals(ID)) { a=i; break; } }Stu.set(a,jia);/*在a的位置更改学生信息 */ System.out.println("完成"); } public static void find(ArrayList<student> Stu) /*查找所有学生 */{ System.out.println("学号"+" "+"名字"+" "+"性别"+" "+"出生日期"+" "+"政治地位"+" "+"家庭住址"+" "+"电话"+" "+"宿舍号"); for(int i = 0; i < Stu.size(); i++) { student s = Stu.get(i); System.out.println(s.getID()+" "+s.getName()+" "+s.getSex()+" "+s.getBorn()+" "+s.getStatus()+" "+s.getAddress()+" "+s.getNumber()+" "+s.getDormitory()); } } }
class student/*学生类 */{ private String ID;/*学号*/ private String name;/*名字*/ private String sex;/*性别*/ private String born;/*出生日期*/ private String status;/*政治地位*/ private String address;/*地址*/ private String number;/*电话*/ private String dormitory;/*宿舍号*/ /*变量 */ public student() { }/*无参构造 */ public student(String ID, String name, String sex, String born, String status, String address, String number, String dormitory) { this.ID = ID; this.name = name; this.sex = sex; this.born = born; this.status = status; this.address = address; this.number = number; this.dormitory = dormitory; }/*有参构造 */ public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBorn() { return born; } public void setBorn(String born) { this.born = born; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getDormitory() { return dormitory; } public void setDormitory(String dormitory) { this.dormitory = dormitory; }/*方法 */ }