package XianMu.XianMuIoStudent;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class DemoTest {
public static void main(String[] args) throws IOException {
ArrayList<Student> array=new ArrayList <>();
// showStudent();
// FileWriter fw=new FileWriter("F:\\untitledxuexi\\fr.txt");
// FileReader fr=new FileReader("F:\\untitledxuexi\\fr.txt");
// BufferedWriter bw=new BufferedWriter(fw);
// BufferedReader br=new BufferedReader(fr);
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 sh = sc.nextLine();
switch (sh) {
case "1":
readData(array);
showStudent(array);
break;
case "2":
addStudent(array);
writeData(array);
break;
case "3":
readData(array);
deleteStudent(array);
writeData(array);
break;
case "4":
readData(array);
updateStudent(array);
writeData(array);
break;
case "5":
default:
System.out.println("谢谢使用");
System.exit(0);
break;
}
}
}
//从文件读入集合
public static void readData(ArrayList<Student> array) throws IOException{
BufferedReader br=new BufferedReader(new FileReader("F:\\untitledxuexi\\fr.txt"));
String s;
while ((s=br.readLine())!=null){
String st[]=s.split(",");
Student student=new Student();
student.setId(st[0]);
student.setName(st[1]);
student.setAge(st[2]);
student.setAddress(st[3]);
array.add(student);
}
br.close();
}
//把集合中的数据写入文件
public static void writeData(ArrayList<Student > array) throws IOException{
BufferedWriter bw=new BufferedWriter(new FileWriter("F:\\\\untitledxuexi\\\\fr.txt"));
for (int i = 0; i < array.size(); i++) {
Student s=array.get(i);
StringBuilder sb=new StringBuilder();
sb.append(s.getId()).append(",").append(s.getName()).append(",").
append(s.getAge()).append(",").append(s.getAddress());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
}
public static void showStudent(ArrayList<Student> array){
//首先判断这个集合是否有数据,并且让方法停止
if (array.size()==0){
System.out.println("不好意思目前木有学生");
return;
}
System.out.println("名字\t学号\t年龄\t地址");
for (int i = 0; i < array.size(); i++) {
Student student=array.get(i);
System.out.println(student.getName()+"\t\t"+student.getId()+"\t\t"+student.getAge()+"\t\t"+student.getAddress());
}
}
public static void addStudent(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
Student stu=new Student();
String id;
while(true) {
System.out.println("学号");
id = sc.nextLine();
// stu.setId(sc.nextLine());
//判断学号是否被占用
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
if (array.get(i).getId().equals(id)) {
flag = true; //说明学号被占用
}
}
if (flag == true) {
System.out.println("已经有学号了请重新输入");
}
else {
break;
}
}
stu.setId(id);
System.out.println("名字");
stu.setName(sc.nextLine());
System.out.println("年龄");
stu.setAge(sc.nextLine());
System.out.println("地址");
stu.setAddress(sc.nextLine());
array.add(stu);
System.out.println("添加成功");
}
public static void deleteStudent(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("请输入要删除的学生的学号");
String id = sc.nextLine();
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
if (array.get(i).getId().equals(id)) {
array.remove(i);
flag = true;
System.out.println("删除成功");
break;
}
}
if (flag==false) {
System.out.println("系统中木有这个学生请重写输入");
}
else {break;}
}
}
public static void updateStudent(ArrayList<Student> array){
Scanner sc=new Scanner(System.in);
while (true) {
System.out.println("请输入要修改的学生的学号");
String id = sc.nextLine();
boolean flag = false;
for (int i = 0; i < array.size(); i++) {
if (array.get(i).getId().equals(id)) {
array.remove(i);
addStudent(array);
flag = true;
System.out.println("修改成功");
break;
}
}
if (flag==false) {
System.out.println("系统中木有这个学生请重写输入");
}
else {break;}
}
}
}
删除线格式
package XianMu.XianMuIoStudent;
public class Student {
private String name;
private String id;
private String age;
private String address;
public Student() {
}
public Student(String name, String id, String age, String address) {
this.name = name;
this.id = id;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}