学生选课系统是我们很常见的一种web应用程序,大家可以通过web登陆校园网,并且访问后台数据库,修改个人信息等方式进行选课,其实现原理大家可能并不是特别了解,下面这段代码则简单的将这个过程进行了一次演示,这不仅仅是一次简单的对选课系统的演示,也是对面向对象思想的一次实践,在整个过程中,用到了一些容器类,所以这段代码也是对容器类的一次整合应用和加深对List的体会,也同样是对分布式开发的一次尝试
选课系统的设计要求是:学生可以选择多门课程,课程也应该包含多个学生。
选课系统的设计思路是:分别创建学生类Student和课程类Course。学生类中包含课程列表和课程名称等属性;课程类中包含学生列表等属性
具体代码如下:
①:首先是创建Student类
public class Student {
private String name; //姓名
private String course; //课程名称
private List<Course> courseList; //课程队列
//实现对属性的封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
//重写构造函数
public Student(String name)
{
this.setName(name);
this.setCourseList(new ArrayList<Course>());
}
//构造添加方法,对list进行添加行为
public void add(Course c)
{
this.courseList.add(c);
}
}
②:创建课程类:
public class Course {
private String name; //课程名称
private float score; //课程学分
private List<Student> studentList; //学生队列
//实现属性的封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
//重写构造函数
public Course(String name , float score)
{
this.setName(name);
this.setScore(score);
this.setStudentList(new ArrayList<Student>());
}
//构造田间函数,对list进行添加动作
public void add(Student s)
{
this.studentList.add(s);
}
}
③:创建测试类:
public class TestSelectCourse {
public static void main(String args[])
{
List<Course> c = new ArrayList<Course>(); //创建课程队列,并通过泛型进行限制
List<Student> s = new ArrayList<Student>(); //创建学生队列
//实例化出几个学生,模拟数据库中的学生档案
Student s1 = new Student("张三");
Student s2 = new Student("李四");
Student s3 = new Student("王五");
// Student s4 = new Student("赵六");
// Student s5 = new Student("嘎嘎");
//将初始化出的学生加入队列
s.add(s1);
s.add(s2);
s.add(s3);
//初始化几门课程,并添加到课程队列
Course c1 = new Course("课程一",2.0f);
Course c2 = new Course("课程二",2.5f);
Course c3 = new Course("课程三",3.0f);
c.add(c1);
c.add(c2);
c.add(c3);
//设置关系,不但将学生添加到所选课程中的学生队列了,而且还应该将课程也添加进学生的课程队列
s1.add(c1);
s1.add(c2);
c1.add(s1);
c2.add(s1);
s2.add(c3);
s2.add(c1);
c3.add(s2);
c1.add(s2);
s3.add(c2);
c2.add(s3);
//通过双循环进行对数据的遍历并在控制台中输出
Iterator<Course> iter = c.iterator();
while(iter.hasNext())
{
Course cc = iter.next();
System.out.println("课程信息:"+cc.getName());
Iterator<Student> iters = cc.getStudentList().iterator();
while(iters.hasNext())
{
Student ss = iters.next();
System.out.println("|-"+ss.getName());
}
}
System.out.println("********分**割**线*********");
Iterator<Student> iter1 = s.iterator();
while(iter1.hasNext())
{
Student ss1 = iter1.next();
System.out.println("学生信息:"+ss1.getName());
Iterator<Course> iterc1 = ss1.getCourseList().iterator();
while(iterc1.hasNext())
{
Course cc1 = iterc1.next();
System.out.println("|-"+cc1.getName());
}
}
}
}
运行结果如下:
课程信息:课程一
|-张三
|-李四
课程信息:课程二
|-张三
|-王五
课程信息:课程三
|-李四
********分**割**线*********
学生信息:张三
|-课程一
|-课程二
学生信息:李四
|-课程三
|-课程一
学生信息:王五
|-课程二
结果分析:
结果正确的完成了学生含有多门课程,课程含有多个学生的业务要求
posted on 2010-10-30 22:59
Soap MacTavish 阅读(484)
评论(0) 编辑 收藏