package com.company.homework;
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class StudentManager {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Student> studentList = new ArrayList<>();
Student student1 = new Student(001, "小明", "男", 98, 80, 99);
studentList.add(student1);
System.out.println(student1.getTotalScore());
Scanner sc = new Scanner(System.in);
//创建一个实验对象
while (true) {
System.out.println("欢迎来到学生档案管理系统");
System.out.println("你的身份是\n" + "1 - student\n" + "2 - teacher \n");
int user = sc.nextInt();
switch (user) {
case 1:
System.out.println("Enter your choice\n" + "1 - list all students\n" + "2 - end program");
int choice1 = sc.nextInt();
//判断选择的功能
if (choice1 == 2) {
System.out.println("over");
break;
}
//分支
switch (choice1) {
case 1:
list(studentList);
break;
}
break;
case 2:
System.out.println("Enter your choice\n" + "1 - list all students\n" + "2 - find a student \n" +
"3 - update a student\n" + "4 - add a new student\n" + "5 - delete an student by id\n"
+ "6 - save student information\n" + "7 - load student information\n" +
"8 - sort by totalScore\n" + "9 - end program");
int choice = sc.nextInt();
//判断选择的功能
if (choice == 9) {
System.out.println("over");
break;
}
//分支
switch (choice) {
case 1:
list(studentList);
break;
case 2:
find(studentList);
break;
case 3:
update(studentList);
break;
case 4:
add(studentList);
break;
case 5:
delete(studentList);
break;
case 6:
save(studentList);
break;
case 7:
load(studentList);
break;
case 8:
sort(studentList);
break;
case 9:
break;
}
}
}
}
public static void list(ArrayList<Student> studentList) {
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
}//打印所有所有学生信息方法
public static void find(ArrayList<Student> studentList) {
System.out.println("请输入所要查找学生的信息");
System.out.println("1 - by id\n" + "2 - by name \n");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
//依照学号查找
System.out.println("请输入学号");
int snum = scanner.nextInt();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).snum == snum) {
System.out.println(studentList.get(i));
} else {
System.out.println("未找到此学号学生");
}
}
case 2:
//依照姓名查找
System.out.println("请输入姓名");
String name = scanner.next();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).name.equals(name)) {
System.out.println(studentList.get(i));
} else {
System.out.println("未找到此姓名学生");
}
}
}
}//依照特定值查找方法
public static void update(ArrayList<Student> studentList) {
System.out.println("请输入所要查找学生的信息");
System.out.println("1 - by id\n" + "2 - by name \n");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
//先查询,后修改
System.out.println("请输入学号");
int snum = scanner.nextInt();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).snum == snum) {
System.out.println("请输入要修改的信息");
System.out.println("1 - name\n" + "2 - sex \n" + "3 - mathScore \n" + "4 - englishScore \n" + "5 - physicalScore \n");
int info = scanner.nextInt();
switch (info) {
case 1:
System.out.println("修改后的名字是");
String name = scanner.next();
studentList.get(i).setName(name);
System.out.println("成功修改");
break;
case 2:
System.out.println("修改后的性别是");
String sex = scanner.next();
studentList.get(i).setSex(sex);
System.out.println("成功修改");
break;
case 3:
System.out.println("修改后的mathScore是");
int mathScore = scanner.nextInt();
studentList.get(i).setMathScore(mathScore);
System.out.println("成功修改");
break;
case 4:
System.out.println("修改后的englishScore是");
int englishScore = scanner.nextInt();
studentList.get(i).setEnglishScore(englishScore);
System.out.println("成功修改");
break;
case 5:
System.out.println("修改后的physicalScore是");
int physicalScore = scanner.nextInt();
studentList.get(i).setPhysicalScore(physicalScore);
System.out.println("成功修改");
break;
}
} else {
System.out.println("未找到此学号");
}
}
break;
case 2:
System.out.println("请输入姓名");
String name = scanner.next();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).name.equals(name)) {
System.out.println("请输入要修改的信息");
System.out.println("1 - name\n" + "2 - sex \n" + "3 - mathScore \n" + "4 - englishScore \n" + "5 - physicalScore \n");
int info = scanner.nextInt();
switch (info) {
case 1:
System.out.println("修改后的名字是");
String name_tem = scanner.next();
studentList.get(i).setName(name_tem);
System.out.println("成功修改");
break;
case 2:
System.out.println("修改后的性别是");
String sex = scanner.next();
studentList.get(i).setSex(sex);
System.out.println("成功修改");
break;
case 3:
System.out.println("修改后的mathScore是");
int mathScore = scanner.nextInt();
studentList.get(i).setMathScore(mathScore);
System.out.println("成功修改");
break;
case 4:
System.out.println("修改后的englishScore是");
int englishScore = scanner.nextInt();
studentList.get(i).setEnglishScore(englishScore);
System.out.println("成功修改");
break;
case 5:
System.out.println("修改后的physicalScore是");
int physicalScore = scanner.nextInt();
studentList.get(i).setPhysicalScore(physicalScore);
System.out.println("成功修改");
break;
}
} else {
System.out.println("未找到此姓名");
}
}
break;
}
}//修改学生信息方法
public static void add(ArrayList<Student> studentList) {
Scanner scanner = new Scanner(System.in);
Student student = new Student();
System.out.println("请输入学号");
int snum = scanner.nextInt();
//循环遍历看添加的学号是否已经存在
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).snum == snum) {
System.out.println("已存在此学号,请重新输入");
break;
} else {
student.setSnum(snum);
System.out.println("请输入姓名");
String name = scanner.next();
student.setName(name);
System.out.println("请输入姓别");
String sex = scanner.next();
student.setSex(sex);
System.out.println("请输入mathScore");
int mathScore = scanner.nextInt();
student.setMathScore(mathScore);
System.out.println("请输入englishScore");
int englishScore = scanner.nextInt();
student.setEnglishScore(englishScore);
System.out.println("请输入physicalScore");
int physicalScore = scanner.nextInt();
student.setPhysicalScore(physicalScore);
student.setTotalScore(student.getEnglishScore() + student.getMathScore() + student.getPhysicalScore());
studentList.add(student);
System.out.println("添加成功");
break;
}
}
}//添加学生方法
public static void delete(ArrayList<Student> studentList) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号");
int snum = scanner.nextInt();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).snum == snum) {
studentList.remove(i);
System.out.println("删除成功");
}
}
}//依照学号删除方法
public static void save(ArrayList<Student> studentList) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("lib/studentinformation.txt");
// 创建文件输出流
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
//创建面向对象输出流
for (int i = 0; i < studentList.size(); i++) {
objectOutputStream.writeObject(studentList.get(i));
}
objectOutputStream.close();
System.out.println("保存成功");
}//将已有的学生信息加载到磁盘文件中保存方法
public static void load(ArrayList<Student> studentList) throws IOException, ClassNotFoundException {
FileInputStream fileInputStream = new FileInputStream("lib/studentinformation.txt");
// 创建文件输入流
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
//创建面向对象输入流
for (int i = 0; i < studentList.size(); i++) {
//使用try catch 捕获EOFException(此异常是文件读到末尾时jdk抛出)
try {
Student student_temp = new Student();
student_temp = (Student) objectInputStream.readObject();
studentList.add(student_temp);
} catch (EOFException e) {
System.out.println("读写完毕");
objectInputStream.close();
}
}
}//从磁盘文件加载学生信息
public static void sort(ArrayList<Student> studentList) {
Object[] a = studentList.toArray();
studentList.sort(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
if (s1.getTotalScore() > s2.getTotalScore())
return -1;
return 1;
}
});
for (int i = 0; i < studentList.size(); i++) {
System.out.println("姓名: " + studentList.get(i).getName() + " 总成绩 "
+ studentList.get(i).getTotalScore() + studentList.get(i));
}
}//按总成绩排序打印学生信息方法
}
学生信息管理系统(java基于控制台简易版)
最新推荐文章于 2024-04-18 04:14:29 发布