【学生操作系统】使用java实现学生管理系统,无需连接数据库,实现学生数据的增删改查(使用数组)

需要实现学生管理系统,需要进行登录操作,登录满足开发的完整性和 满足面向对象,所以我们队用户进行了封装 

这是包的命名

实现用户登录,需要先创建User实体

User.java

package student.bean;
/**
 *用户实体类
 */
public class User {

    private String username;
    private String password;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

登录的操作实现,因为没有用到数据库,所以我们直接写死!!!

LoginDao.java

package student.dao;

import student.bean.Student;
import student.bean.User;

public class LoginDao {

    public boolean login(User user){
        if ("admin".equals(user.getUsername())&&
                "123456".equals(user.getPassword())){
            return true;
        }
        return false;
    }
}

实现登录的操作后,我们对登录页面进行编写

LoginMenu.java

package student.ui;

import student.bean.User;
import student.dao.LoginDao;
import student.dao.StudentDao;

import java.util.Scanner;

public class LoginMenu {
    /**
     * 用户登录界面
     */

    StudentMenu studentMenu = new StudentMenu();
    User user = new User();
    LoginDao loginDao = new LoginDao();
    StudentDao studentDao = new StudentDao();

    public void login() {
        Scanner sc = new Scanner(System.in);
        Scanner sc1 = new Scanner(System.in);
        System.out.println("学员管理系统");
        System.out.println("------------------------");
        while (true) {
            System.out.println("【用户登录】");
            System.out.print("请输入用户名:");
            user.setUsername(sc.nextLine());
            System.out.print("请输入密 码:");
            user.setPassword(sc.nextLine());
            if (loginDao.login(user)) {
//                studentMenu.mainMenu();
                break;
            } else {
                System.out.println("用户密码错误,请重新输入!!!");
                continue;
            }
        }
    }
}

登录界面写好后开始进入系统,对各项功能进行编写

 既然知道了需要实现哪些功能,这就需要我们去创建一个实体类,学生类,那我们也应该创建一个学生实体,因此,我们去创建了一个学生实体;

Student.java

package student.bean;

import student.util.Util;

import java.util.Arrays;

/**
 *学生实体类
 */

public class Student {
    Util util=new Util();
    private String id;//学号
    private String name;//姓名
    private double[] score;//成绩
    private double avg;//平均成绩
    private double sum=0.0;//总分

    private int index;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public void Student(){}

    public Student() {
    }

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student(String id, String name, double[] score) {
        this.id = id;
        this.name = name;
        setScore(score);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public double getAvg() {
        return avg;
    }


    public double getSum() {
        return sum;
    }

    public double[] getScore() {
        return score;
    }

    public void setScore(double[] score) {
        this.score = score;
        this.sum = this.score[0]+ this.score[1]+ this.score[2];
        double temp=this.sum/this.score.length;
        this.avg=util.format("0.00",temp);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", score=" + Arrays.toString(score) +
                ", avg=" + avg +
                ", sum=" + sum +
                '}';
    }

}

写完学生这个实体类后,我们接下来就是在Dao中写入各种方法,实现对实体的操作!!这里就不过多赘述了,直接上代码!!

StudentDao.java

package student.dao;

import student.bean.Student;
import student.ui.StudentMenu;

import javax.swing.plaf.IconUIResource;
import java.util.Objects;
import java.util.Scanner;

/**
 * 操作学生类
 */
public class StudentDao {
    static int n=2;
    private static StudentMenu studentMenu=new StudentMenu();
    private static Student[] student=new Student[50];
    private static StudentDao studentDao=new StudentDao();

    static {
        double scores[]={99,88,77};
        double scores1[]={88,77,66};
        Student s1=new Student("1001","jack",scores);
        Student s2=new Student("1002","peter",scores1);
        student[0]=s1;
        student[1]=s2;
    }

