天天看點

8_21分享——學生管理系統 ZJL

Student類

package com.zjl.entity;

import com.zjl.comparable.Comparable;

public class Student implements Comparable<Student> {
    int id;
    String name;
    int age;
    String number;

    @Override
    public String toString() {
        return id+"\t\t"+name+"\t\t"+age+"\t\t"+number;
    }

    public Student(int id, String name, int age, String number) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.number = number;
    }

    public Student(String name, int age, String number) {
    }

    public String getName() {
        return name;
    }

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }


    @Override
    public int compareTo(Student student) {
        if (student == null) {
            throw new NullPointerException("is null");
        }
        if (this.getAge() > student.getAge()) {
            return 1;
        } else if (this.getAge() < student.getAge()) {
            return -1;
        } else {
            return 0;
        }
    }
}      
package com.zjl.demo;

import com.zjl.manger.StudentManger;

import java.util.Scanner;

public class StudentDemo {

    public static void main(String[] args) {
        while (true) {
            System.out.println("是否進入學生管理系統");
            System.out.println("1.是");
            System.out.println("2.否");
            Scanner input = new Scanner(System.in);
            int is = input.nextInt();
            if (is == 1) {
                StudentManger.run();
            } else if (is == 2) {
                System.out.println("好的,下次再見");
                break;
            } else {
                System.out.println("輸入錯誤,請重新選擇");
            }
        }
    }
}      
package com.zjl.manger;

import com.zjl.entity.Student;

import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StudentManger {
    static List<Student> list = new ArrayList<>();
    static Scanner input = new Scanner(System.in);
    static Student student = null;
    static boolean b = true;
    static String filePath="D:\\桌面\\Student";
    static File file=new File(filePath);
    static FileOutputStream fos=null;
    static FileInputStream fis=null;
    //運作程式
     public static void run() {
        menu();
    }

    //顯示菜單
    static void menu() {
        System.out.println("歡迎進入學生管理系統");
        //循環菜單界面
        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("++++++++++++++++++++++++");
            System.out.println("請選擇");
            int choose = input.nextInt();
            //根據輸入判斷進入什麼方法
            if (choose == 1) {
                addStu();
            } else if (choose == 2) {
                delStu();
            } else if (choose == 3) {
                updateStu();
            } else if (choose == 4) {
                selStu();
            } else if (choose == 5) {
                System.out.println("下次再見");
                break;
            }
            else {
                System.out.println("輸入錯誤,請重新輸入");
            }
        }
    }

    //添加學生資訊
    public static void addStu() {
         //判斷檔案是否存在 如果不存在 則建立一個
         if (!file.exists()){
             try {
                 file.createNewFile();
                 String info="學号    姓名    年齡     電話";
                 byte[] infos=info.getBytes();
                 fos=new FileOutputStream(filePath,true);
                 fos.write(infos,0,infos.length);
             } catch (IOException e) {
                 e.printStackTrace();
             }finally {
                 try {
                     fos.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
        while (true) {
            student = new Student();
            int a = 0;
            System.out.println("請輸入要添加學生學号");
            int id = input.nextInt();
            //根據學号判斷是否重複
            for (int i = 0; i < list.size(); i++) {
                System.out.println("進入for");
                Student student = list.get(i);
                if (id == student.getId()) {
                    System.out.println("添加失敗,該學号已存在,請重新輸入");
                    a = 1;
                    break;
                }
            }
            //根據a的值 判斷學号是否存在,如存在 則從新開始循環
            if (a == 1) {
                continue;
            }
            System.out.println("請輸入要添加學生姓名");
            String name = input.next();
            System.out.println("請輸入要添加學生年齡");
            int age = input.nextInt();
            System.out.println("請輸入要輸入的學生電話");
            String number = input.next();
            //修改學生屬性,存放到list中
            student.setName(name);
            student.setAge(age);
            student.setId(id);
            student.setNumber(number);
            list.add(student);
            System.out.println("添加成功");
            System.out.println("請問是否繼續添加嗎 1.是 2.否");
            int is = input.nextInt();
            //判斷是否繼續添加學生
            if (is == 1) {
                continue;
            } else {
                break;
            }
        }
    }

    //删除學生資訊
    public static void delStu() {
        System.out.println("請輸入要删除的學生學号");
        int id = input.nextInt();
        //使用for周遊集合,如果存在輸入的id,則講該元素删除
        for (int i = 0; i < list.size(); i++) {
            System.out.println("進入for");
            Student student = list.get(i);
            if (id == student.getId()) {
                list.remove(student);
                System.out.println("删除成功");
                b = true;
                break;
            } else {
                b = false;
            }
        }
        if (b == false) {
            System.out.println("該學号已不存在");
        }
    }

    //修改學生資訊
    public static void updateStu() {
        System.out.println("請輸入要修改的學生學号");
        int id= input.nextInt();
        //判斷集合中是否有輸入的名稱
        for (int i = 0; i < list.size(); i++) {
            System.out.println("進入for");
            Student student = list.get(i);
            System.out.println(student.getName());
            if (id==student.getId()) {
                b = true;
                break;
            } else {
                b = false;
            }
        }
        if (b) {
            System.out.println("查有此人");
            System.out.println("目前資訊為 學号:"+student.getId()+" 姓名:"+student.getName()+
                    " 年齡:"+student.getAge()+" 電話"+student.getNumber());
            System.out.println("請輸入修改後的姓名");
            String name = input.next();
            student.setName(name);
            System.out.println("請輸入修改後的年齡");
            int age = input.nextInt();
            student.setAge(age);
            System.out.println("請輸入修改後的電話");
            String number = input.next();
            student.setNumber(number);
            System.out.println("修改成功;修改後的資料為:學号:"+student.getId()+" 姓名:"+student.getName()+
                    " 年齡:"+student.getAge()+" 電話"+student.getNumber());
        }else {
            System.out.println("對不起,沒有找到此人");
        }
    }

    //查詢學生資訊
    public static void selStu() {
         if (!file.exists()){
             System.out.println("目前沒有學生資訊");
         }else {
             System.out.println("學号\t\t姓名\t\t年齡\t\t電話\t\t");
             Iterator iterator = list.iterator();
             //使用Iterator周遊集合 進行輸出
             while (iterator.hasNext()) {
                 Object obj = iterator.next();
                 System.out.println(obj);
             }
         }
    }
}