学生成绩档案管理系统——项目实现
学生成绩档案管理系统——项目实现
源代码
package StudentsSystem;
import java.io.*;
public class test {
public static void main(String[] args) throws Exception {
//创建数据库实例对象
Database database = new Database();
//创建一个学生成绩档案管理系统
StudentsService studentSystem = new StudentsService(database);
//读取学生信息
StringBuffer re = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\13959\\Desktop\\students message.txt"));
String s = br.readLine();
while (s != null) {
re.append(s);
s = br.readLine();
}
s = re.toString();
String[] str = s.split(" ");
//启动系统
studentSystem.start(str);
}
}
package StudentsSystem;
/**
* 学生类
*/
public class Student {
private int ID;
private String Name;
private String Major;
private double Math;
private double English;
private double Politics;
private double PE;
private double Score;
public Student() {
}
public double getScore() {
Score = Math + English + Politics + PE;
return Score;
}
public String getMajor() {
return Major;
}
public double getPE() {
return PE;
}
public void setPE(double PE) {
this.PE = PE;
}
public double getPolitics() {
return Politics;
}
public void setPolitics(double politics) {
Politics = politics;
}
public double getEnglish() {
return English;
}
public void setEnglish(double english) {
English = english;
}
public double getMath() {
return Math;
}
public void setMath(double math) {
Math = math;
}
public void setMajor(String major) {
Major = major;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
package StudentsSystem;
import java.util.ArrayList;
import java.util.List;
public class Database {
private List<Student> studentList = new ArrayList<>();
public Database() {
}
public List<Student> getStudentList() {
return studentList;
}
}
package StudentsSystem;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
/**
* 封装了Student相关数据访问方法
*/
public class StudentDao {
private List<Student> studentList;
public StudentDao(Database database) {
this.studentList = database.getStudentList();
}
/**
* 根据姓名查询学生信息
*/
public Student findByName(String name) {
Student student = new Student();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getName().equals(name))
student = studentList.get(i);
}
return student;
}
/**
* 根据ID查询该学生信息
*
* @param num
* @return
*/
public Student findByID(int num) {
Student student = new Student();
for (int i = 0; i < studentList.size(); i++) {
if (studentList.get(i).getID() == num)
student = studentList.get(i);
}
return student;
}
/**
* 删除学生信息
*
* @param
*/
public void delete(Student student) {
studentList.remove(student);
}
/**
* 新增学生信息
*
* @param student
*/
public void insert(Student student) {
studentList.add(student);
}
/**
* 双向冒泡排序
* @return
*/
public List<Student> bubbleSort() {
int left = 0, right = studentList.size() - 1;
while (left < right) {
for (int i = left + 1; i <= right; i++) {
if (studentList.get(left).getScore() < studentList.get(i).getScore()) {
change(i,left);
}
}
left++;
for (int i = right - 1; i >= left; i--) {
if (studentList.get(right).getScore() > studentList.get(i).getScore()) {
change(i,right);
}
}
right--;
}
return studentList;
}
/**
* 交换
*/
public void change(int i,int j){
Student student = studentList.get(i);
studentList.set(i, studentList.get(j));
studentList.set(j, student);
}
/**
* 希尔排序
*
* @return
*/
public List<Student> shellSort() {
int interval = studentList.size() / 2;
while (interval >= 1) {
// 从 interval 开始往后遍历,将遍历到的数据与其小组进行插入排序
for (int i = interval; i < studentList.size(); i++) {
Student temp = studentList.get(i);
int j = i;
while (j - interval >= 0 && studentList.get(j - interval).getScore() < temp.getScore()) {
studentList.set(j, studentList.get(j - interval));
j = interval;
}
studentList.set(j, temp);
}
interval = interval / 2;
}
return studentList;
}
/**
* 快速排序
*
* @return
*/
public List<Student> quickSort() {
return sort(studentList, 0, studentList.size() - 1);
}
public List<Student> sort(List<Student> list, int left, int right) {
double pivot = list.get(left).getScore();
int i = left;
int j = right;
while (i < j) {
if ((i < j) && (list.get(j).getScore() < pivot)) {
j--;
}
if ((i < j) && (list.get(i).getScore() > pivot)) {
i++;
}
if ((list.get(i).getScore() == list.get(j).getScore()) && (i < j)) {
i++;
} else {
Student temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
if (i - 1 > left) list = sort(list, left, i - 1);
if (j + 1 < right) list = sort(list, j + 1, right);
return list;
}
/**
* 堆排序
*
* @return
*/
public List<Student> heapSort() {
int length = studentList.size();
//构建大根堆,这里其实就是把待排序序列,变成一个大根堆结构的数组
buildHeap(studentList, length);
// 交换堆顶和当前末尾的节点,重置大根堆
for (int i = length - 1; i > 0; i--) {
Student student = studentList.get(0);
studentList.set(0, studentList.get(i));
studentList.set(i, student);
length--;
heapify(studentList, 0, length);
}
return studentList;
}
private void buildHeap(List<Student> studentList, int length) {
// 从最后一个非叶节点开始向前遍历,调整节点性质,使之成为大根堆
for (int i = length / 2; i >= 0; i--) {
heapify(studentList, i, length);
}
}
private void heapify(List<Student> studentList, int i, int length) {
//根据堆的性质,找出它左右节点的索引
int left = 2 * i + 1;
int right = 2 * i + 2;
// 默认当前节点(父节点)是最大值。
int largestIndex = i;
if (left < length && studentList.get(left).getScore() < studentList.get(largestIndex).getScore())
// 如果有左节点,并且左节点的值更大,更新最大值的索引
largestIndex = left;
if (right < length && studentList.get(right).getScore() < studentList.get(largestIndex).getScore())
largestIndex = right;
// 如果有右节点,并且右节点的值更大,更新最大值的索引
if (largestIndex != i) {
// 如果最大值不是当前非叶子节点的值,那么就把当前节点和最大值的子节点值互换
// 互换之后,子节点的值变了,如果该子节点也有自己的子节点,仍需要再次调整
Student student = studentList.get(i);
studentList.set(i, studentList.get(largestIndex));
studentList.set(largestIndex, student);
}
}
/**
* 存储学生信息
*/
public void writing() {
File file = null;
FileWriter fw = null;
file = new File("C:\\Users\\13959\\Desktop\\students message.txt");
try {
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file);
for (int i = 0; i < studentList.size(); i++) {
fw.write(studentList.get(i).getID() + " "); //向文件中写内容
fw.write(studentList.get(i).getName() + " ");
fw.write(studentList.get(i).getMajor() + " ");
fw.write(studentList.get(i).getMath() + " ");
fw.write(studentList.get(i).getEnglish() + " ");
fw.write(studentList.get(i).getPolitics() + " ");
fw.write(studentList.get(i).getPE() + " " + "\n");
fw.flush();
}
System.out.println("保存成功!");
} catch (
IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package StudentsSystem;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class StudentsService {
/**
* 学生成绩档案管理系统
*/
private StudentDao studentDao;
/**
* 构造方法
*
* @param database
*/
public StudentsService(Database database) {
//初始化DAO对象
studentDao = new StudentDao(database);
}
Scanner scanner = new Scanner(System.in);
Random random = new Random();
/**
* 起始菜单
*/
public void start(String[] str) {
/**
* 读取学生信息
*/
for (int i = 0; i <= str.length - 7; i = i + 7) {
Student student = new Student();
student.setID(Integer.parseInt(str[i]));
student.setName(str[i + 1]);
student.setMajor(str[i + 2]);
student.setMath(Double.parseDouble(str[i + 3]));
student.setEnglish(Double.parseDouble(str[i + 4]));
student.setPolitics(Double.parseDouble(str[i + 5]));
student.setPE(Double.parseDouble(str[i + 6]));
studentDao.insert(student);
}
//显示登陆注册起始菜单
System.out.println("//*******************起始菜单*******************//");
System.out.println("1.登陆");
System.out.println("2.退出系统");
System.out.println("请选择:");
int select;
if (!scanner.hasNextInt()) {
System.out.println("输入非法!请重新选择!");
String a = scanner.next();
start(str);
} else {
select = scanner.nextInt();
switch (select) {
case 1:
login();
break;
case 2:
System.out.println("已退出系统!");
break;
default:
System.out.println("输入非法!请重新输入:");
start(str);
}
}
}
/**
* 登陆
*/
public void login() {
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
if (userName.equals("admin")) {
if (password.equals("0000")) {
//随机产生四位验证码
String identifyCode = "";
for (int i = 1; i < 5; i++) {
identifyCode = identifyCode + random.nextInt(10);
}
System.out.println("*****" + identifyCode + "*****");
System.out.println("请输入验证码");
String inputNum = scanner.next();
//判断验证码是否输入正确
if (!inputNum.equals(identifyCode)) {
System.out.print("验证码输入错误!");
backToStart();
} else {
System.out.println("登陆成功!");
System.out.println("文件已读取!");
mainMenu();
}
} else {
System.out.print("密码错误!");
backToStart();
}
} else {
System.out.print("用户名错误!");
backToStart();
}
}
/**
* 返回起始菜单
*/
public void backToStart() {
System.out.println("输入0重新登陆,其他任意键退出系统");
String result = scanner.next();
if (result.equals("0")) {
login();
}
}
/**
* 主菜单
*/
public void mainMenu() {
System.out.println("//********************主菜单*********************//");
System.out.println("1.浏览学生排名信息");
System.out.println("2.查找学生信息");
System.out.println("3.后台管理学生信息(新增和删除)");
System.out.println("0.重新登陆");
System.out.println("请选择:");
int select;
if (!scanner.hasNextInt()) {
System.out.println("输入非法!请重新输入!");
String a = scanner.next();
mainMenu();
} else {
select = scanner.nextInt();
switch (select) {
case 1:
sortStudent();
break;
case 2:
searchMenu();
break;
case 3:
studentManage();
break;
case 0:
login();
break;
default:
System.out.println("输入非法!请重新输入:");
mainMenu();
}
}
}
/**
* 返回主菜单
*/
public void backToMainMenu() {
System.out.println("输入0返回主菜单,其他任意键退出系统");
String result = scanner.next();
if (result.equals("0")) {
mainMenu();
}
}
/**
* 浏览学生排名信息
*/
public void sortStudent() {
System.out.println("请输入需要使用的排序方法:");
System.out.println("1.双向冒泡排序");
System.out.println("2.希尔排序");
System.out.println("3.快速排序");
System.out.println("4.堆排序");
int select = scanner.nextInt();
switch (select) {
case 1:
List<Student> bubbleList = studentDao.bubbleSort();//双向冒泡排序
output(bubbleList);
break;
case 2:
List<Student> shellList = studentDao.shellSort();//希尔排序
output(shellList);
break;
case 3:
List<Student> quickList = studentDao.quickSort();//快速排序
output(quickList);
break;
case 4:
List<Student> heapList = studentDao.heapSort();//堆排序
output(heapList);
break;
}
}
public void output(List<Student> studentList){
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i).getID() + " " + studentList.get(i).getName()
+ " " + studentList.get(i).getMajor() + " " + studentList.get(i).getMath()
+ " " + studentList.get(i).getEnglish() + " " + studentList.get(i).getPolitics()
+ " " + studentList.get(i).getPE() + " " + studentList.get(i).getScore());
}
backToMainMenu();
}
/**
* 查找学生信息
*/
public void searchMenu() {
//显示查询菜单
System.out.println("//****************学生信息查询****************//");
System.out.println("1.根据姓名查询");
System.out.println("2.根据学号查询");
System.out.println("0.返回主菜单");
System.out.println("请选择:");
int select;
if (!scanner.hasNextInt()) {
System.out.println("输入非法!请重新输入!");
String a = scanner.next();
searchMenu();
} else {
select = scanner.nextInt();
switch (select) {
case 1:
nameSearch();
break;
case 2:
IDSearch();
break;
case 0:
mainMenu();
break;
default:
System.out.println("输入非法!请重新输入:");
searchMenu();
}
}
}
/**
* 返回查询菜单
*/
public void backToSearchMenu() {
System.out.println("输入0返回查询菜单,其他任意键退出系统");
String result = scanner.next();
if (result.equals("0")) {
searchMenu();
}
}
/**
* 根据姓名查询学生信息
*/
public void nameSearch() {
System.out.println("请输入学生姓名:");
String studentName = scanner.next();
Student student = studentDao.findByName(studentName);
print(student);
}
/**
* 根据学号查询学生信息
*/
public void IDSearch() {
System.out.println("请输入学生学号:");
int ID = scanner.nextInt();
Student student = studentDao.findByID(ID);
print(student);
}
/**
* 打印学生信息
*/
public void print(Student student){
System.out.println(student.getID() + " " + student.getName()
+ " " + student.getMajor() + " " + student.getMath()
+ " " + student.getEnglish() + " " + student.getPolitics()
+ " " + student.getPE() + " " + student.getScore());
backToSearchMenu();
}
/**
* 学生信息管理
*/
public void studentManage() {
System.out.println("//****************后台管理系统****************//");
System.out.println("1.新增学生信息");
System.out.println("2.修改学生成绩");
System.out.println("3.删除学生信息");
System.out.println("0,返回主菜单");
System.out.println("请选择:");
int select;
if (!scanner.hasNextInt()) {
System.out.println("输入非法!请重新输入!");
String a = scanner.next();
studentManage();
} else {
select = scanner.nextInt();
switch (select) {
case 1:
addStudent();
break;
case 2:
modifyScore();
break;
case 3:
deleteStudent();
break;
case 0:
mainMenu();
break;
default:
System.out.println("输入非法!请重新输入:");
studentManage();
}
}
}
/**
* 返回后台管理系统
*/
public void backToManage() {
System.out.println("输入0返回后台管理系统,其他任意键退出系统");
String result = scanner.next();
if (result.equals("0")) {
studentManage();
}
}
/**
* 新增学生信息
*/
public void addStudent() {
Student student = new Student();
System.out.println("请输入新增的学生姓名:");
String addName = scanner.next();
student.setName(addName);
System.out.println("请输入新增学生学号:");
int addID = scanner.nextInt();
student.setID(addID);
System.out.println("请输入该学生专业:");
String addMajor = scanner.next();
student.setMajor(addMajor);
System.out.println("请输入该学生数学成绩:");
double addMath = scanner.nextDouble();
student.setMath(addMath);
System.out.println("请输入该学生英语成绩:");
double addEnglish = scanner.nextDouble();
student.setEnglish(addEnglish);
System.out.println("请输入该学生政治成绩:");
double addPolitics = scanner.nextDouble();
student.setPolitics(addPolitics);
System.out.println("请输入该学生体育成绩:");
double addPE = scanner.nextDouble();
student.setPE(addPE);
studentDao.insert(student);
System.out.println("添加成功!");
studentDao.writing();
backToManage();
}
/**
* 修改学生四门课成绩
*/
public void modifyScore() {
System.out.println("请输入需要修改的学生ID:");
int modifyID = scanner.nextInt();
System.out.println("请输入需要修改的科目:");
System.out.println("1.数学:");
System.out.println("2.英语:");
System.out.println("3.政治:");
System.out.println("4.体育:");
int select = scanner.nextInt();
switch (select) {
case 1:
System.out.println("请输入新成绩:");
double newMath = scanner.nextDouble();
studentDao.findByID(modifyID).setMath(newMath);
System.out.println("修改成功!");
break;
case 2:
System.out.println("请输入新成绩:");
double newEnglish = scanner.nextDouble();
studentDao.findByID(modifyID).setEnglish(newEnglish);
System.out.println("修改成功!");
break;
case 3:
System.out.println("请输入新成绩:");
double newPolitics = scanner.nextDouble();
studentDao.findByID(modifyID).setPolitics(newPolitics);
System.out.println("修改成功!");
break;
case 4:
System.out.println("请输入新成绩:");
double newPE = scanner.nextDouble();
studentDao.findByID(modifyID).setPE(newPE);
System.out.println("修改成功!");
break;
}
studentDao.writing();
backToMainMenu();
}
/**
* 删除学生信息
*/
public void deleteStudent() {
System.out.println("请输入需要删除的学生ID:");
int modifyID = scanner.nextInt();
studentDao.delete(studentDao.findByID(modifyID));
System.out.println("删除成功!");
studentDao.writing();
backToMainMenu();
}
}
运行结果
2021.3.15修改:可输入选择需要使用的排序方法。