java集合实现学生管理系统

主要思路是:先创建一个用户用来储存账号密码,在进行封装存入一个集合中,然后再创建一个学生,里面有学生的个人信息,然后存在学生集合中,然后建立一个具有功能的包也就是增删查改,

1.创建用户类

主要功能存储账号密码进行封装

package HomeWork.User;



public class User {
    private String name;
    private String passWork;

    public User() {
    }

    public User(String name, String passWork) {
        this.name = name;
        this.passWork = passWork;
    }

    public String getName() {
        return name;
    }

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

    public String getPassWork() {
        return passWork;
    }

    public void setPassWork(String passWork) {
        this.passWork = passWork;
    }
}

 2.创建学生类

主要的作用就是存储学生的个人信息进行封装存入集合中

package HomeWork.User;

import HomeWork.Function.StudentFunction.*;

import java.util.ArrayList;

public class Student{
    private String studentName;
    private String studentId;
    private int studentAge;
    private String address;
    public IOPeration[] ioPerations;

    public String getStudentName() {
        return studentName;
    }
//在创建对象的时候赋值给数组这些功能
    public Student() {
        this.ioPerations = new IOPeration[]{
                new StudentExit()
               ,new StudentAdd(),
                ,new StudentSort()
new StudentRemove(),
new StudentFind() ,
new StudentSet(),
new StudentShow() 
        };
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public void doOperation(int index, ArrayList list){
        ioPerations[index].work(list);
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public int getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(int studentAge) {
        this.studentAge = studentAge;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Student(String studentName, String studentId, int studentAge, String address) {
        this.studentName = studentName;
        this.studentId = studentId;
        this.studentAge = studentAge;
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentName='" + studentName + '\'' +
                ", studentId='" + studentId + '\'' +
                ", studentAge=" + studentAge +
                ", address='" + address + '\'' +
                '}';
    }
}

 对于上述的,IOPeration[] ioPerations数组,我会在下文进行分析,而重写的toString就是为了让打印学生的对象名而不出现地址值

3. 登录与注册功能

主要实现登录与注册功能,登录成功就会跳转学生信息的增删查改功能,而登录成功就会跳转

Student类中的doOperation方法,来调用你刚刚你的接口数组的值,而那些索引就是每一个功能

(增删查改)

package HomeWork.Function;

import HomeWork.User.Student;
import HomeWork.User.User;

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

public class UserFunction {
    static ArrayList list = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
     static ArrayList arrays = new ArrayList();
    //注册
    public static void Register(){
        User u = new User();
        System.out.println("请输入你要注册的用户名:");
        String name = sc.nextLine();
        u.setName(name);
        System.out.println("请输入你要注册的密码:");
        String passWork = sc.nextLine();
        u.setPassWork(passWork);
        arrays.add(u);
    }
    //登录
    public static void LogIn(){
        System.out.println("请输入你要登录的用户名:");
        String name = sc.nextLine();
        System.out.println("请输入你要登录的密码:");
        String passWork = sc.nextLine();
        for (User array : arrays) {
            if (array.getName().equals(name) && array.getPassWork().equals(passWork)){
                System.out.println("登录成功!!!");
                while(true){
                    Test.print2();
                    Scanner sc = new Scanner(System.in);
                    int index = sc.nextInt();
                    Student s = new Student();
                    s.doOperation(index,list);
                }
            }
            }
            System.out.println("账号密码不匹配登录失败!!!");
        }

    }

 下面就来介绍关于对学生操作的功能

4.IOPeration接口

主要实现:接收这个接口的都必须实现work方法并需要传入存学生的集合

并且可以创建IOPeration类型的数组,里面可以存入这些功能通过向上转型就可以索引直接调用,

也就student类中为什么有IOPeration[] ioPerations数组

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

import java.util.ArrayList;

public interface IOPeration {
    void work(ArrayList list);
}

5.添加功能

把你输入的数据封装到学生对象中,在把学生对象存到集合中即可

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

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

public class StudentAdd implements IOPeration{
    @Override
    public void work(ArrayList list) {
        System.out.println("添加学生!");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生学号:");
        String id = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生地址:");
        String address = sc.next();
        Student s = new Student(name,id,age,address);
        list.add(s);
        System.out.println("学生已添加成功");
    }
}

 6.删除功能

先查找你要删除的学号查找到之后进行删除

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

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

public class StudentRemove implements IOPeration {

    @Override
    public void work(ArrayList list) {
        if (list.size() == 0) {
            System.out.println("无学生信息无法删除!!!");
            return;
        }
        System.out.println("删除图书!!!");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生学号:");
        String id = sc.next();
        for (Student student : list) {
            if (student.getStudentId().equals(id)) {
                list.remove(student);
                System.out.println("恭喜你删除成功!!!");
                return;
            }
        }
    }
}

7.查询功能

因为这里索引是需要使用的所以不能使用增强for循环

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

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

public class StudentFind implements IOPeration{

    @Override
    public void work(ArrayList list) {
        System.out.println("查询学生!!!");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查询的学生学号:");
        String id = sc.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStudentId().equals(id)){
                System.out.println("学生信息为:" + list.get(i));
                return;
            }
        }
        System.out.println("无此学生信息");
    }
}

8.查询全部信息功能

因为student重写了tostring方法所以打印的不会是地址值

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

import java.util.ArrayList;

public class StudentShow implements IOPeration{
    @Override
    public void work(ArrayList list) {
        if (list.size() == 0){
            System.out.println("无学生信息!!!");
            return;
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}

9.修改功能

主要作用:就是输入你要修改的学号然后如果有学生就会跳转你选择修改什么页面,然后选择修改什么就直接通过set方法赋值

package HomeWork.Function.StudentFunction;

import HomeWork.Function.UserFunction;
import HomeWork.User.Student;

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

public class StudentSet implements IOPeration{
    @Override
    public void work(ArrayList list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号:");
        String id = sc.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStudentId().equals(id)){
                while (true) {
                    System.out.println("请输入你要修改的信息:");
                    System.out.println("1.姓名");
                    System.out.println("2.学号");
                    System.out.println("3.年龄");
                    System.out.println("4.地址");
                    int choose = sc.nextInt();
                    switch (choose){
                        case 1:
                            System.out.println("请输入修改的新姓名");
                            String newName = sc.next();
                            list.get(i).setStudentName(newName);
                            break;
                        case 2:
                            System.out.println("请输入修改的新学号");
                            String newId = sc.next();
                            list.get(i).setStudentId(newId);
                            break;
                        case 3:
                            System.out.println("请输入修改的新年龄");
                            int newAge = sc.nextInt();
                            list.get(i).setStudentAge(newAge);
                            break;
                        case 4:
                            System.out.println("请输入修改的新地址");
                            String newAddress = sc.next();
                            list.get(i).setAddress(newAddress);
                            break;
                        default:
                            System.out.println("无此选项!!!");
                    }
                    System.out.println("是否要添加新的数据:");
                    System.out.println("1.是");
                    System.out.println("2.否");
                    int x = sc.nextInt();
                    if (x == 2){
                        return;
                    }
                }
            }
        }
        System.out.println("无此学生");
    }
}

10.退出功能

利用System.exit()参数0表示正常退出系统,就跟app的查号一模一样

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

import java.util.ArrayList;

public class StudentExit implements IOPeration{

