Java基础——学生管理系统

  1. 用内存存储学生信息。(采用集合的方式)
    步骤:
    A. 定义学生类
    B. 学生管理系统的主界面的代码编写
    C. 学生管理系统的查看所有学生的代码编写
    D. 学生管理系统的添加学生的代码编写
    E. 学生管理系统的删除学生的代码编写
    F. 学生管理系统的修改学生的代码编写

代码:
学生类:Student.java

public class Student {
    private String id;
    private String name;
    private String age;
    private String address;

    public Student(){}

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

    public String getId() {
        return id;
    }

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

    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;
    }
}

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<Student>();

        while(true) {   //停止的另一种方式,设置标志位flag = true  while(flag)
            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.print("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //查看所有学生
                    findAllStudent(array);
                    System.out.println();
                    break;
                case "2":
                    //添加学生
                    addStudent(array);
                    System.out.println();
                    break;
                case "3":
                    deleteStudent(array);
                    System.out.println();
                    break;
                case "4":
                    updateStudent(array);
                    System.out.println();
                    break;
                case "5":

                default:
                    // System.out.println("------              5. 退出                              -----");
                    System.out.println("                     感谢您的使用                               ");
                    //flag = false;
                    System.exit(0);
                    break;
            }
        }
    }

    //查看所有学生
    public static void findAllStudent(ArrayList<Student> array){
        if(array.size() == 0){
            System.out.println("无学生信息,请重新选择");
            return;
        }
        System.out.println();
        System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄\t\t\t\t居住地");
        System.out.println("-------------------------------------------------------------------------");
        for(int x = 0; x < array.size(); x++)
        {
            Student s = array.get(x);
            System.out.println(s.getId()+"\t\t\t\t" + s.getName() + "\t\t\t\t" + s.getAge() + "\t\t\t\t" + s.getAddress());
        }
        System.out.println("-------------------------------------------------------------------------");
    }

    //添加学生
    public static void addStudent(ArrayList<Student> array)
    {
        Scanner in = new Scanner(System.in);
        String id ="";

        while(true) {
            System.out.print("输入学号:");
            id = in.nextLine();
            int x;
            boolean flag = false;
            for(x = 0; x < array.size(); x++ )
            {
                Student s = array.get(x);
                if(id.equals(s.getId())) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                System.out.println("学号被占用,请重新输入");
            }
            else{
                break;
            }

        }
        System.out.print("输入姓名:");
        String name = in.nextLine();
        System.out.print("输入年龄:");
        String age = in.nextLine();
        System.out.print("输入地址:");
        String address = in.nextLine();

        //创建学生对象
        Student s = new Student(id,name,age,address);
        /*
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        */
        array.add(s);
        System.out.println("添加学生成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> array){
        //思路:根据学号删除。先遍历学生列表查看学生是否存在
        Scanner in = new Scanner(System.in);
        System.out.print("输入要删除学生的学号:");
        String id = in.nextLine();

        int index = -1;
        for(int x = 0; x < array.size(); x++){
            Student s = array.get(x);
            if(id.equals(s.getId())){
                index = x;
                break;
            }
        }
        if(index == -1){
            System.out.print("该学生信息不存在,是否继续当前删除操作(Yes or No):");
            String choice = in.nextLine();
            if(choice.equalsIgnoreCase("Yes")){
                deleteStudent(array);
            }else if(choice.equalsIgnoreCase("No"))
            {
                return;
            }
            else{
                System.out.println("输入信息有误,返回主界面");
                return;
            }
        }else
        {
            array.remove(index);
            System.out.println("删除学生成功");
        }

    }

    //修改学生信息
    public static void updateStudent(ArrayList<Student> array){
        //输入学号查找学生是否存在
        Scanner in = new Scanner(System.in);
        System.out.print("输入要更新学生的学号:");
        String id = in.nextLine();

        int index = -1;
        for(int x = 0; x < array.size(); x++){
            Student s = array.get(x);
            if(id.equals(s.getId())){
                index = x;
                break;
            }
        }
        if(index == -1){
            System.out.print("该学生信息不存在,是否继续当前更新操作(Yes or No):");
            String choice = in.nextLine();
            if(choice.equalsIgnoreCase("Yes")){
                updateStudent(array);
            }else if(choice.equalsIgnoreCase("No"))
            {
                return;
            }
            else{
                System.out.println("输入信息有误,返回主界面");
                return;
            }
        }else
        {
            Student s = array.get(index);
            boolean flag = true;
            System.out.println("-----选择更新学生信息选项------");
            System.out.println("--      1. 姓名         --");
            System.out.println("--      2. 年龄         --");
            System.out.println("--      3. 地址         --");
            System.out.println("--      4. 返回主界面    --");
            System.out.println("--------------------------");
            while(flag) {
                System.out.print("请输入您的选择:");
                Scanner sc = new Scanner(System.in);
                String choice = sc.nextLine();
                switch (choice) {
                    case "1":
                        System.out.print("输入修改姓名:");
                        String name = in.nextLine();
                        s.setName(name);
                        System.out.print("继续更新操作(Yes or No):");
                        String a = in.nextLine();
                        if(a.equalsIgnoreCase("Yes"))
                            break;
                        else if(a.equalsIgnoreCase("No")){
                            array.set(index,s);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                        }
                    case "2":
                        System.out.print("输入修改年龄:");
                        String age = in.nextLine();
                        s.setAge(age);
                        System.out.print("继续更新操作(Yes or No):");
                        String b = in.nextLine();
                        if(b.equalsIgnoreCase("Yes"))
                            break;
                        else if(b.equalsIgnoreCase("No")){
                            array.set(index,s);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                        }

                    case "3":
                        System.out.print("输入修改地址:");
                        String address = in.nextLine();
                        s.setAddress(address);
                        System.out.print("继续更新操作(Yes or No):");
                        String c = in.nextLine();
                        if(c.equalsIgnoreCase("Yes"))
                            break;
                        else if(c.equalsIgnoreCase("No")){
                            array.set(index,s);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                    }
                    case "4":
                        array.set(index,s);
                        System.out.println("修改信息完成。");
                        return;
                    default:
                        System.out.println("输入信息有误。返回主界面");
                        return;
                }
            }
        }
    }
}

