设计一个菜单驱动的学生成绩管理程序,管理n个学生的m门考试科目成绩,实现以下基本功能。
(1)能够新增学生信息,并计算总分和平均分。
(2)能够根据学号修改和删除某学生信息。
(3)能够显示所有学生的成绩信息。
(4)能够分别按总分和学号进行排序。
(5)能够根据学号查询该学生的基本信息。
(6)学生成绩数据最终保存在文件scores.txt中,能够对文件读、写学生数据。
程序运行时,菜单形式如下。
Management for Students's scores
1.Append record
2.List record
3.Delete record
4.Modify record
5.Search record
6.Sort in descending order by sum
7.Sort in ascending order by sum
8.Sort in descending order by num
9.Sort in ascending order by num
W.Write to a File
R.Read from a File
0.Exit
Please input your choice:
要求用面向对象方法组织程序结构,合理设计各个自定义函数。同时,程序能够进行异常处理,检查用户输入数据的有效性,在用户输入数据有错误(如类型错误)或无效时,不会中断程序的执行,程序具有一定的健壮性。
package Grademanagement;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Student {
public static Scanner in;
private String major;
private long id;
private String name;
private float chinese;
private float math;
private float english;
private float sum;
private double average;
private String line;
public Student() {
}
public Student(String major, long id, String name, float chinese, float math, float english, float sum, double average) {
this.major = major;
this.id = id;
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.sum = sum;
this.average = average;
}
public String getMajor() {
return this.major;
}
public void setMajor(String major) {
this.major = major;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public float getChinese() {
return this.chinese;
}
public void setChinese(float chinese) {
this.chinese = chinese;
}
public float getMath() {
return this.math;
}
public void setMath(float math) {
this.math = math;
}
public float getEnglish() {
return this.english;
}
public void setEnglish(float english) {
this.english = english;
}
public float getSum(Student student) {
return student.getChinese() + student.getMath() + student.getEnglish();
}
public void setSum(float sum) {
this.sum = sum;
}
public double getAverage(Student student) {
return (double)((student.getChinese() + student.getMath() + student.getEnglish()) / 3.0F);
}
public void setAverage(double average) {
this.average = average;
}
public boolean isValid(ArrayList<Student> students, long id) {
Iterator var4 = students.iterator();
Student student;
do {
if (!var4.hasNext()) {
return false;
}
student = (Student)var4.next();
} while(student.getId() != id);
return true;
}
public void append(ArrayList<Student> students) {
System.out.println("Please input your data");
System.out.print("major:");
String major = in.next();
while(true) {
System.out.print("id:");
long id = in.nextLong();
if (!this.isValid(students, id)) {
System.out.print("name:");
String name = in.next();
System.out.print("chinese:");
float chinese = in.nextFloat();
System.out.print("math:");
float math = in.nextFloat();
System.out.print("english:");
float english = in.nextFloat();
Student student = new Student();
student.setMajor(major);
student.setName(name);
student.setId(id);
student.setChinese(chinese);
student.setMath(math);
student.setEnglish(english);
float sum = this.getSum(student);
student.setSum(sum);
double average = this.getAverage(student);
student.setAverage(average);
students.add(student);
System.out.println("Append Successfully");
return;
}
System.out.println("The student number is reduplicate, please re-enter");
}
}
public void list(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("No data currently, please add data first");
} else {
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
Iterator var2 = students.iterator();
while(var2.hasNext()) {
Student student = (Student)var2.next();
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void delete(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
System.out.print("Enter the student id to delete the student's score:");
long id_in = in.nextLong();
int index = -1;
for(int i = 0; i < students.size(); ++i) {
Student student = (Student)students.get(i);
if (student.getId() == id_in) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("The student id is not found. Please confirm before deleting");
} else {
students.remove(index);
System.out.println("Delete Successfully");
}
}
}
public void modify(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
System.out.print("Please enter the student id to be modified:");
long id = in.nextLong();
Iterator var4 = students.iterator();
if (var4.hasNext()) {
Student student = (Student)var4.next();
if (id == student.getId()) {
System.out.print("Please re-enter the student's major:");
String major = in.next();
System.out.println("Please re-enter the student's grades");
System.out.print("chinese:");
float chinese = in.nextFloat();
System.out.print("math:");
float math = in.nextFloat();
System.out.print("english:");
float english = in.nextFloat();
student.setMajor(major);
student.setChinese(chinese);
student.setMath(math);
student.setEnglish(english);
float sum = this.getSum(student);
student.setSum(sum);
double average = this.getAverage(student);
student.setAverage(average);
System.out.println("Modify Successfully");
} else {
System.out.println("No corresponding student found, please confirm and re-enter");
}
}
}
}
public void search(ArrayList<Student> students) {
if (students.size() == 0) {
System.out.println("No data currently, please add and try again");
} else {
System.out.print("Please enter the student ID of the student you want to search:");
long id = in.nextLong();
int index = -1;
Student student = new Student();
for(int i = 0; i < students.size(); ++i) {
student = (Student)students.get(i);
if (student.getId() == id) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("No student with corresponding student id found, please confirm and re-enter");
} else {
System.out.println("The scores of students with corresponding student id are as follows:");
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void descend_sum(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
Student student;
for(int i = 0; i < students.size() - 1; ++i) {
if (((Student)students.get(i)).getSum((Student)students.get(i)) < ((Student)students.get(i + 1)).getSum((Student)students.get(i + 1))) {
student = (Student)students.get(i);
students.set(i, students.get(i + 1));
students.set(i + 1, student);
}
}
System.out.println("Student grades after sorting are as follows:");
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
Iterator var4 = students.iterator();
while(var4.hasNext()) {
student = (Student)var4.next();
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void ascend_sum(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
Student student;
for(int i = 0; i < students.size() - 1; ++i) {
if (((Student)students.get(i)).getSum((Student)students.get(i)) > ((Student)students.get(i + 1)).getSum((Student)students.get(i + 1))) {
student = (Student)students.get(i);
students.set(i, students.get(i + 1));
students.set(i + 1, student);
}
}
System.out.println("Student grades after sorting are as follows:");
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
Iterator var4 = students.iterator();
while(var4.hasNext()) {
student = (Student)var4.next();
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void descend_num(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
Student student;
for(int i = 0; i < students.size() - 1; ++i) {
if (((Student)students.get(i)).getId() < ((Student)students.get(i + 1)).getId()) {
student = (Student)students.get(i);
students.set(i, students.get(i + 1));
students.set(i + 1, student);
}
}
System.out.println("Student grades after sorting are as follows:");
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
Iterator var4 = students.iterator();
while(var4.hasNext()) {
student = (Student)var4.next();
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void ascend_num(ArrayList<Student> students) {
if (students.isEmpty()) {
System.out.println("No data currently, please add and try again");
} else {
Student student;
for(int i = 0; i < students.size() - 1; ++i) {
if (((Student)students.get(i)).getId() > ((Student)students.get(i + 1)).getId()) {
student = (Student)students.get(i);
students.set(i, students.get(i + 1));
students.set(i + 1, student);
}
}
System.out.println("Student grades after sorting are as follows:");
System.out.println("major\t\tid\t\tname\t\tchinese\t\tmath\t\tenglish\t\tsum\t\taverage");
Iterator var4 = students.iterator();
while(var4.hasNext()) {
student = (Student)var4.next();
System.out.format("%s\t\t%d\t\t%s\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.1f\t\t%.2f\n", student.getMajor(), student.getId(), student.getName(), student.getChinese(), student.getMath(), student.getEnglish(), student.getSum(student), student.getAverage(student));
}
}
}
public void write(ArrayList<Student> students) {
File score = new File("C:/Users/24383/Desktop/score.txt");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(score));
Iterator var4 = students.iterator();
while(var4.hasNext()) {
Student stu = (Student)var4.next();
StringBuilder sb = new StringBuilder();
sb.append(stu.getMajor()).append(" ").append(stu.getId()).append(" ").append(stu.getName()).append(" ").append(stu.getChinese()).append(" ").append(stu.getMath()).append(" ").append(stu.getEnglish()).append(" ").append(stu.getSum(stu)).append(" ").append(stu.getAverage(stu));
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
} catch (IOException var7) {
var7.printStackTrace();
}
System.out.println("Write Successfully");
}
public void read(ArrayList<Student> students) {
Student student = new Student();
try {
String pathname = "C:/Users/24383/Desktop/score.txt";
File filename = new File(pathname);
FileInputStream fileInputStream = new FileInputStream(filename);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader br = new BufferedReader(inputStreamReader);
String line = null;
while((line = br.readLine()) != null) {
String[] arr = line.split(" ");
student.setMajor(arr[0]);
student.setId(Long.parseLong(arr[1]));
student.setName(arr[2]);
student.setChinese(Float.parseFloat(arr[3]));
student.setMath(Float.parseFloat(arr[4]));
student.setEnglish(Float.parseFloat(arr[5]));
student.setSum(Float.parseFloat(arr[6]));
student.setAverage(Double.parseDouble(arr[7]));
students.add(student);
System.out.println(line);
}
System.out.println("Read successfully");
br.close();
} catch (IOException var10) {
var10.printStackTrace();
}
}
static {
in = new Scanner(System.in);
}
}
package Grademanagement;
import java.io.*;
import java.util.*;
public class p6 {
public static Scanner in=new Scanner(System.in);
public static ArrayList<Student> students = new ArrayList<>();
public static void main(String[] args) {
boolean logic = true;
while (logic){
System.out.println("Management for Students's scores");
System.out.println("1.Append record");
System.out.println("2.List record");
System.out.println("3.Delete record");
System.out.println("4.Modify record");
System.out.println("5.Search record");
System.out.println("6.Sort in descending order by sum");
System.out.println("7.Sort in ascending order by sum");
System.out.println("8.Sort in descending order by num");
System.out.println("9.Sort in ascending order by num");
System.out.println("W.Write to a File");
System.out.println("R.Read from a File");
System.out.println("0.Exit");
System.out.print("Please input your choice:");
Student student = new Student();
String choice=in.next();
switch (choice){
case "0":
System.out.print("run ended");
logic=false;
break;
case "1":
student.append(students);
break;
case "2":
student.list(students);
break;
case "3":
student.delete(students);
break;
case "4":
student.modify(students);
break;
case "5":
student.search(students);
break;
case "6":
student.descend_sum(students);
break;
case "7":
student.ascend_sum(students);
break;
case "8":
student.descend_num(students);
break;
case "9":
student.ascend_num(students);
break;
case "W":
student.write(students);
break;
case "R":
student.read(students);
break;
}
}
}
}