import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { 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); case 2 -> delete(Stu); case 3 -> modify(Stu); case 4 -> find(Stu); case 0 ->/*正则表达式 */ { System.out.println("结束"); System.exit(0);/*结束死循环 */ } } FileWriter mm=new FileWriter("D://itcast"); mm.write("ID"+" "+"name"+" "+"sex"+" "+"born"+" " +"status"+" "+"adress"+" "+"number"+" "+"dormitory"); mm.write("\n"); for(student k:Stu){ mm.write(k.getID() + " " + k.getName() + " " + k.getSex() + " " + k.getBorn() + " " + k.getStatus() + " " + k.getAddress() + " " + k.getNumber() + " " + k.getDormitory()); mm.write("\n");mm.flush(); }mm.close();/*利用文件存储 */ }} 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("完成"); } 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); if(s.getID().equals(ID)){ Stu.remove(i);break;}/*删除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 a=0; for( int 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("学号"+"\t\t\t"+"名字"+"\t\t\t"+"性别"+"\t\t\t"+"出生日期"+"\t\t\t"+"政治地位"+"\t\t\t"+"家庭住址"+"\t\t\t"+"电话"+"\t\t\t"+"宿舍号"); for (student s : Stu) /*增强for的循环*/{ System.out.println(s.getID() + "\t\t\t" + s.getName() + "\t\t\t" + s.getSex() + "\t\t\t" + s.getBorn() + "\t\t\t" + s.getStatus() + "\t\t\t" + s.getAddress() + "\t\t\t" + s.getNumber() + "\t\t\t" + 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 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; }/*方法 */ }
将学生信息存储到文件中
最新推荐文章于 2022-11-23 18:53:12 发布