    /**
     * 显示所有学生信息
     */
    public  void showStudent(){
        System.out.println("所有学员信息:");
        System.out.println("------------------------------------------------------------------------");
        System.out.println("学号\t\t姓名\t成绩一\t成绩二\t" +
                "成绩三\t平均分\t总分");
        for(int i=0;i<n;i++){
            System.out.println(student[i].getId()+"\t\t"+student[i].getName()
            +"\t"+student[i].getScore()[0]+"\t"+student[i].getScore()[1]
                    +"\t"+student[i].getScore()[2]+"\t"+
                    student[i].getAvg()+"\t"+student[i].getSum());
        }
    }

    /**
     * 按总分位置,插入学生信息
     */
    public void insertSotrStudent(Scanner sc){
        studentMenu.addStudent(sc);
        sortStudentSum();
    }

    /**
     * 通过学号查找学生
     * @param id 学号
     * @return 返回查询出来的结果,是一个Student
     */
    public Student selectStudentId(String id){
        return getStuById(id);
    }

    /**
     * 通过学号模糊查询学生个人信息
     * @param id 学生学号
     * @return 查询出来的数据集
     */
    public Student[] selectStudentIds(String id){
        return getStuByIds(id);
    }

    /**
     * 通过学号模糊查询学生个人信息
     * @param name 学生学号
     * @return 查询出来的数据集
     */
    public Student[] selectStudentNmaes(String name){
        return getStuByNames(name);
    }

    /**
     * 通过平均值查询学生个人信息
     * @param min 最小平均值
     * @param max 最大平均值
     * @return 查询出来的student数组
     */
    public Student[] selectStudentAvgs(double min ,double max){
        return getStuByAvgs(min,max);
    }

    /**
     * 通过学号模糊查询学生个人信息
     * @param name 学生学号
     * @return 查询出来的数据集
     */
    private Student[] getStuByNames(String name) {
        int count=0;
        for (int i=0;i<n;i++){
            if (student[i].getName().contains(name)){
                count++;
            }
        }
        Student[] students=new Student[count];
        int j=0;
        for (int i=0;i<n;i++){
            if (student[i].getName().contains(name)){
                students[j]=student[i];
                j++;
                continue;
            }
        }
        return students;
    }

    /**
     * 通过平均值查询学生个人信息
     * @param min 最小平均值
     * @param max 最大平均值
     * @return 查询出来的student数组
     */
    private Student[] getStuByAvgs(double min,double max) {
        int count=0;
        for (int i=0;i<n;i++){
            if (student[i].getAvg()>=min &&student[i].getAvg()<=max){
                count++;
            }
        }
        Student[] students=new Student[count];
        int j=0;
        for (int i=0;i<n;i++){
            if (student[i].getAvg()>=min &&student[i].getAvg()<=max){
                students[j]=student[i];
                j++;
                continue;
            }
        }
        return students;
    }


    /**
     * 增加学生信息
     * @param stu 学生信息
     * @param
     */
    public  int addStudent(Student stu){
        student[n]=stu;
        return ++n;
    }


    /**
     * 按照sum总分从小到大排序
     */
    public void sortStudentSum(){
        for (int i=0;i<n;i++){
            for (int j=i;j<n-1;j++){
                if (student[j].getSum()>student[j+1].getSum()){
                    Student stuTemp=new Student();
                    stuTemp=student[j];
                    student[j]=student[j+1];
                    student[j+1]=stuTemp;
                }
            }
        }
        showStudent();
    }

    /**
     * 查看学生信息
     * @param id 需要查询的学号
     * @return 返回查询出来的结果
     */
    public Student getStuById(String id){
        Student tempStudent=null;
        for (int i=0;i<n;i++){
            if (student[i].getId().equals(id)){
                tempStudent=student[i];
                student[i].setIndex(i);
                break;
            }
        }
        return tempStudent;
    }

    /**
     * 查看学生信息
     * @param id 需要查询的学号
     * @return 返回查询出来的结果
     */
    public Student[] getStuByIds(String id){
        int count=0;
        for (int i=0;i<n;i++){
            if (student[i].getId().contains(id)){
               count++;
            }
        }
        Student[] students=new Student[count];
        int j=0;
        for (int i=0;i<n;i++){
            if (student[i].getId().contains(id)){
                students[j]=student[i];
                j++;
                continue;
            }
        }
        return students;
    }

