数据结构实践一:代码实现

学生成绩档案管理系统

代码实现
一. 数据类及数据交互类
  • 学生信息类
package pojo;

/**
 * 学生信息类
 */
public class Student implements java.io.Serializable {

    //学号
    private String stuNum;
    //姓名
    private String stuName;
    //专业
    private String stuMajor;
    //课程1成绩
    private double stuScore1;
    //课程2成绩
    private double stuScore2;
    //课程3成绩
    private double stuScore3;
    //课程4成绩
    private double stuScore4;
    //总成绩
    private double stuTotalScore;
    //排名,初始为0
    private int stuRank;

    /**
     * 初始化学生信息
     *
     * @param stuNum
     * @param stuName
     * @param stuMajor
     * @param stuScore1
     * @param stuScore2
     * @param stuScore3
     * @param stuScore4
     */
    public Student(String stuNum, String stuName, String stuMajor, double stuScore1, double stuScore2, double stuScore3,
                   double stuScore4) {
        this.stuNum = stuNum;
        this.stuName = stuName;
        this.stuMajor = stuMajor;
        this.stuScore1 = stuScore1;
        this.stuScore2 = stuScore2;
        this.stuScore3 = stuScore3;
        this.stuScore4 = stuScore4;
        this.stuTotalScore = (this.stuScore1 + this.stuScore2 + this.stuScore3 + this.stuScore4);
        this.stuRank = 0;
    }

    /**
     * 获取学生学号
     *
     * @return
     */
    public String getStuNum() {
        return stuNum;
    }

    /**
     * 更新学生学号
     *
     * @param stuNum
     */
    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    /**
     * 获取学生姓名
     *
     * @return
     */
    public String getStuName() {
        return stuName;
    }

    /**
     * 更新学生姓名
     *
     * @param stuName
     */
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    /**
     * 获取学生专业
     *
     * @return
     */
    public String getStuMajor() {
        return stuMajor;
    }

    /**
     * 更新学生专业
     *
     * @param stuMajor
     */
    public void setStuMajor(String stuMajor) {
        this.stuMajor = stuMajor;
    }

    /**
     * 获取课程1成绩
     *
     * @return
     */
    public double getStuScore1() {
        return stuScore1;
    }

    /**
     * 更新课程1成绩
     *
     * @param stuScore1
     */
    public void setStuScore1(double stuScore1) {
        this.stuScore1 = stuScore1;
        setStuTotalScore();
    }

    /**
     * 获取课程2成绩
     *
     * @return
     */
    public double getStuScore2() {
        return stuScore2;
    }

    /**
     * 更新课程2成绩
     *
     * @param stuScore2
     */
    public void setStuScore2(double stuScore2) {
        this.stuScore2 = stuScore2;
        setStuTotalScore();
    }

    /**
     * 获取课程3成绩
     *
     * @return
     */
    public double getStuScore3() {
        return stuScore3;
    }

    /**
     * 更新课程3成绩
     *
     * @param stuScore3
     */
    public void setStuScore3(double stuScore3) {
        this.stuScore3 = stuScore3;
        setStuTotalScore();
    }

    /**
     * 获取课程4成绩
     *
     * @return
     */
    public double getStuScore4() {
        return stuScore4;
    }

    /**
     * 更新课程4成绩
     *
     * @param stuScore4
     */
    public void setStuScore4(double stuScore4) {
        this.stuScore4 = stuScore4;
        setStuTotalScore();
    }

    /**
     * 获取总成绩
     *
     * @return
     */
    public double getStuTotalScore() {
        setStuTotalScore();
        return stuTotalScore;
    }

    /**
     * 更新总成绩
     */
    public void setStuTotalScore() {
        this.stuTotalScore = (this.stuScore1 + this.stuScore2 + this.stuScore3 + this.stuScore4);
    }

    /**
     * 获取排名
     *
     * @return
     */
    public int getStuRank() {
        return stuRank;
    }

    /**
     * 更新排名
     *
     * @param stuRank
     */
    public void setStuRank(int stuRank) {
        this.stuRank = stuRank;
    }
}

  • 系统账户信息类
package pojo;

/**
 * 系统用户类
 */
public class Account implements java.io.Serializable{

    //用户账户名
    private String accountName;
    //账户登录密码
    private String accountPassword;

    /**
     * 初始化用户账户信息
     *
     * @param accountName
     * @param accountPassword
     */
    public Account(String accountName,String accountPassword) {
        this.accountName=accountName;
        this.accountPassword=accountPassword;
    }

    /**
     * 获取账户名
     * @return
     */
    public String getAccountName() {
        return accountName;
    }

    /**
     * 更改账户名
     * @param accountName
     */
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    /**
     * 获取账户密码
     * @return
     */
    public String getAccountPassword() {
        return accountPassword;
    }

    /**
     * 更改账户密码
     * @param accountPassword
     */
    public void setAccountPassword(String accountPassword) {
        this.accountPassword = accountPassword;
    }
}

  • 学生信息交互类
package pojo;

import java.io.*;
import java.util.List;

/**
 * 学生信息文本交互类
 */
