欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实验一————————实验代码

程序员文章站 2022-03-17 12:15:20
...

package dao;

import data.Database;
import pojo.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentDao {
private Database database;

public StudentDao(Database database) {
    this.database = database;
}

/**
 *浏览
 */
public void overed(){
    for (Student student:database.getStudents()){
        System.out.println(student.getNumber()+"\t"+student.getName()+"\t"+student.getMaior()+"\t"+student.getChinese()+"\t"+student.getMath()+"\t"+student.getEnglish()+"\t"+student.getPhysics()+"\t"+student.getSum());
    }
}
/**
 * 增加
 */
public void insert(Student student){
    database.getStudents().add(student);
}

/**
 * 修改(仅修改四门科目成绩)
 */
public void modifychinese(int number,int sored){
    //修改语文
    for (Student student:database.getStudents()){
        if(student.getNumber()==number){
            student.setChinese(sored);
        }
    }
}
public void modifymath(int number,int sored){
    //修改数学
    for (Student student:database.getStudents()){
        if(student.getNumber()==number){
            student.setMath(sored);
        }
    }
}
public void modifyenglish(int number,int sored){
    //修改英语
    for (Student student:database.getStudents()){
        if(student.getNumber()==number){
            student.setEnglish(sored);
        }
    }
}
public void modifyphysics(int number,int sored){
    //修改物理
    for (Student student:database.getStudents()){
        if(student.getNumber()==number){
            student.setPhysics(sored);
        }
    }
}
/**
 * 删除
 */
public void delete(int number){
    int i=0;
    for (Student student:database.getStudents()){
        if (number==student.getNumber()){
            i++;
            database.getStudents().remove(student.getNumber()-1);
        }
    }
    if (i==0){
        System.out.println("删除信息未存在");
    }
}

/**
 * 双向冒泡排序
 */
public List<Student> doubleBubbleSort(List<Student> studentList){
    List<Student> list=studentList;
    Student student=null;
    int left=0,right=studentList.size()-1;
    while(left<right)
    {
        for(int i=left+1;i<=right;i++){
            if(list.get(left).getSum()<list.get(i).getSum()){
                student=list.get(i);
                list.set(i,list.get(left));
                list.set(left,student);
            }
        }
        left++;
        for(int i=right-1;i>=left;i--){
            if(list.get(right).getSum()>list.get(i).getSum()){
                student=list.get(i);
                list.set(i,list.get(right));
                list.set(right,student);
            }
        }
        right--;
    }
    return list;
}

/**
 * 希尔排序
 */
public List<Student> shellSort(List<Student> studentList){
    List<Student> list=studentList;
    Student student=null;
    int j;
    for (int gap = list.size() / 2; gap >  0; gap /= 2) {
        for (int i = gap; i < list.size(); i++) {
            student=list.get(i);
            double tmp=student.getSum();
            for (j = i; j >= gap && tmp>list.get(j-gap).getSum(); j -= gap) {
                list.set(j,list.get(j-gap));
            }
            list.set(j,student);
        }
    }
    return list;
}

/**
 * 快速排序
 */
public List<Student> quickSort(List<Student> studentList){
    List<Student> list=studentList;
    quickSort1(list,0,list.size()-1);
    return list;
}
public void quickSort1(List<Student> studentList,int left,int right){
    if(left<right){
        int i=left,j=right;
        Student student=studentList.get(left);
        double x=student.getSum();
        while(i<j){
            while((i<j)&&(studentList.get(j).getSum()<x)){
                j--;
            }
            if(i<j){
                studentList.set(i,studentList.get(j));
                i++;
            }
            while((i<j)&&(studentList.get(i).getSum()>x)){
                i++;
            }
            if(i<j){
                studentList.set(j,studentList.get(i));
                j--;
            }
        }
        studentList.set(i,student);
        quickSort1(studentList,left,i-1);
        quickSort1(studentList,i+1,right);
    }
}
/**
 * 堆排序
 */
public List<Student> heapSort(List<Student> studentList){
    List<Student> list=studentList;
    int len = list.size();

    buildMaxHeap(list, len);

    for (int i = len - 1; i > 0; i--) {
        swap(list, 0, i);
        len--;
        heapify(list, 0, len);
    }
    return list;
}

private void buildMaxHeap(List<Student> studentList, int len) {
    for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
        heapify(studentList, i, len);
    }
}

