简易学生管理系统

package com.test.practise.studentManagement;

public class Student {
    private String name;
    private String sex;
    private long telephone;

    public Student(String name, String sex, long telephone) {
        this.name = name;
        this.sex = sex;
        this.telephone = telephone;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public long getTelephone() {
        return telephone;
    }

    public void setTelephone(long telephone) {
        this.telephone = telephone;
    }
    public void pullStudent(){
        System.out.println("姓名:"+name+"\t性别:"+sex+"\t手机号码:"+telephone);
    }

    @Override
    public String toString() {
        return "姓名:"+name+"\t性别:"+sex+"\t手机号码:"+telephone;
    }
}

package com.test.practise.studentManagement;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Function {
    private static ArrayList<Student> students = new ArrayList<Student>();
    public ArrayList<Student> addStudents(Student stu){
        students.add(stu);
        return students;
    }
    public ArrayList<Student> putAllStudents(){
        return students;
    }
    public ArrayList<Student> deleteStudents(Student stu){
        students.remove(stu);

        return students;
    }
    public ArrayList<Student> alterStudents(Student stu){
        while (true){
            System.out.println("----输入需要修改的信息----\n1.姓名\n2.性别\n3.电话号码\n4.无需修改 退出");
            Scanner sc2 = new Scanner(System.in);
            int select1 = sc2.nextInt();
            switch (select1){
                case 1:
                    System.out.println("输入新修改的名字:");
                    Scanner sc3 = new Scanner(System.in);
                    String newName = sc3.nextLine();
                    stu.setName(newName);
                    break;
                case 2:
                    System.out.println("输入新修改的性别:");
                    Scanner sc4 = new Scanner(System.in);
                    String newSex = sc4.nextLine();
                    stu.setSex(newSex);
                    break;
                case 3:
                    System.out.println("输入新修改的电话号码:");
                    Scanner sc5 = new Scanner(System.in);
                    long newTelephoneNum = sc5.nextLong();
                    stu.setTelephone(newTelephoneNum);
                    break;
                case 4:
                    System.out.println("退出!");
                    break;

            }
            return students;
        }
    }
    public ArrayList<Student> selectBySNames(String selName){
        ArrayList<Student> students1 = new ArrayList<>();
        for (Student stu : students) {
            if (stu.getName().equals(selName)){
                students1.add(stu);
            }
        }
        return students1;
    }
    public void writeStudentToFile(FileOutputStream fis) throws IOException {
        for (Student stu : students) {
            fis.write(stu.toString().getBytes());
        }
    }
}

package com.test.practise.studentManagement;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Management {
    private static Function function = new Function();
    public static void main (String[] args)throws IOException {
        while (true){
            System.out.println("-----系统主菜单功能-----");
            System.out.println(
                    "1.添加学员\n" +
                    "2.删除学员\n" +
                    "3.修改学员\n" +
                    "4.查询学员信息\n" +
                    "5.显示所有学员信息\n" +
                    "6.保存学员信息\n" +
                    "7.退出系统");
            System.out.println("选择对应功能:");
            Scanner scanner = new Scanner(System.in);
            int select = scanner.nextInt();
            switch (select){
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    alterStudent();
                    break;
                case 4:
                    selectBySName();
                    break;
                case 5:
                    putAllStudent();
                    break;
                case 6:
                    writeToFile();
                    break;
            }
        }
    }
    public static void addStudent(){
        System.out.println("输入学生姓名:");
        Scanner sc1 = new Scanner(System.in);
        String name = sc1.nextLine();
        System.out.println("输入学生性别:");
        Scanner sc2 = new Scanner(System.in);
        String sex = sc2.nextLine();
        System.out.println("输入学生电话:");
        Scanner sc3 = new Scanner(System.in);
        long telephoneNum = sc3.nextLong();

        Student stu = new Student(name,sex,telephoneNum);
        function.addStudents(stu);
        System.out.println("添加成功!");
    }
    public static void deleteStudent(){
        ArrayList<Student> result = new ArrayList<Student>();
        System.out.println("输入需要删除的学生的名字:");
        Scanner sc1 = new Scanner(System.in);
        String delName = sc1.nextLine();

        result = function.selectBySNames(delName);
        if (result.isEmpty()){
            System.out.println("无此学生!");
        }else {
            result = function.deleteStudents(result.get(0));
            System.out.println("----现有学生----");
            for (Student stu : result) {
               stu.pullStudent();
            }
        }
    }
    public static void alterStudent(){
        ArrayList<Student> result = new ArrayList<Student>();
        System.out.println("输入需要修改的学生的名字:");
        Scanner sc1 = new Scanner(System.in);
        String altName = sc1.nextLine();

        result = function.selectBySNames(altName);

        if (result.isEmpty()){
            System.out.println("无此学生!");
        }else {
            result = function.alterStudents(result.get(0));

            System.out.println("----打印修改后学生信息----");
            for (Student stu : result) {
                stu.pullStudent();
            }
        }
    }
    public static void selectBySName(){
        System.out.println("输入需要查找的学生的名字:");
        Scanner sc1 = new Scanner(System.in);
        String selName = sc1.nextLine();

        ArrayList<Student> result = new ArrayList<Student>();
        result = function.selectBySNames(selName);

        if (result.isEmpty()){
            System.out.println("无此学生!");
        }
        else {
            System.out.println("----找到以下学生----");
            for (Student stu : result) {
                stu.pullStudent();
            }
        }
    }
    public static void putAllStudent(){
        ArrayList<Student> result = new ArrayList<Student>();
        result = function.putAllStudents();
        for (Student stu : result) {
            stu.pullStudent();
        }
    }
    public static void writeToFile() throws IOException {
        FileOutputStream fis = new FileOutputStream("D:\\JavaCode\\JavaSE\\student.txt");
        function.writeStudentToFile(fis);
        fis.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值