- package com.itheima;
- import java.io.*;
- import java.util.*;
- /**
- *
- * 第2题:有五个学生,每个学生有3门课(语文、数学、英语)的成绩,写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,
- * 三门课成绩),然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。要求:stu.txt文件的格式要比较直观,打开这个文件,
- * 就可以很清楚的看到学生的信息。
- *
- */
- public class Test2 {
- public static void main(String[] args) throws IOException {
- //定义学生成绩
- StudentScore ss = null;
- //创建学生管理系统
- StudentManage sm = new StudentManage();
- //读取键盘录入
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //定义一行数据
- String line = null;
- //按照提示进行选择功能
- 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("退出请按0");
- //读取按下的数字
- String number = br.readLine();
- //根据数字选择功能
- if (number.equals("1")) {
- System.out.println("请输入姓名:");
- String name = br.readLine();
- System.out.println("请输入语文成绩:");
- double chineseScore = Double.parseDouble(br.readLine());
- System.out.println("请输入数学成绩:");
- double mathScore = Double.parseDouble(br.readLine());
- System.out.println("请输入英语成绩:");
- double englishScore = Double.parseDouble(br.readLine());
- ss = new StudentScore(name, chineseScore, mathScore,
- englishScore);
- sm.addScore(ss);
- } else if (number.equals("2")) {
- System.out.println("请输入你要查找的学生姓名:");
- String name = br.readLine();
- sm.lookUp(name);
- } else if (number.equals("3")) {
- System.out.println("请输入你要修改的学生姓名:");
- String name = br.readLine();
- System.out.println("请输入修改后的语文成绩:");
- double chineseScore = Double.parseDouble(br.readLine());
- System.out.println("请输入修改后的数学成绩:");
- double mathScore = Double.parseDouble(br.readLine());
- System.out.println("请输入修改后的英语成绩:");
- double englishScore = Double.parseDouble(br.readLine());
- sm.reviseScore(name, chineseScore, mathScore, englishScore);
- } else if (number.equals("4")) {
- System.out.println("请输入你要注销的学生姓名:");
- String name = br.readLine();
- sm.delete(name);
- } else if (number.equals("5")) {
- sm.scoreSort();
- } else if (number.equals("0")) {
- System.out.println("祝您生活愉快,再见");
- System.exit(0);
- } else {
- System.out.println("输入有误");
- }
- }
- }
- }
- /**
- *
- * 学生管理系统类 成员变量:ArrayList 方法:添加,查询,修改,注销学生信息
- *
- */
- class StudentManage {
-
- // 定义list,用来存储StudentScore对象
- private List<StudentScore> list = null;
- // 构造方法,初始化list
- public StudentManage() {
- list = new ArrayList<StudentScore>();
- }
- public List<StudentScore> getList() {
- return list;
- }
- public void setList(List<StudentScore> list) {
- this.list = list;
- }
- // 添加学生成绩
- public void addScore(StudentScore score) {
- list.add(score);
-
-
- System.out.println("添加学生成绩成功");
- }
- // 查询学生成绩
- public void lookUp(String name) {
- int i;
- for (i = 0; i < list.size(); i++) {
- StudentScore student = (list.get(i));
- if (name.equals(student.getName())) {
- System.out.println("该学生信息为:");
- System.out.println("姓名:" + name);
- System.out.println("语文成绩: " + student.getChineseScore());
- System.out.println("数学成绩: " + student.getMathScore());
- System.out.println("英语成绩: " + student.getEnglishScore());
- break;
- }
- }
- if (i == list.size()) {
- System.out.println("对不起,没有此学生信息");
- }
- }
- // 修改学生成绩
- public void reviseScore(String name, double chineseScore, double mathScore,
- double englishScore) {
- System.out.println(chineseScore);
- int i = 0;
- for (i = 0; i < list.size(); i++) {
- StudentScore student = list.get(i);
- System.out.println(chineseScore);
- if (name.equals(student.getName())) {
- System.out.println(chineseScore);
- student.setChineseScore(chineseScore);
- student.setMathScore(mathScore);
- student.setEnglishScore(englishScore);
- System.out.print(student.getChineseScore());
- System.out.println("修改成绩成功 ");
- break;
- }
- }
- }
- // 注销学生信息
- public void delete(String name) {
- int i = 0;
- if (list.size() == 0) {
- System.out.println("对不起,学生信息列表为空,请添加学生信息");
- }
- for (i = 0; i < list.size(); i++) {
- StudentScore student = list.get(i);
- if (name.equals(student.getName())) {
- list.remove(i);
- System.out.print("注销学生信息成功");
- }
- break;
- }
- if (i == list.size() && (list.size() != 0)) {
- System.out.println("对不起,没有此学生信息");
- }
- }
- // 按学生成绩总分从高到低进行排序
- public void scoreSort() throws IOException {
- FileWriter fw = null;
- Collections.sort(list);
- for (int i = list.size() - 1; i >= 0; i--) {
- String s = list.get(i).getInfo();
- fw = new FileWriter("src/stu.txt", true);
- fw.write(s);
- fw.write("\r\n");
- fw.flush();
- System.out.println(s);
- }
- }
- }
- /**
- *
- * 学生成绩类 成员变量:语文,数学,英语成绩 方法:get();set();
- */
- class StudentScore implements Comparable<StudentScore> {
- // 定义学生姓名
- private String name;
- // 定义语文,数学,英语成绩
- private double chineseScore, mathScore, englishScore;
- // 定义总成绩
- private double sum;
- // 带参数的构造方法,初始化语文,数学,英语成绩
- public StudentScore(String name, double chineseScore, double mathScore,
- double englishScore) {
- this.name = name;
- this.chineseScore = chineseScore;
- this.mathScore = mathScore;
- this.englishScore = englishScore;
- this.sum = chineseScore + mathScore + englishScore;
- }
- // 自定义定义对象的比较规则
- public int compareTo(StudentScore o) {
- return Double.valueOf(this.sum).compareTo(Double.valueOf(o.sum));
- }
- // 定义获取学生成绩信息的方法
- public String getInfo() {
- return "姓名 :" + name + ";语文成绩: " + chineseScore + ";数学成绩: " + mathScore
- + ";英语成绩: " + englishScore + ";总成绩: " + sum;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getChineseScore() {
- return chineseScore;
- }
- public void setChineseScore(double chineseScore) {
- this.chineseScore = chineseScore;
- }
- public double getMathScore() {
- return mathScore;
- }
- public void setMathScore(double mathScore) {
- this.mathScore = mathScore;
- }
- public double getEnglishScore() {
- return englishScore;
- }
- public void setEnglishScore(double englishScore) {
- this.englishScore = englishScore;
- }
- }
复制代码 |