public class StuIO {

    /**
     * 学生信息导出
     *
     * @param studentsList
     */
    public void outPut(List<Student> studentsList) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\IDEA\\Stu Info Sys\\Student.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(studentsList);
            objectOutputStream.flush();
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
        }
    }

    /**
     * 学生信息导入
     *
     * @return
     */
    public List<Student> inPut() {
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\IDEA\\Stu Info Sys\\Student.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            List<Student> studentList = (List<Student>) objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
            return studentList;
        } catch (Exception e) {
            return null;
        }
    }

}

  • 账户信息交互类
package pojo;

import java.io.*;
import java.util.List;

/**
 * 用户账户文本交互类
 */
public class AccountIO {

    /**
     * 用户账户信息导出
     * @param accountsList
     */
    public void outPut(List<Account> accountsList){
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("D:\\IDEA\\Stu Info Sys\\Account.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(accountsList);
            objectOutputStream.flush();
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
        }
    }

    /**
     * 用户账户信息导入
     * @return
     */
    public List<Account> inPut(){
        try {
            FileInputStream fileInputStream=new FileInputStream("D:\\IDEA\\Stu Info Sys\\Account.txt");
            ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
            List<Account>accountsList=(List<Account>)objectInputStream.readObject();
            objectInputStream.close();
            fileInputStream.close();
            return accountsList;
        } catch (Exception e) {
            return null;
        }
    }

}

二. 数据库类
package database;

import pojo.Account;
import pojo.AccountIO;
import pojo.StuIO;
import pojo.Student;

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

/**
 * 数据库类
 */
public class Database implements java.io.Serializable {

    //账户信息表
    List<Account> accountsList = new ArrayList<>();
    //学生信息表
    List<Student> studentsList = new ArrayList<>();

    AccountIO accountIO = new AccountIO();
    StuIO stuIO = new StuIO();

