//定义一课程表,模拟一个选课的过程,统计有多少人进行了选课操作
//File:Student.java
class Student{
private String num;
private String name;
private int age;
private char sex;
private String major;
private String classes;
public Student(){}
public Student(String num,String name,int age,char sex,String major,String classes){
this.num=num;
this.name=name;
this.age=age;
this.sex=sex;
this.major=major;
this.classes=classes;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
//省略部分getter和setter方法
}
//File:Course.java
import java.util.ArrayList;
class Course{
private String id;
private String name;
private int credithour;
protected static int counter;
private ArrayList <Student>stulist=new ArrayList<Student>();
public Course(){}
public Course(String id,String name,int credithour){
this.id=id;
this.name=name;
this.credithour=credithour;
}
public void bookCourse(Student stu){
stulist.add(stu);
counter++;
}
public void getMessage(){
System.out.println("课程编号:"+id);
System.out.println("课程名称:"+name);
System.out.println("课程学分:"+credithour);
System.out.println("已选课人数:"+counter);
System.out.println("选课人名单:");
for(int i=0;i<stulist.size();i++)
System.out.println(((Student)stulist.get(i)).getName());
}
}
//File:CourseTest.java
class CourseTest{
public static void main(String[] args){
Course courseA=new Course("1001","数据结构",3);
Course courseB=new Course("1003","操作系统",2);
Student stuA=new Student("001","小刚",20,'M',"软件工程","三班");
Student stuB=new Student("002","小红",21,'f',"软件工程","五班");
Student stuC=new Student("003","张三",22,'M',"软件工程","二班");
Student stuD=new Student("004","莉莉",19,'f',"软件工程","四班");
courseA.bookCourse(stuA);
courseA.bookCourse(stuB);
courseB.bookCourse(stuC);
courseB.bookCourse(stuD);
courseA.getMessage();
courseB.getMessage();
System.out.println("共有"+Course.counter+"个学生已选课");
}
}
//输出结果如下
C:/Documents and Settings/Administrator/桌面/CourseTest>java CourseTest
课程编号:1001
课程名称:数据结构
课程学分:3
已选课人数:4
选课人名单:
小刚
小红
课程编号:1003
课程名称:操作系统
课程学分:2
已选课人数:4
选课人名单:
张三
莉莉
共有4个学生已选课
C:/Documents and Settings/Administrator/桌面/CourseTest>
//File:Student.java
class Student{
private String num;
private String name;
private int age;
private char sex;
private String major;
private String classes;
public Student(){}
public Student(String num,String name,int age,char sex,String major,String classes){
this.num=num;
this.name=name;
this.age=age;
this.sex=sex;
this.major=major;
this.classes=classes;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
//省略部分getter和setter方法
}
//File:Course.java
import java.util.ArrayList;
class Course{
private String id;
private String name;
private int credithour;
protected static int counter;
private ArrayList <Student>stulist=new ArrayList<Student>();
public Course(){}
public Course(String id,String name,int credithour){
this.id=id;
this.name=name;
this.credithour=credithour;
}
public void bookCourse(Student stu){
stulist.add(stu);
counter++;
}
public void getMessage(){
System.out.println("课程编号:"+id);
System.out.println("课程名称:"+name);
System.out.println("课程学分:"+credithour);
System.out.println("已选课人数:"+counter);
System.out.println("选课人名单:");
for(int i=0;i<stulist.size();i++)
System.out.println(((Student)stulist.get(i)).getName());
}
}
//File:CourseTest.java
class CourseTest{
public static void main(String[] args){
Course courseA=new Course("1001","数据结构",3);
Course courseB=new Course("1003","操作系统",2);
Student stuA=new Student("001","小刚",20,'M',"软件工程","三班");
Student stuB=new Student("002","小红",21,'f',"软件工程","五班");
Student stuC=new Student("003","张三",22,'M',"软件工程","二班");
Student stuD=new Student("004","莉莉",19,'f',"软件工程","四班");
courseA.bookCourse(stuA);
courseA.bookCourse(stuB);
courseB.bookCourse(stuC);
courseB.bookCourse(stuD);
courseA.getMessage();
courseB.getMessage();
System.out.println("共有"+Course.counter+"个学生已选课");
}
}
//输出结果如下
C:/Documents and Settings/Administrator/桌面/CourseTest>java CourseTest
课程编号:1001
课程名称:数据结构
课程学分:3
已选课人数:4
选课人名单:
小刚
小红
课程编号:1003
课程名称:操作系统
课程学分:2
已选课人数:4
选课人名单:
张三
莉莉
共有4个学生已选课
C:/Documents and Settings/Administrator/桌面/CourseTest>