Java簡單學生資訊管理系統

專注的阿熊發表於2021-07-20

/*

  * Student

*/

package Gailic_001;

public class Student {

    private String sid;

    private String name;

    private String age;

    private String address;

    public Student() {

    }

    public Student(String sid, String name, String age, String address) {

        this.sid = sid;

        this.name = name;

        this.age = age;

        this.address = address;

    }

    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 String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    public String getAddress() {

        return address;

    }

    public void setAddress(String address) {

        this.address = address;

    }

}

/*

* StudentManager

*/

package Gailic_001;

import java.util.ArrayList;

import java.util.Scanner;

public class StudentManager {

    public static void main(String[] args) {

        // 建立學生集合,用於儲存學生資訊

        ArrayList<Student> students = new ArrayList<Student>();

        Scanner sc = new Scanner(System.in);

        while (true) {

            // 顯示歡迎資訊和系統選單

            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(" 請輸入你的選擇: ");

            String choose = sc.nextLine();

            switch (choose) {

                case "1":

                    addStudent(students);

                    break;

                case "2":

                    deleteStudent(students);

                    break;

                case "3":

                    changeStudent(students);

                    break;

                case "4":

                    findAllStudent(students);

                    break;

                case "5":

                    System.out.println(" 謝謝使用! ");

                    System.exit(-1);

            }

        }

    }

    // 新增學生方法

    public static void addStudent(ArrayList<Student> students){

        Scanner sc = new Scanner(System.in);

        String sid;

        // 先判斷輸入的學號是否已經存在

        while (true) {

            System.out.println(" 請輸入學號: ");

            sid = sc.nextLine();

            if (isUsed(students, sid)) {

                System.out.println(" 該學號已經存在,請重新輸入! ");

            } else {

                break;

            }

        }

        // 錄入學生資訊

        System.out.println(" 請輸入姓名: ");

        String name = sc.nextLine();

        System.out.println(" 請輸入年齡: ");

        String age = sc.nextLine();

        System.out.println(" 請輸入居住地: ");

        String address = sc.nextLine();

        // 建立學生物件並新增

        Student s = new Student();

        s.setSid(sid);

        s.setName(name);

        s.setAge(age);

        s.setAddress(address);

        students.add(s);

        System.out.println(" 新增學生成功! ");

    }

    // 檢視所有學生資訊方法

    public static void findAllStudent(ArrayList<Student> students){

        // 判斷集合中是否存在資料

        if(students.size()==0){

            System.out.println(" 還沒有任何資料,請先新增資料! ");

            return;

        }

        // 新增表頭資訊

        System.out.println("----------------------------------------------");

        System.out.println(" 學號 \t\t\t 姓名 \t 年齡 \t\t 居住地 ");

        System.out.println("----------------------------------------------");

        for(int i=0;i<students.size();i++){

System.out.println(students.get(i).getSid()+"\t\t"+students.get(i).getName()+"\t"+students.get(i).getAge()+" \t\t"+students.get(i).getAddress());

        }

        System.out.println("----------------------------------------------");

    }

    // 刪除學生

    public static void deleteStudent(ArrayList<Student> students){

        Scanner sc = new Scanner(System.in);

        String sid;

        while (true) {

            System.out.println(" 請輸入要刪除學生的學號: ");

            sid = sc.nextLine();

            if(!isUsed(students,sid)){

                System.out.println(" 學號不存在!外匯跟單gendan5.com請重新輸入! ");

            }else {

                break;

            }

        }

        for(int i=0; i<students.size(); i++){

            if(students.get(i).getSid().equals(sid)){

                students.remove(i);

            }

        }

        System.out.println(" 刪除學生成功! ");

    }

    // 修改學生資訊

    public static void changeStudent(ArrayList<Student> students){

        Scanner sc = new Scanner(System.in);

        String sid;

        while (true) {

            System.out.println(" 請輸入要修改學生的學號: ");

            sid = sc.nextLine();

            if (!isUsed(students, sid)) {

                System.out.println(" 學號不存在!請重新輸入! ");

            }else {

                break;

            }

        }

        // 修改後的學生資訊

        System.out.println(" 請輸入修改後的學生姓名: ");

        String name = sc.nextLine();

        System.out.println(" 請輸入修改後的學生年齡: ");

        String age = sc.nextLine();

        System.out.println(" 請輸入修改後的學生居住地: ");

        String address = sc.nextLine();

        for(int i=0;i<students.size();i++){

            if(students.get(i).getSid().equals(sid)){

                students.get(i).setSid(sid);

                students.get(i).setName(name);

                students.get(i).setAge(age);

                students.get(i).setAddress(address);

                System.out.println(" 修改學生資訊成功! ");

                return;

            }

        }

    }

    // 檢測學號是否已被使用

    public static boolean isUsed(ArrayList<Student> students,String sid){

        boolean flag = false;

        for(int i=0;i<students.size();i++){

            if(students.get(i).getSid().equals(sid)){

                flag = true;

            }

        }

        return flag;

    }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2782335/,如需轉載,請註明出處,否則將追究法律責任。

相關文章