我们今天来做一个学生选课程序:
首先思考,要做选课系统,首先我们需要有学生类,课程类,还有一个选课测试类;
在深入思考:
学生类需要有学生的一些属性(学号,姓名,课程表),课程表是用一个集合来实现的(假设我们并不知道学生要选多少课程,需要动态数组,就用到了集合);
课程类需要有课程的一些属性(课程号,课程名,选这门课的学生表),学生表是用一个集合来实现的(假设我们并不知道这门课程要有多少学生来选择,所以需要动态数组,就用到了集合);
测试类很简单,实例化几个学生,几门课程,然后相互添加,最后在打印出来就行了。
下面附代码:
学生类:
public class Student {
private int id;
private String name;
private List<Course> courseList;
public Student(int id,String name){
this.setId(id);
this.setName(name);
this.setCourseList(new ArrayList<Course>());
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public void addCourse(Course c){
this.courseList.add(c);
}
public void removeCourse(Course c){
this.courseList.remove(c);
}
}
课程类:
public class Course {
private int number;
private String name;
private float grade;
private List<Student> studentList;
public Course(int number,String name,float grade){
this.setNumber(number);
this.setName(name);
this.setGrade(grade);
this.setStudentList(new ArrayList<Student>());
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getGrade() {
return grade;
}
public void setGrade(float grade) {
this.grade = grade;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public void addStudent(Student s){
this.studentList.add(s);
}
public void removeStudent(Student s){
this.studentList.remove(s);
}
}
选课测试类:
public class Select {
public static void main(String[] args){
Student s1 = new Student(001,"张三");
Student s2 = new Student(002,"李四");
Student s3 = new Student(003,"王五");
Student s4 = new Student(004,"赵六");
Course c1 = new Course(1,"java班",3.5f);
Course c2 = new Course(2,".net班",4.0f);
Course c3 = new Course(3,"数据库班",5.0f);
Course c4 = new Course(4,"美工班",2.5f);
s1.addCourse(c1);
c1.addStudent(s1);
s1.addCourse(c2);
c2.addStudent(s1);
s2.addCourse(c2);
c2.addStudent(s2);
s2.addCourse(c3);
c3.addStudent(s2);
s2.addCourse(c4);
c4.addStudent(s2);
s3.addCourse(c3);
c3.addStudent(s3);
s3.addCourse(c4);
c4.addStudent(s3);
s4.addCourse(c4);
c4.addStudent(s4);
s4.addCourse(c1);
c1.addStudent(s4);
//随便选一个学生和一门课程列表
System.out.println("学生姓名:"+s2.getName());
Iterator<Course> courselist = s2.getCourseList().iterator();
while(courselist.hasNext()){
Course c = courselist.next();
System.out.println("选中了"+c.getName()+" 学分为"+c.getGrade());
}
System.out.println("---------------------------");
System.out.println("课程名称:"+c3.getName());
Iterator<Student> studentlist = c3.getStudentList().iterator();
while(studentlist.hasNext()){
Student s = studentlist.next();
System.out.println("拥有"+s.getId()+"号学生 学生姓名是"+s.getName());
}
}
}
下面看一下运行结果: