Java实践项目:学生信息管理系统(增、删、改、查、打印)

2023-12-28 17:38:05

1、前言

本文创建Java程序设计学生信息管理系统

2、StudentManage(学生信息管理包)

Operation(接口)

package StudentManage;

import java.util.ArrayList;

public interface Operation {
    //所有学生相关的方法都用接口来实现
    public abstract void peration(ArrayList<Student> array);
}

基本学生信息类(Student)?

package StudentManage;
//创建一个设计学生类
public class Student {
    private String id;
    private String name;
    private String sex;
    private int age;
    private String phonenum;
    private double chinese;
    private double math;
    private double english;
    private double java;
    private double total;

    public Student() {
        super();
    }

    public Student(String id, String name, String sex, int age, String phonenum, double chinese, double math, double english, double java, double total) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phonenum = phonenum;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.java = java;
        this.total = total;
    }

    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 getSex() {
        return sex;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getPhonenum() {
        return phonenum;
    }

    public void setPhonenum(String phonenum) {
        this.phonenum = phonenum;
    }

    public double getChinese() {
        return chinese;
    }

    public void setChinese(double chinese) {
        this.chinese = chinese;
    }

    public double getMath() {
        return math;
    }

    public void setMath(double math) {
        this.math = math;
    }

    public double getEnglish() {
        return english;
    }

    public void setEnglish(double english) {
        this.english = english;
    }

    public double getJava() {
        return java;
    }

    public void setJava(double java) {
        this.java = java;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return  "学号:" + id + "\t\t" +
                "名字:" + name + "\t\t" +
                "性别:" + sex + "\t\t" +
                "电话号码:" +phonenum + "\t\t" +
                "年龄:" + age +"\t\t" +
                "语文成绩:" + chinese +"\t\t" +
                "数学成绩:" + math +"\t\t" +
                "英语成绩:" + english +"\t\t" +
                "Java成绩:" + java +"\t\t" +
                "总成绩:" +total ;
    }
}

实现类 (实现方法)

查找学生信息实现类(FindOperation)

package StudentManage;

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

public class FindOperation implements Operation{
    @Override
    public void peration(ArrayList<Student> array) {
        //通过学生的学号、电话、名字
        System.out.println("请输入学生的学号(姓名或电话)");
        Scanner sc = new Scanner(System.in);
        String num = sc.next();

        int index = -1;

        //遍历存放学生的集合
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            //判断输入的学生信息是不是要找的学生
            if (s.getId().equals(num)||s.getName().equals(num)||
                    s.getPhonenum().equals(num)){
                index = i;
                break;
            }
        }

        //判断查找学生是否存在
        if (index == -1){
            System.out.println("找不到该学生");
        }else {
            System.out.println("该学生的信息为:");
            Student s = array.get(index);
            System.out.println(s);
        }
    }
}

添加学生信息实现类(AddOperation)

package StudentManage;

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

public class AddOperation implements Operation{
    @Override
    public void peration(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        //对学号进行验证 如果存在 提示已存在请重新输入
        boolean flag = false;//用于记录用户是否存在
        char input = 'N';//用于记录用户是否继续操作
        do {
            System.out.println("请输入学生的学号:");
            String id = sc.next();//拿学号去验证
            for (int i = 0; i < array.size(); i++) {
                if (array.get(i).getId().equals(id)){
                    flag = true;
                    break;
                }
            }
            if (flag) {
                System.out.println("该学生信息已经存在,请重新输入!");
            } else {
                //不存在继续输入剩余信息
                System.out.println("请输入学生姓名");
                String name = sc.next();
                System.out.println("请输入学生性别");
                String sex = sc.next();
                System.out.println("请输入学生年龄");
                int age = sc.nextInt();
                System.out.println("请输入学生电话号码");
                String phonenum = sc.next();
                System.out.println("请输入学生语文成绩");
                double chinese = sc.nextDouble();
                System.out.println("请输入学生数学成绩");
                double math = sc.nextDouble();
                System.out.println("请输入学生英语成绩");
                double english = sc.nextDouble();
                System.out.println("请输入学生Java成绩");
                double java = sc.nextDouble();
                Student s = new Student(id,name,sex,age,phonenum,chinese,math,english,
                        java,(chinese+math+english+java));
                array.add(s);
                System.out.println("是否继续添加(Y/N)");
                input =  sc.next().charAt(0);
            }
        } while (input == 'Y');

    }
}

删除学生信息实现类(DeleteOperation)

package StudentManage;

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

