一个简单的学生管理系统应该包括:学生对象,对学生对象的操作(管理):添加学生信息、删除学生信息、修改学生信息和显示学生信息,班级管理,添加班级,删除班级,修改班级信息,查询班级信息,学生有ID ,学生Student(属性封装){ id:Long唯一 学号:String,唯一 姓名:String 电话:String,唯一 年龄:Integer 性别:Integer、Boolean 爱好:String[] 班级:Class } 班级Class(属性封装){ id:Long唯一 班级名:String }还有一个界面显示。java简易代码
时间: 2024-03-25 13:40:15 浏览: 56
以下是一个简单的学生管理系统的 Java 代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
private long id;
private String studentNumber;
private String name;
private String phone;
private int age;
private int gender;
private String[] hobbies;
private Class clazz;
// getter and setter methods for all attributes
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
ArrayList<Class> classList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加学生信息 2.删除学生信息 3.修改学生信息 4.显示学生信息 5.添加班级 6.删除班级 7.修改班级信息 8.查询班级信息 9.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1: // 添加学生信息
Student student = new Student();
System.out.println("请输入学生ID:");
student.setId(scanner.nextLong());
System.out.println("请输入学生学号:");
student.setStudentNumber(scanner.next());
System.out.println("请输入学生姓名:");
student.setName(scanner.next());
System.out.println("请输入学生电话:");
student.setPhone(scanner.next());
System.out.println("请输入学生年龄:");
student.setAge(scanner.nextInt());
System.out.println("请输入学生性别(0表示男,1表示女):");
student.setGender(scanner.nextInt());
System.out.println("请输入学生爱好(多个用逗号隔开):");
student.setHobbies(scanner.next().split(","));
System.out.println("请输入学生班级ID:");
long classId = scanner.nextLong();
boolean foundClass = false;
for (Class clazz : classList) {
if (clazz.getId() == classId) {
student.setClazz(clazz);
foundClass = true;
break;
}
}
if (!foundClass) {
System.out.println("没有找到对应班级!");
} else {
studentList.add(student);
System.out.println("添加学生信息成功!");
}
break;
case 2: // 删除学生信息
System.out.println("请输入要删除的学生ID:");
long id = scanner.nextLong();
boolean found = false;
for (Student s : studentList) {
if (s.getId() == id) {
studentList.remove(s);
found = true;
System.out.println("删除学生信息成功!");
break;
}
}
if (!found) {
System.out.println("没有找到对应学生!");
}
break;
case 3: // 修改学生信息
System.out.println("请输入要修改的学生ID:");
long modifyId = scanner.nextLong();
boolean modifyFound = false;
for (Student s : studentList) {
if (s.getId() == modifyId) {
System.out.println("请输入要修改的学生信息:");
System.out.println("学号:" + s.getStudentNumber());
System.out.print("修改为:");
s.setStudentNumber(scanner.next());
System.out.println("姓名:" + s.getName());
System.out.print("修改为:");
s.setName(scanner.next());
System.out.println("电话:" + s.getPhone());
System.out.print("修改为:");
s.setPhone(scanner.next());
System.out.println("年龄:" + s.getAge());
System.out.print("修改为:");
s.setAge(scanner.nextInt());
System.out.println("性别:" + s.getGender());
System.out.print("修改为:");
s.setGender(scanner.nextInt());
System.out.println("爱好:" + String.join(",", s.getHobbies()));
System.out.print("修改为:");
s.setHobbies(scanner.next().split(","));
System.out.println("班级ID:" + s.getClazz().getId());
System.out.print("修改为:");
long modifyClassId = scanner.nextLong();
boolean modifyClassFound = false;
for (Class clazz : classList) {
if (clazz.getId() == modifyClassId) {
s.setClazz(clazz);
modifyClassFound = true;
break;
}
}
if (!modifyClassFound) {
System.out.println("没有找到对应班级!");
}
modifyFound = true;
System.out.println("修改学生信息成功!");
break;
}
}
if (!modifyFound) {
System.out.println("没有找到对应学生!");
}
break;
case 4: // 显示学生信息
System.out.println("所有学生信息如下:");
for (Student s : studentList) {
System.out.println("ID:" + s.getId() + " 学号:" + s.getStudentNumber() + " 姓名:" + s.getName() + " 电话:" + s.getPhone() + " 年龄:" + s.getAge() + " 性别:" + s.getGender() + " 爱好:" + String.join(",", s.getHobbies()) + " 班级:" + s.getClazz().getName());
}
break;
case 5: // 添加班级
Class clazz = new Class();
System.out.println("请输入班级ID:");
clazz.setId(scanner.nextLong());
System.out.println("请输入班级名称:");
clazz.setName(scanner.next());
classList.add(clazz);
System.out.println("添加班级成功!");
break;
case 6: // 删除班级
System.out.println("请输入要删除的班级ID:");
long deleteClassId = scanner.nextLong();
boolean deleteClassFound = false;
for (Class clazz1 : classList) {
if (clazz1.getId() == deleteClassId) {
classList.remove(clazz1);
deleteClassFound = true;
System.out.println("删除班级成功!");
break;
}
}
if (!deleteClassFound) {
System.out.println("没有找到对应班级!");
} else {
for (Student s : studentList) {
if (s.getClazz().getId() == deleteClassId) {
s.setClazz(null);
}
}
}
break;
case 7: // 修改班级信息
System.out.println("请输入要修改的班级ID:");
long modifyClassId = scanner.nextLong();
boolean modifyClassFound = false;
for (Class clazz1 : classList) {
if (clazz1.getId() == modifyClassId) {
System.out.println("请输入要修改的班级名称:");
System.out.println("班级名称:" + clazz1.getName());
System.out.print("修改为:");
clazz1.setName(scanner.next());
modifyClassFound = true;
System.out.println("修改班级信息成功!");
break;
}
}
if (!modifyClassFound) {
System.out.println("没有找到对应班级!");
}
break;
case 8: // 查询班级信息
System.out.println("请输入要查询的班级ID:");
long queryClassId = scanner.nextLong();
boolean queryClassFound = false;
for (Class clazz1 : classList) {
if (clazz1.getId() == queryClassId) {
System.out.println("班级ID:" + clazz1.getId() + " 班级名称:" + clazz1.getName());
queryClassFound = true;
break;
}
}
if (!queryClassFound) {
System.out.println("没有找到对应班级!");
}
break;
case 9: // 退出
System.out.println("感谢使用学生管理系统!");
System.exit(0);
default:
System.out.println("输入的操作不正确,请重新选择!");
}
}
}
}
class Class {
private long id;
private String name;
// getter and setter methods for all attributes
}
```
该程序中使用了 ArrayList 来存储学生和班级对象,并通过 switch-case 语句来实现各种操作。其中,学生对象和班级对象都使用了属性封装,通过 getter 和 setter 方法来访问和修改属性值。在添加、删除、修改学生信息和班级信息时,需要先输入对应的 ID 或者名称,然后根据输入的值来查找对应的对象。同时,还需要处理删除班级时,将班级下的所有学生的班级属性置为 null 的情况。
阅读全文