import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) throws IOException{
menu();
}
public static void menu()throws IOException{
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);
int n= sc.nextInt();
while (true){
switch (n){
case 1:
addStudent();
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
System.out.println("请输入你选择序号");
break;
case 2:
deleteStudent();
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
System.out.println("请输入你选择序号");
break;
case 3:
chaKanStudent();
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
System.out.println("请输入你选择序号");
break;
case 4:
xiuGaiStudent();
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
System.out.println("请输入你选择序号");
break;
case 5:
System.out.println("谢谢使用学生管理系统!");
System.exit(-1);
default:
System.out.println("请输入正确的序号");
System.out.println("序号的范围是:1~5");
System.out.println("1.增加学生");
System.out.println("2.删除学生");
System.out.println("3.查看学生信息");
System.out.println("4.修改学生信息");
System.out.println("5.退出");
System.out.println("请输入你选择序号");
break;
}
n= sc.nextInt();
}
}
//增加学生信息
public static void addStudent()throws IOException {
//创建字符缓冲输出流,写数据
Scanner sc1=new Scanner(System.in);
Scanner sc2=new Scanner(System.in);
Student student=new Student();
System.out.println("请输入新增加的学生的姓名:");
student.setName(sc2.nextLine());
System.out.println("请输入新增加的学生的年龄:");
student.setAge(sc1.nextInt());
System.out.println("请输入新增加的学生的学号:");
student.setNumber(sc2.nextLine());
System.out.println("请输入新增加的学生的家庭住址:");
student.setAddress(sc2.nextLine());
System.out.println("请输入新增加的学生的电话:");
student.setTelePhone(sc2.nextLine());
BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt",true));//有学生姓名的文件夹,把学生信息写入的文件夹
bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
bw.newLine();
bw.flush();
bw.close();
System.out.println("学生添加成功!");
}
//删除学生信息
public static void deleteStudent()throws IOException {
//在数据中查找知否有该数据
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
System.out.println("请输入删除的学生的姓名:");
String name = sc2.nextLine();
System.out.println("请输入删除的学生的年龄:");
int age = sc1.nextInt();
System.out.println("请输入删除的学生的学号:");
String number = sc2.nextLine();
System.out.println("请输入删除的学生的家庭住址:");
String address = sc2.nextLine();
System.out.println("请输入删除的学生的电话:");
String telePhone = sc2.nextLine();
BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
int n = 1;
String line;
ArrayList<Student> arrayList = new ArrayList<>();
while ((line = br.readLine()) != null) {
Student s=new Student();
String[] studentList = line.split(",");
s.setName(studentList[0]);
s.setAge(Integer.parseInt(studentList[1]));
s.setNumber(studentList[2]);
s.setAddress(studentList[3]);
s.setTelePhone(studentList[4]);
if ((studentList[0].equals(name)) && (Integer.parseInt(studentList[1]) == age)
&& (studentList[2].equals(number)) && (studentList[3].equals(address))
&& (studentList[4].equals(telePhone))) {
n=0;
}else {
arrayList.add(s);
}
}
if(n==1){
System.out.println("请检查你所输入的信息是否有误!");
System.out.println("删除失败!");
}else {
System.out.println("删除成功!");
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt"));
for(Student student:arrayList){
bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
bw.newLine();
bw.flush();
}
bw.close();
}
//修改学生信息
public static void xiuGaiStudent()throws IOException{
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
Student s=new Student();
System.out.println("请输入要修改的学生的姓名:");
s.setName(sc2.nextLine());
System.out.println("请输入要修改的学生的年龄:");
s.setAge(sc1.nextInt());
System.out.println("请输入要修改的学生的学号:");
s.setNumber(sc2.nextLine());
System.out.println("请输入要修改的学生的家庭住址:");
s.setAddress(sc2.nextLine());
System.out.println("请输入要修改的学生的电话:");
s.setTelePhone(sc2.nextLine());
ArrayList<Student>arrayList=new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
String line;
int n=1;
while ((line = br.readLine()) != null) {
String[] studentList = line.split(",");
Student s1=new Student();
s1.setName(studentList[0]);
s1.setAge(Integer.parseInt(studentList[1]));
s1.setNumber(studentList[2]);
s1.setAddress(studentList[3]);
s1.setTelePhone(studentList[4]);
if ((studentList[0].equals(s.getName())) && (Integer.parseInt(studentList[1]) == s.getAge())
&& (studentList[2].equals(s.getNumber())) && (studentList[3].equals(s.getAddress()))
&& (studentList[4].equals(s.getTelePhone()))) {
Scanner sc3=new Scanner(System.in);
Scanner sc4=new Scanner(System.in);
Student student=new Student();
System.out.println("请输入修改后学生的姓名");
student.setName(sc3.nextLine());
System.out.println("请输入要修改后的学生的年龄:");
student.setAge(sc4.nextInt());
System.out.println("请输入要修改后的学生的学号:");
student.setNumber(sc3.nextLine());
System.out.println("请输入要修改后的学生的家庭住址:");
student.setAddress(sc3.nextLine());
System.out.println("请输入要修改后的学生的电话:");
student.setTelePhone(sc3.nextLine());
arrayList.add(student);
n=0;
}else {
arrayList.add(s1);
}
}
if(n==0){
System.out.println("信息修改成功!");
}else {
System.out.println("请检查你所要修改的学生的信息是否有误!");
System.out.println("信息修改失败!");
}
br.close();
BufferedWriter bw=new BufferedWriter(new FileWriter("书本的课后习题\\xinXi1.txt"));
for(Student student:arrayList){
bw.write(student.getName()+","+student.getAge()+","+student.getNumber()+","+student.getAddress()+","+student.getTelePhone());
bw.newLine();
bw.flush();
}
bw.close();
}
//查看学生信息。
public static void chaKanStudent()throws IOException{
BufferedReader br = new BufferedReader(new FileReader("书本的课后习题\\xinXi1.txt"));
String line;
while ((line= br.readLine())!=null){
Student s=new Student();
String[] studentList = line.split(",");
s.setName(studentList[0]);
s.setAge(Integer.parseInt(studentList[1]));
s.setNumber(studentList[2]);
s.setAddress(studentList[3]);
s.setTelePhone(studentList[4]);
System.out.println("姓名:"+s.getName()+", 年龄:"+s.getAge()+" ,学号:"+s.getNumber()+" ,家庭住址:"+s.getAddress()+" ,家庭电话:"+s.getTelePhone());
}
br.close();
}
}
学生信息管理系统
最新推荐文章于 2024-10-01 18:51:18 发布