public class DeleteOperation implements Operation {
    @Override
    public void peration(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号");
        //输入要删除的学生学号,同时给出提示信息
        String id = sc.next();
        int index = -1;//定义一个存储下标的变量
        char input = 'N';
        for (int i = 0; i < array.size(); i++) {
            //同添加学生信息一样,运用一个循环语句匹配输入的学号与集合中的学号
            Student s = array.get(i);
            if (s.getId().equals(id)) {
                index = i;
                break;
            }
        }
        //不存在学生信息,无法删除
        if (index == -1) {
            System.out.println("该学生信息不存在,请重新输入!");
        } else {
            System.out.println("是否真要删除该学生信息(Y/N)");
            input = sc.next().charAt(0);
            //输入Y,从集合中删除该学生对象
            if (input=='Y'){
                array.remove(index);
                System.out.println("已删除学号为" + id + "的学生信息");
            }else {
                return;
            }
        }
    }
}

更改学生信息实现类(UpdateOperation)

package StudentManage;

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

public class UpdateOperation implements Operation{
    @Override
    public void peration(ArrayList<Student> array) {
        Scanner sc = new Scanner(System.in);
        //判断集合中是否有数据,如果没有显示提示信息
        System.out.println("请输入你要修改学生的学号:");
        //输入要修改的学生学号,同时给出提示信息
        String id = sc.next();
        int index = -1;//创建一个用于记录学生集合下标的变量
        for (int i = 0; i < array.size(); i++) {//同添加学生信息一样,运用一个循环语句匹配输入的学号与集合中的学号
            Student s = array.get(i);
            if (s.getId().equals(id)) {
                index = i;
                break;
            }
        }
        if (index == -1) {//不存在学生信息,无法删除
            System.out.println("该学生信息不存在,请重新输入!");
        } else {
            System.out.println("该学生的原始时间信息为:");
            System.out.println("学号\t\t姓名\t\t性别\t\t年龄\t\t手机号\t\t语文成绩\t\t数学成绩\t\t" +
                    "英语成绩\t\tJava成绩\t\t总成绩");
            Student s = array.get(index);
            System.out.println(s);
            System.out.println("请选择要修改的内容:");
            System.out.println("1、修改学生的基本信息\t 2、修改学生的成绩信息");
            int choose = sc.nextInt();
            // 对用户的选择进行判断
            if (choose == 1){
                // 获取用户输入的修改内容
                System.out.println("请输入修改后的学生姓名、性别、年龄、电话号码");
                String name = sc.next();
                String sex = sc.next();
                int age = sc.nextInt();
                String phonenum = sc.next();
                // 修改该学生的属性值
                s.setName(name);
                s.setSex(sex);
                s.setAge(age);
                s.setPhonenum(phonenum);
            }else if (choose == 2) {
                // 获取用户输入的修改内容
                System.out.println("请输入修改后的学生的语文成绩、数学成绩、英语成绩、Java成绩");
                double chinese = sc.nextDouble();
                double math = sc.nextDouble();
                double english = sc.nextDouble();
                double java = sc.nextDouble();
                double total = chinese+math+english+java;
                s.setChinese(chinese);
                s.setMath(math);
                s.setEnglish(english);
                s.setJava(java);
                s.setTotal(total);
            }else {
                System.out.println("你输入的选项有误");
            }
            // 提示修改操作成功
            System.out.println("修改成功");
            System.out.println("修改后的该学生的信息为:");
            System.out.println(s);
        }
    }
}

打印学生信息实现类(ShowOperation)

package StudentManage;

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

public class ShowOperation implements Operation {
    @Override
    public void peration(ArrayList<Student> array) {
        //判断集合是否为空
        if (array.size() == 0) {
            System.out.println("没有找到任何学生信息");
        } else {
            for (int i = 0; i < array.size(); i++) {
                System.out.println(array.get(i).toString());
            }
        }
    }
}


学生信息测试类(StudentDemo)

package StudentManage;

import UserLogin.UserTest;

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

