创建学生类
/**
* 学生类
* @author DS_xiao_jian
* @date 2023.04.09 下午 03:19:00
*/
public class Student {
/**
* 学号
*/
private String id;
/**
*姓名
*/
private String name;
/**
* 年龄
*/
private int age;
/**
* 生日
*/
private String birth;
public Student(String id, String name, int age, String birth) {
this.id = id;
this.name = name;
this.age = age;
this.birth = birth;
}
public Student() {
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age=" + age +
", birth='" + birth + '\'' +
'}';
}
}
管理员类
/**
* 管理员类
* @author DS_xiao_jian
* @date 2023.04.09 下午 03:35:53
*/
public class Admin {
/**
* 用户名
*/
private String username = "root";
/**
* 密码
*/
private String passworld = "root";
public String getUsername() {
return username;
}
public String getPassworld() {
return passworld;
}
}
输出菜单与功能
import java.util.ArrayList;
import java.util.Scanner;
/**
* 学生管理系统
*
* @author DS_xiao_jian
* @date 2023.04.09 下午 03:39:15
*/
public class StuManagement {
public static void main(String[] args) {
//创建管理员对象
Admin admin = new Admin();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.next().trim();
if (!username.equals(admin.getUsername())) {
System.out.println("用户名错误");
return;
}
System.out.println("请输入密码:");
String password = scanner.next().trim();
if (!password.equals(admin.getPassworld())) {
System.out.println("密码错误");
return;
}
ArrayList<Student> list = new ArrayList<>();
boolean flag = true;
while (flag) {
// 1. 搭建主界面菜单
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("请输入您的选择:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("添加学生");
addStu(list);
System.out.println("本次添加完后,系统共有" + list.size() + "个学生");
break;
case 2:
System.out.println("删除学生");
removeStu(list);
break;
case 3:
System.out.println("修改学生");
updateStu(list);
break;
case 4:
System.out.println("查看学生");
viewAllStu(list);
break;
case 5:
System.out.println("退出");
flag = false;
break;
default:
System.out.println("没有此功能");
break;
}
}
}
/**
* 修改学生信息
*
* @param list 要修改的集合
*/
public static void updateStu(ArrayList<Student> list) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除学生的学号");
String id = scanner.next();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (id.equals(student.getId())) {
System.out.println("请修改学生的姓名");
student.setName(scanner.next());
System.out.println("请修改学生的年龄");
student.setAge(scanner.nextInt());
System.out.println("请修改学生的生日");
student.setBirth(scanner.next());
list.set(i, student);
}
}
}
/**
* 删除学生
*
* @param list 要删除的集合
*/
public static void removeStu(ArrayList<Student> list) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除学生的学号");
String id = scanner.next();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
if (id.equals(student.getId())) {
list.remove(i);
}
}
}
/**
* 查看所有学生
*
* @param list 要查看的集合
*/
public static void viewAllStu(ArrayList<Student> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
/**
* 添加学生
*
* @param list 要添加到的集合
*/
public static void addStu(ArrayList<Student> list) {
Scanner scanner = new Scanner(System.in);
Student student = new Student();
System.out.println("请录入学生的学号");
student.setId(scanner.next());
System.out.println("请录入学生的姓名");
student.setName(scanner.next());
System.out.println("请录入学生的年龄");
student.setAge(scanner.nextInt());
System.out.println("请录入学生的生日");
student.setBirth(scanner.next());
list.add(student);
}
}