    /**
     * 删除学生信息
     * @param id 要删除学生的学号
     * @return 是否产出成功的boolean值
     */
    public boolean deleStudent(String id) {
        boolean flag = true;
        Student[] stuTemp = new Student[n - 1];
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (student[i].getId().equals(id)) {
                flag = false;
                break;
            }
        }
        if (flag) {
            return flag;
        } else {
            for (int i = 0; i < n; i++) {
                if (student[i].getId().equals(id)) {
                    flag = false;
                    continue;
                } else {
                    stuTemp[count] = student[i];
                    count++;
                }
            }
            for (int i = 0; i < stuTemp.length; i++) {
                student[i] = stuTemp[i];
            }
            student[n - 1] = null;
            n = n - 1;
            return flag;
        }
    }

    /**
     * 学生信息修改,修改学生姓名
     * * @param id 学生学号
     * @param value 学生成绩
     */
    public void updateStudent(String id,String  value) {
        Student stu=getStuById(id);
        stu.setName(value);
    }

    /**
     * 学生信息修改,修改学生成绩
     * @param id 学生学号
     * @param scores 学生成绩
     */
    public void updateStudent(String id,double[]  scores) {
        Student stu=getStuById(id);
        stu.setScore(scores);
    }
}

学生实体的操作写完后,就开始写主页面的菜单信息!

StudentMenu.java

package student.ui;

import student.bean.Student;
import student.bean.User;
import student.dao.LoginDao;
import student.dao.StudentDao;

import java.util.Scanner;

public class StudentMenu {
    /**
     * 用户界面
     */
    User user = new User();
    LoginDao loginDao = new LoginDao();
    private static StudentDao studentDao = new StudentDao();
    Scanner sc = new Scanner(System.in);
    Scanner sc1 = new Scanner(System.in);
    Scanner sc2 = new Scanner(System.in);

    public void mainMenu() {

        System.out.println("学员管理系统");
        System.out.println("------------------------");
        while (true) {
            System.out.println("学员管理系统平台");
            System.out.println("--------------------------------------------------");
            System.out.println("\t\t\t\t○功能菜单:");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t1、录入单个学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t2、显示所有学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t3、排序显示所有学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t4、插入单个学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t5、查找学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t6、删除单个学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t7、读取所有学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t8、保存所有学员信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t9、修改学生信息");
            System.out.println("\t\t\t\t\t-----------------------");
            System.out.println("\t\t\t\t\t0、退出系统");
            System.out.print("请选择功能编号:");
            int num = sc1.nextInt();
            switch (num) {
                case 1:
                    addCase();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 2:
                    studentDao.showStudent();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 3:
                    studentDao.sortStudentSum();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 4:
                    studentDao.insertSotrStudent(sc);
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 5:
                    select();
                    continue;
                case 6:
                    deleStudent();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 7:
                    break;
                case 8:
                    break;
                case 9:
                    updateStudent(sc);
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 0:
                    break;
                default:
                    System.out.println("输入错误,请重新输入!!");
                    continue;
            }
            break;
        }
    }

