package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
private void testAdd() {
// TODO Auto-generated method stub
}
public List<Course> coursesToSelect;
public SetTest() {
coursesToSelect=new ArrayList<Course>();
Course cr1 = new Course("1","数据结构");//创建一个课程对象,并调用add方法,添加到备选课程中
coursesToSelect.add(cr1);
Course temp = (Course)coursesToSelect.get(0);
Course cr2 = new Course("2","C语言");
coursesToSelect.add(0,cr2);
Course temp2 = (Course)coursesToSelect.get(0);
Course[] course= {new Course("3","离散数学"),new Course("4","汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3 =(Course)coursesToSelect.get(2);
Course temp4 =(Course)coursesToSelect.get(3);
Course[] course2= {new Course("5","高等数学"),new Course("6","大学英语")};
coursesToSelect.addAll(2,Arrays.asList(course2));
Course temp5 =(Course)coursesToSelect.get(2);
Course temp6 =(Course)coursesToSelect.get(3);
}
public void testGet() { //取得List集合中的元素
int size =coursesToSelect.size();
System.out.println("有如下课程待选:");
for(int i=0;i<size;i++) {
Course cr=(Course)coursesToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[]args) {
SetTest st=new SetTest();
st.testAdd();
st.testGet();
//创建一个学生对象
Student student=new Student("1","小明");
System.out.println("欢迎学生"+student.name+"选课!");
Scanner console=new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.println("请输入ID");
String courseId = console.next(); //输入课程ID
for(Course cr:st.coursesToSelect) {
if(cr.id.equals(courseId)) {
student.courses.add(cr);
}
}
}
st.testForEachForSet(student);
}
public void testForEachForSet(Student student) {
for(Course cr:student.courses) {
System.out.println("选择了课程:"+cr.id+":"+cr.name);
}
}
}
————————————————
版权声明:本文为CSDN博主「不想掉头发。」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_73880686/article/details/133304162
学生选课系统
最新推荐文章于 2024-11-04 20:33:37 发布