package com.imooc;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* 学生类
*
* @author jiuhu
*
*/
public class Student {
private String id; // 学号
private String name;// 姓名
private Set<Course> studentSelectCourses; // 学生选的课程 SET类里的元素是无序不可重复的对象;
// 构造方法
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
this.studentSelectCourses = new HashSet<Course>();
}
// getter,setter
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 Set<Course> getStudentSelectCourses() {
return studentSelectCourses;
}
public void setStudentSelectCourses(Set<Course> studentSelectCourses) {
this.studentSelectCourses = studentSelectCourses;
}
@Override // 覆写toString方法
public String toString() {
return "学号:" + id + ",姓名:" + name;
}
// 学生选课
public void addCourses(Course course) {
this.studentSelectCourses.add(course);
}
// 学生取消选课
public void removeCourses(Course course) {
this.studentSelectCourses.remove(course);
}
// 显示学生选课信息
public void printCourses() {
Iterator<Course> it = this.studentSelectCourses.iterator();
System.out.println(this.name+"同学所选的课程如下:");
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
}
package com.imooc;
/**
* 课程类
*
* @author jiuhu
*
*/
public class Course {
private String id; // 课程编号
private String name;// 课程名称
// 构造方法
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
// getter,setter
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;
}
@Override // 覆写toString方法
public String toString() {
return id + "." + name;
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/*
* 备选课程类
*/
public class SelectToCourses {
private List<Course> coursesList; // 课程列表 -- List对象里的元素有序可重复;
public SelectToCourses() {
super();
this.coursesList = new ArrayList<Course>(); // 创建备选课程对象
}
public List<Course> getCoursesList() {
return coursesList;
}
public void setCoursesList(List<Course> coursesList) {
this.coursesList = coursesList;
}
// 增加备选课程
public void addCourses() {
Course[] courses = { new Course("1", "数据结构"), new Course("2", "C语言"), new Course("3", "大学英语"),
new Course("4", "高等数学"), new Course("5", "微机原理") };
this.coursesList.addAll(Arrays.asList(courses));
// 显示备选课程
this.printCourses();
}
// 显示备选课程
public void printCourses() {
Iterator<Course> it = this.coursesList.iterator();
System.out.println("**可选课程如下:**");
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
// 删除备选课程
public void removeCourses() {
Course[] courses = { new Course("1", "数据结构"), new Course("2", "C语言") };
this.coursesList.removeAll(Arrays.asList(courses));
// 显示课程
this.printCourses();
}
// 修改备选课程
public void modifyCourses() {
// 把4.高等数学改为4.离散数学
this.coursesList.set(0, new Course("1","JAVA编程"));
this.printCourses();
}
}
package com.imooc;
import java.util.Scanner;
/**
* 测试类
*
* @author jiuhu
*
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 0; // 课程序号
int continueSelectCourse; // 继续选课标识 1是 0否
// 创建备选课程对象
SelectToCourses selectTocourses = new SelectToCourses();
selectTocourses.addCourses();
// 创建学生对象
Student student = new Student("1", "小明");
System.out.println("欢迎学生:" + student.getName() + "选课!");
// 创建输入流对象
Scanner input = new Scanner(System.in);
while (true) {
// 输入课程的门数count
System.out.println("你想选多少门课?");
int count = input.nextInt();
while (count > selectTocourses.getCoursesList().size() || count < 0) {
System.out.println("所选课程数量异常,请重新输入!");
System.out.println("你想选多少门课?");
count = input.nextInt();
}
// 循环遍历输入课程序号
for (int i = 0; i < count; i++) {
System.out.print("请输入第" + (i + 1) + "门课程的序号:");
number = input.nextInt();
// 输入的序号不能大于课程的总数或小于1
while (number > selectTocourses.getCoursesList().size() || number < 1) {
System.out.println("输入的课程序号有误,请重新输入!");
System.out.print("请输入第" + (i + 1) + "门课程的序号:");
number = input.nextInt();
}
number--;
student.addCourses(selectTocourses.getCoursesList().get(number)); // 把学生所选的课程加入学生选的课程列表
}
// 打印学生所选的课程
student.printCourses();
System.out.println("继续选课: 1是 0否");
continueSelectCourse = input.nextInt();
if (continueSelectCourse == 1) {
continue;
} else {
input.close();
System.out.println("**您已退出选课系统!**");
break;
} // 关闭输入流
}
}
}