.java自拟选题,要求模拟实现对数据库的增、删、改、查操作。 利用集合2.要求至少要实现两个业务需求。
时间: 2023-12-16 21:03:10 浏览: 68
用JAVA来实现对数据库中信息的增删改查
选题:简单的学生信息管理系统
该系统包含两个业务需求:
1. 数据库的增、删、改、查操作
2. 简单的成绩统计功能(计算平均分、最高分、最低分等)
实现思路:
1. 首先定义一个学生类,包含学号、姓名、性别、年龄、班级等属性,还包含一些方法用于获取和设置属性值。
2. 使用集合(比如List)来模拟数据库,将学生对象存储在集合中。
3. 实现增、删、改、查等操作,比如添加学生信息、删除学生信息、修改学生信息、查询学生信息等。这些操作可以通过遍历集合来实现。
4. 实现成绩统计功能,可以定义一个方法用于计算平均分、最高分、最低分等。该方法可以通过遍历集合来实现。
代码实现:
```java
import java.util.ArrayList;
import java.util.List;
public class Student {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private String clazz; // 班级
private int score; // 成绩
public Student(String id, String name, String gender, int age, String clazz, int score) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.clazz = clazz;
this.score = score;
}
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;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public class StudentManager {
private List<Student> students;
public StudentManager() {
students = new ArrayList<>();
}
public List<Student> getStudents() {
return students;
}
// 添加学生信息
public void addStudent(Student student) {
students.add(student);
}
// 删除学生信息
public void removeStudent(String id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
break;
}
}
}
// 修改学生信息
public void updateStudent(Student student) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(student.getId())) {
students.set(i, student);
break;
}
}
}
// 查询学生信息
public Student getStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
return student;
}
}
return null;
}
// 统计学生成绩
public void scoreStatistics() {
int totalScore = 0;
int maxScore = Integer.MIN_VALUE;
int minScore = Integer.MAX_VALUE;
for (Student student : students) {
totalScore += student.getScore();
if (student.getScore() > maxScore) {
maxScore = student.getScore();
}
if (student.getScore() < minScore) {
minScore = student.getScore();
}
}
System.out.println("平均分:" + (totalScore / students.size()));
System.out.println("最高分:" + maxScore);
System.out.println("最低分:" + minScore);
}
}
public class Main {
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
// 添加学生信息
studentManager.addStudent(new Student("001", "张三", "男", 20, "计算机科学与技术", 85));
studentManager.addStudent(new Student("002", "李四", "女", 19, "软件工程", 90));
studentManager.addStudent(new Student("003", "王五", "男", 21, "信息与计算科学", 75));
studentManager.addStudent(new Student("004", "赵六", "女", 20, "数据科学与大数据技术", 80));
// 查询学生信息
Student student = studentManager.getStudent("002");
System.out.println("学号:" + student.getId() + ",姓名:" + student.getName() + ",性别:" + student.getGender() + ",年龄:" + student.getAge() + ",班级:" + student.getClazz() + ",成绩:" + student.getScore());
// 修改学生信息
student.setScore(95);
studentManager.updateStudent(student);
// 删除学生信息
studentManager.removeStudent("004");
// 统计学生成绩
studentManager.scoreStatistics();
}
}
```
阅读全文