package com.igeek.ioxt;
import javax.swing.;
import java.io.;
import java.util.ArrayList;
import java.util.Scanner;
public class IO学生管理系统 {
private static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws IOException {
ArrayList<Student> list=new ArrayList<>();
String file="student.txt";
while (true) {
System.out.println("=======欢迎进入IO学生管理系统=======");
System.out.println("1.学生信息查询界面:");
System.out.println("2.学生信息添加界面:");
System.out.println("3.学生信息修改界面:");
System.out.println("4.学生信息删除界面: ");
System.out.println("5.退出系统 ");
System.out.println("请输入你的选择:");
int choice = sc.nextInt();
switch (choice) {
case 1:
inquiryStudent(file);
break;
case 2:
addStudent(file);
break;
case 3:
amendStudent(file);
break;
case 4:
delStudent(file);
break;
case 5:
System.exit(0);
default:
System.out.println("你输入的选择错误,请重新选择:");
break;
}
}
}
public static void Read(String str,ArrayList<Student> list) throws IOException {
FileReader fr=new FileReader("student.txt");
BufferedReader br=new BufferedReader(fr);
String line=null;
while ((line=br.readLine())!=null){
String[] srr=line.split(",");
Student student=new Student(srr[0],srr[1],srr[2],srr[3]);
list.add(student);
}
fr.close();
}
public static void Write(String str,ArrayList<Student> list) throws IOException {
FileWriter fw=new FileWriter("student.txt");
BufferedWriter bw=new BufferedWriter(fw);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
StringBuilder sb=new StringBuilder();
sb.append(student.getId()).append(",").append(student.getName()).append(",").append(student.getAge()).append(",").append(student.getAddress());
bw.write(sb.toString());
bw.newLine();
}
bw.close();
}
public static void addStudent(String file) throws IOException {
ArrayList<Student> list=new ArrayList<>();
Read(file,list);
System.out.println("====欢迎来到学生信息添加界面====");
String id;
while (true){
System.out.println("请输入你要添加的学生学号:");
id=sc.next();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getId() == id) {
System.out.println("你输入的学号已经存在,请重新输入");
break;
}
else {
System.out.println("你输入的学号不存在,可以输入");
break;
}
}
break;
}
System.out.println("请输入你要添加的学生的姓名:");
String name=sc.next();
System.out.println("请输入你要添加的学生的年龄:");
String age=sc.next();
System.out.println("请输入你要添加的学生的地址:");
String address=sc.next();
Student student=new Student();
student.setId(id);
student.setName(name);
student.setAge(age);
student.setAddress(address);
list.add(student);
Write(file,list);
System.out.println("恭喜你,学生信息添加成功");
}
public static void inquiryStudent(String file) throws IOException {
ArrayList<Student> list=new ArrayList<>();
Read(file,list);
String id;
System.out.println("请输入你要查询的学生的学号:");
id=sc.next();
if (list.size()==0){
System.out.println("学生系统里面还没有学生信息,请到主界面添加信息");
return;
}
while (true){
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getId() == id) {
System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "地址");
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
break;
}
}
System.out.println("你输入的学号系统中不存在,请到主界面上添加");
break;
}
}
public static void amendStudent(String file) throws IOException {
ArrayList<Student> list=new ArrayList<>();
System.out.println("====欢迎进入学生修改界面====");
String id;
int index = -1;
while (true) {
System.out.println("请输入你要修改的学生学号");
id = sc.next();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getId().equals(id)) {
index = i;
break;
} else {
break;
}
}
if (index == -1) {
System.out.println("你输入的学号错误,请重新输入");
} else {
break;
}
}
Student student1 = new Student();
System.out.print("请输入你要修改的姓名:");
String name = sc.next();
System.out.print("请输入你要修改的年龄:");
String age = sc.next();
System.out.print("请输入你要修改的地址:");
String address = sc.next();
student1.setName(name);
student1.setAge(age);
student1.setAddress(address);
list.set(index, student1);
System.out.println("恭喜你,修改成功");
Write(file,list);
}
public static void delStudent(String file) throws IOException {
ArrayList<Student> list=new ArrayList<>();
System.out.println("欢迎进入删除学生信息界面");
String id;
int index=-1;
if (list.size()==0){
System.out.println("目前还没有学生可供查询,请到主界面添加学生信息");
return;
}
System.out.println("请输入你要删除的学生学号");
id=sc.next();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (student.getId()==id){
index=i;
break;
}
}
if (index==-1){
System.out.println("你输入的学号不存在,请到主界面中添加");
return;
}
list.remove(index);
Write(file,list);
}
}