/
学生选课系统,用来练习collection集合。
/
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Student { //创建学生类
private int id ; //学生学号
private String name ; //学生姓名
private Set<Course> courses ; //学生所选课程
public Student(int id ,String name ){
this.id = id ;
this.name = name ;
this.courses = new HashSet<Course>() ;
}
public void testAdd(Course course){ //添加课程。
courses.add(course) ;
}
public void testRemove(Course course){ //删除所选课程
courses.remove(course) ;
}
public void testIterator(){ //查看所选课程
Iterator course = courses.iterator() ;
while(course.hasNext()){
System.out.println(course.next().toString());
}
}
public Set<Course> getCourses(){
return this.courses ;
}
public String toString(){
return "学号:"+this.id+"/t"+"姓名:"+this.name;
}
}
public class Course { //创建课程类
private int id ; //课程编号
private String name ; //课程名称
public Course(){
}
public Course(int id , String name ){
this.id = id ;
this.name = name ;
}
public int getId(){
return this.id ;
}
public String toString(){
return "编号:"+this.id+"\t"+"名称:"+this.name ;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class CoursesList { //学生选课清单
private ArrayList<Course> courses ; //备选课程
public CoursesList(){
this.courses = new ArrayList<Course>() ;
}
public void testAdd(){
Course course = new Course(1,"数据库基础") ;
courses.add(course) ;
Course course1 = new Course(2,"Java基础") ;
courses.add(course1) ;
Course [] course2 = new Course[]{new Course(3,"数据结构"),new Course(4,"算法基础")} ;
courses.addAll(0,Arrays.asList(course2)) ;
Course [] course3 = {new Course(5,"Oracle基础"),new Course(2,"Java基础")} ;
courses.addAll(3, Arrays.asList(course3)) ;
}
public void testIterator(){
System.out.println("有如下课程待选:");
Iterator<Course> iterator = courses.iterator() ;
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
/*
public void testForeach(){
System.out.println("有如下课程待选:");
for(Course course : courses){
System.out.println(course.toString());
}
}
public void testFor(){
System.out.println("有如下课程待选:");
for(int i = 0 ; i < courses.size(); i++){
System.out.println(courses.get(i).toString());
}
}
*/
public void testRemove(){
courses.remove(5) ;
}
public void testModify(){
courses.set(3,new Course(5,"离散数学")) ;
}
public ArrayList<Course> getCourses(){
return this.courses ;
}
}import java.util.Scanner;
public class TestArrayList {
public static void main(String [] args ){
CoursesList newcourse = new CoursesList() ;
newcourse.testAdd();
newcourse.testIterator();
// System.out.println("*");
// newcourse.testForeach() ;
// System.out.println("***");
// newcourse.testFor();
newcourse.testModify();
newcourse.testIterator();
newcourse.testRemove();
newcourse.testIterator();
Student student = new Student(1,"Jack") ;
System.out.println("每个人选修三门课程,请输入课程编号:");
Scanner input = new Scanner(System.in) ;
for(int i = 0 ; i < 3 ; i++ ){
System.out.println("请输入第"+(i+1)+"门课程编号:");
int num = input.nextInt();
for(Course course : newcourse.getCourses()){
if(course.getId() == num ){
student.testAdd(course);
}
}
}
int n = 0 ;
Label:do{
System.out.println("你所选的课程清单如下:");
student.testIterator();
System.out.println("是否确定所选的课程?");
System.out.println("请输入 Yes/No");
String str = input.next();
if(str.equals("Yes")){
System.out.println("恭喜你选课成功!");
break Label ;
}else if(str.equals("No")){
System.out.println("请输入你需要修改的编号:");
}else{
System.out.println("你输入有误,请重新输入:");
}
int j = input.nextInt() ;
for(Course course : student.getCourses()){
if(course.getId() == j){
student.testRemove(course);
}
}
System.out.println("已删除成功,请输入你要添加的课程的编号。");
int m = input.nextInt() ;
for(Course course : newcourse.getCourses()){
if(course.getId() == m){
student.testAdd(course);
}
}
}while(n < 3 ) ;
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章