ฅ(๑˙o˙๑)ฅ 大家好, 欢迎大家光临我的博客:面向阿尼亚学习
算法学习笔记系列持续更新中~
学了一学期的Java,实现一个基于IO流存取信息的学生成绩管理系统
综合运用学过的知识完成一个学生成绩管理系统并能验证系统。
学生具有学号、姓名、性别、班级、成绩属性。
要求实现以下功能:
// 功能提示
public void Tips() {
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("6.删除学生信息");
System.out.println("7.按平均分排序");
System.out.println("8.退出");
System.out.println("请选择功能");
}
// 功能选择
public void function(StudentDemo studentDemo) throws IOException {
switch (studentDemo.scannerInt()) {
// 添加学生信息
case 1:
studentDemo.addStudent(studentDemo.path);
break;
// 显示所有信息
case 2:
studentDemo.showstudent(studentDemo.path);
break;
// 按照学号查询单个学生信息
case 3:
studentDemo.findstudentbyid(studentDemo.path);
break;
// 查询名字查询单个学生信息
case 4:
studentDemo.findstudentbyname(studentDemo.path);
break;
// 修改学生信息
case 5:
studentDemo.updatestudent(studentDemo.path);
break;
// 删除学生信息
case 6:
studentDemo.deletestudent(studentDemo.path);
break;
//按平均分排序
case 7:
studentDemo.showstudentbyaveScore(studentDemo.path);
break;
// 退出
case 8:
flag = false;
System.out.println("谢谢使用!");
break;
// 输入无效
default: {
System.out.println("请重新输入");
// 再次输入
function(studentDemo);
break;
}
}
// 增添学生信息
public void addStudent(String path) throws IOException {
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData(path, studentArrayList);
boolean flag = false;
String id;
while (true) {
System.out.println("请输入需要添加的学号");
id = scannerString();
// 判断用户是否已经存在
for (int i = 0; i < studentArrayList.size(); i++) {
Student s = studentArrayList.get(i);
if (s.getId().equals(id)) {
flag = true;
break;
}
}
if (flag) {
System.out.println("用户已存在,请重新操作");
} else
break;
}
System.out.println("请输入需要添加的学生姓名");
String name = scannerString();
System.out.println("请输入需要添加的学生性别");
String sex = scannerString();
System.out.println("请输入需要添加的学生班级");
String grade = scannerString();
System.out.println("请输入需要添加的学生语文成绩");
double chineseScore = scannerInt();
System.out.println("请输入需要添加的学生数学成绩");
double mathScore = scannerInt();
System.out.println("请输入需要添加的学生英语成绩");
double englishScore = scannerInt();
double totalScore = chineseScore + mathScore + englishScore;
double aveScore = totalScore / 3;
Student student=new Student(id,name,sex,grade,chineseScore,mathScore,englishScore,totalScore,aveScore);
studentArrayList.add(student);
writeData(path, studentArrayList);
System.out.println("添加成功");
}
// 将集合写入文件
public void writeData(String path, ArrayList<Student> studentArrayList) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
for (int i = 0; i < studentArrayList.size(); i++) {
// 获取对象
Student student = studentArrayList.get(i);
// 使用StringBuilder提高效率
StringBuilder stringBuilder = new StringBuilder();
// 获取并保存数据
stringBuilder.append(student.getId()).append(",").append(student.getName()).append(",")
.append(student.getSex()).append(",").append(student.getGrade()).append(",")
.append(student.getChineseScores()).append(",").append(student.getMathScores()).append(",")
.append(student.getEnglishScores()).append(",").append(student.getTotalScores()).append(",")
.append(student.getAveScores());
// 写入
bufferedWriter.write(stringBuilder.toString());
// 换行
bufferedWriter.newLine();
// 强制刷新
bufferedWriter.flush();
}
bufferedWriter.close();
}
// 将文件读入集合
static void readData(String path, ArrayList<Student> studentArrayList) throws IOException {
// 利用处理流提高性能
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
String line;
while ((line = bufferedReader.readLine()) != null) {
// 获取本行数据,文本文件用空格分隔数据,将数据保存在数组当中
String[] str = line.split(",");
Student student = new Student();
student.setId(str[0]);
student.setName(str[1]);
student.setSex(str[2]);
student.setGrade(str[3]);
student.setChineseScores(Double.parseDouble(str[4]));
student.setMathScores(Double.parseDouble(str[5]));
student.setEnglishScores(Double.parseDouble(str[6]));
student.setTotalScores(Double.parseDouble(str[7]));
student.setAveScores(Double.parseDouble(str[8]));
studentArrayList.add(student);
}
bufferedReader.close();
}
// 显示所有学生信息
void showstudent(String path) throws IOException {
// 读数据到集合中
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
if (studentArrList.size() == 0) {
System.out.println("无可查询信息");
return;
}
Collections.sort(studentArrList);
print(studentArrList);
}
// 查询学生信息
private void findstudentbyid(String path) throws IOException {
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
System.out.println("请输入需要查找的学号");
String id = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrList.size(); i++) {
if (id.equals(studentArrList.get(i).getId()))
{
flag = true;
x = i;
break;
}
}
if (flag==false) {
System.out.println("查无此人");
return;
}
else
{
System.out.println("学号\t姓名\t性别\t班级\t语文\t数学\t英语\t总成绩\t平均成绩");
Student student = studentArrList.get(x);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t"
+ student.getGrade() + "\t" + student.getChineseScores() + "\t" + student.getMathScores() + "\t"
+ student.getEnglishScores() + "\t" + student.getTotalScores() + "\t" + student.getAveScores());
}
}
// 查询学生信息
private void findstudentbyname(String path) throws IOException {
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
System.out.println("请输入需要查找的姓名");
String id = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrList.size(); i++) {
if (id.equals(studentArrList.get(i).getName()))
{
flag = true;
x = i;
break;
}
}
if (flag==false) {
System.out.println("查无此人");
return;
}
else
{
System.out.println("学号\t姓名\t性别\t班级\t语文\t数学\t英语\t总成绩\t平均成绩");
Student student = studentArrList.get(x);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t"
+ student.getGrade() + "\t" + student.getChineseScores() + "\t" + student.getMathScores() + "\t"
+ student.getEnglishScores() + "\t" + student.getTotalScores() + "\t" + student.getAveScores());
}
}
// 修改学生信息
private void updatestudent(String path) throws IOException {
System.out.println("请输入需要修改的学生学号");
String s = scannerString();
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData( path, studentArrayList);
for (int i = 0; i < studentArrayList.size(); i++) {
if (s.equals(studentArrayList.get(i).getId())) {
System.out.println("请输入需要修改后的学生姓名");
String name = scannerString();
System.out.println("请输入需要修改后的学生性别");
String sex = scannerString();
System.out.println("请输入需要修改后的学生班级");
String grade = scannerString();
System.out.println("请输入需要添加的学生语文成绩");
double chineseScore = scannerInt();
System.out.println("请输入需要添加的学生数学成绩");
double mathScore = scannerInt();
System.out.println("请输入需要添加的学生英语成绩");
double englishScore = scannerInt();
double totalScore = chineseScore + mathScore + englishScore;
double aveScore = totalScore / 3;
studentArrayList.get(i).setId(s);
studentArrayList.get(i).setName(name);
studentArrayList.get(i).setSex(sex);
studentArrayList.get(i).setGrade(grade);
studentArrayList.get(i).setChineseScores(chineseScore);
studentArrayList.get(i).setMathScores(mathScore);
studentArrayList.get(i).setEnglishScores(englishScore);
studentArrayList.get(i).setTotalScores(totalScore);
studentArrayList.get(i).setAveScores(aveScore);
} else if (studentArrayList.size() == i) {
System.out.println("查无此人");
return;
}
}
writeData(path, studentArrayList);
System.out.println("修改完成");
}
}
// 删除学生信息
private void deletestudent(String path) throws IOException {
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData(path, studentArrayList);
System.out.println("请输入要删除的学生信息(学号/姓名)");
String id_name = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrayList.size(); i++) {
if (id_name.equals(studentArrayList.get(i).getId()) || id_name.equals(studentArrayList.get(i).getName())) {
flag = true;
x = i;
break;
}
}
if (flag) {
studentArrayList.remove(x);
// 再写入覆盖
writeData(path, studentArrayList);
System.out.println("删除完毕");
} else
System.out.println("查无此人,请重新输入");
}
// 显示所有学生信息
void showstudentbyaveScore(String path) throws IOException {
// 读数据到集合中
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
if (studentArrList.size() == 0) {
System.out.println("无可查询信息");
return;
}
Collections.sort(studentArrList, new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return Double.compare(o2.getAveScores(), o1.getAveScores());
}
});// 匿名类
print(studentArrList);
}
学生类
package Test;
public class Student implements Comparable<Student> {
private String id; // 学号
private String name; // 姓名
private String sex; // 性别
private String grade; // 班级
private double chineseScores = 0; // 语文成绩
private double mathScores = 0; // 数学成绩
private double englishScores = 0; // 英语成绩
private double totalScores = 0; // 总成绩
private double aveScores = 0; // 平均成绩
public Student() {
}
public Student(String id, String name, String sex, String grade, double chineseScores, double mathScores,
double englishScores, double totalScores, double aveScores) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.grade = grade;
this.chineseScores = chineseScores;
this.mathScores = mathScores;
this.englishScores = englishScores;
this.totalScores = totalScores;
this.aveScores = aveScores;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public double getChineseScores() {
return chineseScores;
}
public void setChineseScores(double chineseScores) {
this.chineseScores = chineseScores;
}
public double getMathScores() {
return mathScores;
}
public void setMathScores(double mathScores) {
this.mathScores = mathScores;
}
public double getEnglishScores() {
return englishScores;
}
public void setEnglishScores(double englishScores) {
this.englishScores = englishScores;
}
public double getTotalScores() {
return totalScores;
}
public void setTotalScores(double totalScores) {
this.totalScores = totalScores;
}
public double getAveScores() {
return aveScores;
}
public void setAveScores(double aveScores) {
this.aveScores = aveScores;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
// 如果返回正数说明当前对象大于当前学生对象
// 如果返回负数说明当前对象小于当前学生对象
// 相等说明一致
return Integer.valueOf(id) - Integer.valueOf(o.getId());// 当前对象写在前面,另一个对象写在后面,默认为升序,降序则与之相反
}
}
方法以及测试类
package Test88;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class StudentDemo {
// 文件存放路径
private String path = "d:\\xueshengxinxi.txt";
// 判断是否退出系统
private static boolean flag = true;
public static void main(String[] args) throws IOException {
while (flag) {
StudentDemo studentDemo = new StudentDemo();
studentDemo.Tips();
studentDemo.function(studentDemo);
System.out.println("");
}
}
// 功能提示
public void Tips() {
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("6.删除学生信息");
System.out.println("7.按平均分排序");
System.out.println("8.退出");
System.out.println("请选择功能");
}
// 功能选择
public void function(StudentDemo studentDemo) throws IOException {
switch (studentDemo.scannerInt()) {
// 添加学生信息
case 1:
studentDemo.addStudent(studentDemo.path);
break;
// 显示所有信息
case 2:
studentDemo.showstudent(studentDemo.path);
break;
// 按照学号查询单个学生信息
case 3:
studentDemo.findstudentbyid(studentDemo.path);
break;
// 查询名字查询单个学生信息
case 4:
studentDemo.findstudentbyname(studentDemo.path);
break;
// 修改学生信息
case 5:
studentDemo.updatestudent(studentDemo.path);
break;
// 删除学生信息
case 6:
studentDemo.deletestudent(studentDemo.path);
break;
//按平均分排序
case 7:
studentDemo.showstudentbyaveScore(studentDemo.path);
break;
// 退出
case 8:
flag = false;
System.out.println("谢谢使用!");
break;
// 输入无效
default: {
System.out.println("请重新输入");
// 再次输入
function(studentDemo);
break;
}
}
}
// 输入功能
public int scannerInt() {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
return x;
}
public String scannerString() {
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String x = scanner.next().toString();
return x;
}
// 将文件读入集合
static void readData(String path, ArrayList<Student> studentArrayList) throws IOException {
// 利用处理流提高性能
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
String line;
while ((line = bufferedReader.readLine()) != null) {
// 获取本行数据,文本文件用空格分隔数据,将数据保存在数组当中
String[] str = line.split(",");
Student student = new Student();
student.setId(str[0]);
student.setName(str[1]);
student.setSex(str[2]);
student.setGrade(str[3]);
student.setChineseScores(Double.parseDouble(str[4]));
student.setMathScores(Double.parseDouble(str[5]));
student.setEnglishScores(Double.parseDouble(str[6]));
student.setTotalScores(Double.parseDouble(str[7]));
student.setAveScores(Double.parseDouble(str[8]));
studentArrayList.add(student);
}
bufferedReader.close();
}
// 将集合写入文件
public void writeData(String path, ArrayList<Student> studentArrayList) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path));
for (int i = 0; i < studentArrayList.size(); i++) {
// 获取对象
Student student = studentArrayList.get(i);
// 使用StringBuilder提高效率
StringBuilder stringBuilder = new StringBuilder();
// 获取并保存数据
stringBuilder.append(student.getId()).append(",").append(student.getName()).append(",")
.append(student.getSex()).append(",").append(student.getGrade()).append(",")
.append(student.getChineseScores()).append(",").append(student.getMathScores()).append(",")
.append(student.getEnglishScores()).append(",").append(student.getTotalScores()).append(",")
.append(student.getAveScores());
// 写入
bufferedWriter.write(stringBuilder.toString());
// 换行
bufferedWriter.newLine();
// 强制刷新
bufferedWriter.flush();
}
bufferedWriter.close();
}
// 增添学生信息
public void addStudent(String path) throws IOException {
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData(path, studentArrayList);
boolean flag = false;
String id;
while (true) {
System.out.println("请输入需要添加的学号");
id = scannerString();
// 判断用户是否已经存在
for (int i = 0; i < studentArrayList.size(); i++) {
Student s = studentArrayList.get(i);
if (s.getId().equals(id)) {
flag = true;
break;
}
}
if (flag) {
System.out.println("用户已存在,请重新操作");
} else
break;
}
System.out.println("请输入需要添加的学生姓名");
String name = scannerString();
System.out.println("请输入需要添加的学生性别");
String sex = scannerString();
System.out.println("请输入需要添加的学生班级");
String grade = scannerString();
System.out.println("请输入需要添加的学生语文成绩");
double chineseScore = scannerInt();
System.out.println("请输入需要添加的学生数学成绩");
double mathScore = scannerInt();
System.out.println("请输入需要添加的学生英语成绩");
double englishScore = scannerInt();
double totalScore = chineseScore + mathScore + englishScore;
double aveScore = totalScore / 3;
Student student=new Student(id,name,sex,grade,chineseScore,mathScore,englishScore,totalScore,aveScore);
studentArrayList.add(student);
writeData(path, studentArrayList);
System.out.println("添加成功");
}
// 输出所有信息
static void print(ArrayList<Student> studentArrList) {
System.out.println("学号\t姓名\t性别\t班级\t语文成绩\t数学成绩\t英语成绩\t总成绩\t平均成绩");
for (int i = 0; i < studentArrList.size(); i++) {
Student student = studentArrList.get(i);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t"
+ student.getGrade() + "\t" + student.getChineseScores() + "\t" + student.getMathScores() + "\t"
+ student.getEnglishScores() + "\t" + student.getTotalScores() + "\t" + student.getAveScores());
}
}
// 显示所有学生信息
void showstudent(String path) throws IOException {
// 读数据到集合中
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
if (studentArrList.size() == 0) {
System.out.println("无可查询信息");
return;
}
//加个sort
Collections.sort(studentArrList);
// Collections.sort(studentArrList, new Comparator() {
// public int compare(Student c1, Student c2) {
// return Double.compare(Integer.valueOf(c1.getId()), Integer.valueOf(c2.getId()));
// }
// });// 匿名类
print(studentArrList);
}
// 显示所有学生信息
void showstudentbyaveScore(String path) throws IOException {
// 读数据到集合中
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
if (studentArrList.size() == 0) {
System.out.println("无可查询信息");
return;
}
//加个sort
// Collections.sort(studentArrList);
Collections.sort(studentArrList, new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return Double.compare(o2.getAveScores(), o1.getAveScores());
}
});// 匿名类
print(studentArrList);
}
// 查询学生信息
private void findstudentbyid(String path) throws IOException {
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
System.out.println("请输入需要查找的学号");
String id = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrList.size(); i++) {
if (id.equals(studentArrList.get(i).getId()))
{
flag = true;
x = i;
break;
}
}
if (flag==false) {
System.out.println("查无此人");
return;
}
else
{
System.out.println("学号\t姓名\t性别\t班级\t语文\t数学\t英语\t总成绩\t平均成绩");
Student student = studentArrList.get(x);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t"
+ student.getGrade() + "\t" + student.getChineseScores() + "\t" + student.getMathScores() + "\t"
+ student.getEnglishScores() + "\t" + student.getTotalScores() + "\t" + student.getAveScores());
}
}
// 查询学生信息
private void findstudentbyname(String path) throws IOException {
ArrayList<Student> studentArrList = new ArrayList<Student>();
readData(path, studentArrList);
System.out.println("请输入需要查找的姓名");
String id = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrList.size(); i++) {
if (id.equals(studentArrList.get(i).getName()))
{
flag = true;
x = i;
break;
}
}
if (flag==false) {
System.out.println("查无此人");
return;
}
else
{
System.out.println("学号\t姓名\t性别\t班级\t语文\t数学\t英语\t总成绩\t平均成绩");
Student student = studentArrList.get(x);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getSex() + "\t"
+ student.getGrade() + "\t" + student.getChineseScores() + "\t" + student.getMathScores() + "\t"
+ student.getEnglishScores() + "\t" + student.getTotalScores() + "\t" + student.getAveScores());
}
}
// 删除学生信息
private void deletestudent(String path) throws IOException {
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData(path, studentArrayList);
System.out.println("请输入要删除的学生信息(学号/姓名)");
String id_name = scannerString();
Boolean flag = false;
int x = 0;
for (int i = 0; i < studentArrayList.size(); i++) {
if (id_name.equals(studentArrayList.get(i).getId()) || id_name.equals(studentArrayList.get(i).getName())) {
flag = true;
x = i;
break;
}
}
if (flag) {
studentArrayList.remove(x);
// 再写入覆盖
writeData(path, studentArrayList);
System.out.println("删除完毕");
} else
System.out.println("查无此人,请重新输入");
}
// 修改学生信息
private void updatestudent(String path) throws IOException {
System.out.println("请输入需要修改的学生学号");
String s = scannerString();
ArrayList<Student> studentArrayList = new ArrayList<Student>();
readData( path, studentArrayList);
for (int i = 0; i < studentArrayList.size(); i++) {
if (s.equals(studentArrayList.get(i).getId())) {
System.out.println("请输入需要修改后的学生姓名");
String name = scannerString();
System.out.println("请输入需要修改后的学生性别");
String sex = scannerString();
System.out.println("请输入需要修改后的学生班级");
String grade = scannerString();
System.out.println("请输入需要添加的学生语文成绩");
double chineseScore = scannerInt();
System.out.println("请输入需要添加的学生数学成绩");
double mathScore = scannerInt();
System.out.println("请输入需要添加的学生英语成绩");
double englishScore = scannerInt();
double totalScore = chineseScore + mathScore + englishScore;
double aveScore = totalScore / 3;
studentArrayList.get(i).setId(s);
studentArrayList.get(i).setName(name);
studentArrayList.get(i).setSex(sex);
studentArrayList.get(i).setGrade(grade);
studentArrayList.get(i).setChineseScores(chineseScore);
studentArrayList.get(i).setMathScores(mathScore);
studentArrayList.get(i).setEnglishScores(englishScore);
studentArrayList.get(i).setTotalScores(totalScore);
studentArrayList.get(i).setAveScores(aveScore);
} else if (studentArrayList.size() == i) {
System.out.println("查无此人");
return;
}
}
writeData(path, studentArrayList);
System.out.println("修改完成");
}
}