Boyan5

OOP 2

Oct 19th, 2021 (edited)
198
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         Teacher[] teachers = new Teacher[5];
  4.  
  5.         teachers[0] = new Teacher("Boyan", "0929929", new Subject("Math"));
  6.         teachers[1] = new Teacher("Mitko", "07213", new Subject("Chemistry"));
  7.         teachers[2] = new Teacher("Asen", "09876", new Subject("Geography"));
  8.         teachers[3] = new Teacher("Stilian", "08830", new Subject("Physics"));
  9.         teachers[4] = new Teacher("Blagoy", "07712", new Subject("History"));
  10.  
  11.         for (Teacher s : teachers) {
  12.             System.out.println(s.getName());
  13.             System.out.println(s.getSubject().getSubjectname());
  14.         }
  15.        
  16.         if (hasSubject(new Subject("Math"), teachers)) {
  17.             System.out.println("I have a Math teacher");
  18.         } else {
  19.             System.out.println("I don't have a Math teacher");
  20.         }
  21.  
  22.         if (hasSubject(new Subject("Biology"), teachers)) {
  23.             System.out.println("I have a Biology teacher");
  24.         } else {
  25.             System.out.println("I don't have a Biology teacher");
  26.         }
  27.  
  28.         if (hasSubject(new Subject("History"), teachers)) {
  29.             System.out.println("I have a History teacher");
  30.         } else {
  31.             System.out.println("I don't have a History teacher");
  32.         }
  33.  
  34.         if (hasSubject(new Subject("Physics"), teachers)) {
  35.             System.out.println("I have a Physics teacher");
  36.         } else {
  37.             System.out.println("I don't have a Physics teacher");
  38.         }
  39.     }
  40.     public static boolean hasSubject(Subject subject, Teacher[] arr){
  41.         for (Teacher s:arr) {
  42.             if(s.getSubject().getSubjectname().equals(subject.getSubjectname())){
  43.                 return true;
  44.             }
  45.         }
  46.         return false;
  47.     }
  48. }
  49.  
  50. public class Person {
  51.     private String name;
  52.     private String egn;
  53.  
  54.     public Person(){
  55.         this.name = "";
  56.         this.egn = "";
  57.     }
  58.     public Person(String name ,String egn){
  59.         this.name = name;
  60.         this.egn= egn;
  61.     }
  62.     public String getName() {
  63.         return this.name= name;
  64.     }
  65.     public void setName(String name) {
  66.         this.name = name;
  67.     }
  68.     public String getEgn() {
  69.         return this.egn = egn;
  70.     }
  71.     public void setEgn(String egn) {
  72.         this.egn = egn;
  73.     }
  74. }
  75.  
  76. public class Student extends Person {
  77.     public String fNum;
  78.  
  79.     public Student() {
  80.         super();
  81.         this.fNum = "";
  82.     }
  83.     public Student(String name, String egn, String fNum) {
  84.         super(name, egn);
  85.         this.fNum = fNum;
  86.     }
  87.     public String getfNum() {
  88.  
  89.         return this.fNum = fNum;
  90.     }
  91.     public void setfNum(String fNum) {
  92.  
  93.         this.fNum = fNum;
  94.     }
  95.     public static void sayHi() {
  96.         System.out.println("Hello");
  97.     }
  98.     public void printFnum() {
  99.         System.out.println("My Fnum is:" + this.fNum);
  100.     }
  101. }
  102.  
  103. public class Subject {
  104.     private String subjectname;
  105.  
  106.     public Subject(String name) {
  107.         this.subjectname = name;
  108.     }
  109.     public String getSubjectname() {
  110.         return subjectname;
  111.     }
  112.     public void setSubjectname(String subjectname) {
  113.         this.subjectname = subjectname;
  114.     }
  115. }
  116.  
  117. public class Teacher extends Person {
  118.     private Subject subject;
  119.  
  120.     public Teacher(Subject subject) {
  121.  
  122.         this.subject = subject;
  123.     }
  124.     public Teacher(String name, String egn, Subject subject) {
  125.         super(name, egn);
  126.         this.subject = subject;
  127.     }
  128.     public Subject getSubject() {
  129.  
  130.         return subject;
  131.     }
  132.     public void setSubject(Subject subject) {
  133.  
  134.         this.subject = subject;
  135.     }
  136. }
  137.  
RAW Paste Data