java选择合适的集合 能够查询课程以及剩余的数量
时间: 2023-12-03 11:43:28 浏览: 21
在 Java 中,你可以使用 Map 接口来实现这个功能。具体来说,你可以将课程名称作为键,将剩余数量作为值,然后使用 get() 方法来查询特定的课程并获取其剩余数量。
以下是示例代码:
```
Map<String, Integer> courses = new HashMap<>();
courses.put("Java Programming", 10);
courses.put("Data Structures and Algorithms", 5);
courses.put("Database Management Systems", 8);
// 查询 Java Programming 课程的剩余数量
int remaining = courses.get("Java Programming");
System.out.println("Remaining slots for Java Programming course: " + remaining);
```
在上面的代码中,我们使用 HashMap 类来实现 Map 接口,并通过调用 put() 方法来添加课程及其剩余数量。然后,我们使用 get() 方法来查询特定课程的剩余数量,并将其存储在 remaining 变量中。最后,我们将 remaining 变量的值输出到控制台。
当然,如果你需要对课程及其剩余数量进行更复杂的操作,例如添加、删除或更新课程数量等,那么你可能需要考虑使用其他类型的集合,例如 List 或 Set。
相关问题
请用Java实现一个选课模块,要求: 1、选择合适的集合 2、能够查询课程以及剩余的数量 3、能够查询学生所选的课,也可以通过课反向查询课下面的学生(显示学号+姓名) 4、选课需要记录具体学生。
好的,根据您的要求,我给您提供一个Java实现的选课模块。
首先,我们需要定义两个类,一个是课程类(Course),另一个是学生类(Student)。
```
public class Course {
private String name; // 课程名称
private int capacity; // 课程容量
private int remaining; // 剩余容量
public Course(String name, int capacity) {
this.name = name;
this.capacity = capacity;
this.remaining = capacity;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public int getRemaining() {
return remaining;
}
public boolean select() {
if (remaining > 0) {
remaining--;
return true;
} else {
return false;
}
}
public void unselect() {
remaining++;
}
}
public class Student {
private String id; // 学生编号
private String name; // 学生姓名
private List<Course> courses; // 学生选的课程
public Student(String id, String name) {
this.id = id;
this.name = name;
this.courses = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public void selectCourse(Course course) {
if (course.select()) {
courses.add(course);
}
}
public void unselectCourse(Course course) {
if (courses.remove(course)) {
course.unselect();
}
}
}
```
在课程类中,我们定义了课程名称、课程容量和剩余容量三个属性,以及选择和取消选择课程的方法。在学生类中,我们定义了学生编号、学生姓名和选的课程三个属性,以及选课和退课的方法。
接下来,我们需要定义一个选课系统类(CourseSelectionSystem)来管理课程和学生。
```
public class CourseSelectionSystem {
private Map<String, Course> courses; // 所有课程
private Map<String, Student> students; // 所有学生
public CourseSelectionSystem() {
this.courses = new HashMap<>();
this.students = new HashMap<>();
}
public void addCourse(String name, int capacity) {
Course course = new Course(name, capacity);
courses.put(name, course);
}
public void addStudent(String id, String name) {
Student student = new Student(id, name);
students.put(id, student);
}
public List<Course> getCourses() {
return new ArrayList<>(courses.values());
}
public List<Student> getStudents() {
return new ArrayList<>(students.values());
}
public Course getCourse(String name) {
return courses.get(name);
}
public Student getStudent(String id) {
return students.get(id);
}
public List<Student> getStudentsByCourse(String name) {
Course course = courses.get(name);
if (course == null) {
return new ArrayList<>();
} else {
List<Student> result = new ArrayList<>();
for (Student student : students.values()) {
if (student.getCourses().contains(course)) {
result.add(student);
}
}
return result;
}
}
}
```
在选课系统类中,我们定义了所有课程和学生两个属性,以及添加课程和学生的方法,查询所有课程和学生的方法,以及根据课程查询选了这门课程的学生的方法。
最后,我们可以测试一下我们的选课系统。
```
public static void main(String[] args) {
CourseSelectionSystem system = new CourseSelectionSystem();
// 添加课程
system.addCourse("Java程序设计", 30);
system.addCourse("数据库原理", 40);
system.addCourse("数据结构与算法", 50);
// 添加学生
system.addStudent("001", "张三");
system.addStudent("002", "李四");
system.addStudent("003", "王五");
// 选课
Student student1 = system.getStudent("001");
Course course1 = system.getCourse("Java程序设计");
student1.selectCourse(course1);
Student student2 = system.getStudent("002");
Course course2 = system.getCourse("Java程序设计");
student2.selectCourse(course2);
Student student3 = system.getStudent("003");
Course course3 = system.getCourse("Java程序设计");
student3.selectCourse(course3);
// 查询课程和剩余容量
System.out.println("课程\t剩余容量");
for (Course course : system.getCourses()) {
System.out.println(course.getName() + "\t" + course.getRemaining());
}
// 查询学生选的课程
System.out.println("学号\t姓名\t选课情况");
for (Student student : system.getStudents()) {
System.out.print(student.getId() + "\t" + student.getName() + "\t");
for (Course course : student.getCourses()) {
System.out.print(course.getName() + " ");
}
System.out.println();
}
// 查询选了某门课的学生
System.out.println("选了Java程序设计的学生有:");
for (Student student : system.getStudentsByCourse("Java程序设计")) {
System.out.println(student.getId() + "\t" + student.getName());
}
}
```
输出结果如下:
```
课程 剩余容量
Java程序设计 27
数据库原理 40
数据结构与算法 50
学号 姓名 选课情况
001 张三 Java程序设计
002 李四 Java程序设计
003 王五 Java程序设计
选了Java程序设计的学生有:
001 张三
002 李四
003 王五
```
以上就是一个简单的选课模块的Java实现,希望对您有所帮助。
教务系统来说,选课是其所必备的功能,请实现一个选课模块,要求: 1、选择合适的集合 2、能够查询课程以及剩余的数量 3、能够查询学生所选的课,也可以通过课反向查询课下面的学生(显示学号+姓名) 4、选课需要记录具体学生。用java的hashmap完成
好的,下面是一个简单的选课模块的Java实现,使用HashMap来存储课程和学生的信息:
```java
import java.util.*;
public class CourseSelection {
// 用HashMap来存储课程和学生信息
private Map<String, Course> courses = new HashMap<>();
private Map<String, Student> students = new HashMap<>();
// 添加课程
public void addCourse(String courseName, int capacity) {
Course course = new Course(courseName, capacity);
courses.put(courseName, course);
}
// 添加学生
public void addStudent(String studentId, String name) {
Student student = new Student(studentId, name);
students.put(studentId, student);
}
// 选课
public boolean selectCourse(String studentId, String courseName) {
Student student = students.get(studentId);
Course course = courses.get(courseName);
if (student == null || course == null) {
return false;
}
if (student.getCourses().contains(course)) {
return true; // 已选过该课程
}
if (course.getRemaining() <= 0) {
return false; // 课程已满
}
student.addCourse(course);
course.addStudent(student);
return true;
}
// 查询课程信息
public void printCourseInfo(String courseName) {
Course course = courses.get(courseName);
if (course == null) {
System.out.println("该课程不存在!");
return;
}
System.out.println("课程名称:" + course.getName());
System.out.println("课程容量:" + course.getCapacity());
System.out.println("剩余容量:" + course.getRemaining());
}
// 查询学生选课信息
public void printStudentInfo(String studentId) {
Student student = students.get(studentId);
if (student == null) {
System.out.println("该学生不存在!");
return;
}
System.out.println("学生姓名:" + student.getName());
System.out.println("学生编号:" + student.getId());
System.out.println("所选课程:");
List<Course> courses = student.getCourses();
for (Course course : courses) {
System.out.println(course.getName());
}
}
// 查询某课程下的学生信息
public void printCourseStudents(String courseName) {
Course course = courses.get(courseName);
if (course == null) {
System.out.println("该课程不存在!");
return;
}
System.out.println("课程名称:" + course.getName());
System.out.println("选课学生:");
List<Student> students = course.getStudents();
for (Student student : students) {
System.out.println(student.getId() + " " + student.getName());
}
}
public static void main(String[] args) {
CourseSelection selection = new CourseSelection();
selection.addCourse("Java程序设计", 50);
selection.addCourse("数据结构", 40);
selection.addCourse("计算机组成原理", 30);
selection.addStudent("10001", "张三");
selection.addStudent("10002", "李四");
selection.addStudent("10003", "王五");
selection.selectCourse("10001", "Java程序设计");
selection.selectCourse("10001", "数据结构");
selection.selectCourse("10002", "Java程序设计");
selection.selectCourse("10003", "Java程序设计");
selection.printCourseInfo("Java程序设计");
selection.printStudentInfo("10001");
selection.printCourseStudents("Java程序设计");
}
}
// 课程类
class Course {
private String name;
private int capacity;
private List<Student> students = new ArrayList<>();
public Course(String name, int capacity) {
this.name = name;
this.capacity = capacity;
}
public String getName() {
return name;
}
public int getCapacity() {
return capacity;
}
public int getRemaining() {
return capacity - students.size();
}
public List<Student> getStudents() {
return students;
}
public void addStudent(Student student) {
students.add(student);
}
}
// 学生类
class Student {
private String id;
private String name;
private List<Course> courses = new ArrayList<>();
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public List<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
}
```
以上代码演示了一个简单的选课系统,可以添加课程和学生信息,选课,并查询课程和学生的选课信息。其中,课程和学生信息以HashMap进行存储,选课时需要对课程和学生的存在性进行判断,同时需要记录学生选课信息和课程选课学生信息。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)