Java实例---简单的上课管理系统

源码分析

Course.java

 1 package com.ftl.many2many;
 2 
 3 import java.util.*;
 4 
 5 public class Course
 6 {
 7     private int credit;
 8     private String name;
 9     private List<Student> allStudent;
10     public int getCredit()
11     {
12         return credit;
13     }
14     public void setCredit(int credit)
15     {
16         this.credit = credit;
17     }
18     public String getName()
19     {
20         return name;
21     }
22     public void setName(String name)
23     {
24         this.name = name;
25     }
26     public List<Student> getAllStudent()
27     {
28         return allStudent;
29     }
30     public void setAllStudent(List<Student> allStudent)
31     {
32         this.allStudent = allStudent;
33     }
34     public Course()
35     {
36         this.allStudent = new ArrayList<Student>();
37     }
38     public Course(String name, int credit)
39     {
40         this();
41         this.setCredit(credit);
42         this.setName(name);
43     }
44     
45     public String toString()
46     {
47         return "课程名称:" + this.name + "\t课程学分:" + this.credit;
48     }
49 }
View Code

School.java

 1 package com.ftl.many2many;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 public class School
 6 {
 7     private String name;
 8     private List<Student> allStudent;
 9     public School()
10     {
11         this.allStudent = new ArrayList<Student>();
12     }
13     public School(String name)
14     {
15         this();
16         this.name = name;
17     }
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     public List<Student> getAllStudent() {
25         return allStudent;
26     }
27     public void setAllStudent(List<Student> allStudent) {
28         this.allStudent = allStudent;
29     }
30     public String toString()
31     {
32         return "学校姓名" + this.name;
33     }
34     
35     
36     
37 }
View Code

Student.java

 1 package com.ftl.many2many;
 2 
 3 import java.util.*;
 4 public class Student 
 5 {
 6     private int age;
 7     private String name;
 8     private School school;
 9     private List<Course> allCourse;
10     
11     public Student()
12     {
13         this.allCourse = new ArrayList<Course>();
14     }
15     public Student(String name, int age)
16     {
17         this();
18         this.setName(name);
19         this.setAge(age);
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public School getSchool() {
28         return school;
29     }
30     public void setSchool(School school) {
31         this.school = school;
32     }
33     public int getAge() {
34         return age;
35     }
36     public void setAge(int age) {
37         this.age = age;
38     }
39     public List<Course> getAllCourse() {
40         return allCourse;
41     }
42     public void setAllCourse(List<Course> allCourse) {
43         this.allCourse = allCourse;
44     }
45     public String toString()
46     {
47         return "学生姓名:" + this.name + "\t 年龄:" + this.age;
48     }
49 }
View Code

testDemo.java

 1 package com.ftl.many2many;
 2 
 3 import java.util.Iterator;
 4 
 5 public class testDemo
 6 {
 7     public static void main(String[] args)
 8     {
 9         School sch = new School("海风大学");
10         Student s1 = new Student("张三", 12);
11         Student s2 = new Student("赵四", 22);
12         Student s3 = new Student("张5", 11);
13         Course c1 = new Course("计算机", 3);
14         Course c2 = new Course("语文", 1);
15         Course c3 = new Course("数学", 2);
16         //3个学生一个学校
17         s1.setSchool(sch);
18         s2.setSchool(sch);
19         s3.setSchool(sch);
20         //一个学校3个学生
21         sch.getAllStudent().add(s1);
22         sch.getAllStudent().add(s2);
23         sch.getAllStudent().add(s3);
24         //第一门课3个xues
25         c1.getAllStudent().add(s3);
26         c1.getAllStudent().add(s2);
27         c1.getAllStudent().add(s1);
28         s2.getAllCourse().add(c2);
29         s1.getAllCourse().add(c1);
30         s3.getAllCourse().add(c3);
31         
32         //第二门一个学生
33         c2.getAllStudent().add(s3);
34         s3.getAllCourse().add(c2);
35         c3.getAllStudent().add(s3);
36         s3.getAllCourse().add(c3);
37         //输出一门课信息,观察一门课多少学生:
38         System.out.println(c1);
39         Iterator<Student> iter = null;
40         iter = c1.getAllStudent().iterator();
41         System.out.println("C1 选课情况 :  ");
42         while(iter.hasNext())
43         {
44             Student c =  iter.next();
45             System.out.println("\t|-" + c);
46         }
47         System.out.println("----------------------------");
48         System.out.println("学校学生情况: ");
49         iter = sch.getAllStudent().iterator();
50         while(iter.hasNext())
51         {
52             Student stu = (Student) iter.next();
53             System.out.println("\t|-" + stu);
54         }
55         System.out.println("----------------------------");
56         //张5的选课情况:
57         System.out.println("学生张武选课情况: ");
58         System.out.println(s3);
59         Iterator<Course> it = s3.getAllCourse().iterator();
60         while(it.hasNext())
61         {
62             Course c = it.next();
63             System.out.println("\t|-" + c);
64         }
65         
66     }
67 }
View Code

源码下载

点击下载

posted @ 2018-07-22 21:44  小a玖拾柒  阅读(667)  评论(0编辑  收藏  举报