private void heapify(List<Student> studentList, int i, int len) {
    int left = 2 * i + 1;
    int right = 2 * i + 2;
    int largest = i;

    if (left < len && studentList.get(left).getSum() < studentList.get(largest).getSum()) {
        largest = left;
    }

    if (right < len && studentList.get(right).getSum() < studentList.get(largest).getSum()) {
        largest = right;
    }

    if (largest != i) {
        swap(studentList, i, largest);
        heapify(studentList, largest, len);
    }
}

private void swap(List<Student> studentList, int i, int j) {
    Student student=studentList.get(i);
    studentList.set(i,studentList.get(j));
    studentList.set(j,student);
}
/**
 * 排名赋值并打印
 * @param studentList
 */
public void showPlace(List<Student> studentList){
    for(int i=0;i<studentList.size();i++){
        studentList.get(i).setRanking(i+1);
    }
    int i=1;
    for(Student each:studentList){
        System.out.println(i+"."+" 姓名 "+each.getName()+" 专业 "+each.getMaior()+" 总分 "+each.getSum()+" 名次 "+each.getRanking());
        i++;
    }
}
/**
 * 根据学号查询信息
 */
public void showId(int number){
    int i=0;
    for (Student student:database.getStudents()){
        if (number==student.getNumber())
            i++;
            System.out.println(student.getNumber()+"\t"+student.getName()+"\t"+student.getMaior()+"\t"+student.getChinese()+"\t"+student.getMath()+"\t"+student.getEnglish()+"\t"+student.getPhysics()+"\t"+student.getSum());
    }
    if (i==0){
        System.out.println("暂时未有该学生的信息!");
    }
}
/**
 * 根据姓名查询信息
 */
public void showname(String name){
    int i=0;
    for (Student student:database.getStudents()){
        if (student.getName().equals(name)){
            i++;
            System.out.println("\t"+student.getNumber()+"\t"+student.getName()+"\t"+student.getMaior()+"\t"+student.getChinese()+"\t"+student.getMath()+"\t"+student.getEnglish()+"\t"+student.getPhysics()+"\t"+student.getSum());
        }
    }
    if (i==0){
        System.out.println("暂时未有该学生的信息!");
    }
}
/**
 * 获得所有学生信息
 */
public List<Student> allstudent(){
    List<Student>students=new ArrayList<>();
    for(Student student:database.getStudents()){
        students.add(student);
    }
    return students;
}

}
package data;

import pojo.Student;

import java.util.ArrayList;
import java.util.List;

public class Database {
//用于存储所有学生信息
private ListstudentList=new ArrayList<>();
public List getStudents() {
return studentList;
}
public void setStudents(List students) {
this.studentList = students;
}
public Database(){
}
}
package pojo;

