用Java写一个学生管理系统,功能要求增加学生信息,查找学生信息,更新学生信息,删除学生信息,退出系统,还要在输入学生信息后返回学生对象,(注:学生信息用数组存储,并且使用单例模式)
时间: 2023-05-30 21:05:45 浏览: 83
以下是一个简单的示例代码,实现了上述功能:
```
import java.util.Scanner;
public class StudentManagementSystem {
private static StudentManagementSystem instance = null; // 单例模式的实例
private Student[] students; // 学生信息数组
private int count; // 学生信息数量
private StudentManagementSystem() {
students = new Student[100]; // 初始化学生信息数组
count = 0;
}
public static StudentManagementSystem getInstance() {
if (instance == null) {
instance = new StudentManagementSystem();
}
return instance;
}
// 增加学生信息
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息:");
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("学号:");
int id = scanner.nextInt();
Student student = new Student(name, age, gender, id);
students[count++] = student;
System.out.println("添加成功!");
}
// 查找学生信息
public void searchStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的学生姓名:");
String name = scanner.nextLine();
for (int i = 0; i < count; i++) {
if (students[i].getName().equals(name)) {
System.out.println(students[i]);
return;
}
}
System.out.println("没有找到该学生!");
}
// 更新学生信息
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要更新的学生姓名:");
String name = scanner.nextLine();
for (int i = 0; i < count; i++) {
if (students[i].getName().equals(name)) {
System.out.println("请输入更新后的学生信息:");
System.out.print("姓名:");
name = scanner.nextLine();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("学号:");
int id = scanner.nextInt();
students[i].setName(name);
students[i].setAge(age);
students[i].setGender(gender);
students[i].setId(id);
System.out.println("更新成功!");
return;
}
}
System.out.println("没有找到该学生!");
}
// 删除学生信息
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生姓名:");
String name = scanner.nextLine();
for (int i = 0; i < count; i++) {
if (students[i].getName().equals(name)) {
for (int j = i; j < count - 1; j++) {
students[j] = students[j+1];
}
count--;
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该学生!");
}
// 退出系统
public void exitSystem() {
System.out.println("感谢使用学生管理系统,再见!");
System.exit(0);
}
// 主菜单
public void menu() {
Scanner scanner = new Scanner(System.in);
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. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
searchStudent();
break;
case 3:
updateStudent();
break;
case 4:
deleteStudent();
break;
case 5:
exitSystem();
break;
default:
System.out.println("无效的选择,请重新输入!");
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = StudentManagementSystem.getInstance();
system.menu();
}
}
class Student {
private String name;
private int age;
private String gender;
private int id;
public Student(String name, int age, String gender, int id) {
this.name = name;
this.age = age;
this.gender = gender;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "学生信息:" +
"姓名='" + name + '\'' +
", 年龄=" + age +
", 性别='" + gender + '\'' +
", 学号=" + id;
}
}
```
阅读全文