    /**
     * 数据库初始化
     */
    public Database() {
        try {
            accountsList = accountIO.inPut();
            studentsList = stuIO.inPut();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 账户信息表导出
     *
     * @param accountsList
     */
    public void accountOut(List<Account> accountsList) {
        accountIO.outPut(accountsList);
    }

    /**
     * 账户信息表导入
     */
    public void accountIn() {
        accountsList = accountIO.inPut();
    }

    /**
     * 学生信息表导出
     *
     * @param studentsList
     */
    public void stuOut(List<Student> studentsList) {
        stuIO.outPut(studentsList);
    }

    /**
     * 学生信息表导入
     */
    public void stuIn() {
        studentsList = stuIO.inPut();
    }

    /**
     * 获取学生信息表
     *
     * @return
     */
    public List<Student> getStudentsList() {
        return studentsList;
    }

    /**
     * 更新学生信息表
     *
     * @param studentsList
     */
    public void setStudentsList(List<Student> studentsList) {
        this.studentsList = studentsList;
    }

    /**
     * 获取账户信息表
     *
     * @return
     */
    public List<Account> getAccountsList() {
        return accountsList;
    }

    /**
     * 更新账户信息表
     *
     * @param accountList
     */
    public void setAccountsList(List<Account> accountList) {
        this.accountsList = accountList;
    }

}

二. 数据操作类
  • 学生信息操作类
package dao;

import database.Database;
import pojo.Student;

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

/**
 * 学生信息方法类
 */
public class StuDao {

    private Database database;

    /**
     * 初始化
     *
     * @param database
     */
    public StuDao(Database database) {
        this.database = database;
    }

    /**
     * 展示所有学生信息
     */
    public void showAllStus() {
        List<Student> list = database.getStudentsList();
        database.stuOut(list);
        if (list != null) {
            int n = 1;
            for (Student student : list) {
                System.out.println(n +
                        "\t学号:" + student.getStuNum() +
                        "\t姓名:" + student.getStuName()
                        + "\t专业:" + student.getStuMajor()
                        + "\t课程1成绩:" + student.getStuScore1()
                        + "\t课程2成绩:" + student.getStuScore2()
                        + "\t课程3成绩:" + student.getStuScore3()
                        + "\t课程4成绩:" + student.getStuScore4()
                        + "\t总分:" + student.getStuTotalScore());
                n++;
            }
        } else {
            System.out.println("无学生信息录入");
        }
    }

    /**
     * 获取学生人数
     *
     * @return
     */
    public int StusNum() {
        return database.getStudentsList().size();
    }

    /**
     * 展示指定学生信息
     *
     * @param student
     */
    public void showStu(Student student) {
        System.out.println("1" +
                "\t学号:" + student.getStuNum() +
                "\t姓名:" + student.getStuName()
                + "\t专业:" + student.getStuMajor()
                + "\t课程1成绩:" + student.getStuScore1()
                + "\t课程2成绩:" + student.getStuScore2()
                + "\t课程3成绩:" + student.getStuScore3()
                + "\t课程4成绩:" + student.getStuScore4()
                + "\t总分:" + student.getStuTotalScore()
                + "\t排名:" + student.getStuRank());
    }

    /**
     * 展示多个学生信息
     *
     * @param studentList
     */
    public void showStus(List<Student> studentList) {
        int n = 1;
        for (Student student : studentList) {
            System.out.println(n +
                    "\t学号:" + student.getStuNum() +
                    "\t姓名:" + student.getStuName()
                    + "\t专业:" + student.getStuMajor()
                    + "\t课程1成绩:" + student.getStuScore1()
                    + "\t课程2成绩:" + student.getStuScore2()
                    + "\t课程3成绩:" + student.getStuScore3()
                    + "\t课程4成绩:" + student.getStuScore4()
                    + "\t总分:" + student.getStuTotalScore()
                    + "\t排名:" + student.getStuRank());
            n++;
        }
    }

    /**
     * 添加学生
     *
     * @param student
     */
    public void addStu(Student student) {
        if (isEmpty() == false) {
            database.getStudentsList().add(student);
        } else {
            List<Student> list = new ArrayList<>();
            list.add(student);
            database.setStudentsList(list);
        }
        database.stuOut(database.getStudentsList());
    }

    /**
     * 判断学生信息表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        if (database.getStudentsList() == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取学生信息表
     *
     * @return
     */
    public List<Student> getStus() {
        return database.getStudentsList();
    }

    /**
     * 更新学生信息表
     *
     * @param list
     */
    public void setStus(List<Student> list) {
        database.setStudentsList(list);
    }

    /**
     * 删除学生
     *
     * @param num
     */
    public void deleteStu(int num) {
        database.getStudentsList().remove(num);
        database.stuOut(database.getStudentsList());
    }

    /**
     * 学号查找学生
     *
     * @param num
     * @return
     */
    public Student searchStuByNum(String num) {
        Student s = null;
        for (Student stu : database.getStudentsList()) {
            if (num.equals(stu.getStuNum())) {
                s = stu;
            }
        }
        return s;
    }

    /**
     * 姓名查找学生
     *
     * @param name
     * @return
     */
    public List<Student> searchStuByName(String name) {
        List<Student> list = new ArrayList<>();
        for (Student stu : database.getStudentsList()) {
            if (name.equals(stu.getStuName())) {
                list.add(stu);
            }
        }
        return list;
    }

    /**
     * 更新学生信息
     *
     * @param student
     * @param choice
     * @param score
     */
    public void setStu(Student student, int choice, double score) {
        switch (choice) {
            case 1:
                student.setStuScore1(score);
                break;
            case 2:
                student.setStuScore2(score);
                break;
            case 3:
                student.setStuScore3(score);
                break;
            case 4:
                student.setStuScore4(score);
                break;
        }
        System.out.println("修改完成!");
    }

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

    /**
     * 快速排序
     *
     * @param studentsList
     * @param left
     * @param right
     * @return
     */
    public List<Student> quickSort(List<Student> studentsList, int left, int right) {
        if (left < right) {
            List<Student> list = studentsList;
            int pivot = (left + right) / 2;
            Student stu = studentsList.get(right);
            studentsList.set(right, studentsList.get(pivot));
            studentsList.set(pivot, stu);
            pivot = partition(studentsList, left, right);
            quickSort(list, left, pivot - 1);
            quickSort(list, pivot + 1, right);
            rank(list);
            return list;
        } else {
            return null;
        }
    }

    /**
     * 一次划分
     *
     * @param studentsList
     * @param left
     * @param right
     * @return
     */
    public int partition(List<Student> studentsList, int left, int right) {
        int i = left, j = right;
        Student student = studentsList.get(right);
        while (i != j) {
            while ((i < j) && (studentsList.get(i).getStuTotalScore() >= student.getStuTotalScore())) {
                i++;
            }
            if (i < j) {
                studentsList.set(j, studentsList.get(i));
                j--;
            }
            while ((i < j) && (studentsList.get(j).getStuTotalScore() <= student.getStuTotalScore())) {
                j--;
            }
            if (i < j) {
                studentsList.set(i, studentsList.get(j));
                i++;
            }
        }
        studentsList.set(i, student);
        return i;

    }

    /**
     * 希尔排序
     *
     * @param studentsList
     * @return
     */
    public List<Student> shellSort(List<Student> studentsList) {
        List<Student> list = studentsList;
        Student stu = null;
        for (int d = list.size() / 2; d > 0; d /= 2) {
            for (int i = d; i < list.size(); i++) {
                stu = list.get(i);
                int j;
                for (j = i - d; j >= 0 && stu.getStuTotalScore() > list.get(j).getStuTotalScore(); j -= d) {
                    list.set(j + d, list.get(j));
                }
                list.set(j + d, stu);
            }
        }
        rank(list);
        return list;
    }

    /**
     * 堆排序
     *
     * @param studentsList
     * @return
     */
    public List<Student> HeapSort(List<Student> studentsList) {
        List<Student> list = studentsList;
        Student stu = null;
        int m = list.size();
        for (int i = (m - 1) / 2; i >= 0; i--) {
            sifi(list, i, m);
        }
        for (int i = m - 1; i > 0; i--) {
            stu = list.get(i);
            list.set(i, list.get(0));
            list.set(0, stu);
            sifi(list, 0, --m);
        }
        rank(list);
        return list;
    }

    /**
     * 调整堆
     *
     * @param studentsList
     * @param k
     * @param m
     */
    public void sifi(List<Student> studentsList, int k, int m) {
        int left = 2 * k + 1;
        int right = 2 * k + 2;
        int largest = k;
        if (left < m && studentsList.get(left).getStuTotalScore() < studentsList.get(largest).getStuTotalScore()) {
            largest = left;
        }
        if (right < m && studentsList.get(right).getStuTotalScore() < studentsList.get(largest).getStuTotalScore()) {
            largest = right;
        }
        if (largest != k) {
            //swap(studentList, i, largest);
            Student student = studentsList.get(largest);
            studentsList.set(largest, studentsList.get(k));
            studentsList.set(k, student);
            sifi(studentsList, largest, m);
        }
    }

    /**
     * 成绩排名
     *
     * @param list
     */
    public void rank(List<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStuRank(i + 1);
        }
    }
}