public class StudentDemo {
    public static void main(String[] args) {
        //创建一个用于储存学生信息的集合
        ArrayList<Student> array = new ArrayList<>();

        Scanner sc = new Scanner(System.in);
        //创建测试用的对象
        Student s1 = new Student("123", "张三", "男", 18, "1234789",
                85, 85, 85, 85, 340);
        Student s2 = new Student("183", "李四", "男", 18, "1234789",
                85, 85, 85, 85, 340);
        Student s3 = new Student("193", "王五", "男", 18, "1234789",
                85, 85, 85, 85, 340);
        //存入集合
        array.add(s1);
        array.add(s2);
        array.add(s3);

        //登录
        //该方法表明用户是以什么身份登录的
        while (true) {
            int i = UserTest.main(args);
            //根据角色显示登录界面
            if (i == 1){//1是教师 2学生 0失败
                //老师界面
                while (true) {
                    screan();
                    int choose = sc.nextInt();
                    switch (choose){
                        case 1:
                            //查找学生信息
                            FindOperation find = new FindOperation();
                            find.peration(array);
                            break;
                        case 2:
                            //添加用户信息
                            AddOperation add = new AddOperation();
                            add.peration(array);
                            break;
                        case 3:
                            //删除学生信息
                            DeleteOperation delete = new DeleteOperation();
                            delete.peration(array);
                            break;
                        case 4:
                            //修改学生选项
                            UpdateOperation update = new UpdateOperation();
                            update.peration(array);
                            break;
                        case 5:
                            //打印学生信息
                            ShowOperation print = new ShowOperation();
                            print.peration(array);
                            break;
                        case 6://推出系统
                            System.exit(0);
                            break;
                    }
                }
            }else if (i == 2){
                //学生进行查询
                System.out.println("进行学生成绩查询");
                FindOperation find = new FindOperation();
                find.peration(array);
            }else {
                System.out.println("登录失败");
            }
        }
    }
    //界面
    public static void screan(){
        System.out.println("---------欢迎使用学生信息管理系统---------");
        System.out.println("1.查询学生信息\t2.添加学生信息\t3.删除学生信息\t4.修改学生信息\t" +
                "5.打印学生信息\t6.退出系统");
    }
}

3、UserLogin(用户登录包)

用户信息存储类(User)

package UserLogin;

public class User {
    //用户名,密码属性
    private String username;//用户名
    private String password;//密码

    public User() {
        super();
    }

    public User(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "用户名" + username + ",密码" + password;
    }
}

用户信息登录方法(Login)

package UserLogin;

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

public class Login {
    //登录验证
    public static int judge(ArrayList<User> arrayList) {
        //测试添加默认账户
        //教师账户
        User u1 = new User("zhangsan", "123456");
        User u2 = new User("lisi", "111111");

        //学生
        User u3 = new User("李华", "000000");
        User u4 = new User("赵四", "012345");

        //存储
        arrayList.add(u1);
        arrayList.add(u2);
        arrayList.add(u3);
        arrayList.add(u4);

        //输入
        System.out.println("----登录页面---\n 账户:");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        System.out.println("密码:");
        String password = sc.next();

        boolean flag = false;
        //验证登录信息
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getUsername().equals(name) &&
            arrayList.get(i).getPassword().equals(password)){
                flag = true;
                break;
            }
        }

        if (flag){
            System.out.println("登录成功!欢迎" + name + "欢迎使用");
            return 1;//成功
        }else {
            System.out.println("账户或密码有误,请找管理员修改后重试。");
            return 0;//错误
        }
    }

    //注册
    public static void add(ArrayList<User> arrayList){
        System.out.println("新知账户号:");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        System.out.println("输入新增密码:");
        String password = sc.next();

        boolean flag = false;
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getUsername().equals(name)){
                flag = true;
                break;
            }
        }

        if (flag){
            System.out.println("此账户已存在,请重新注册");
        }else {
            User u = new User();
            u.setUsername(name);
            u.setPassword(password);
            arrayList.add(u);
        }
    }
}

用户登录界面(UserTest)

package UserLogin;

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

public class UserTest {
    public static int main(String[] args){
        ArrayList<User> arrayList = new ArrayList<>();
        System.out.println("---欢迎进入学生信息管理系统---");
        System.out.println("请输入你的用户选项:");
        System.out.println("1、管理员(教师)\t 2、普通用户(学生)");
        Scanner sc = new Scanner(System.in);
        int choose = sc.nextInt();
        //判断账户类型
        if (choose==1){
            Login x = new Login();
            int y = x.judge(arrayList);
            if (y==1){
                //教师管理页面 教师选择进入哪个页面
                oper(arrayList);
                return 1;//
            }

        }else if (choose == 2){
            Login x = new Login();
            int y = x.judge(arrayList);
            if (y == 1){
                //进入学生管理选项
                return 2;// 返回2   表示学生登录成功
            }
        }
        return 0;//  表示登录失败
    }

    //教师进入学生管理系统
    public static void oper(ArrayList<User> arrayList){
        //教师登录以后,可以添加用户操作
        Scanner sc = new Scanner(System.in);

        //注册多个用户
        for (;;) {
            System.out.println("请输入对应的操作:");
            System.out.println("1、学生信息网页\t 2、添加教师登录账户\t 3、添加学生登录账户");
            int choose = sc.nextInt();
            //判断选择选项
            switch (choose){
                case 1:
                    return;
                case 2:
                case 3:
                    Login t = new Login();
                    t.add(arrayList);
                    break;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }
}

文章来源:https://blog.csdn.net/rch041126/article/details/135272206
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。