实现学生的管理系统代码Java,要求实现 1,学生的注册 2,学生的登入 3,学生的删除 4,学生的查询(包含条件查询,根据学生姓名查询,包括输入第一个姓(如张三的张,输入张这第一个字的时候就出现所有姓张的学生)) 5,学生的信息修改 学生属性 学号(唯一) 用户名 真实名 密码 年龄 性别
时间: 2023-09-23 11:11:06 浏览: 57
Java学生管理系统(控制台)实现了学生姓名,学号,居住地,年龄的添加,删除,修改,查询。解决了学号的重复问题,不存在的问题等
5星 · 资源好评率100%
以下是一个简单的学生管理系统的 Java 代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
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("6. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
register(studentList, scanner);
break;
case 2:
login(studentList, scanner);
break;
case 3:
delete(studentList, scanner);
break;
case 4:
search(studentList, scanner);
break;
case 5:
modify(studentList, scanner);
break;
case 6:
exit = true;
break;
default:
System.out.println("无效的操作,请重试!");
}
}
}
private static void register(ArrayList<Student> studentList, Scanner scanner) {
System.out.println("请输入学号:");
String id = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.println("该学号已经被注册!");
return;
}
}
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入真实姓名:");
String realName = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
System.out.println("请输入性别(男/女):");
String gender = scanner.next();
Student student = new Student(id, username, realName, password, age, gender);
studentList.add(student);
System.out.println("注册成功!");
}
private static void login(ArrayList<Student> studentList, Scanner scanner) {
System.out.println("请输入学号:");
String id = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
if (student.getPassword().equals(password)) {
System.out.println("登录成功!");
return;
} else {
System.out.println("密码错误!");
return;
}
}
}
System.out.println("该学号不存在!");
}
private static void delete(ArrayList<Student> studentList, Scanner scanner) {
System.out.println("请输入学号:");
String id = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
studentList.remove(student);
System.out.println("删除成功!");
return;
}
}
System.out.println("该学号不存在!");
}
private static void search(ArrayList<Student> studentList, Scanner scanner) {
System.out.println("请选择查询方式:");
System.out.println("1. 根据学号查询");
System.out.println("2. 根据姓名查询");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学号:");
String id = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("该学号不存在!");
break;
case 2:
System.out.println("请输入姓名首字母:");
String firstLetter = scanner.next().substring(0, 1);
boolean found = false;
for (Student student : studentList) {
if (student.getRealName().startsWith(firstLetter)) {
System.out.println(student);
found = true;
}
}
if (!found) {
System.out.println("没有找到任何学生!");
}
break;
default:
System.out.println("无效的操作,请重试!");
}
}
private static void modify(ArrayList<Student> studentList, Scanner scanner) {
System.out.println("请输入学号:");
String id = scanner.next();
for (Student student : studentList) {
if (student.getId().equals(id)) {
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:
System.out.println("请输入新的用户名:");
String username = scanner.next();
student.setUsername(username);
System.out.println("修改成功!");
break;
case 2:
System.out.println("请输入新的真实姓名:");
String realName = scanner.next();
student.setRealName(realName);
System.out.println("修改成功!");
break;
case 3:
System.out.println("请输入新的密码:");
String password = scanner.next();
student.setPassword(password);
System.out.println("修改成功!");
break;
case 4:
System.out.println("请输入新的年龄:");
int age = scanner.nextInt();
student.setAge(age);
System.out.println("修改成功!");
break;
case 5:
System.out.println("请输入新的性别(男/女):");
String gender = scanner.next();
student.setGender(gender);
System.out.println("修改成功!");
break;
default:
System.out.println("无效的操作,请重试!");
}
return;
}
}
System.out.println("该学号不存在!");
}
}
class Student {
private String id;
private String username;
private String realName;
private String password;
private int age;
private String gender;
public Student(String id, String username, String realName, String password, int age, String gender) {
this.id = id;
this.username = username;
this.realName = realName;
this.password = password;
this.age = age;
this.gender = gender;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
@Override
public String toString() {
return "学号:" + id + ",用户名:" + username + ",真实姓名:" + realName + ",年龄:" + age + ",性别:" + gender;
}
}
```
注意:该代码只是一个简单的示例,实际使用时需要根据具体需求进行修改和完善。
阅读全文