    @Override
    public void work(ArrayList list) {
        System.out.println("系统退出!!!");
        System.exit(0);
    }
}

 11.按年龄排序

 利用sort自定义排序规则进行排序

package HomeWork.Function.StudentFunction;

import HomeWork.User.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class StudentSort implements IOPeration{

    //使学生按照年龄进行排序
    @Override
    public void work(ArrayList list) {
        /*Collections.sort(list, new Comparator() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getStudentId().compareTo(o2.getStudentId());
            }
        });*/

        //改为匿名内部类的形式
        Collections.sort(list, (o1, o2) -> o1.getStudentId().compareTo(o2.getStudentId())
                );
        //调用输出所有学生信息查看排序
        new StudentShow().work(list);
    }
}

12.测试类

package HomeWork.Function;

import java.util.Scanner;

public class Test {
        static Scanner sc = new Scanner(System.in);


        public static void main(String[] args) {
            function1();

        }

        //打印表头
        public static void print() {
            System.out.println("——————————————————————————————");
            System.out.println("欢迎来到学生管理系统:");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出");
            System.out.println("——————————————————————————————");
            System.out.println("请输入你的选择:");
        }
    //打印表头
    public static void print2() {
        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.查询学生全部信息");
        System.out.println("6.按年龄进行排序");
        System.out.println("0.退出系统");
        System.out.println("——————————————————————————————");
        System.out.println("请输入你的选择:");
    }


    //实现功能
        public static void function1(){
            while (true){
                print();
                int choose = sc.nextInt();
                switch (choose){
                    case 1:
                        UserFunction.Register();
                        System.out.println("恭喜你注册成功!!!");
                        break;
                    case 2:
                        UserFunction.LogIn();
                        break;
                    case 3:
                        System.out.println("系统已退出!!!");
                        System.exit(0);
                    default:
                        System.out.println("无此选项!!");
                }
            }
        }

    }

有什么可以优化还请大佬解答,有什么错还请指正谢谢大家!!!祝大家周末愉快

 

你可能感兴趣的:(java,开发语言)