    /**
     * 修改学生信息
     */
    private void updateStudent(Scanner sc) {
        System.out.println("单个学员信息的修改");
        while (true) {
            System.out.println("请输入学号:");
            String id = sc.nextLine();
            Student stutemp = studentDao.getStuById(id);
            if (stutemp == null) {
                System.out.println("学号未找到,是否继续修改?(y/n)");
                String str = sc.nextLine();
                if ("y".equals(str)) {
                    continue;
                } else {
                    break;
                }
            } else {
                System.out.println("学号\t\t姓名\t成绩一\t成绩二\t" +
                        "成绩三\t平均分\t总分");
                System.out.println(stutemp.getId() + "\t\t" + stutemp.getName()
                        + "\t" + stutemp.getScore()[0] + "\t" + stutemp.getScore()[1]
                        + "\t" + stutemp.getScore()[2] + "\t" +
                        stutemp.getAvg() + "\t" + stutemp.getSum());
                System.out.println("-----------------------");
                System.out.println("1、修改姓名");
                System.out.println("-----------------------");
                System.out.println("2、修改成绩");
                System.out.println("-----------------------");
                System.out.println("0、退出");
                System.out.println("请输入编号选择功能:");
                int s = sc1.nextInt();
                switch (s) {
                    case 1:
                        System.out.println("请输入你要修改的值:");
                        String valueName = sc.nextLine();
                        studentDao.updateStudent(id, valueName);
                        System.out.println("修改成功!是否继续修改?(y/n)");
                        String str1 = sc.nextLine();
                        if ("y".equals(str1)) {
                            continue;
                        } else {
                            break;
                        }
                    case 2:
                        double[] scores = new double[3];
                        for (int i = 0; i < 3; i++) {
                            System.out.println("请输入成绩" + (i +1)+ "修改后的值:");
                            scores[i] = sc2.nextDouble();
                        }
                        studentDao.updateStudent(id, scores);
                        System.out.println("修改成功!是否继续修改?(y/n)");
                        String str2 = sc.nextLine();
                        if ("y".equals(str2)) {
                            continue;
                        } else {
                            break;
                        }
                    case 0:
                        break;
                }
                break;
            }
        }
    }

    /**
     * 单个学员信息录入
     *
     * @param sc 扫描器
     * @param
     * @return
     */
    public void addStudent(Scanner sc) {
        System.out.println("单个学员信息的循环录入");
        boolean flag = false;
        String id;
        do {
            flag = false;
            System.out.println("学号:");
            id = sc.nextLine();
            Student tempStu = studentDao.getStuById(id);
//            Student tempStu=null;
            if (tempStu != null) {
                System.out.println(id + "学号重复了,请重新输入!!");
                flag = true;
            }
        } while (flag);
        System.out.println("姓名:");
        String stuName = sc.nextLine();
        double[] scores = new double[3];
        for (int i = 0; i < scores.length; i++) {
            System.out.println("成绩:" + (i + 1));
            scores[i] = Double.parseDouble(sc.nextLine());
        }
        Student stu = new Student(id, stuName, scores);
        studentDao.addStudent(stu);
    }

    /**
     * 封装case 1:中的方法
     */
    public void addCase() {
        Scanner scanner = new Scanner(System.in);
        addStudent(scanner);
        studentDao.showStudent();
    }

    public void deleStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要删除的学号:");
            String id = scanner.nextLine();
            boolean flag = studentDao.deleStudent(id);
            if (flag) {
                System.out.println("学号未找到,是否继续删除?(y/n)");
                String str = scanner.nextLine();
                if ("y".equals(str)) {
                    continue;
                } else {
                    break;
                }
            }
            {
                System.out.println("删除成功!是否继续删除?(y/n)");
                String str = scanner.nextLine();
                if ("y".equals(str)) {
                    continue;
                } else {
                    break;
                }
            }
        }

    }

    /**
     * 封装case5的方法
     */
    public void select() {
        while (true) {
            System.out.println("查找学生信息:");
            System.out.println("-----------------------");
            System.out.println("1、通过学号查找学生信息");
            System.out.println("-----------------------");
            System.out.println("2、通过姓名查找学生信息");
            System.out.println("-----------------------");
            System.out.println("3、通过平均成绩查询学生信息");
            System.out.println("-----------------------");
            System.out.println("4、退出");
            System.out.println("请输入编号选择功能:");
            int s = sc1.nextInt();
            switch (s) {
                case 1:
                    selectStudentId();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 2:
                    selectStudentNames();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 3:
                    selectStudentAvgs();
                    System.out.println("按任意键进入菜单选择!!");
                    sc.nextLine();
                    continue;
                case 4:
                    break;
            }
            break;
        }
    }

    /**
     * 封装case5中case1,通过学号模糊查找学生
     */
    public void selectStudentId() {
//        Student student = new Student();
        while (true) {
            System.out.println("请输入你要查找的学号:");
            Scanner sc = new Scanner(System.in);
            String id = sc.nextLine();
            Student[] student = studentDao.selectStudentIds(id);
            if (student != null) {
                System.out.println("学号包含" + id + "的学生个人信息为:");
                System.out.println("------------------------------------------------------------------------");
                System.out.println("学号\t\t姓名\t成绩一\t成绩二\t" +
                        "成绩三\t平均分\t总分");
                for (int i = 0; i < student.length; i++) {
                    System.out.println(student[i].getId() + "\t\t" + student[i].getName()
                            + "\t" + student[i].getScore()[0] + "\t" + student[i].getScore()[1]
                            + "\t" + student[i].getScore()[2] + "\t" +
                            student[i].getAvg() + "\t" + student[i].getSum());
                }
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            } else {
                System.out.println("学号不存在!");
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            }
        }
    }

    /**
     * 封装case5中case2,通过学号模糊查找学生
     */
    public void selectStudentNames() {
        while (true) {
            System.out.println("请输入你要查找的姓名:");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            Student[] student = studentDao.selectStudentNmaes(name);
            if (student != null) {
                System.out.println("姓名包含" + name + "的学生个人信息为:");
                System.out.println("------------------------------------------------------------------------");
                System.out.println("学号\t\t姓名\t成绩一\t成绩二\t" +
                        "成绩三\t平均分\t总分");
                for (int i = 0; i < student.length; i++) {
                    System.out.println(student[i].getId() + "\t\t" + student[i].getName()
                            + "\t" + student[i].getScore()[0] + "\t" + student[i].getScore()[1]
                            + "\t" + student[i].getScore()[2] + "\t" +
                            student[i].getAvg() + "\t" + student[i].getSum());
                }
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            } else {
                System.out.println("姓名不存在!");
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            }
        }
    }

    /**
     * 封装case5中case2,通过平均成绩查找学生
     */
    public void selectStudentAvgs() {
        while (true) {
            System.out.println("请输入你要查找的最低平均值:");
            double minAvg = sc2.nextDouble();
            System.out.println("请输入你要查找的最高平均值:");
            double maxAvg = sc2.nextDouble();
            Student[] student = studentDao.selectStudentAvgs(minAvg, maxAvg);
            if (student != null) {
                System.out.println("平均成绩在" + minAvg + "-" + maxAvg + "中的学生个人信息为:");
                System.out.println("------------------------------------------------------------------------");
                System.out.println("学号\t\t姓名\t成绩一\t成绩二\t" +
                        "成绩三\t平均分\t总分");
                for (int i = 0; i < student.length; i++) {
                    System.out.println(student[i].getId() + "\t\t" + student[i].getName()
                            + "\t" + student[i].getScore()[0] + "\t" + student[i].getScore()[1]
                            + "\t" + student[i].getScore()[2] + "\t" +
                            student[i].getAvg() + "\t" + student[i].getSum());
                }
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            } else {
                System.out.println("成绩区间不存在!");
                System.out.println("是否继续查询?(y/n)");
                String s = sc.nextLine();
                if ("y".equals(s)) {
                    continue;
                } else {
                    break;
                }
            }
        }
    }
}

最后,这些都写完,项目也已经基本写完了,接下来就是把登录页面,和主页面进行拼接,让他们可以连续执行

StudentTest.java

package student.ui;

import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        LoginMenu loginMenu=new LoginMenu();
        loginMenu.login();

        StudentMenu studentMenu=new StudentMenu();
        studentMenu.mainMenu();
    }
}

最后就是我自己写的一个工具类,实现double数据取两位数

Util.java

package student.util;

import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 *
 */
public class Util {
    public static double format(String rule, double num){
        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        num= Double.parseDouble(df.format(num));
        return num;
    }
}

到这里,这个项目就算完成了,但是还有很多地方是不够完善的,例如没有进行成绩判断哪些,这些就需要同学们自己进行修改了。刚开始学习java,还有很多不规范的地方,也希望各位指正!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值