无信息,查看信息时:
Java基础——学生管理系统_第1张图片

添加学生:
Java基础——学生管理系统_第2张图片
Java基础——学生管理系统_第3张图片
在查看信息:
Java基础——学生管理系统_第4张图片
删除学生:
Java基础——学生管理系统_第5张图片
修改学生:
Java基础——学生管理系统_第6张图片
Java基础——学生管理系统_第7张图片

退出:
Java基础——学生管理系统_第8张图片

  1. 文本存储学生信息

学生类:

public class Student {
    private String id;
    private String name;
    private String age;
    private String address;

    public Student(){}

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

    public String getId() {
        return id;
    }

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

    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;
    }
}

思路:
Java基础——学生管理系统_第9张图片

(1)输出流:将集合中的内容输出到文本文件中
集合写入文本文件:
需求:键盘录入学生信息(学号,姓名,年龄,居住地)存入集合。然后遍历集合把每一个学生信息存入文本文件(每一个学生信息为一行数据,自己定义分割标记)

分析:
A. 定义学生类
B. 创建集合对象
C. 写方法实现键盘录入学生信息,并把学生对象作为元素添加集合
D. 创建输出缓冲流对象
E. 遍历集合,得到每一个学生信息,并把学生信息按照一定的格式写入文本文件。(123456,wang,22,wuhan)
F. 释放资源

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class WriteDemo {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> array = new ArrayList<Student>();
        Student s1 = new Student("123456","wang","22","wuhan");
        Student s2 = new Student("123457","zhang","28","beijing");
        Student s3 = new Student("123458","lisi","20","wixi");
        array.add(s1);
        array.add(s2);
        array.add(s3);

        BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));

        for(int x = 0; x < array.size(); x++)
        {
            Student s = array.get(x);
            StringBuilder sb = new StringBuilder();
            sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}