  • 账户信息操作类
package dao;

import database.Database;
import pojo.Account;

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

/**
 * 系统用户方法类
 */
public class AccountDao {

    private Database database;

    /**
     * 初始化
     *
     * @param database
     */
    public AccountDao(Database database) {
        this.database = database;
    }

    /**
     * 判断系统用户表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        if (database.getAccountsList() == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过用户名查找用户账户信息
     *
     * @param accountName
     * @return
     */
    public Account findByName(String accountName) {
        Account account = null;
        //查找该用户名是否存在
        for (Account acc : database.getAccountsList()) {
            if (acc.getAccountName().equals(accountName)) {
                account = acc;
                break;
            }
        }
        return account;
    }

    /**
     * 添加用户
     *
     * @param account
     */
    public void addAccount(Account account) {
        if (isEmpty() == false) {
            database.getAccountsList().add(account);
            database.accountOut(database.getAccountsList());
        } else {
            List<Account> list = new ArrayList<>();
            list.add(account);
            database.setAccountsList(list);
            database.accountOut(database.getAccountsList());
        }
    }
}

四. 系统交互类
  • 主系统
package service;

import database.Database;
import pojo.Account;

import java.util.Scanner;

/**
 * 主系统类
 */
public class MainSystem {

    static Database database = new Database();
    static StuSystem stuSystem = new StuSystem(database);
    static AccountSystem accountSystem = new AccountSystem(database);
    Scanner scan = new Scanner(System.in);

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String[] args) {
        run();
    }

    /**
     * 系统入口
     */
    static void run() {
        //判断用户是否已登录
        if (accountSystem.start()) {
            System.out.println("欢迎使用学生信息管理系统:");
            MainMenu();
        }
    }

    /**
     * 系统状态监控
     */
    static void MainMenu() {
        switch (stuSystem.MainMenu()) {
            case 0:
                //返回系统登录界面
                run();
                break;
            case 1:
                if (stuSystem.showStudents() == 0) {
                    //从展示所有学生信息界面返回上层
                    MainMenu();
                }
                break;
            case 2:
                if (stuSystem.findStudent() == 0) {
                    //从查找学生界面返回上层
                    MainMenu();
                }
                break;
            case 3:
                if (stuSystem.addStu() == 0) {
                    //从新增学生信息界面返回上层
                    MainMenu();
                }
                break;
            case 4:
                if (stuSystem.setStu() == 0) {
                    //从修改学生信息界面返回上层
                    MainMenu();
                }
                break;
            case 5:
                if (stuSystem.deleteStu() == 0) {
                    //从删除学生信息界面返回上层
                    MainMenu();
                }
        }
    }
}

  • 用户登录子系统
package service;

import dao.AccountDao;
import database.Database;
import pojo.Account;

import java.util.Random;
import java.util.Scanner;

/**
 * 用户子系统
 */
public class AccountSystem {

    private Scanner scan = new Scanner(System.in);
    private AccountDao accountDao;

    public AccountSystem(Database database) {
        this.accountDao = new AccountDao(database);
    }

    /**
     * 系统登录界面
     *
     * @return
     */
    public boolean start() {
        int choice;
        while (true) {
            System.out.println("系统登录界面:");
            System.out.println("1. 登录");
            System.out.println("2. 注册");
            System.out.println("3. 退出系统");
            System.out.println("请选择:");
            String choice1 = scan.next();
            try {
                choice = Integer.parseInt(choice1);
                break;
            } catch (NumberFormatException e) {
                System.out.println("选项输入有误!请重新输入!");
            }
        }
        switch (choice) {
            case 1:
                return login();
            case 2:
                regist();
                return start();
            case 3:
                System.out.println("退出了系统!");
                return false;
            default:
                System.out.println("无效的选项,请重新输入!");
                return start();
        }
    }

