Java

HashSet学生管理系统

使用HashSet试验建立学生管理系统。

主程序

package com.cztcode.student;

import java.util.HashSet;
import java.util.Scanner;

public class main {
    private static HashSet<Student> set = new HashSet<Student>();

    public static void main(String[] args) {
        System.out.println("*********************************");
        System.out.println("*\t\t\t\t*");
        System.out.println("*\t欢迎使用学生信息管理系统\t*");
        System.out.println("*\t\t\t\t*");
        System.out.println("*********************************");
        while (true) {
            menu();
        }

    }

    static void menu() {
        System.out.println("1、添加学生信息");
        System.out.println("2、删除学生信息");
        System.out.println("3、修改学生信息");//地址传递
        System.out.println("4、查询学生信息");//name
        System.out.println("请输入操作,以Enter键结束:");
        Scanner in;
        in = new Scanner(System.in);
        int option = in.nextInt();
        switch (option) {
            case 1:
                System.out.println("添加");
                System.out.println("依次输入 姓名 学号 电话 性别");
                Student Tstu = new Student(in.next(), in.next(), in.next(), in.next());
                if (set.contains(Tstu)) {
                    System.out.println("已经添加过");
                } else {
                    set.add(Tstu);
                }
                break;
            case 2:
                System.out.println("输入学号");
                String id2 = in.next();
                if (!set.contains(id2)) {
                    System.out.println("未查询到");
                    return;

                } else {
                    set.remove(id2);
                }
                break;
            case 3:
                System.out.println("输入学号");
                String id3 = in.next();
                if (!set.contains(id3)) {
                    System.out.println("未查询到");
                    return;

                } else {
                    for (Student stu : set) {
                        if (stu.getSno().equals(id3)) {
                            System.out.println("依次输入 姓名 学号 电话 性别");
                            stu.setName(in.next());
                            stu.setSex(in.next());
                            stu.setSno(in.next());
                            stu.setTel(in.next());
                            System.out.println("添加成功");
                            return;
                        }
                    }
                }
                break;
            case 4:
                System.out.println("输入学号");
                String id = in.next();
                if (!set.contains(id)) {
                    System.out.println("未查询到");
                    return;

                } else {
                    for (Student stu : set) {
                        if (stu.getSno().equals(id)) {
                            System.out.println(in.next());
                            return;
                        }
                    }
                }

                break;
            default:
                System.out.println("I'm Sorry,there is not the " + option + " option,please try again.");
        }
    }

}

Student类

package com.cztcode.student;

import java.util.Objects;

public class Student {
    private String name;
    private String sno;
    private String tel;
    private String sex;

    Student(String string){
        this.sno=string;
    };

    @Override
    public int hashCode() {
        return Objects.hash(sno);
    }

    public Student(String name, String sno, String tel, String sex) {
        super();
        this.name = name;
        this.sno = sno;
        this.tel = tel;
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return sno.equals(student.sno);
    }



}

主要使用了set的几个方法。

发表评论