java:学生管理系统
时间: 2023-11-29 11:46:41 浏览: 60
以下是一个简单的Java学生管理系统的实现,包括添加、删除、修改和查看学生信息的功能。该系统由Student.java、Main.java、FunctionModule.java和Extents.java四个部分组成。
1. Student.java:定义了一个学生类,包括学生的姓名、年龄、性别和学号等属性。
```java
public class Student {
private String name;
private int age;
private String gender;
private String id;
public Student(String name, int age, String gender, String 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 String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
```
2. FunctionModule.java:定义了一个功能模块类,包括添加、删除、修改和查看学生信息的方法。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class FunctionModule {
private ArrayList<Student> students = new ArrayList<Student>();
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别:");
String gender = scanner.next();
System.out.println("请输入学生学号:");
String id = scanner.next();
Student student = new Student(name, age, gender, id);
students.add(student);
System.out.println("添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该学生!");
}
public void modifyStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别:");
String gender = scanner.next();
students.get(i).setName(name);
students.get(i).setAge(age);
students.get(i).setGender(gender);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该学生!");
}
public void viewStudent() {
for (int i = 0; i < students.size(); i++) {
System.out.println("姓名:" + students.get(i).getName() + " 年龄:" + students.get(i).getAge() + " 性别:" + students.get(i).getGender() + " 学号:" + students.get(i).getId());
}
}
}
```
3. Main.java:定义了一个主类,包括学生管理系统的主界面和各个功能的调用。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
FunctionModule functionModule = new FunctionModule();
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.退出系统");
System.out.println("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
functionModule.addStudent();
break;
case 2:
functionModule.deleteStudent();
break;
case 3:
functionModule.modifyStudent();
break;
case 4:
functionModule.viewStudent();
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
4. Extents.java:定义了一个扩展类,包括学号遍历和清空控制台的方法。
```java
public class Extents {
public static void traverseId(FunctionModule functionModule) {
for (int i = 0; i < functionModule.students.size(); i++) {
System.out.println("学号:" + functionModule.students.get(i).getId());
}
}
public static void clearConsole() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
```
阅读全文