发布时间:2023-12-08 10:30
package pojo;
import java.util.Objects;
public class StudentNode {
private int id;
private String name;
private String major;
private double A_grade;
private double B_grade;
private double C_grade;
private double D_grade;
private double all_grade;
private int sort;
public StudentNode() {
}
public StudentNode(int id, String name, String major, double A_grade,
double B_grade, double C_grade, double D_grade,
double all_grade, int sort) {
this.id = id;
this.name = name;
this.major = major;
this.A_grade = A_grade;
this.B_grade = B_grade;
this.C_grade = C_grade;
this.D_grade = D_grade;
this.all_grade = all_grade;
this.sort = sort;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getMath_grade() {
return A_grade;
}
public void setMath_grade(double A_grade) {
this.A_grade = A_grade;
}
public double getEnglish_grade() {
return B_grade;
}
public void setEnglish_grade(double B_grade) {
this.B_grade = B_grade;
}
public double getJava_grade() {
return C_grade;
}
public void setJava_grade(double C_grade) {
this.C_grade = C_grade;
}
public double getCpp_grade() {
return D_grade;
}
public void setCpp_grade(double D_grade) {
this.D_grade = D_grade;
}
public void setAll_grade() {
this.all_grade = this.A_grade + this.B_grade + this.C_grade + this.D_grade;
}
public double getAll_grade() {
return all_grade;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StudentNode that = (StudentNode) o;
return id == that.id &&
Double.compare(that.A_grade, A_grade) == 0 &&
Double.compare(that.B_grade, B_grade) == 0 &&
Double.compare(that.C_grade, C_grade) == 0 &&
Double.compare(that.D_grade, D_grade) == 0 &&
Double.compare(that.all_grade, all_grade) == 0 &&
Double.compare(that.sort, sort) == 0 &&
Objects.equals(name, that.name) &&
Objects.equals(major, that.major);
}
public int hashCode() {
return Objects.hash(id, name, major, A_grade, B_grade,
C_grade, D_grade, all_grade, sort);
}
public String toString() {
return id + \"\\t\" + name + \"\\t\" + major + \"\\t \" + A_grade + \"\\t \" + B_grade + \"\\t \" +
C_grade + \"\\t \" + D_grade + \"\\t \" + all_grade + \"\\t \" + sort;
}
}
package dao;
import data.Database;
public class StudentDao {
private Database database;
public StudentDao(Database database)
{
this.database = database;
}
public Database getDatabase() {
return database;
}
}
package service;
import dao.StudentDao;
import data.Database;
import pojo.StudentNode;
import sort.BidirectionalBubbleSort;
import sort.HeapSort;
import sort.QuickSort;
import sort.ShellSort;
import tools.Compare;
import java.util.Collections;
import java.util.Scanner;
public class StudentService {
private Scanner key = new Scanner(System.in);
private StudentDao studentDao;
private BidirectionalBubbleSort bidirectional_bubble_sort = new BidirectionalBubbleSort();
private HeapSort heap_sort = new HeapSort();
private QuickSort quick_sort = new QuickSort();
private ShellSort shell_sort = new ShellSort();
public StudentService(Database database) {
studentDao = new StudentDao(database);
}
/**
* 起始目录
*/
public void start() {
System.out.print(\"******题目一:学生档案管理系统******\");
System.out.println();
mean();
}
public void mean() {
System.out.println(\"##您可选择的目录如下: \");
System.out.println(\"#1.新增学生信息(包括学号、姓名、专业、4门课成绩)\");
System.out.println(\"#2.浏览学生信息(对学生排序后信息进行浏览)\");
System.out.println(\"#3.查找学生信息(查找符合条件的某条记录)\");
System.out.println(\"#4.保存学生信息(保存学生成绩档案信息到文本文件)\");
System.out.println(\"#5.加载学生信息(登录系统后将磁盘文件中保存的学生成绩信息读取到内存中)\");
System.out.println(\"#6.结束\");
System.out.println(\"请选择:\");
int choice = key.nextInt();
switch (choice) {
case 1:
add();//新增学生信息
break;
case 2:
browse();//浏览学生信息
break;
case 3:
find();//查找学生信息
break;
case 4:
save();//保存学生信息
break;
case 5:
load();//加载学生信息
break;
case 6:
// exit();
System.exit(0); // 退出系统
break;
default:
System.out.println(\"输入错误,请重新输入!\");
mean();
}
}
/**
* 浏览学生信息
*/
public void browse() {
//bidirectional_bubble_sort.sortByAll(studentDao.getDatabase().list);
//heap_sort.sortByAll(studentDao.getDatabase().list);
quick_sort.sortByAll(studentDao.getDatabase().list, 0, studentDao.getDatabase().list.size() - 1);
//shell_sort.sortByAll(studentDao.getDatabase().list);
System.out.println(\"学号\" + \"\\t\" + \"姓名\" + \"\\t\" + \"专业\" + \"\\t\" + \"A课程成绩\" + \"\\t\" +
\"B课程成绩\" + \"\\t\" + \"C课程成绩\" + \"\\t\" + \"D课程成绩\" + \"\\t\" + \"总成绩\" + \"\\t\" + \"排名\");
for (StudentNode s : studentDao.getDatabase().list) {
System.out.println(s.toString());
}
// 下一步操作
operation();
}
/**
* 浏览学生信息的后续操作
*/
public void operation() {
System.out.println(\"请输入您的操作:\");
System.out.println(\"1.修改学生信息\");
System.out.println(\"2.删除学生信息\");
System.out.println(\"3.新增学生信息\");
System.out.println(\"4.返回初始目录\");
int choice = key.nextInt();
switch (choice) {
case 1:
modification();//修改学生信息
break;
case 2:
delete();//删除学生信息
break;
case 3:
add();//新增学生信息
break;
case 4:
mean();
//返回主菜单
break;
default:
System.out.println(\"输入错误,请重新输入\");
operation();
}
}
/**
* 查找需要修改学生的信息
*/
public void modification() {
StudentNode student = new StudentNode();
System.out.println(\"请输入您要修改的学生学号\");
int id = key.nextInt();
student = searchById(id);
if (student == null) {
System.out.println(\"没有找到该学生,请重新输入\");
modification();
} else {
System.out.println(\"该学生信息如下:\");
System.out.println(\"学号\" + \"\\t\" + \"姓名\" + \"\\t\" + \"专业\" + \"\\t\" + \"A课程成绩\" + \"\\t\" +
\"B课程成绩\" + \"\\t\" + \"C课程成绩\" + \"\\t\" + \"D课程成绩\" + \"\\t\" + \"总成绩\" + \"\\t\" + \"排名\");
System.out.println(student.toString());
//修改学生信息
modification2(student);
}
}
/**
* 确认是否修改
*
*/
public void modification2(StudentNode student) {
System.out.println(\"请问是否确认修改该学生信息\");
System.out.println(\"确认请输入1,不确认请输入2\");
String choice = key.next();
if (choice.equals(\"1\")) {
// 修改学生信息
modification3(student);
} else if (choice.equals(\"2\")) {
System.out.println(\"已重新返回学生信息\");
browse();
} else {
System.out.println(\"输入错误,请重新输入!\");
modification2(student);
}
}
/**
* 修改学生信息
*
*/
public void modification3(StudentNode student) {
System.out.println(\"请输入您要修改的学生成绩\");
System.out.println(\"1.A课程成绩\");
System.out.println(\"2.B课程成绩\");
System.out.println(\"3.C课程成绩\");
System.out.println(\"4.D课程成绩\");
int choice = key.nextInt();
switch (choice) {
case 1:
// System.out.println(\"您要修改的项目为A课程成绩\");
System.out.println(\"请输入修改后的A课程成绩\");
double math_grade = key.nextDouble();
student.setMath_grade(math_grade);
student.setAll_grade();
break;
case 2:
// System.out.println(\"您要修改的项目为B课程成绩\");
System.out.println(\"请输入修改后的B课程成绩\");
double english_grade = key.nextDouble();
student.setEnglish_grade(english_grade);
student.setAll_grade();
break;
case 3:
// System.out.println(\"您要修改的项目为C课程成绩\");
System.out.println(\"请输入修改后的C课程成绩\");
double java_grade = key.nextDouble();
student.setJava_grade(java_grade);
student.setAll_grade();
break;
case 4:
// System.out.println(\"您要修改的项目为D课程成绩\");
System.out.println(\"请输入修改后的D课程成绩\");
double cpp_grade = key.nextDouble();
student.setCpp_grade(cpp_grade);
student.setAll_grade();
break;
default:
System.out.println(\"输入错误,请重新输入!\");
modification3(student);
}
modification4(student);
}
/**
* 是否要继续修改
*
*/
public void modification4(StudentNode student) {
System.out.println(\"修改成功,请问是否要继续修改\");
System.out.println(\"是请输入1,否请输入2\");
String choice = key.next();
if (choice.equals(\"1\")) {
// 修改学生信息
modification3(student);
} else if (choice.equals(\"2\")) {
System.out.println(\"为您返回学生信息\");
browse();
} else {
System.out.println(\"输入错误,请重新输入\");
modification4(student);
}
}
/**
* 查找需要删除学生的信息
*/
public void delete() {
StudentNode student = null;
System.out.println(\"请输入您要删除的学生学号\");
int id = key.nextInt();
student = searchById(id);
if (student == null) {
System.out.println(\"没有找到该学生,请重新输入\");
delete();
} else {
System.out.println(\"该学生信息如下:\");
System.out.println(\"学号\" + \"\\t\" + \"姓名\" + \"\\t\" + \"专业\" + \"\\t\" + \"A课程成绩\" + \"\\t\" +
\"B课程成绩\" + \"\\t\" + \"C课程成绩\" + \"\\t\" + \"D课程成绩\" + \"\\t\" + \"总成绩\" + \"\\t\" + \"排名\");
System.out.println(student.toString());
//删除学生信息
delete2(student);
}
}
/**
* 确认是否删除
*
*/
public void delete2(StudentNode student) {
System.out.println(\"请问是否确认删除该学生信息\");
System.out.println(\"确认请输入1,不确认请输入2\");
String choice = key.next();
if (choice.equals(\"1\")) {
// 删除学生信息
delete3(student);
} else if (choice.equals(\"2\")) {
System.out.println(\"已为您重新返回学生信息\");
browse();
} else {
System.out.println(\"输入错误,请重新输入\");
delete2(student);
}
}
/**
* 删除学生信息
*
*/
public void delete3(StudentNode student) {
studentDao.getDatabase().list.remove(student);
delete4();
}
/**
* 是否要继续删除
*/
public void delete4() {
System.out.println(\"删除成功,请问是否要继续删除?\");
System.out.println(\"是请输入1,否请输入2\");
String choice = key.next();
if (choice.equals(\"1\")) {
// 删除学生信息
delete();
} else if (choice.equals(\"2\")) {
System.out.println(\"已为您重新返回学生信息\");
browse();
} else {
System.out.println(\"输入错误,请重新输入\");
delete4();
}
}
/**
* 新增学生信息
*/
public void add() {
System.out.println(\"请输入学号\");
int id = key.nextInt();
//检查学号是否与库中重复
if (inspect(id)) {
//若重复,重新输入
System.out.println(\"学号重复,请重新输入\");
add();
} else {
//不重复,继续录入其他信息
System.out.println(\"请输入姓名\");
String name = key.next();
System.out.println(\"请输入专业\");
String major = key.next();
System.out.println(\"请输入A课程成绩\");
double math_grade = key.nextDouble();
System.out.println(\"请输入B课程成绩\");
double english_grade = key.nextDouble();
System.out.println(\"请输入C课程成绩\");
double java_grade = key.nextDouble();
System.out.println(\"请输入D课程成绩\");
double cpp_grade = key.nextDouble();
double all_grade = math_grade + english_grade + java_grade + cpp_grade;
int sort = studentDao.getDatabase().list.size() + 2;
StudentNode student = new StudentNode(id, name, major, math_grade, english_grade,
java_grade, cpp_grade, all_grade, sort);
studentDao.getDatabase().list.add(student);
System.out.println(\"添加成功,已为您返回!\");
mean();
}
}
/**
* 检查学号是否与库中重复
* 重复返回true,不重复返回false
*
*/
public boolean inspect(int id) {
for (StudentNode s : studentDao.getDatabase().list) {
if (s.getId() == id)
return true;
}
return false;
}
/**
* 查找学生信息
*/
public void find() {
System.out.println(\"请选择查找的方式:\");
System.out.println(\"1.学生学号\");
System.out.println(\"2.学生姓名\");
int choice = key.nextInt();
switch (choice) {
case 1:
//学号查找
searchById();
break;
case 2:
//姓名查找
searchByName();
break;
default:
System.out.println(\"输入错误,请重新输入!\");
find();
}
}
/**
* 使用学号查找学生信息
*
*/
public void searchById() {
StudentNode student = null;
System.out.println(\"请输入您要查找学生的学号\");
int id = key.nextInt();
for (StudentNode s : studentDao.getDatabase().list) {
if (s.getId() == id) {
student = s;
}
}
if (student == null) {
System.out.println(\"抱歉,没有找到该学生\");
nextBySearchById();
} else {
System.out.println(\"该学生信息如下:\");
System.out.println(\"学号\" + \"\\t\" + \"姓名\" + \"\\t\" + \"专业\" + \"\\t\" + \"A课程成绩\" + \"\\t\" +
\"B课程成绩\" + \"\\t\" + \"C课程成绩\" + \"\\t\" + \"D课程成绩\" + \"\\t\" + \"总成绩\" + \"\\t\" + \"排名\");
System.out.println(student.toString());
nextBySearch(student);
}
}
/**
* 使用学号查找学生信息(以删除修改信息为目的)
*
*/
public StudentNode searchById(int id) {
StudentNode student = null;
for (StudentNode s : studentDao.getDatabase().list) {
if (s.getId() == id) {
student = s;
}
}
return student;
}
/**
* 使用学号查找失败后的下一步操作
*/
public void nextBySearchById() {
System.out.println(\"请您选择接下来的选择:\");
System.out.println(\"1.重新输入学生学号\");
System.out.println(\"2.使用学生姓名查找\");
System.out.println(\"3.返回初始目录\");
int choice = key.nextInt();
switch (choice) {
case 1:
searchById();
break;
case 2:
searchByName();
break;
case 3:
mean();
break;
default:
System.out.println(\"输入错误,请重新输入!\");
nextBySearchById();
}
}
/**
* 查找成功后的下一步操作
*/
public void nextBySearch(StudentNode student) {
System.out.println(\"请输入接下来的选择\");
System.out.println(\"1.修改学生信息\");
System.out.println(\"2.删除学生信息\");
System.out.println(\"3.返回初始目录\");
int choice = key.nextInt();
switch (choice) {
case 1:
modification3(student);
break;
case 2:
delete3(student);
break;
case 3:
mean();
break;
default:
System.out.println(\"输入错误,请重新输入!\");
nextBySearchById();
}
}
/**
* 使用姓名查找学生信息
*
*/
public void searchByName() {
StudentNode student = null;
System.out.println(\"请输入您要查找学生的姓名:\");
String name = key.next();
for (StudentNode s : studentDao.getDatabase().list) {
if (s.getName().equals(name)) {
student = s;
}
}
if (student == null) {
System.out.println(\"抱歉,没有找到该学生!\");
nextBySearchByName();
} else {
System.out.println(\"该学生信息如下:\");
System.out.println(\"学号\" + \"\\t\" + \"姓名\" + \"\\t\" + \"专业\" + \"\\t\" + \"A课程成绩\" + \"\\t\" +
\"B课程成绩\" + \"\\t\" + \"C课程成绩\" + \"\\t\" + \"D课程成绩\" + \"\\t\" + \"总成绩\" + \"\\t\" + \"排名\");
System.out.println(student.toString());
nextBySearch(student);
}
}
/**
* 使用姓名查找失败后的下一步操作
*/
public void nextBySearchByName() {
System.out.println(\"请您作接下来的选择:\");
System.out.println(\"1.重新输入学生姓名\");
System.out.println(\"2.使用学生学号查找\");
System.out.println(\"3.返回初始目录\");
int choice = key.nextInt();
switch (choice) {
case 1:
searchByName();
break;
case 2:
searchById();
break;
case 3:
mean();
break;
default:
System.out.println(\"输入错误,请重新输入!\");
nextBySearchById();
}
}
/**
* 保存学生信息
*/
public void save() {
Compare c = new Compare();
Collections.sort(studentDao.getDatabase().list, c);
studentDao.getDatabase().save();
System.out.println(\"保存成功!\");
mean();
}
/**
* 加载学生信息
*/
public void load() {
studentDao = new StudentDao(new Database());
System.out.println(\"加载成功!\");
mean();
}
}
package tools;
import pojo.StudentNode;
import java.util.Comparator;
public class Compare implements Comparator {
public int compare(StudentNode o1, StudentNode o2) {
if (o1.getId() > o2.getId())
return 1;
else
return -1;
}
}
package data;
import pojo.StudentNode;
import java.util.ArrayList;
public class Database {
public ReaderWriter rw = new ReaderWriter();
public ArrayList list = new ArrayList<>();
public Database() {
ArrayList list1 = rw.reader();
for (String s : list1) {
String student[] = s.split(\" \");
list.add(new StudentNode(Integer.parseInt(student[0]), student[1], student[2],
Double.parseDouble(student[3]), Double.parseDouble(student[4]),
Double.parseDouble(student[5]), Double.parseDouble(student[6]),
Double.parseDouble(student[7]), Integer.parseInt(student[8])));
}
}
public Database(ArrayList list) {
this.list = list;
}
/**
* 保存信息
*/
public void save() {
ArrayList list1 = new ArrayList<>();
for (StudentNode s : list) {
String s1 = s.getId() + \" \" + s.getName() + \" \" + s.getMajor() + \" \" + s.getMath_grade() + \" \" +
s.getEnglish_grade() + \" \" + s.getJava_grade() + \" \" + s.getCpp_grade() + \" \" +
s.getAll_grade() + \" \" + s.getSort();
list1.add(s1);
}
rw.writer(list1);
}
}
package data;
import java.io.*;
import java.util.ArrayList;
public class ReaderWriter {
public BufferedReader br;
public BufferedWriter bw;
public File file = new File(\"E:/javaText/javaOne/JavaSE/student.txt\");
public ReaderWriter() {
}
public ArrayList reader() {
ArrayList list = new ArrayList<>();
//创建字符缓冲输入流
try {
br = new BufferedReader(
new InputStreamReader(new FileInputStream(file)));
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public boolean writer(ArrayList list) {
try {
bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(file)));
for (String s : list) {
bw.write(s);
bw.newLine();
}
bw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
package sort;
import pojo.StudentNode;
import java.util.ArrayList;
public class BidirectionalBubbleSort {
/**
* 根据总成绩排序
*
*/
public void sortByAll(ArrayList list) {
boolean needNextPass = true;
for (int i = 0, j; i < list.size() / 2 && needNextPass; i++) {
needNextPass = false;
// 正向排序
for (j = i; j < list.size() - 1 - i; j++) {
if (list.get(j).getAll_grade() < list.get(j + 1).getAll_grade()) {
StudentNode student = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, student);
needNextPass = true;
}
}
// 反向排序
for (--j; j > i; j--) {
if (list.get(j).getAll_grade() > list.get(j - 1).getAll_grade()) {
StudentNode student = list.get(j);
list.set(j, list.get(j - 1));
list.set(j - 1, student);
needNextPass = true;
}
}
}
for (StudentNode s : list) {
s.setSort(list.indexOf(s) + 1);
}
}
}
package sort;
import pojo.StudentNode;
import java.util.ArrayList;
public class ShellSort {
public void sortByAll(ArrayList list) {
for (int gap = (int) Math.floor((list.size()) / 2); gap > 0; gap = (int) Math.floor(gap / 2)) {
for (int i = gap; i < list.size(); i++) {
int j = i;
double value = list.get(i).getAll_grade();
StudentNode student = list.get(i);
while (j - gap >= 0 && value > list.get(j - gap).getAll_grade()) {
list.set(j, (list.get(j - gap)));
j = j - gap;
}
list.set(j, student);
}
}
for (StudentNode s : list) {
s.setSort(list.indexOf(s) + 1);
}
}
}
package sort;
import pojo.StudentNode;
import java.util.ArrayList;
public class QuickSort {
public void sortByAll(ArrayList list, int low, int high) {
int i, j;
StudentNode student;
double temp;
if (low > high) {
return;
}
i = low;
j = high;
//temp就是基准位
temp = list.get(low).getAll_grade();
while (i < j) {
//先看右边,依次往左递减
while (temp >= list.get(j).getAll_grade() && i < j) {
j--;
}
//再看左边,依次往右递增
while (temp <= list.get(i).getAll_grade() && i < j) {
i++;
}
//如果满足条件则交换
if (i < j) {
student = list.get(j);
list.set(j, list.get(i));
list.set(i, student);
}
}
//最后将基准为与i和j相等位置的数字交换
student = list.get(low);
list.set(low, list.get(i));
list.set(i, student);
//递归调用左半数组
sortByAll(list, low, j - 1);
//递归调用右半数组
sortByAll(list, j + 1, high);
for (StudentNode s : list) {
s.setSort(list.indexOf(s) + 1);
}
}
}
package sort;
import pojo.StudentNode;
import java.util.ArrayList;
public class HeapSort {
public void sortByAll(ArrayList list) {
//一次调整,最大值到了根节点
for (int i = (list.size() - 1 - 1) / 2; i >= 0; i--) {//i相当于start
adjust(list, i, list.size() - 1);
}
//最后一个值与根节点交换
StudentNode student = new StudentNode();
for (int j = 0; j < list.size() - 1; j++) {
student = list.get(0);
list.set(0, list.get(list.size() - 1 - j));
list.set(list.size() - 1 - j, student);
adjust(list, 0, list.size() - 1 - 1 - j);//调整最大的树
}
for (StudentNode s : list) {
s.setSort(list.indexOf(s) + 1);
}
}
public static void adjust(ArrayList list, int start, int end) {
StudentNode student = list.get(start);
//一次调整过程
for (int i = 2 * start + 1; i <= end; i = 2 * i + 1) {
//找到左右孩子的最大值
if (i < end && list.get(i).getAll_grade() > list.get(i + 1).getAll_grade()) {
i++; //最大值下标
}
if (list.get(i).getAll_grade() < student.getAll_grade()) {
list.set(start, list.get(i)); //设置第start个位置元素的值
start = i;
}
if (list.get(i).getAll_grade() > student.getAll_grade()) {
break;
}
}
list.set(start, student);
}
}
package main;
import service.StudentService;
import data.Database;
/**
* 创建其他类的对象,调用这些对象的方法。
*
*/
public class Main {
public static void main(String[] args) {
Database database = new Database();
StudentService studentService = new StudentService(database);
studentService.start();
}
}