    /**
     * 用户登录
     *
     * @return
     */
    public boolean login() {
        System.out.println("请输入用户名:");
        String accountName = scan.next();
        System.out.println("请输入密码:");
        String password = scan.next();
        if (accountDao.isEmpty() == false) {
            if (accountDao.findByName(accountName) != null) {
                if (password.equals(accountDao.findByName(accountName).getAccountPassword())) {
                    System.out.println("登陆成功!\n");
                    return true;
                } else {
                    while (true) {
                        int choice;
                        while (true) {
                            System.out.println("密码错误!");
                            System.out.println("是否重新登录?");
                            System.out.println("1. 重新登陆");
                            System.out.println("2. 返回主菜单");
                            System.out.println("3. 退出系统");
                            String choice1 = scan.next();
                            try {
                                choice = Integer.parseInt(choice1);
                                break;
                            } catch (NumberFormatException e) {
                                System.out.println("选项输入有误!请重新输入!");
                            }
                        }
                        switch (choice) {
                            case 1:
                                return login();
                            case 2:
                                return start();
                            case 3:
                                return false;
                            default:
                                System.out.println("选项输入有误!请重新输入!");
                        }
                    }
                }
            } else {
                while (true) {
                    int choice;
                    while (true) {
                        System.out.println("用户名有误!请重新输入!");
                        System.out.println("是否重新登录?");
                        System.out.println("1. 重新登陆");
                        System.out.println("2. 返回主菜单");
                        System.out.println("3. 退出系统");
                        String choice1 = scan.next();
                        try {
                            choice = Integer.parseInt(choice1);
                            break;
                        } catch (NumberFormatException e) {
                            System.out.println("选项输入有误!请重新输入!");
                        }
                    }
                    switch (choice) {
                        case 1:
                            return login();
                        case 2:
                            return start();
                        case 3:
                            return false;
                        default:
                            System.out.println("选项输入有误!请重新输入!");
                    }
                }
            }
        } else {
            while (true) {
                int choice;
                while (true) {
                    System.out.println("系统无用户!");
                    System.out.println("是否进行注册?");
                    System.out.println("1. 注册用户");
                    System.out.println("0. 退出系统");
                    String choice1 = scan.next();
                    try {
                        choice = Integer.parseInt(choice1);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("选项输入有误!请重新输入!");
                    }
                }
                switch (choice) {
                    case 1:
                        regist();
                        return start();
                    case 0:
                        return false;
                    default:
                        System.out.println("选项输入有误!请重新输入!");
                }
            }
        }
    }

    /**
     * 用户注册
     */
    public void regist() {
        System.out.println("请输入用户名:");
        String accountName = scan.next();
        System.out.println("请输入密码:");
        String password = scan.next();
        System.out.println("请再次输入密码:");
        if (scan.next().equals(password)) {
            if (accountDao.isEmpty() == false) {
                if (accountDao.findByName(accountName) == null) {
                    String code = "";
                    for (int i = 0; i < 4; i++) {
                        code += new Random().nextInt(10);
                    }
                    System.out.println("验证码:" + code);
                    System.out.println("请输入验证码:");
                    if (scan.next().equals(code)) {
                        accountDao.addAccount(new Account(accountName, password));
                        System.out.println("注册成功!\n");
                        return;
                    } else {
                        while (true) {
                            int choice;
                            while (true) {
                                System.out.println("验证码有误!");
                                System.out.println("是否重新注册?");
                                System.out.println("1. 重新注册");
                                System.out.println("2. 返回主菜单");
                                System.out.println("3. 退出系统");
                                String choice1 = scan.next();
                                try {
                                    choice = Integer.parseInt(choice1);
                                    break;
                                } catch (NumberFormatException e) {
                                    System.out.println("选项输入有误!请重新输入!");
                                }
                            }
                            switch (choice) {
                                case 1:
                                    regist();
                                    return;
                                case 2:
                                    return;
                                case 3:
                                    return;
                                default:
                                    System.out.println("选项输入有误!请重新输入!");
                            }
                        }
                    }
                } else {
                    while (true) {
                        int choice;
                        while (true) {
                            System.out.println("该用户名已被注册!");
                            System.out.println("是否重新注册?");
                            System.out.println("1. 重新注册");
                            System.out.println("2. 返回主菜单");
                            System.out.println("3. 退出系统");
                            String choice1 = scan.next();
                            try {
                                choice = Integer.parseInt(choice1);
                                break;
                            } catch (NumberFormatException e) {
                                System.out.println("选项输入有误!请重新输入!");
                            }
                        }
                        switch (choice) {
                            case 1:
                                regist();
                                return;
                            case 2:
                                return;
                            case 3:
                                return;
                            default:
                                System.out.println("选项输入有误!请重新输入!");
                        }
                    }
                }
            } else {
                String code = "";
                for (int i = 0; i < 4; i++) {
                    code += new Random().nextInt(10);
                }
                System.out.println("验证码:" + code);
                System.out.println("请输入验证码:");
                if (scan.next().equals(code)) {
                    accountDao.addAccount(new Account(accountName, password));
                    System.out.println("注册成功!\n");
                    return;
                } else {
                    while (true) {
                        int choice;
                        while (true) {
                            System.out.println("验证码有误!");
                            System.out.println("是否重新注册?");
                            System.out.println("1. 重新注册");
                            System.out.println("2. 返回主菜单");
                            System.out.println("3. 退出系统");
                            String choice1 = scan.next();
                            try {
                                choice = Integer.parseInt(choice1);
                                break;
                            } catch (NumberFormatException e) {
                                System.out.println("选项输入有误!请重新输入!");
                            }
                        }
                        switch (choice) {
                            case 1:
                                regist();
                                return;
                            case 2:
                                return;
                            case 3:
                                return;
                            default:
                                System.out.println("选项输入有误!请重新输入!");
                        }
                    }
                }
            }
        } else {
            while (true) {
                System.out.println("两次密码不一致!");
                System.out.println("是否重新注册?");
                System.out.println("1. 重新注册");
                System.out.println("2. 返回主菜单");
                System.out.println("3. 退出系统");
                int choice;
                while (true) {
                    String choice1 = scan.next();
                    try {
                        choice = Integer.parseInt(choice1);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("选项输入有误!请重新输入!");
                    }
                }
                switch (choice) {
                    case 1:
                        regist();
                        return;
                    case 2:
                        return;
                    case 3:
                        return;
                    default:
                        System.out.println("选项输入有误!请重新输入!");
                }
            }
        }
    }

}

  • 学生管理子系统
