用java编写一个管理系统
时间: 2023-08-25 16:07:43 浏览: 74
好的,管理系统可以有很多种类型,例如图书管理系统、学生管理系统、员工管理系统等等。这里我以学生管理系统为例,给您提供一个简单的代码框架。
```java
import java.util.Scanner;
public class StudentManagementSystem {
private static final int MAX_STUDENTS = 100; // 最大学生数
private static Student[] students = new Student[MAX_STUDENTS]; // 存储学生信息的数组
private static int numStudents = 0; // 当前学生数
public static void main(String[] args) {
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("0. 退出系统");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
searchStudent();
break;
case 5:
displayAllStudents();
break;
case 0:
System.out.println("谢谢使用!");
return;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
// 添加学生
private static void addStudent() {
if (numStudents >= MAX_STUDENTS) {
System.out.println("学生数已达到上限!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String id = scanner.nextLine();
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请输入性别:");
String gender = scanner.nextLine();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
Student student = new Student(id, name, gender, age);
students[numStudents] = student;
numStudents++;
System.out.println("添加成功!");
}
// 删除学生
private static void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < numStudents; i++) {
if (students[i].getId().equals(id)) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生!");
}
// 修改学生信息
private static void modifyStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < numStudents; i++) {
if (students[i].getId().equals(id)) {
System.out.println("请输入新的姓名:");
String name = scanner.nextLine();
System.out.println("请输入新的性别:");
String gender = scanner.nextLine();
System.out.println("请输入新的年龄:");
int age = scanner.nextInt();
students[i].setName(name);
students[i].setGender(gender);
students[i].setAge(age);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该学生!");
}
// 查询学生信息
private static void searchStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < numStudents; i++) {
if (students[i].getId().equals(id)) {
System.out.println(students[i]);
return;
}
}
System.out.println("未找到该学生!");
}
// 显示所有学生信息
private static void displayAllStudents() {
for (int i = 0; i < numStudents; i++) {
System.out.println(students[i]);
}
}
}
class Student {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "学号:" + id +
",姓名:" + name +
",性别:" + gender +
",年龄:" + age;
}
}
```
这个简单的学生管理系统实现了添加、删除、修改、查询和显示所有学生信息的功能。您可以根据自己的需求进行修改和扩展。
阅读全文