把困难踩在脚下

迎难而上

 

学生选课模拟程序

学生选课程序:一个学生可以选多个课程,同样一门课程不只是一个同学在选,有好多同

学选这门课程,这样学生和课程之间就是一个多对多的关系。为此我们封装一个学生类,

一个课程类。学生类里包括学生最基本的信息:姓名和年龄还有一个盛放课程的List集合

;在学生类里还有一个添加课程的方法addClasse;课程类里包括课程的基本信息:课程名

称和学分,还有一个盛放学生的List集合;在课程类里还有一个添加学生的方法

addStudent。本程序还有一个测试类,在测试类里定义了一个List集合cseList,

用来盛放课程,然后定义了三门课程和四个学生。一个学

生选择一门课程(使用Student类里的添加方法将这门课程添加到Student类定义的盛放课

程的集合里),那么这门课程必须添加这个学生(使用Course类里的添加方法将这个学生

添加到Course类定义的盛放学生的集合里)。

程序代码如下:

//学生类
package com.dr.xuanke;

import java.util.ArrayList;
import java.util.List;

public class Student {
 private String name;
 private int age;
 private List<Course> cseList;
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public List<Course> getCseList() {
  return cseList;
 }
 public void setCseList(List<Course> cseList) {
  this.cseList = cseList;
 }
 public Student(String name,int age)
 {
  this.setName(name);
  this.setAge(age);
  this.setCseList(new ArrayList<Course>());
 }
 public void addCourse(Course cse)
 {
  this.cseList.add(cse);
 }
 public String toString()
 {
  return "\t\t\t|-"+"姓名:"+" "+this.name+"  "+",年

龄:"+this.age;
 }

}
//课程类
package com.dr.xuanke;

import java.util.ArrayList;
import java.util.List;

public class Course {
 private String name;
 private float score;
 private List<Student> stuList;
 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> getStuList() {
  return stuList;
 }
 public void setStuList(List<Student> stuList) {
  this.stuList = stuList;
 }
 public Course(String name,float score)
 {
  this.setName(name);
  this.setScore(score);
  this.setStuList(new ArrayList<Student>());
 }
 public void addStu(Student stu)
 {
  this.stuList.add(stu);
 }
 public String toString()
 {
     return "课程名称|-"+this.getName()+" "+",学分:"+this.getScore(); 
 }

}
//测试类
package com.dr.xuanke;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {
        public static void main(String args[])
        {
         List<Course> cseList=new ArrayList<Course>();
         Course cse1=new Course("高数",2.0f);
         Course cse2=new Course("英语",3.0f);
         Course cse3=new Course("语文",5.0f);
         Student stu1=new Student("张三",20);
         Student stu2=new Student("李四",21);
         Student stu3=new Student("王五",30);
         Student stu4=new Student("李明",35);
         cseList.add(cse1);
         cseList.add(cse2);
         cseList.add(cse3);
         
         
         
         
         //张三选择高数、英语
         stu1.addCourse(cse1);
         cse1.addStu(stu1);
         stu1.addCourse(cse2);
         cse2.addStu(stu1);
         
         //李四选择语文、英语
         stu2.addCourse(cse3);
         cse3.addStu(stu2);
         stu2.addCourse(cse2);
         cse2.addStu(stu2);
         
         
         //王五选择高数、语文
         stu3.addCourse(cse1);
         cse1.addStu(stu3);
         stu3.addCourse(cse3);
         cse3.addStu(stu3);
         
         
         //输出结果
         Iterator<Course> iter1=cseList.iterator();
         while(iter1.hasNext())
         {
                        Course cse=iter1.next();
          System.out.println(cse);
          Iterator<Student> iter2=cse.getStuList().iterator();
          while(iter2.hasNext())
          {
           System.out.println(iter2.next());
          }
         }
         
         
        }
}
程序的输出结果如图所示:



此程序需要注意两点:1.了解课程与学生之间是多对多的关系,这就要考虑在课程和学生

类里定义两个盛放对方的集合。2.在新建课程对象和新建学生对象时一定要对类中定义的

集合初始化(在Course类的构造函数Course中的代码:this.setStuList(new

ArrayList<Student>());和在Student类的构造函数Student中的代码:this.setCseList

(new ArrayList<Course>());)。

posted on 2010-10-28 09:59 冯魁 阅读(609) 评论(2)  编辑  收藏

评论

# re: 学生选课模拟程序 2010-10-28 10:32 勇泽

这不会是你的课程设计吧?  回复  更多评论   

# re: 学生选课模拟程序[未登录] 2010-10-28 20:26 冯魁

@勇泽
不是,这如果是课程设计就太简单了!  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

快乐每一天!

Everything is an object!

常用链接

留言簿(2)

随笔档案

学习网站

搜索

最新评论

阅读排行榜

评论排行榜