package service;

import dao.StuDao;
import database.Database;
import pojo.Student;

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

/**
 * 学生信息管理子系统
 */
public class StuSystem {

    private Scanner scan = new Scanner(System.in);
    private StuDao stuDao;

    /**
     * 初始化
     *
     * @param database
     */
    public StuSystem(Database database) {
        stuDao = new StuDao(database);
    }

    /**
     * 功能菜单
     *
     * @return
     */
    public int MainMenu() {
        System.out.println("1. 浏览学生信息");
        System.out.println("2. 查找学生信息");
        System.out.println("3. 新增学生信息");
        System.out.println("4. 修改学生信息");
        System.out.println("5. 删除学生信息");
        System.out.println("0. 返回登陆界面");
        System.out.println("请选择:");
        String choice1 = scan.next();
        int choice;
        try {
            choice = Integer.parseInt(choice1);
        } catch (NumberFormatException e) {
            System.out.println("输入有误!请重新输入!");
            return MainMenu();
        }
        if (choice > 5 || choice < 0) {
            System.out.println("输入有误!请重新输入!\n");
            //返回主菜单
            return MainMenu();
        }
        //反馈到主系统
        return choice;
    }

    /**
     * 浏览学生信息
     *
     * @return
     */
    public int showStudents() {
        System.out.println("是否对学生成绩进行排名?");
        System.out.println("1.是");
        System.out.println("2.否");
        String choice1 = scan.next();
        int choice;
        try {
            choice = Integer.parseInt(choice1);
        } catch (NumberFormatException e) {
            System.out.println("输入有误!请重新输入!");
            return showStudents();
        }
        if (choice == 1) {
            while (true) {
                System.out.println("请选择排序方式");
                System.out.println("1.双向冒泡排序");
                System.out.println("2.希尔排序");
                System.out.println("3.快速排序");
                System.out.println("4.堆排序");
                choice1 = scan.next();
                try {
                    choice = Integer.parseInt(choice1);
                    if (choice > 0 && choice < 5) {
                        break;
                    } else {
                        System.out.println("输入有误!请重新输入!");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            List<Student> list = new ArrayList<>();
            list.addAll(stuDao.getStus());
            switch (choice) {
                case 1:
                    list = stuDao.doubleBubbleSort(list);
                    break;
                case 2:
                    list = stuDao.shellSort(list);
                    break;
                case 3:
                    list = stuDao.quickSort(list, 0, list.size() - 1);
                    break;
                case 4:
                    list = stuDao.HeapSort(list);
            }
            while (true) {
                stuDao.showStus(list);
                System.out.println("0. 返回上层");
                System.out.println("请输入指令:");
                choice1 = scan.next();
                try {
                    choice = Integer.parseInt(choice1);
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
                //判断选项是否合法
                if (choice != 0) {
                    System.out.println("输入有误!请重新输入!");
                } else {
                    return 0;
                }
            }
        } else {
            while (true) {
                stuDao.showAllStus();
                System.out.println("0. 返回上层");
                System.out.println("请输入指令:");
                choice1 = scan.next();
                try {
                    choice = Integer.parseInt(choice1);
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                    return showStudents();
                }
                //判断选项是否合法
                if (choice != 0) {
                    System.out.println("输入有误!请重新输入!");
                } else {
                    return 0;
                }
            }
        }
    }

    /**
     * 查找学生信息
     *
     * @return
     */
    public int findStudent() {
        System.out.println("1. 按学号查询");
        System.out.println("2. 按姓名查询");
        System.out.println("0. 返回上层");
        System.out.println("请输入您的选择:");
        String choice1 = scan.next();
        int choice;
        try {
            choice = Integer.parseInt(choice1);
        } catch (NumberFormatException e) {
            System.out.println("输入有误!请重新输入!");
            return findStudent();
        }
        switch (choice) {
            case 0:
                //反馈到主系统
                System.out.println();
                return 0;
            case 1:
                System.out.println("请输入您要查询的学号:");
                Student stu = stuDao.searchStuByNum(scan.next());
                if (stu == null) {
                    System.out.println("未查询到该学号!");
                } else {
                    stuDao.showStu(stu);
                }
                //返回学生查找主界面
                return findStudent();
            case 2:
                System.out.println("请输入您要查询的姓名:");
                List<Student> list = stuDao.searchStuByName(scan.next());
                if (list.isEmpty()) {
                    System.out.println("未查询到该姓名!");
                } else {
                    stuDao.showStus(list);
                }
                //返回学生查找主界面
                return findStudent();
            default:
                System.out.println("输入有误!请重新输入!\n");
                //返回学生查找主界面
                return findStudent();
        }
    }

    /**
     * 添加学生信息
     *
     * @return
     */
    public int addStu() {
        System.out.println("学生学号:");
        String num = scan.next();
        if (stuDao.isEmpty() == false) {
            if (stuDao.searchStuByNum(num) == null) {
                System.out.println("学生姓名:");
                String name = scan.next();
                System.out.println("学生专业:");
                String major = scan.next();
                double score;
                String s;
                while (true) {
                    System.out.println("课程1成绩:");
                    s = scan.next();
                    try {
                        score = Double.parseDouble(s);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                double score1 = score;
                while (true) {
                    System.out.println("课程2成绩:");
                    s = scan.next();
                    try {
                        score = Double.parseDouble(s);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                double score2 = score;
                while (true) {
                    System.out.println("课程3成绩:");
                    s = scan.next();
                    try {
                        score = Double.parseDouble(s);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                double score3 = score;
                while (true) {
                    System.out.println("课程4成绩:");
                    s = scan.next();
                    try {
                        score = Double.parseDouble(s);
                        break;
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                double score4 = score;
                stuDao.addStu(new Student(num, name, major, score1, score2, score3, score4));
                System.out.println("添加成功!");
                stuDao.showAllStus();
                int choice;
                while (true) {
                    System.out.println("是否继续添加?");
                    System.out.println("1.继续添加");
                    System.out.println("0.返回上层");
                    System.out.println("请输入指令:");
                    String choice1 = scan.next();
                    try {
                        choice = Integer.parseInt(choice1);
                        if (choice == 0 || choice == 1) {
                            break;
                        } else {
                            System.out.println("输入有误!请重新输入!");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                switch (choice) {
                    case 0:
                        return 0;
                    case 1:
                        return addStu();
                    default:
                        return addStu();
                }
            } else {
                System.out.println("学号已存在!请重新输入!");
                return addStu();
            }
        } else {
            System.out.println("学生姓名:");
            String name = scan.next();
            System.out.println("学生专业:");
            String major = scan.next();
            double score;
            String s;
            while (true) {
                System.out.println("课程1成绩:");
                s = scan.next();
                try {
                    score = Double.parseDouble(s);
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            double score1 = score;
            while (true) {
                System.out.println("课程2成绩:");
                s = scan.next();
                try {
                    score = Double.parseDouble(s);
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            double score2 = score;
            while (true) {
                System.out.println("课程3成绩:");
                s = scan.next();
                try {
                    score = Double.parseDouble(s);
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            double score3 = score;
            while (true) {
                System.out.println("课程4成绩:");
                s = scan.next();
                try {
                    score = Double.parseDouble(s);
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            double score4 = score;
            Student stu = new Student(num, name, major, score1, score2, score3, score4);
            stuDao.addStu(new Student(num, name, major, score1, score2, score3, score4));
            System.out.println("添加成功!");
            stuDao.showAllStus();
            int choice;
            while (true) {
                System.out.println("是否继续添加?");
                System.out.println("1.继续添加");
                System.out.println("0.返回上层");
                System.out.println("请输入指令:");
                String choice1 = scan.next();
                try {
                    choice = Integer.parseInt(choice1);
                    if (choice == 0 || choice == 1) {
                        break;
                    } else {
                        System.out.println("输入有误!请重新输入!");
                    }
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            switch (choice) {
                case 0:
                    return 0;
                case 1:
                    return addStu();
                default:
                    return addStu();
            }
        }
    }

    /**
     * 修改学生信息
     *
     * @return
     */
    public int setStu() {
        System.out.println("1. 按学号查询");
        System.out.println("2. 按姓名查询");
        System.out.println("0. 返回上层");
        System.out.println("请输入您的选择:");
        String choice1 = scan.next();
        int choice;
        try {
            choice = Integer.parseInt(choice1);
        } catch (NumberFormatException e) {
            System.out.println("输入有误!请重新输入!");
            return setStu();
        }
        switch (choice) {
            case 0:
                //反馈到主系统
                return 0;
            case 1:
                System.out.println("请输入您要查询的学号:");
                Student stu = stuDao.searchStuByNum(scan.next());
                if (stu == null) {
                    System.out.println("未查询到该学号!");
                } else {
                    while (true) {
                        stuDao.showStu(stu);
                        System.out.println("请输入要更改成绩的课程序号:");
                        int choice2;
                        while (true) {
                            String c = scan.next();
                            try {
                                choice2 = Integer.parseInt(c);
                                if (choice2 > 0 && choice2 < 5) {
                                    break;
                                }
                            } catch (NumberFormatException e) {
                                System.out.println("输入有误!请重新输入!");
                            }
                        }
                        System.out.println("请输入要更改后的成绩:");
                        double score;
                        while (true) {
                            String s = scan.next();
                            try {
                                score = Double.parseDouble(s);
                                break;
                            } catch (NumberFormatException e) {
                                System.out.println("选项输入有误!请重新输入!");
                            }
                        }
                        stuDao.setStu(stu, choice2, score);
                        stuDao.showStu(stu);
                        while (true) {
                            System.out.println("是否继续修改该学生成绩?");
                            System.out.println("1.继续修改");
                            System.out.println("0.返回上层");
                            System.out.println("请输入指令:");
                            choice1 = scan.next();
                            try {
                                choice = Integer.parseInt(choice1);
                                if (choice == 0 || choice == 1) {
                                    break;
                                } else {
                                    System.out.println("输入有误!请重新输入!");
                                }
                            } catch (NumberFormatException e) {
                                System.out.println("输入有误!请重新输入!");
                            }
                        }
                        if (choice == 0) {
                            break;
                        }
                    }
                }
                return setStu();
            case 2:
                System.out.println("请输入您要查询的姓名:");
                List<Student> list = stuDao.searchStuByName(scan.next());
                if (list == null) {
                    System.out.println("未查询到该姓名!");
                } else {
                    int choice2;
                    while (true) {
                        stuDao.showStus(list);
                        System.out.println("请输入要修改成绩的学生序号:");
                        try {
                            String c = scan.next();
                            choice2 = Integer.parseInt(c) - 1;
                            if (choice2 >= 0 && choice2 < list.size()) {
                                break;
                            } else {
                                System.out.println("输入有误!请重新输入!");
                            }
                        } catch (NumberFormatException e) {
                            System.out.println("输入有误!请重新输入!");
                        }
                    }
                    while (true) {
                        stuDao.showStu(list.get(choice2));
                        System.out.println("请输入要更改成绩的课程序号:");
                        int choice3;
                        while (true) {
                            String c = scan.next();
                            try {
                                choice3 = Integer.parseInt(c);
                                if (choice3 > 0 && choice3 < 5) {
                                    break;
                                } else {
                                    System.out.println("输入有误!请重新输入!");
                                }
                            } catch (NumberFormatException e) {
                                System.out.println("输入有误!请重新输入!");
                            }
                        }
                        System.out.println("请输入要更改后的成绩:");
                        double score;
                        while (true) {
                            String s = scan.next();
                            try {
                                score = Double.parseDouble(s);
                                break;
                            } catch (NumberFormatException e) {
                                System.out.println("输入有误!请重新输入!");
                            }
                        }
                        stuDao.setStu(list.get(choice2), choice3, score);
                        stuDao.showStu(list.get(choice2));
                        while (true) {
                            System.out.println("是否继续修改该学生成绩?");
                            System.out.println("1.继续修改");
                            System.out.println("0.返回上层");
                            System.out.println("请输入指令:");
                            choice1 = scan.next();
                            try {
                                choice = Integer.parseInt(choice1);
                                if (choice == 0 || choice == 1) {
                                    break;
                                } else {
                                    System.out.println("输入有误!请重新输入!");
                                }
                            } catch (NumberFormatException e) {
                                System.out.println("输入有误!请重新输入!");
                            }
                        }
                        if (choice == 0) {
                            break;
                        }
                    }
                }
                //返回学生查找主界面
                return setStu();
            default:
                System.out.println("选项输入错误!请重新输入!\n");
                //返回学生查找主界面
                return setStu();
        }
    }

    /**
     * 删除学生信息
     *
     * @return
     */
    public int deleteStu() {
        while (true) {
            stuDao.showAllStus();
            System.out.println("0.返回上层");
            System.out.println("请输入要删除的学生序号:");
            int choice;
            while (true) {
                String c = scan.next();
                try {
                    choice = Integer.parseInt(c) - 1;
                    break;
                } catch (NumberFormatException e) {
                    System.out.println("输入有误!请重新输入!");
                }
            }
            if (choice >= 0 && choice < stuDao.StusNum()) {
                stuDao.deleteStu(choice);
                System.out.println("删除成功!");
                stuDao.showAllStus();
                int choice1;
                while (true) {
                    System.out.println("是否继续删除?");
                    System.out.println("1.继续删除");
                    System.out.println("0.返回上层");
                    System.out.println("请输入指令:");
                    String c = scan.next();
                    try {
                        choice1 = Integer.parseInt(c);
                        if (choice1 == 0 || choice1 == -1) {
                            break;
                        } else {
                            System.out.println("输入有误!请重新输入!");
                        }
                    } catch (NumberFormatException e) {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
                if (choice1 == 0) {
                    return 0;
                }
            } else if (choice == -1) {
                return 0;
            } else {
                System.out.println("输入有误!请重新输入!");
            }
        }
    }

}


运行截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值