/**

  • 学生基本信息类
    */
    public class Student {
    //学号
    private int number;
    //姓名
    private String name;
    //专业
    private String maior;
    //四门科目成绩
    private int chinese,math ,english,physics;
    //总分
    private int sum;
    //名次
    private int ranking;

    public Student() {
    }

    public Student(int number, String name, String maior, int chinese, int math, int english, int physics,int ranking) {
    this.number = number;
    this.name = name;
    this.maior = maior;
    this.chinese = chinese;
    this.math = math;
    this.english = english;
    this.physics = physics;
    this.ranking = ranking;
    }

    public int getSum() {
    return chinese+math+english+physics;
    }

    public void setSum(int sum) {
    this.sum = sum;
    }

    public double getNumber() {
    return number;
    }

    public void setNumber(int number) {
    this.number = number;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getMaior() {
    return maior;
    }

    public void setMaior(String maior) {
    this.maior = maior;
    }

    public int getChinese() {
    return chinese;
    }

    public void setChinese(int chinese) {
    this.chinese = chinese;
    }

    public int getMath() {
    return math;
    }

    public void setMath(int math) {
    this.math = math;
    }

    public int getEnglish() {
    return english;
    }

    public void setEnglish(int english) {
    this.english = english;
    }

    public int getPhysics() {
    return physics;
    }

    public void setPhysics(int physics) {
    this.physics = physics;
    }

    public int getRanking() {
    return ranking;
    }

    public void setRanking(int ranking) {
    this.ranking = ranking;
    }
    }
    package system;
    /**

  • 系统操作
    */

import dao.StudentDao;
import data.Database;
import pojo.Student;

import java.util.List;
import java.util.Scanner;

public class Operate {
private Scanner scanner=new Scanner(System.in);
private StudentDao studentdao;
/**
* 通过构造方法传入数据库
*/
public Operate (Database database){
studentdao=new StudentDao(database);
}

public void start(){
    System.out.println("学生信息系统:");
    System.out.println("请选择操作项:");
    System.out.println("1.浏览");
    System.out.println("2.增加");
    System.out.println("3.删除");
    System.out.println("4.修改");
    System.out.println("5.查询"+"   "+"请选择:");
    int chioce=scanner.nextInt();
    switch (chioce){
        case 1:
            //浏览
            over();break;
        case 2:
            //增加
            overlist();break;
        case 3:
            //删除
            studentdao.overed();
            System.out.println("请输入删除学生的学号:");
            int number=scanner.nextInt();
             studentdao.delete(number);
             start();
            break;
        case 4:
            //修改
            hack();start();break;
        case 5:
            //查询
            find();start();break;
            default:System.out.println("无效的选项,请重新输入!");
            start();
    }
}
public  void over(){
    List<Student>list=null;
    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("请输入你的选择");
    String choose=scanner.next();
    switch (choose){
        case "1":
            list=studentdao.doubleBubbleSort(studentdao.allstudent());
            studentdao.showPlace(list);
            start();
            break;
        case "2":
            list=studentdao.shellSort(studentdao.allstudent());
            studentdao.showPlace(list);
            start();
            break;
        case "3":
            list=studentdao.quickSort(studentdao.allstudent());
            studentdao.showPlace(list);
            start();
            break;
        case "4":
            list=studentdao.heapSort(studentdao.allstudent());
            studentdao.showPlace(list);
            break;
        case "5":
            start();
            break;
    }
}
public void overlist(){
    Student student=new Student();
    System.out.println("请输入学号:");
    int  numb=scanner.nextInt();
    student.setNumber(numb);
    System.out.println("请输入姓名:");
    String name=scanner.next();
    student.setName(name);
    System.out.println("请输入专业:");
    String major=scanner.next();
    student.setMaior(major);
    System.out.println("请输入语文成绩:");
    int chinese=scanner.nextInt();
    student.setChinese(chinese);
    System.out.println("请输入数学成绩:");
    int math=scanner.nextInt();
    student.setMath(math);
    System.out.println("请输入英语成绩:");
    int english=scanner.nextInt();
    student.setEnglish(english);
    System.out.println("请输入物理成绩:");
    int physics=scanner.nextInt();
    student.setPhysics(physics);
    studentdao.insert(student);
    System.out.println("添加成功!回到上一步");
    start();
}
public void hack(){
    studentdao.overed();
    System.out.println("请输入要修改的学生学号:");
    int num=scanner.nextInt();
    System.out.println("需要修改哪一门科目:");
    System.out.println("1.语文");
    System.out.println("2.数学");
    System.out.println("3.英语");
    System.out.println("4.物理");
    int chioce=scanner.nextInt();
    System.out.println("请输入修改后的成绩:");
    int sored=scanner.nextInt();
    switch (chioce){
        case 1:
            studentdao.modifychinese(num ,sored);break;
        case 2:
            studentdao.modifymath(num,sored);break;
        case 3:
            studentdao.modifyenglish(num,sored);break;
        case 4:
            studentdao.modifyphysics(num,sored);break;
        default:System.out.println("无效的选项,请重新输入!");
        hack();
    }
    start();
}

public void find(){
System.out.println(“请选择查询方式:”);
System.out.println(“1.根据学号查询”);
System.out.println(“2.根据姓名查询”);
System.out.println(“输入其他项回到上一步”);
int choose=scanner.nextInt();
switch (choose){
case 1:
System.out.println(“请输入学号”);
int numb=scanner.nextInt();
studentdao.showId(numb);
find();break;
case 2:
System.out.println(“请输入姓名”);
String name=scanner.next();
studentdao.showname(name);
find();break;
default:
start();
}
}
}

import data.Database;
import system.Operate;

public class Main {

public static void main(String[] args) {
    Database database=new Database();
    Operate operate=new Operate(database);
    operate.start();
}

}

上一篇: JAVA调用JS代码

下一篇: 函数的调用