Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. public class Classwork {
  2.  
  3. public static void main(String[] args){
  4.  
  5. Course courseA = new Course("Курс А");
  6. Course courseB = new Course("Курс В");
  7.  
  8. Student S1 = new Student("Иванов", "Иван");
  9. Student S2 = new Student("Петров", "Петр");
  10.  
  11. p("Регистрация на курсы");
  12. S1.registerForCourse(courseB);
  13. S2.registerForCourse(courseA);
  14. courseA.registerStudent(S2);
  15.  
  16. p("Студенты курса А");
  17. courseA.outp();
  18. p("Студенты курса В");
  19. courseB.outp();
  20.  
  21. p("Количество студентов на курсе А" + courseA.nrOfRegisteredStudent());
  22. p("Количество студентов на курсе А" + courseA.nrOfRegisteredStudent());
  23. }
  24.  
  25. public static void p(String s){
  26. System.out.println(s);
  27. }
  28. }
  29.  
  30.  
  31.  
  32.  
  33. _______________________________________________________
  34. _______________________________________________________
  35. public class Student {
  36.  
  37. static int nextId = 0;
  38. final int id;
  39. final String firstName, lastName;
  40.  
  41. public Student(String fn, String ln){
  42. nextId++;
  43. id = nextId;
  44. firstName = fn;
  45. lastName=ln;
  46. }
  47.  
  48. public String getFirstName(){
  49. return firstName;
  50. }
  51.  
  52. public String getLastName(){
  53. return lastName;
  54. }
  55.  
  56. public void registerForCourse(Course c){
  57. c.registerStudent(this);
  58. }
  59.  
  60. public void unregisterForCourse(Course c) {
  61. c.unregisterStudent(this);
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. _______________________________________________________
  69. _______________________________________________________
  70. import java.util.HashSet;
  71.  
  72. public class Course {
  73.  
  74. static int nextId = 0;
  75. final int id;
  76. final String name;
  77.  
  78. public Course(String n){
  79. id = nextId;
  80. nextId++;
  81. name = n;
  82. }
  83.  
  84. String getName(){
  85. return name;
  86. }
  87.  
  88. final HashSet<Student> registeredStudents = new HashSet<Student>();
  89.  
  90. void registerStudent(Student s){
  91. registeredStudents.add(s);
  92. }
  93.  
  94. void unregisterStudent(Student s){
  95. registeredStudents.remove(s);
  96. }
  97.  
  98. HashSet<Student> registeredStudents(){
  99. return registeredStudents;
  100. }
  101.  
  102. int nrOfRegisteredStudent(){
  103. return registeredStudents.size();
  104. }
  105.  
  106. void outp(){
  107. for (Student s: registeredStudents)
  108. System.out.println(s.getFirstName());
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement