1.系统可以保存用户的姓名、学号、性别、年龄、分数
2.系统的最大容量为100
3.系统可以进行信息添加、信息查看、信息修改、信息删除、信息按成绩由高到低排序、信息查询
package mypc;
import java.io.;
import java.util.;
import mypc.Student;
public class Stu {
public static void main(String[] args) throws IOException {
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 choiceNumber = sc.nextLine();
String fileName=“student.txt”;
switch (choiceNumber) {
case "1":findAllStudent(fileName);break;
case "2":addStudent( fileName);break;
case "3":deletStudent(fileName);break;
case "4":updateStudent(fileName);break;
case "5":default:System.out.println("谢谢使用!");
System.exit(0);//
break;
}
}
}
public static void read(String fileName,ArrayListarray) throws IOException{
BufferedReader br=new BufferedReader(new FileReader(fileName));
String line;
while((line=br.readLine())!=null){
String []chs=line.split(",");
Student s=new Student(chs[0],chs[1],chs[2],chs[3]);
array.add(s);
}
br.close();
}
public static void write(String fileName,ArrayListarray) throws IOException{
BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
for (int i = 0; i < array.size(); i++) {
StringBuilder sb=new StringBuilder();
sb.append(array.get(i).getId()).append(",").append(array.get(i).getName()).append(",").append(array.get(i).getAge()).append(",").append(array.get(i).getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
// 编写查看所有学生信息的方法
public static void findAllStudent(String fileName) throws IOException {
ArrayListarray=new ArrayList();
read( fileName,array);
System.out.println(“学号\t\t姓名\t年龄\t居住地”);
for (int i = 0; i < array.size(); i++) {
Student s = new Student();
s = array.get(i);
System.out.println(s.getId() + “\t\t” + s.getName() + “\t” + s.getAge() + “\t” + s.getAddress());
}
}
public static void addStudent(String fileName) throws IOException{
ArrayListarray=new ArrayList();
read( fileName,array);
Scanner sc=new Scanner(System.in);
boolean flag;
String id;
while(true){
System.out.println("请输入学生学号:");
id=sc.nextLine();
flag=false;
for (int i = 0; i < array.size(); i++) {
if(id.equals(array.get(i).getId())){
flag=true;break;
}
}
if(flag){System.out.println("你输入的学号已存在,请重新输入");
}
else{
break;
}
}
if(flag==false){
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(id,name,age,address);
array.add(s);write(fileName,array);
System.out.println("添加学生成功!");
}
}
public static void deletStudent(String fileName) throws IOException{
ArrayListarray=new ArrayList();
read( fileName,array);
Scanner sc=new Scanner(System.in);
System.out.println(“请输入要删除的学号”);
String id=sc.nextLine();
int index=-1;
for (int i = 0; i < array.size(); i++) {
if(id.equals(array.get(i).getId())){
index=i;
}
}
if(index==-1){
System.out.println(“你要删除的学号对应的学生信息不存在”);
return;
}
else{
array.remove(index);write(fileName,array);
System.out.println(“删除成功!”);
}
}
public static void updateStudent(String fileName) throws IOException{
ArrayListarray=new ArrayList();read(fileName,array);
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改的学生学号");
String id=sc.nextLine();
int index=-1;
for (int i = 0; i < array.size(); i++) {
if(id.equals(array.get(i).getId())){
index=i;
}
}
if(index==-1){
System.out.println("没找到你要修改的学生信息");
}
else{
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(id,name,age,address);
array.set(index, s);write(fileName,array);
System.out.println("修改成功!");
}
}
}