设计一个学生管理系统 要求: 1、创建一个Java类,名为Student,包含以下属性:学生姓名(name)、学生年龄(age)、学生成绩(score)。 2、创建一个接口,名为Comparable,包含一个抽象方法compareTo(),用于
时间: 2024-03-01 21:51:09 浏览: 95
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
比较两个学生对象的成绩大小。
3、创建一个学生管理系统类(StudentManagementSystem),包含以下功能:
(1)添加学生
(2)删除学生
(3)修改学生信息
(4)按照学生成绩排序显示所有学生信息
具体实现如下:
1、Student类的代码如下:
```
public class Student {
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
```
2、Comparable接口的代码如下:
```
public interface Comparable {
int compareTo(Student student);
}
```
3、StudentManagementSystem类的代码如下:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void deleteStudent(Student student) {
students.remove(student);
}
public void modifyStudent(Student student, String name, int age, int score) {
student.setName(name);
student.setAge(age);
student.setScore(score);
}
public void sortByScore() {
Collections.sort(students, (s1, s2) -> s2.getScore() - s1.getScore());
}
public void showStudents() {
for (Student student : students) {
System.out.println("姓名:" + student.getName() + ",年龄:" + student.getAge() + ",成绩:" + student.getScore());
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
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、按照学生成绩排序显示所有学生信息");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生成绩:");
int score = scanner.nextInt();
Student student = new Student(name, age, score);
system.addStudent(student);
break;
case 2:
System.out.println("请输入学生姓名:");
name = scanner.next();
for (Student s : system.students) {
if (s.getName().equals(name)) {
system.deleteStudent(s);
break;
}
}
break;
case 3:
System.out.println("请输入学生姓名:");
name = scanner.next();
for (Student s : system.students) {
if (s.getName().equals(name)) {
System.out.println("请输入学生姓名:");
String newName = scanner.next();
System.out.println("请输入学生年龄:");
int newAge = scanner.nextInt();
System.out.println("请输入学生成绩:");
int newScore = scanner.nextInt();
system.modifyStudent(s, newName, newAge, newScore);
break;
}
}
break;
case 4:
system.sortByScore();
system.showStudents();
break;
default:
System.out.println("输入错误,请重新选择!");
break;
}
}
}
}
```
以上就是一个简单的学生管理系统的设计和实现,可以根据实际情况进行修改和扩展。
阅读全文