(2)输入流:从文本文件中读取信息到集合中

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ReaderDemo {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("array.txt"));

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

        String line;
        while((line = br.readLine())!= null){
            String[] strArray = line.split(",");

            Student s = new Student(strArray[0],strArray[1],strArray[2],strArray[3]);
            array.add(s);
        }
        br.close();


        System.out.println("学号\t\t姓名\t年龄\t居住地");
        for(int x = 0; x < array.size(); x++)
        {
            Student s = array.get(x);
            System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t\t" + s.getAddress());
        }
    }
}

IO版本代码如下所示:
代码:
注意:一定要先在本项目下创建文件Student.txt,要不然会报错找不到文件

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManage {
    public static void main(String[] args) throws IOException {
        String fileName = "Student.txt";

        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.print("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //查看所有学生
                    findAllStudent(fileName);
                    System.out.println();
                    break;
                case "2":
                    //添加学生
                    addStudent(fileName);
                    System.out.println();
                    break;
                case "3":
                    deleteStudent(fileName);
                    System.out.println();
                    break;
                case "4":
                    updateStudent(fileName);
                    System.out.println();
                    break;
                case "5":

                default:
                    // System.out.println("------              5. 退出                              -----");
                    System.out.println("                     感谢您的使用                               ");
                    //flag = false;
                    System.exit(0);
                    break;
            }
        }
    }

    //IO输入流:读文件
    public static void readData(String fileName, ArrayList<Student> array) throws IOException {
        //创建输入缓冲流对象
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        String line;
        while((line = br.readLine()) != null){
            String[] datas = line.split(",");
            Student s = new Student(datas[0],datas[1],datas[2],datas[3]);
            array.add(s);
        }
        br.close();
    }

    //IO输出流:写文件
    public static void writeData(String fileName, ArrayList<Student> array) throws IOException {
        //创建输入缓冲流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

        for(int x = 0; x < array.size(); x++)
        {
            Student s = array.get(x);
            StringBuilder sb = new StringBuilder();
            sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());

            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }

        bw.close();
    }



    //查看所有学生
    public static void findAllStudent(String fileName) throws IOException {
        ArrayList<Student> array = new ArrayList<Student>();
        readData(fileName,array);

        if(array.size() == 0){
            System.out.println("无学生信息,请重新选择");
            return;
        }
        System.out.println();
        System.out.println("学号\t\t\t\t姓名\t\t\t\t年龄\t\t\t\t居住地");
        System.out.println("-------------------------------------------------------------------------");
        for(int x = 0; x < array.size(); x++)
        {
            Student s = array.get(x);
            System.out.println(s.getId()+"\t\t\t\t" + s.getName() + "\t\t\t\t" + s.getAge() + "\t\t\t\t" + s.getAddress());
        }
        System.out.println("-------------------------------------------------------------------------");
    }

    //添加学生
    public static void addStudent(String fileName) throws IOException {
        ArrayList<Student> array = new ArrayList<Student>();
        readData(fileName,array);

        Scanner in = new Scanner(System.in);
        String id ="";

        while(true) {
            System.out.print("输入学号:");
            id = in.nextLine();
            int x;
            boolean flag = false;
            for(x = 0; x < array.size(); x++ )
            {
                Student s = array.get(x);
                if(id.equals(s.getId())) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                System.out.println("学号被占用,请重新输入");
            }
            else{
                break;
            }

        }
        System.out.print("输入姓名:");
        String name = in.nextLine();
        System.out.print("输入年龄:");
        String age = in.nextLine();
        System.out.print("输入地址:");
        String address = in.nextLine();

        //创建学生对象
        Student s = new Student(id,name,age,address);
        /*
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        */
        array.add(s);
        writeData(fileName, array);
        System.out.println("添加学生成功");
    }

    //删除学生
    public static void deleteStudent(String fileName) throws IOException {
        ArrayList<Student> array = new ArrayList<>();

        readData(fileName,array);
        //思路:根据学号删除。先遍历学生列表查看学生是否存在
        Scanner in = new Scanner(System.in);
        System.out.print("输入要删除学生的学号:");
        String id = in.nextLine();

        int index = -1;
        for(int x = 0; x < array.size(); x++){
            Student s = array.get(x);
            if(id.equals(s.getId())){
                index = x;
                break;
            }
        }
        if(index == -1){
            System.out.print("该学生信息不存在,是否继续当前删除操作(Yes or No):");
            String choice = in.nextLine();
            if(choice.equalsIgnoreCase("Yes")){
                deleteStudent(fileName);
            }else if(choice.equalsIgnoreCase("No"))
            {
                return;
            }
            else{
                System.out.println("输入信息有误,返回主界面");
                return;
            }
        }else
        {
            array.remove(index);
            writeData(fileName,array);
            System.out.println("删除学生成功");
        }

    }

    //修改学生信息
    public static void updateStudent(String fileName) throws IOException {
        ArrayList<Student> array = new ArrayList<Student>();
        readData(fileName,array);

        //输入学号查找学生是否存在
        Scanner in = new Scanner(System.in);
        System.out.print("输入要更新学生的学号:");
        String id = in.nextLine();

        int index = -1;
        for(int x = 0; x < array.size(); x++){
            Student s = array.get(x);
            if(id.equals(s.getId())){
                index = x;
                break;
            }
        }
        if(index == -1){
            System.out.print("该学生信息不存在,是否继续当前更新操作(Yes or No):");
            String choice = in.nextLine();
            if(choice.equalsIgnoreCase("Yes")){
                updateStudent(fileName);
            }else if(choice.equalsIgnoreCase("No"))
            {
                return;
            }
            else{
                System.out.println("输入信息有误,返回主界面");
                return;
            }
        }else
        {
            Student s = array.get(index);
            boolean flag = true;
            System.out.println("-----选择更新学生信息选项------");
            System.out.println("--      1. 姓名         --");
            System.out.println("--      2. 年龄         --");
            System.out.println("--      3. 地址         --");
            System.out.println("--      4. 返回主界面    --");
            System.out.println("--------------------------");
            while(flag) {
                System.out.print("请输入您的选择:");
                Scanner sc = new Scanner(System.in);
                String choice = sc.nextLine();
                switch (choice) {
                    case "1":
                        System.out.print("输入修改姓名:");
                        String name = in.nextLine();
                        s.setName(name);
                        System.out.print("继续更新操作(Yes or No):");
                        String a = in.nextLine();
                        if(a.equalsIgnoreCase("Yes"))
                            break;
                        else if(a.equalsIgnoreCase("No")){
                            array.set(index,s);
                            writeData(fileName,array);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                        }
                    case "2":
                        System.out.print("输入修改年龄:");
                        String age = in.nextLine();
                        s.setAge(age);
                        System.out.print("继续更新操作(Yes or No):");
                        String b = in.nextLine();
                        if(b.equalsIgnoreCase("Yes"))
                            break;
                        else if(b.equalsIgnoreCase("No")){
                            array.set(index,s);
                            writeData(fileName,array);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                        }

                    case "3":
                        System.out.print("输入修改地址:");
                        String address = in.nextLine();
                        s.setAddress(address);
                        System.out.print("继续更新操作(Yes or No):");
                        String c = in.nextLine();
                        if(c.equalsIgnoreCase("Yes"))
                            break;
                        else if(c.equalsIgnoreCase("No")){
                            array.set(index,s);
                            writeData(fileName,array);
                            System.out.println("更新操作完成,返回主界面");
                            return;
                        }
                        else{
                            System.out.println("输入信息有误,返回主界面");
                            return;
                        }
                    case "4":
                        array.set(index,s);
                        writeData(fileName,array);
                        System.out.println("修改信息完成。");
                        return;
                    default:
                        System.out.println("输入信息有误。返回主界面");
                        return;
                }
            }
        }
    }
}

Java基础——学生管理系统_第10张图片
Java基础——学生管理系统_第11张图片
Java基础——学生管理系统_第12张图片
Java基础——学生管理系统_第13张图片

Java基础——学生管理系统_第14张图片

Java基础——学生管理系统_第15张图片
Java基础——学生管理系统_第16张图片

Java基础——学生管理系统_第17张图片

你可能感兴趣的:(语言,Java)