1. 程式人生 > 實用技巧 >Java Day16-學生管理系統

Java Day16-學生管理系統

完成學生管理系統(StudentManagementSystem)

最多隻能錄入8名學生

(學生學號,學生名字,學生性別,學生地址)增刪改查(新增完學生後,自動展示所有學生資訊(只展示學號,姓名))
(當執行修改,刪除,查一個學生操作時,如果有該學生,需要展示該生全部資訊)
(當完成修改,刪除,自動展示所有學生資訊(只展示學號,姓名))

建立DateBase類

public class DateBase {

    static int[] stuID = new int[8];
    static String[] stuName = new String[8];
    static String[] stuSex = new String[8];
    static String[] stuSite = new String[8];

}

建立Method類

public class Method {

    static int index = 0;

    public static void addStuMsg(int Id, String Name, String Sex, String Site) {
        for (int i = 0; i < DateBase.stuID.length; i++) {
            if (Id == DateBase.stuID[i] || index >= 8) {
                System.out.println("學號已被佔用!或學生資訊已滿!新增失敗!");
                return;
            }
        }
        DateBase.stuID[index] = Id;
        DateBase.stuName[index] = Name;
        DateBase.stuSex[index] = Sex;
        DateBase.stuSite[index] = Site;
        index++;
    }

    public static void showStuMsg() {
        System.out.println("學生學號\t學生姓名\t學生性別\t學生地址");
        for (int i = 0; i < DateBase.stuID.length && i < DateBase.stuName.length && i < DateBase.stuSex.length && i < DateBase.stuSite.length; i++) {
            if (DateBase.stuID[i] != 0) {
                System.out.println(DateBase.stuID[i] + "\t" + DateBase.stuName[i] + "\t" + DateBase.stuSex[i] + "\t" + DateBase.stuSite[i]);
            }
        }
    }

    public static void showStuTwoMsg() {
        System.out.println("學生學號\t學生姓名");
        for (int i = 0; i < DateBase.stuID.length && i < DateBase.stuName.length; i++) {
            if (DateBase.stuID[i] != 0) {
                System.out.println(DateBase.stuID[i] + "\t" + DateBase.stuName[i]);
            }
        }
    }

    public static int findOneStuId(int Id) {
        System.out.println("學生學號\t學生姓名\t學生性別\t學生地址");
        int findOneIndex = -1;
        for (int i = 0; i < DateBase.stuID.length; i++) {
            if (Id == (DateBase.stuID[i])) {
                findOneIndex = i;
                System.out.println(DateBase.stuID[i] + "\t" + DateBase.stuName[i] + "\t" + DateBase.stuSex[i] + "\t" + DateBase.stuSite[i]);
            }
        }
        return findOneIndex;
    }

    public static void deleteStuMsg(int deleteIndex) {
        for (int i = deleteIndex; i < DateBase.stuID.length - 1; i++) {
            DateBase.stuID[i] = DateBase.stuID[i + 1];
            DateBase.stuName[i] = DateBase.stuName[i + 1];
            DateBase.stuSex[i] = DateBase.stuSex[i + 1];
            DateBase.stuSite[i] = DateBase.stuSite[i + 1];
        }
        DateBase.stuID[DateBase.stuID.length - 1] = 0;
        DateBase.stuName[DateBase.stuName.length - 1] = null;
        DateBase.stuSex[DateBase.stuSex.length - 1] = null;
        DateBase.stuSite[DateBase.stuSite.length - 1] = null;
        index--;
    }
    public static void oneKeyAddStuMsgByEight(){
        System.out.println("\n一鍵新增八個自定義學生資訊 Success!\n");
        addStuMsg(1,"南子旭","男","河南省");
        addStuMsg(2,"法外狂徒張三","未知","未知");
        addStuMsg(3,"蕪湖大司馬","男","安徽省");
        addStuMsg(4,"旭旭寶寶","男","山東省");
        addStuMsg(5,"迪迦","未知","M78星雲");
        addStuMsg(6,"高斯","未知","M78星雲");
        addStuMsg(7,"艾斯","未知","M78星雲");
        addStuMsg(8,"泰羅","未知","M78星雲");
        showStuMsg();
    }
}

建立StudentService類

import java.util.Scanner;

public class StudentService {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("\n歡迎來到學生管理系統1.0\n請選擇以下操作");
            System.out.print("1.新增學生資訊  ");
            System.out.print("2.查詢全部學生資訊  ");
            System.out.print("3.通過學生學號查詢學生資訊  ");
            System.out.print("4.修改學生資訊  ");
            System.out.print("5.刪除學生資訊  ");
            System.out.print("6.一鍵新增八個自定義學生資訊  ");
            System.out.println("0.退出系統  ");
            System.out.print("請選擇:");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    System.out.println("請輸入學生學號");
                    int Id = sc.nextInt();
                    System.out.println("請輸入學生姓名");
                    String Name = sc.next();
                    System.out.println("請輸入學生性別");
                    String Sex = sc.next();
                    System.out.println("請輸入學生地址");
                    String Site = sc.next();
                    Method.addStuMsg(Id, Name, Sex, Site);
                    System.out.println("新增成功!");
                    Method.showStuTwoMsg();
                    break;
                case 2:
                    Method.showStuMsg();
                    break;
                case 3:
                    System.out.println("請輸入需要查詢學生的學號:");
                    int findOneStuId = sc.nextInt();
                    Method.findOneStuId(findOneStuId);
                    if (findOneStuId == -1) {
                        System.out.println("查無此人");
                        break;
                    }
                    break;
                case 4:
                    System.out.println("請輸入需要修改資訊的學生學號:");
                    int updateStuId = sc.nextInt();
                    Method.showStuMsg();
                    int updateIndex = Method.findOneStuId(updateStuId);
                    if (updateIndex == -1) {
                        System.out.println("查無此人!");
                        break;
                    }
                    System.out.println("請輸入修改後的學生學號:");
                    DateBase.stuID[updateIndex] = sc.nextInt();
                    System.out.println("請輸入修改後的學生姓名:");
                    DateBase.stuName[updateIndex] = sc.next();
                    System.out.println("請輸入修改後的學生性別:");
                    DateBase.stuSex[updateIndex] = sc.next();
                    System.out.println("請輸入修改後的學生地址:");
                    DateBase.stuSite[updateIndex] = sc.next();
                    System.out.println("修改成功!");
                    Method.showStuTwoMsg();
                    break;
                case 5:
                    System.out.println("請輸入需要刪除資訊的學生學號:");
                    int deleteStuID = sc.nextInt();
                    Method.showStuMsg();
                    int deleteIndex = Method.findOneStuId(deleteStuID);
                    if (deleteIndex == -1){
                        System.out.println("查無此人!");
                        break;
                    }
                    System.out.println("確認刪除學生資訊? 1.確認 2.取消");
                    int a = sc.nextInt();
                    if (a == 2){
                        break;
                    }
                    Method.deleteStuMsg(deleteIndex);
                    System.out.println("刪除成功!");
                    Method.showStuTwoMsg();
                    break;
                case 6:
                    Method.oneKeyAddStuMsgByEight();
                    break;
                case 0:
                    return;
            }
        }
    }
}