public class students { private String id; private String name; private String age; private String address; private int[] score=new int[3]; public students() { } public students(String id, String name, String age, String address,int arr[]) { this.id = id; this.name = name; this.age = age; this.address = address; for(int i=0;i<3;i++) { this.score[i]=arr[i]; } } 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 getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public void setScore(int arr[]) { for(int i=0;i<3;i++) { this.score[i]=arr[i]; } } public int getChineseScore() { return score[0]; } public void setChineseScore(int score) { this.score[0]=score; } public int getMathScore() { return score[1]; } public void setMathScore(int score) { this.score[1]=score; } public int getEnglishScore() { return score[2]; } public void setEnglishScore(int score) { this.score[2]=score; } }
import java.util.ArrayList; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.*; public class StuMange { public static void main(String[] args) throws IOException { ArrayList<students> array=new ArrayList<students>(); LoadAllInfo(array); 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("6 学号排序"); System.out.println("7 退出"); System.out.println("请输入你的选择:"); Scanner sc = new Scanner(System.in); String choiceString = sc.nextLine(); switch (choiceString) { case "1": findAllStudent(array); break; case "2": addStudent(array); break; case "3": deleteStudent(array); break; case "4": updateStudent(array); break; case "5": default: System.out.println("谢谢你的使用 "); System.exit(0); break; } } } private static void LoadAllInfo(ArrayList<students>arrayList) throws IOException { BufferedReader br = new BufferedReader(new FileReader("student.txt")); String line; while((line=br.readLine())!=null) { String [] strAString=line.split(","); students s2=new students(); s2.setId(strAString[0]); s2.setName(strAString[1]); s2.setAge(strAString[2]); s2.setAddress(strAString[3]); s2.setChineseScore(Integer.parseInt(strAString[4])); s2.setMathScore(Integer.parseInt(strAString[5])); s2.setEnglishScore(Integer.parseInt(strAString[6])); arrayList.add(s2); } } private static void updateStudent(ArrayList<students> array) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("请输入你要修改学生的学号:"); String id = sc.nextLine(); int index = -1; for (int x = 0; x < array.size(); x++) { students s = array.get(x); if (s.getId().equals(id)) { index = x; break; } } if (index != -1) { System.out.println("请输入新姓名:"); String name = sc.nextLine(); System.out.println("请输入新年龄:"); String age = sc.nextLine(); System.out.println("请输入新地址:"); String address = sc.nextLine(); System.out.println("请输入新的成绩单(语数外)"); int arr[]=new int [3]; for(int i=0;i<3;i++) { arr[i]=sc.nextInt(); } students s = new students(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); s.setScore(arr); array.set(index, s); System.out.println("修改学生成功"); RefreshInfo(array); } else { System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新选择你的操作"); return; } } private static void deleteStudent(ArrayList<students> array) throws IOException { Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除学生的学号:"); String id = sc.nextLine(); int index = -1; for (int x = 0; x < array.size(); x++) { students s = array.get(x); if (s.getId().equals(id)) { index = x; break; } } if (index != -1) { array.remove(index); RefreshInfo(array); System.out.println("删除学生成功"); } else { System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新选择你的操作"); return; } } private static void RefreshInfo(ArrayList<students> array) throws IOException { BufferedWriter bW=new BufferedWriter(new FileWriter("student.txt")); for(int i=0;i<array.size();i++) { StringBuilder strbuilder=new StringBuilder(); students s1=array.get(i); strbuilder.append(s1.getId()).append(","). append(s1.getName()).append(","). append(s1.getAge()).append(","). append(s1.getAddress()).append(","). append(s1.getChineseScore()).append(","). append(s1.getMathScore()).append(","). append(s1.getEnglishScore()); bW.write(strbuilder.toString()); bW.newLine(); bW.flush(); } bW.close(); } private static void addStudent(ArrayList<students> array) throws IOException { Scanner sc = new Scanner(System.in); String id; while (true) { System.out.println("请输入学号:"); id = sc.nextLine(); boolean flag = false; for (int x = 0; x < array.size(); x++) { students s = array.get(x); if (s.getId().equals(id)) { flag = true; } } if (flag == true) { 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(); System.out.println("请输入语文,数学,英语的成绩"); int arr[]=new int[3]; for(int i=0;i<3;i++) { arr[i]=sc.nextInt(); } students s = new students(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); s.setScore(arr); array.add(s); RefreshInfo(array); System.out.println("添加学生成功"); } private static void findAllStudent(ArrayList<students> array) { if (array.size() == 0) { System.out.println("不好意思,目前没有学生信息可供查看,请回去重新选择你的操作"); return; } System.out.println("学号\t姓名\t年龄\t居住地\t语文\t数学\t英语"); for (int x = 0; x < array.size(); x++) { students s = array.get(x); System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" +s.getAddress()+"\t"+s.getChineseScore()+"\t"+s.getMathScore()+"\t"+s.getEnglishScore()); } } }