【基础】学生管理系统

【基础】学生管理系统

package xuesheng;

public class TestSdudentSystem {
     
    public static void main(String [] args){
     
        SdydentSystem system = new SdydentSystem();
        system.welcome();
        system.menu();
    }
}
package xuesheng;

public class Student {
     
    private String name;//姓名
    private Gender gender;//枚举性别
    private int number;//学号
    private int age;//年龄

    //构造方法
    //无参构造
    public Student(){
     
    }
    //全参构造
    public Student(String name,Gender gender,int number,int age){
     
        this.name = name;
        this.gender = gender;
        this.number = number;
        this.age = age;
    }

    //geter seter
    public String getName(){
     
        return name;
    }
    public void setName(String name){
     
        this.name = name;
    }

    public Gender getGender(){
     
        return gender;
    }
    public void setGender(Gender gender){
     
        this.gender = gender;
    }
    public int getNumber(){
     
        return number;
    }
    public void setNumber(int number){
     
        this.number = number;
    }
    public int getAge(){
     
        return age;
    }
    public void setAge(int age){
     
        this.age = age;
    }

    //重写toString
    @Override
    public String toString(){
     
        return "Student{" + "number = "
                + number + ", name = "
                + name + ",gender = "
                + gender + ",age = "
                + age +"}";
    }
}

enum Gender{
     //枚举好像没学,}
package xuesheng;

import java.util.*;

/*
实现类
重写方法
*/
public class SdydentSystem implements StudentInterface{
     
    private Map<Integer,Student> students = new HashMap<>();
    private Scanner scanner = new Scanner(System.in);



    public void welcome(){
     
        System.out.println("--------学生管理系统----------");
    }

    public void exit(){
     
        System.out.println("----------退出系统------------");
    }

    public void menu(){
     
        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("其他任意数字--->退出系统。");

        int input = My.getNumber("请选择操作:",scanner);
        switch(input){
     
            case 1 : showAll();returnMenu();break;
            case 2 : addStdudent();returnMenu();break;
            case 3 : findByNumber();returnMenu();break;
            case 4 : findByName();returnMenu();break;
            case 5 : modifyByNumber();returnMenu();break;
            case 6 : removeByNumber();returnMenu();break;
            default : exit();break;
        }
    }

    public void returnMenu(){
     
        int i = My.getNumber("输入0返回目录,或输入其他任意数字退出系统:",scanner);
        if(i == 0){
     
            menu();
        }else{
     
            exit();
        }
    }

    @Override
    //重写遍历
    public void showAll(){
     
        Set<Integer> numbers = students.keySet();
        //迭代器
        Iterator<Integer> iterator = numbers.iterator();
        while(iterator.hasNext()){
     
            Integer number = iterator.next();
            Student student = students.get(number);
            System.out.println(student);
        }
    }

    @Override
    //重写添加学生
    public boolean addStdudent(){
     
        int number = My.getNumber("输入学号:",scanner);
        int age = My.getNumber("输入年龄:",scanner);
        String name = My.getName("输入姓名",scanner);
        Gender gender =  My.getName("输入性别:",scanner)
                            .equals("男") ? Gender.: Gender.;
        Student stu = new Student(name,gender,number,age);
        students.put(number,stu);
        System.out.println("添加成功。");
        return true;
    }

    //查找学生
    @Override
    //重写通过姓名查找
    public List<Student> findByName(){
     
        String name = My.getName("请输入要查找的姓名:",scanner);
        Set<Integer> numbers = students.keySet();
        List<Student> list = new LinkedList<>();
        //迭代器
        Iterator<Integer> iterator = numbers.iterator();
        while(iterator.hasNext()){
     
            Integer number = iterator.next();
            Student stu = students.get(number);
            if(stu.getName().equals(name)){
     
                list.add(stu);
            }
        }
                if(list.size() != 0){
     
            System.out.println("已找到该学生,共" + list.size() + "名:");
            System.out.println(list);

        }else{
     
            System.out.println("查无此人。");
        }
        return list;
    }

    @Override
    //重写通过学号查找
    public Student findByNumber(){
     
        int number = My.getNumber("请输入要查找的学号:",scanner);
        Student stu = students.get(number);
        if(stu != null){
     
            System.out.println("已找到该学生:" + stu);
        }else{
     
            System.out.println("查无此人。");
        }
        return stu;
    }

    @Override
    //重写通过学号删除学生
    public boolean removeByNumber(){
     
        int number = My.getNumber("请输入要删除学生的学号:",scanner);
        Student stu = students.get(number);
        if(stu != null){
     
            System.out.println("已删除该学生:" + stu);
            students.remove(number);
            return true;
        }else{
     
            System.out.println("查无此人。");
            return false;
        }
    }

    @Override
    //重写通过学号修改学生
    public boolean modifyByNumber(){
     
        int number = My.getNumber("请输入要修改学生的学号:",scanner);
        Student stu = students.get(number);
        if(stu != null){
     
            int number0 = My.getNumber("输入学号:",scanner);
            int age0 = My.getNumber("输入年龄:",scanner);
            String name0 = My.getName("输入姓名",scanner);
            Gender gender0 =  My.getName("输入性别:",scanner)
                    .equals("男") ? Gender.: Gender.;
            stu.setNumber(number0);
            stu.setName(name0);
            stu.setGender(gender0);
            stu.setAge(age0);
            students.put(number0,stu);
            System.out.println("已修改该学生信息:" + stu);
            return true;
            }else{
     
            System.out.println("查无此人。");
            return false;
        }
    }
}
package xuesheng;

import java.util.Scanner;

public class My {
     
    public static int getRandomNumber(int min ,int max){
     
        //???
        return (int) (Math.random() * (max - min) + min);
    }

    //获取学号/年龄
    public static int getNumber(String message, Scanner scanner){
     
        System.out.println(message);
        int number = scanner.nextInt();
        return number;
    }
    //获取姓名
    public static String getName(String message, Scanner scanner){
     
        System.out.println(message);
        String name = scanner.next();
        return name;
    }

}
package xuesheng;

import java.util.List;

/*
接口
接口方法默认是public,可省略
*/
public interface StudentInterface{
     
    //遍历
    void showAll();

    boolean addStdudent();//添加学生

    //查找学生
    //通过姓名查找
    List<Student> findByName();
    //通过学号查找
    Student findByNumber();

    //通过学号删除学生
    boolean removeByNumber();

    //通过学号修改学生
    boolean modifyByNumber();

}

你可能感兴趣的:(给自己看,java,编程语言)