学生管理系统

package student;

import java.util.Objects;

public class Student {
    private String sid;
    private String name;
    private   int age;
    private String address;

    public Student(String sid, String name, int age, String address) throws Exception{
        if(age<0||age>150){
            throw new Exception("年龄不合法 0<=age<=150");
        }
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.address = address;
    }
    public Student(String sid)throws Exception{
        this(sid,"",0,"");
    }
    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws Exception{
        if(age<0||age>150){
            throw new Exception("年龄不合法 0<=age<=150");}
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return sid+ '\t' + name + '\t' + age +"\t" + address;
    }

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

    @Override
    public int hashCode() {
        return Objects.hashCode(sid);
    }
}
———————————————————————————————————————————————————————————————————————————————————————
package tool;

import student.Student;
import  java.util.ArrayList;
import java.util.Scanner;

public class Tool implements iTool{

    @Override
    public void showMenu() {
        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.print("请输入你的选择<1-6>:");
    }

    @Override
    public void add(ArrayList<Student> students) throws Exception{
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学生的学号:");
            String sid=sc.next();
        System.out.println("请输入学生的姓名:");
            String name=sc.next();
        System.out.println("请输入学生的年龄:");
        int age;
        while (true){
            try{
                age=sc.nextInt();
                break;
            } catch (Exception e) {
                System.out.println("输入错误");
                System.out.println("请重新输入");
            }
            sc.nextLine();
        }
        System.out.println("请输入学生的居住地:");
            String address=sc.next();
        Student student=new Student(sid,name,age,address);
        if(students.contains(student)){
            System.out.println("此学号信息已存在不可以再添加。");
    }
        else {
            students.add(student);
        System.out.println("学号信息添加成功。");
        }
    }

    @Override
    public void edit(ArrayList<Student> students) throws Exception {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的学生的sid:");
            String sid=sc.next();
        Student student=new Student(sid);
            int index=students.indexOf(student);
        if(index==-1){
            System.out.println("你查找的sid的学生不存在,并不存在,无法修改信息!");
            return;
        }
            System.out.println("你要修改的学生的信息是:");
            System.out.println(students.get(index));
            System.out.println("请输入学生要修改的sid:");
                sid=sc.next();
            System.out.println("请输入学生要修改的姓名:");
                String name=sc.next();
            System.out.println("请输入学生要修改的年龄:");
                int age=sc.nextInt();
            System.out.println("请输入学生要修改的地址:");
                String address=sc.next();
            Student s=students.get(index);
            s.setSid(sid);
            s.setName(name);
            s.setAge(age);
            s.setAddress(address);
        System.out.println("一条记录别修改,修改后的信息为:");
        System.out.println(students.get(index));
        }
    @Override
    public void del(ArrayList<Student> students) throws Exception {
        Scanner sc=new Scanner(System.in);
            System.out.println("请输入要删除的学生sid:");
            String sid=sc.next();
            Student student=new Student(sid);
            int  index=students.indexOf(student);
            if(index==-1){
                System.out.println("你查找的sid的学生不存在,并不存在,无法删除信息!");
                return;
        }
        System.out.println("你要删除的学生的信息是:");
        System.out.println(students.get(index));
        students.remove(index);
        System.out.println("一条记录别删除。");
    }

    @Override
    public void showAll(ArrayList<Student> students) {
        if(students.isEmpty()){
            System.out.println("列表中没有记录~!");
            return;
        }
        for (Student s:students){
            System.out.println(s);
        }
    }

    @Override
    public void find(ArrayList<Student> students) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的学生的sid:");
        String sid = sc.next();
        Student student = new Student(sid);
        int index = students.indexOf(student);
        if (index == -1) {
            System.out.println("你查找的sid的学生不存在,请重新输入。");
            return;
    }
        System.out.println("你查找的学生的信息是:");
        System.out.println(students.get(index));
    }
}

———————————————————————————————————————————

import java.util.InputMismatchException;
import java.util.Scanner;
import student.Student;
import tool.Tool;
import java.util.ArrayList;
public class MainTest {
    public static void main(String[] args) throws Exception{
        Scanner sc=new Scanner(System.in);
        Tool tool=new Tool();
        ArrayList<Student> students=new ArrayList<>();
        while (true){
            tool.showMenu();
            int ch ;
            try{
                ch=sc.nextInt();
            }catch (InputMismatchException e){
                System.out.println("输入了非法信息,不能采纳此项。");
                sc.nextLine();
                continue;//因为异常不需要下面信息
            }
            switch (ch){
                case 1:
                    tool.add(students);
                    break;

                case 2:
                    tool.del(students);
                    break;
                case 3:
                    tool.edit(students);
                    break;
                case 4:
                    tool.find(students);
                    break;
                case 5:
                    tool.showAll(students);
                    break;
                case 6:
                    sc.close();
                    System.out.println("程序结束");
                    return;
                default:
                    System.out.println("没有此项~");
            }
        }
    }
}

———————————————————————————————————————————

package tool;

import student.Student;

import java.util.ArrayList;

public interface iTool {
    void showMenu();
    void add(ArrayList<Student> students) throws Exception;
    void edit(ArrayList<Student> students)throws Exception;
    void del(ArrayList<Student> students)throws Exception;
    void showAll(ArrayList<Student> students);
    void find(ArrayList<Student> students)throws Exception;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值