天天看點

利用JAVA制作學生管理系統

前言

經過為期一個多星期的時間,利用java完成了項目學生管理系統,下面就把整個項目的各個功能的具體實作總結一下與大家分享。

一.main

public static void main(String[] args)
    ArrayList<Student> array = new ArrayList<Student>();
    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("請輸入你的選擇:");

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();

        switch (line) {
            case "1":
                System.out.println("添加學生");
                addStudent(array);
                break;
            case "2":
                deleteStudent(array);
                break;
            case "3":
                amendStudent(array);
                break;
            case "4":
                findAllStudnet(array);
                break;
            case "5":
                System.out.println("謝謝使用");
                System.exit(0);
        }

    }
}
      

二.添加學生

public static void addStudent(ArrayList<Student> array) {
    Scanner sc = new Scanner(System.in);
    String sid;

    while (true) {
        System.out.println("請輸入學生學号:");
         sid = sc.nextLine();

        boolean flag = isUsed(array, sid);
        if (flag) {
            System.out.println("你輸入的學号被占用,請重新輸入");
        } else {
            break;
        }
    }

    System.out.println("請輸入學生姓名:");
    String name = sc.nextLine();

    System.out.println("請輸入學生年齡:");
    String age = sc.nextLine();

    System.out.println("請輸入學生居住地:");
    String adress = sc.nextLine();

    Student s = new Student();
    s.setSid(sid);
    s.setName(name);
    s.setAge(age);
    s.setAddress(adress);

    array.add(s);

    System.out.println("添加學生成功");
}      

三.檢視所有學生

public static void findAllStudnet(ArrayList<Student> array) {
    if (array.size() == 0) {
        System.out.println("無資訊,請輸入資訊再查詢");
        return;
    }
    System.out.println("學号\t\t\t姓名\t\t年齡\t\t居住地");

    for (int i = 0; i < array.size(); i++) {
        Student s = array.get(i);
        System.out.println(s.getSid() + "\t" + s.getName() + "\t\t" + s.getAge() + "歲\t\t" + s.getAddress());
    }
}      

四.删除學生資訊

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

        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你要删除的學生的學号");
        String sid = sc.nextLine();

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }

        }
        if (index == -1) {
            System.out.println("該資訊不存在,請重新輸入");

        } else {
            array.remove(index);
            System.out.println("删除學生資訊成功");
        }
    }      

五.修改學生資訊

public static void amendStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("無資訊,請輸入資訊再修改");
            return;
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你要修改的學生的學号:");
        String sid = sc.nextLine();

        System.out.println("請輸入學生的新名字:");
        String name = sc.nextLine();
        System.out.println("請輸入學生的新年齡:");
        String age = sc.nextLine();
        System.out.println("請輸入學生的新居住地:");
        String adress = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(adress);

        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (student.getSid().equals(sid)) {
                array.set(i, s);
                break;
            }
        }
        System.out.println("修改學生資訊成功");

    }
}      

最後附上完整代碼:

package StudentMannager;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentMannager {

    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();
        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("請輸入你的選擇:");

            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();

            switch (line) {
                case "1":
                    System.out.println("添加學生");
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    amendStudent(array);
                    break;
                case "4":
                    findAllStudnet(array);
                    break;
                case "5":
                    System.out.println("謝謝使用");
                    System.exit(0);
            }

        }
    }

    public static void addStudent(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        String sid;

        while (true) {
            System.out.println("請輸入學生學号:");
             sid = sc.nextLine();

            boolean flag = isUsed(array, sid);
            if (flag) {
                System.out.println("你輸入的學号被占用,請重新輸入");
            } else {
                break;
            }
        }

        System.out.println("請輸入學生姓名:");
        String name = sc.nextLine();

        System.out.println("請輸入學生年齡:");
        String age = sc.nextLine();

        System.out.println("請輸入學生居住地:");
        String adress = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(adress);

        array.add(s);

        System.out.println("添加學生成功");
    }

    public static boolean isUsed(ArrayList<Student> array,String sid) {
        boolean flag = false;

        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(sid)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public static void findAllStudnet(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("無資訊,請輸入資訊再查詢");
            return;
        }
        System.out.println("學号\t\t\t姓名\t\t年齡\t\t居住地");

        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getSid() + "\t" + s.getName() + "\t\t" + s.getAge() + "歲\t\t" + s.getAddress());
        }
    }

    public static void deleteStudent(ArrayList<Student> array) {
//        if (array.size()==0){
//            System.out.println("無資訊,請輸入資訊後再删除");
//            return;
//        }
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你要删除的學生的學号");
        String sid = sc.nextLine();

        int index = -1;
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }

        }
        if (index == -1) {
            System.out.println("該資訊不存在,請重新輸入");

        } else {
            array.remove(index);
            System.out.println("删除學生資訊成功");
        }

//
//        for (int i =0;i<array.size();i++){
//            Student s =array.get(i);
//            if (s.getSid().equals(sid)){
//                array.remove(i);
//                break;
//
//            }
//
//            System.out.println("删除學生成功");
//        }
    }

    public static void amendStudent(ArrayList<Student> array) {
        if (array.size() == 0) {
            System.out.println("無資訊,請輸入資訊再修改");
            return;
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你要修改的學生的學号:");
        String sid = sc.nextLine();

        System.out.println("請輸入學生的新名字:");
        String name = sc.nextLine();
        System.out.println("請輸入學生的新年齡:");
        String age = sc.nextLine();
        System.out.println("請輸入學生的新居住地:");
        String adress = sc.nextLine();

        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(adress);

        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (student.getSid().equals(sid)) {
                array.set(i, s);
                break;
            }
        }
        System.out.println("修改學生資訊成功");

    }
}