【无标题】java简单学生管理系统

发布于:2023-01-10 ⋅ 阅读:(448) ⋅ 点赞:(0)

自学的 jdk安装有问题输出不了汉字  所以用了很多塑料英语

Test2.java

package com.ah.www;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Callable;

public class Test2 {
    public static void main(String[] args) {
        ArrayList<NewStudent> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("--------------welcome--------------");
            System.out.println("1.add student");
            System.out.println("2.delate student");
            System.out.println("3.change student");
            System.out.println("4.inquire student");
            System.out.println("5.quit");
            Scanner sc = new Scanner(System.in);
            System.out.println("please set your choose");
            int choose = sc.nextInt();
            switch (choose) {
                case 1 -> add(list);
                case 2 -> delate(list);
                case 3 -> change(list);
                case 4 -> inquire(list);
                case 5 -> {
                    System.out.println("quit");
                    break loop;
                }
                default -> System.out.println("");
            }
            System.out.println();
        }
    }

    //add student
    public static void add(ArrayList<NewStudent> list) {
        NewStudent stu = new NewStudent();
        Scanner sc = new Scanner(System.in);

        System.out.println("id:");
        String id = sc.next();
        stu.setId(id);
        if (judge(list, id)) {
            System.out.println("id repeat");
            return;
        }

        System.out.println("name:");
        String name = sc.next();
        stu.setName(name);

        System.out.println("age:");
        int age = sc.nextInt();
        stu.setAge(age);

        System.out.println("location:");
        String location = sc.next();
        stu.setLocation(location);

        list.add(stu);
        System.out.println("success");
    }

    //delate student
    public static void delate(ArrayList<NewStudent> list) {
        System.out.println("please set id you want to delate:");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(list, id);
        if (0 <= index) {
            list.remove(index);
            System.out.println("delate   " + id + " cuccess");
        }else {
            System.out.println("there is no id you want");
        }
    }

    //change student
    public static void change(ArrayList<NewStudent> list) {
        System.out.println("please set id you want to change:");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getIndex(list,id);
        if(0 <= index){
            System.out.println("please set name you want to change:");
            String name = sc.next();
            list.get(index).setName(name);
            System.out.println("please set age you want to change:");
            int age = sc.nextInt();
            list.get(index).setAge(age);
            System.out.println("please set location you want to change:");
            String location = sc.next();
            list.get(index).setLocation(location);
            System.out.println("success");
        }else {
            System.out.println("there is no id you want");
        }
    }

    //inquire student
    public static void inquire(ArrayList<NewStudent> list) {
        System.out.println(list.size());
        if (list.size() == 0) {
            System.out.println("there is no person");
            return;
        }
        System.out.println("id  name    age  location");
        for (int i = 0; i < list.size(); i++) {
            NewStudent stu = list.get(i);
            System.out.println(stu.getId() + " " + stu.getName() +
                    "  " + stu.getAge() + "   " + stu.getLocation());

        }
    }

    //judge
    public static boolean judge(ArrayList<NewStudent> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            NewStudent student = list.get(i);
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public static int getIndex(ArrayList<NewStudent> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            NewStudent temp = list.get(i);
            if (temp.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}

NewStudent.java

package com.ah.www;

public class NewStudent {
    private String id;
    private String name;
    private int age;
    private String location;

    public NewStudent() {
    }

    public NewStudent(String id, String name, int age, String location) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.location = location;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到