Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Classwork {
- public static void main(String[] args){
- Course courseA = new Course("Курс А");
- Course courseB = new Course("Курс В");
- Student S1 = new Student("Иванов", "Иван");
- Student S2 = new Student("Петров", "Петр");
- p("Регистрация на курсы");
- S1.registerForCourse(courseB);
- S2.registerForCourse(courseA);
- courseA.registerStudent(S2);
- p("Студенты курса А");
- courseA.outp();
- p("Студенты курса В");
- courseB.outp();
- p("Количество студентов на курсе А" + courseA.nrOfRegisteredStudent());
- p("Количество студентов на курсе А" + courseA.nrOfRegisteredStudent());
- }
- public static void p(String s){
- System.out.println(s);
- }
- }
- _______________________________________________________
- _______________________________________________________
- public class Student {
- static int nextId = 0;
- final int id;
- final String firstName, lastName;
- public Student(String fn, String ln){
- nextId++;
- id = nextId;
- firstName = fn;
- lastName=ln;
- }
- public String getFirstName(){
- return firstName;
- }
- public String getLastName(){
- return lastName;
- }
- public void registerForCourse(Course c){
- c.registerStudent(this);
- }
- public void unregisterForCourse(Course c) {
- c.unregisterStudent(this);
- }
- }
- _______________________________________________________
- _______________________________________________________
- import java.util.HashSet;
- public class Course {
- static int nextId = 0;
- final int id;
- final String name;
- public Course(String n){
- id = nextId;
- nextId++;
- name = n;
- }
- String getName(){
- return name;
- }
- final HashSet<Student> registeredStudents = new HashSet<Student>();
- void registerStudent(Student s){
- registeredStudents.add(s);
- }
- void unregisterStudent(Student s){
- registeredStudents.remove(s);
- }
- HashSet<Student> registeredStudents(){
- return registeredStudents;
- }
- int nrOfRegisteredStudent(){
- return registeredStudents.size();
- }
- void outp(){
- for (Student s: registeredStudents)
- System.out.println(s.getFirstName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement