package project.Dao;
import project.Date.Database;
import project.pojo.Student;
import java.util.List;
public class StudentDao {
private Database database;
public StudentDao(Database database) {
this.database = database;
}
public void insert(Student student) {
database.getStudentList().add(student);
}
public List<Student> doubleBuddleSort() {
Student student = new Student();
int left = 0, right = database.getStudentList().size() - 1;
while (left < right) {
for (int i = left + 1; i <= right; i++) {
if (database.getStudentList().get(left).getTotalScore() < database.getStudentList().get(i).getTotalScore()) {
student = database.getStudentList().get(left);
database.getStudentList().set(left, database.getStudentList().get(i));
database.getStudentList().set(i, student);
}
}
left++;
for (int i = right - 1; i >= left; i--) {
if (database.getStudentList().get(i).getTotalScore() < database.getStudentList().get(right).getTotalScore()) {
student = database.getStudentList().get(right);
database.getStudentList().set(right, database.getStudentList().get(i));
database.getStudentList().set(i, student);
}
}
right--;
}
return database.getStudentList();
}
public List<Student> ShellSort() {
List<Student> studentList = database.getStudentList();
int length = studentList.size();
int gap = 1;
while (gap < length) {
gap = gap * 3 + 1;
}
while (gap > 0) {
for (int i = gap; i < length; i++) {
Student student = studentList.get(i);
int j = i - gap;
while (j >= 0 && studentList.get(j).getTotalScore() > student.getTotalScore()) {
studentList.set(j + gap, studentList.get(j));
j -= gap;
}
studentList.set(j + gap, student);
}
gap = gap / 3;
}
for (int i = 0; i < studentList.size(); i++)
studentList.get(i).setRank(studentList.size() - i);
return studentList;
}
public List<Student> QuickSort() {
List<Student> studentList = database.getStudentList();
sort(studentList, 0, studentList.size() - 1);
for (int i = 0; i < studentList.size(); i++)
studentList.get(i).setRank(studentList.size() - i);
return studentList;
}
private void sort(List<Student> studentList, int startIndex, int endIndex) {
if (endIndex <= startIndex)
return;
int midIndex = partition(studentList, startIndex, endIndex);
sort(studentList, startIndex, midIndex - 1);
sort(studentList, midIndex + 1, endIndex);
}
private int partition(List<Student> studentList, int startIndex, int endIndex) {
double mid = studentList.get(startIndex).getTotalScore();
int mark = startIndex;
for (int i = startIndex + 1; i <= endIndex; i++) {
if (studentList.get(i).getTotalScore() < mid) {
mark++;
Student student = studentList.get(mark);
studentList.set(mark, studentList.get(i));
studentList.set(i, student);
}
}
studentList.set(startIndex, studentList.get(mark));
studentList.set(mark, studentList.get(startIndex));
return mark;
}
public List<Student> HeapSort() {
List<Student> studentList = database.getStudentList();
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--;
sink(studentList, 0, length);
}
for (int i = 0; i < studentList.size(); i++)
studentList.get(i).setRank(studentList.size() - i);
return studentList;
}
private void buildHeap(List<Student> studentList, int length) {
for (int i = length / 2; i >= 0; i--) {
sink(studentList, i, length);
}
}
private void sink(List<Student> studentList, int index, int length) {
int leftchild = 2 * index + 1;
int rightchild = 2 * index + 2;
int present = index;
if (leftchild < length && studentList.get(leftchild).getTotalScore() > studentList.get(present).getTotalScore())
present = leftchild;
if (rightchild < length && studentList.get(rightchild).getTotalScore() > studentList.get(present).getTotalScore())
present = rightchild;
if (present != index) {
Student student = studentList.get(index);
studentList.set(index, studentList.get(present));
studentList.set(present, student);
}
}
public void delete(int studentNumber) {
database.getStudentList().remove(studentNumber - 1);
int i = 1;
for (Student student : database.getStudentList()) {
if (i + 1 == student.getStudentNumber()) {
student.setStudentNumber(student.getStudentNumber() - 1);
}
i++;
}
}
public Student findByStudentNumber(int studentNumber) {
Student student = new Student();
for (Student student1 : database.getStudentList()) {
if (student1.getStudentNumber() == studentNumber) {
student = student1;
break;
}
}
return student;
}
public Student findByStudentName(String studentName) {
Student student = new Student();
for (Student student1 : database.getStudentList()) {
if (student1.getStudentName().equals(studentName)) {
student = student1;
break;
}
}
return student;
}
public void modifyChineseScore(int studentNumber, int newScore) {
for (Student student : database.getStudentList()) {
if (student.getStudentNumber() == studentNumber) {
student.setChineseScore(newScore);
}
}
}
public void modifyMathScore(int studentNumber, int newScore) {
for (Student student : database.getStudentList()) {
if (student.getStudentNumber() == studentNumber) {
student.setChineseScore(newScore);
}
}
}
public void modifyEnglishScore(int studentNumber, int newScore) {
for (Student student : database.getStudentList()) {
if (student.getStudentNumber() == studentNumber) {
student.setChineseScore(newScore);
}
}
}
public void modifyComputerScore(int studentNumber, int newScore) {
for (Student student : database.getStudentList()) {
if (student.getStudentNumber() == studentNumber) {
student.setChineseScore(newScore);
}
}
}
}
package project.Date;
import project.pojo.Student;
import java.util.ArrayList;
import java.util.List;
public class Database {
public Database(List<Student>studentList){
this.studentList=studentList;
}
private List<Student> studentList = new ArrayList<>();
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public Database(){
studentList.add(new Student(1101,"小绿","计算机科学与技术",100,50,95,60,305));
studentList.add(new Student(1102,"小红","物联网",90,60,90,70,310));
studentList.add(new Student(1103,"小明","信息安全",80,70,85,85,320));
studentList.add(new Student(1104,"小黑","人工智能",70,80,80,85,315));
studentList.add(new Student(1105,"小蓝","通信工程",60,90,75,75,300));
studentList.add(new Student(1106,"小黄","软件工程",100,100,70,95,365));
}
}
package project.pojo;
public class Student {
private int studentNumber;
private String studentName;
private String studentMajor;
private int mathScore;
private int englishScore;
private int chineseScore;
private int computerScore;
private int totalScore;
private int rank;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public Student(int studentNumber, String studentName, String studentMajor, int mathScore, int englishScore, int chineseScore, int computerScore, int totalScore, int rank) {
this.studentNumber = studentNumber;
this.studentName = studentName;
this.studentMajor = studentMajor;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.chineseScore = chineseScore;
this.computerScore = computerScore;
this.totalScore = totalScore;
this.rank = rank;
}
public Student(){
}
public Student(int studentNumber, String studentName, String studentMajor, int mathScore, int englishScore, int chineseScore, int computerScore, int totalScore) {
this.studentNumber = studentNumber;
this.studentName = studentName;
this.studentMajor = studentMajor;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.chineseScore = chineseScore;
this.computerScore = computerScore;
this.totalScore = totalScore;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentMajor() {
return studentMajor;
}
public void setStudentMajor(String studentMajor) {
this.studentMajor = studentMajor;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getComputerScore() {
return computerScore;
}
public void setComputerScore(int computerScore) {
this.computerScore = computerScore;
}
public int getTotalScore() {
return totalScore;
}
public void setTotalScore(int totalScore) {
this.totalScore = totalScore;
}
}
package project.Service;
import project.Dao.StudentDao;
import project.Date.Database;
import project.pojo.Student;
import java.util.Scanner;
public class Service {
private Scanner scanner = new Scanner(System.in);
private int choice;
private String name;
private StudentDao studentDao;
public Service(Database database) {
studentDao = new StudentDao(database);
}
public void start(){
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("请输入你的选择:");
System.out.println("---------------------------------");
choice=scanner.nextInt();
switch (choice){
case 1:
query();
break;
case 2:
add();
break;
case 3:
delete();
break;
case 4:
modify();
break;
case 5:
browse();
break;
default:
break;
}
}
public void query(){
System.out.println("1.学号查询");
System.out.println("2.姓名查询");
choice=scanner.nextInt();
switch (choice){
case 1:
System.out.println("请输入学号");
choice=scanner.nextInt();
System.out.println("姓名:"+studentDao.findByStudentNumber(choice).getStudentName()+'\t'+"学号:"+studentDao.findByStudentNumber(choice).getStudentNumber()+'\t'+"语文成绩:"+studentDao.findByStudentNumber(choice).getChineseScore()+'\t'+"数学成绩:"+studentDao.findByStudentNumber(choice).getMathScore()+'\t'+"英语成绩"+studentDao.findByStudentNumber(choice).getEnglishScore()+'\t'+"计算机成绩"+studentDao.findByStudentNumber(choice).getComputerScore());
start();
break;
case 2:
System.out.println("请输入姓名:");
name=scanner.next();
System.out.println("姓名:"+studentDao.findByStudentName(name).getStudentName()+'\t'+"学号:"+studentDao.findByStudentName(name).getStudentNumber()+'\t'+"语文成绩:"+studentDao.findByStudentName(name).getChineseScore()+'\t'+"数学成绩:"+studentDao.findByStudentName(name).getMathScore()+'\t'+"英语成绩"+studentDao.findByStudentName(name).getEnglishScore()+'\t'+"计算机成绩"+studentDao.findByStudentName(name).getComputerScore());
start();
break;
default:
System.out.println("输入错误,请重新输入:");
query();
break;
}
}
public void add(){
Student student=new Student();
System.out.println("请输入学生学号:");
student.setStudentNumber(scanner.nextInt());
System.out.println("请输入学生姓名:");
student.setStudentName(scanner.next());
System.out.println("请输入学生语文成绩:");
student.setChineseScore(scanner.nextInt());
System.out.println("请输入学生数学成绩:");
student.setMathScore(scanner.nextInt());
System.out.println("请输入学生英语成绩:");
student.setEnglishScore(scanner.nextInt());
System.out.println("请输入学生计算机成绩:");
student.setComputerScore(scanner.nextInt());
student.setTotalScore(student.getChineseScore()+student.getEnglishScore()+student.getMathScore()+student.getComputerScore());
studentDao.insert(student);
start();
}
public void delete(){
System.out.println("请输入要删除的学生的学号:");
studentDao.delete(scanner.nextInt());
start();
}
public void modify(){
System.out.println("请输入要修改学生的学号");
choice=scanner.nextInt();
System.out.println("输入要修改的科目:");
System.out.println("1.语文");
System.out.println("2.数学");
System.out.println("3.英语");
System.out.println("4.计算机");
switch (scanner.nextInt()){
case 1:
System.out.println("请输入修改后的成绩:");
studentDao.modifyChineseScore(choice,scanner.nextInt());
start();
break;
case 2:
System.out.println("请输入修改后的成绩:");
studentDao.modifyMathScore(choice,scanner.nextInt());
start();
break;
case 3:
System.out.println("请输入修改后的成绩:");
studentDao.modifyEnglishScore(choice,scanner.nextInt());
start();
break;
case 4:
System.out.println("请输入修改后的成绩:");
studentDao.modifyComputerScore(choice,scanner.nextInt());
start();
break;
default:
System.out.println("输入错误,请重新输入:");
modify();
start();
break;
}
}
public void browse(){
System.out.println("0.返回上一层菜单");
System.out.println("1.双向冒泡排序");
System.out.println("2.希尔排序");
System.out.println("3.快速排序");
System.out.println("4.堆排序");
System.out.println("请输入序号:");
int num = scanner.nextInt();
switch (num) {
case 0:
start();
break;
case 1:
doubleBuddleSort();
browse();
break;
case 2:
ShellSort();
browse();
break;
case 3:
QuickSort();
browse();
break;
case 4:
HeapSort();
browse();
break;
default:
System.out.println("无效的选项,请重新输入!");
browse();
break;
}
}
public void doubleBuddleSort(){
int i=1;
for(Student student:studentDao.doubleBuddleSort()){
System.out.println("第"+i+"名:"+"姓名:"+student.getStudentName()+" 总分:"+student.getTotalScore());
i++;
}
}
public void ShellSort(){
int i=studentDao.ShellSort().size();
for(Student student:studentDao.ShellSort()){
System.out.println("第"+i+"名:"+"姓名:"+student.getStudentName()+" 总分:"+student.getTotalScore());
i--;
}
}
public void QuickSort(){
int i=studentDao.QuickSort().size();
for(Student student:studentDao.QuickSort()){
System.out.println("第"+i+"名:"+"姓名:"+student.getStudentName()+" 总分:"+student.getTotalScore());
i--;
}
}
public void HeapSort(){
int i=studentDao.HeapSort().size();
for(Student student:studentDao.HeapSort()){
System.out.println("第"+i+"名:"+"姓名:"+student.getStudentName()+" 总分:"+student.getTotalScore());
i--;
}
}
}
package project.Test;
import project.Date.Database;
import project.Service.Service;
public class test {
public static void main(String[] args) {
Database database=new Database();
Service service=new Service(database);
service.start();
}
}