Advertisement
dzocesrce

[NP] Hospital System

Apr 19th, 2025
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.66 KB | None | 0 0
  1. package aud6.generics.hospitalSystem;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import java.util.stream.Collectors;
  7.  
  8. class HospitalSystem<T extends Patient>{
  9.     private ArrayList<T> patients;
  10.  
  11.     public HospitalSystem(ArrayList<T> patients) {
  12.         this.patients = patients;
  13.     }
  14.     //todo: add instance variable(s)
  15.  
  16. //todo: constructor
  17.  
  18. public void printResults(){
  19.  
  20.     //todo: define specific service implementations with lambda expressions
  21.     PatientService<T>findAllWithDoctor= (patients1, filter) -> (ArrayList<T>) patients1.stream().
  22.             filter(p->p.getDoctor().equals(filter)).collect(Collectors.toList());
  23.  
  24.     PatientService<T>countAllWithGenderAndAgeLessThanSixty= (patients1, filter) -> (ArrayList<T>) patients1.stream()
  25.             .filter(p->p.getAge()<60 && p.getGender().equals(filter)).collect(Collectors.toList());
  26.  
  27.     PatientService<T>findAllWithNameSorted= (patients1, filter) -> (ArrayList<T>) patients1.stream()
  28.             .filter(p->p.getName().equals(filter)).sorted().collect(Collectors.toList());
  29.  
  30.             System.out.println("FIRST SERVICE INFORMATION");
  31.     findAllWithDoctor.patientsInformation(this.patients,"Peter").forEach(System.out::println);
  32.  
  33.     System.out.println("SECOND SERVICE INFORMATION");
  34.     countAllWithGenderAndAgeLessThanSixty.patientsInformation(this.patients,"FEMALE").forEach(System.out::println);
  35.  
  36.     System.out.println("THIRD SERVICE INFORMATION");
  37.     findAllWithNameSorted.patientsInformation(this.patients,"Sarah").forEach(System.out::println);
  38.     }
  39. }
  40.  
  41. enum Gender {
  42.     FEMALE,
  43.     MALE
  44. }
  45.  
  46. interface Patient {
  47.     long getCode();
  48.  
  49.     String getName();
  50.  
  51.     int getAge();
  52.  
  53.     String getGender();
  54.  
  55.     String getDoctor();
  56.     //todo: write methods definitions here
  57. }
  58.  
  59. interface PatientService<T>{
  60. ArrayList<T>patientsInformation(ArrayList<T>patients, String filter);
  61. }
  62.  
  63. class PatientFromDB2 implements Patient, Comparable<PatientFromDB2> {
  64.     private String code;
  65.     private String name;
  66.     private int age;
  67.     private Gender gender;
  68.     private String doctor;
  69.     private String symptom;
  70.  
  71.     public PatientFromDB2(String code, String name, int age, Gender gender, String doctor, String symptom) {
  72.         this.code = code;
  73.         this.name = name;
  74.         this.age = age;
  75.         this.gender = gender;
  76.         this.doctor = doctor;
  77.         this.symptom = symptom;
  78.     }
  79.  
  80.     public static PatientFromDB2 createPatientFromDB2(String line) {
  81.         String[] parts = line.split("\\s+");
  82.         String code = parts[0];
  83.         String name = parts[1];
  84.         int age = Integer.parseInt(parts[2]);
  85.         Gender gender = parts[3].equals("FEMALE") ? Gender.FEMALE : Gender.MALE;
  86.         String doctor = parts[4];
  87.         String symptom = parts[5];
  88.         return new PatientFromDB2(code, name, age, gender, doctor, symptom);
  89.     }
  90.  
  91.     @Override
  92.     public long getCode() {
  93.         return this.code.hashCode();
  94.     }
  95.  
  96.     @Override
  97.     public String getName() {
  98.         return this.name;
  99.     }
  100.  
  101.     @Override
  102.     public int getAge() {
  103.         return this.age;
  104.     }
  105.  
  106.     @Override
  107.     public String getGender() {
  108.         return this.gender.toString();
  109.     }
  110.  
  111.     @Override
  112.     public String getDoctor() {
  113.         return this.doctor;
  114.     }
  115.  
  116.     @Override
  117.     public String toString() {
  118.         return "PatientFromDB2{" +
  119.                 "code='" + code + '\'' +
  120.                 ", name='" + name + '\'' +
  121.                 ", age=" + age +
  122.                 ", gender=" + gender +
  123.                 ", doctor='" + doctor + '\'' +
  124.                 ", symptom='" + symptom + '\'' +
  125.                 '}';
  126.     }
  127.  
  128.     @Override
  129.     public int compareTo(PatientFromDB2 o) {
  130.         return Long.compare(this.getCode(), o.getCode());
  131.     }
  132. }
  133.  
  134. class PatientFromDB1 implements Patient, Comparable<PatientFromDB1> {
  135.     private long code;
  136.     private String name;
  137.     private int age;
  138.     private Gender gender;
  139.     private String doctor;
  140.  
  141.     public PatientFromDB1(long code, String name, int age, Gender gender, String doctor) {
  142.         this.code = code;
  143.         this.name = name;
  144.         this.age = age;
  145.         this.gender = gender;
  146.         this.doctor = doctor;
  147.     }
  148.  
  149.     public static PatientFromDB1 createPatientFromDB1(String line) {
  150.         String[] parts = line.split("\\s+");
  151.         long code = Long.parseLong(parts[0]);
  152.         String name = parts[1];
  153.         int age = Integer.parseInt(parts[2]);
  154.         Gender gender = parts[3].equals("FEMALE") ? Gender.FEMALE : Gender.MALE;
  155.         String doctor = parts[4];
  156.         return new PatientFromDB1(code, name, age, gender, doctor);
  157.     }
  158.  
  159.     @Override
  160.     public long getCode() {
  161.         return this.code;
  162.     }
  163.  
  164.     @Override
  165.     public String getName() {
  166.         return this.name;
  167.     }
  168.  
  169.     @Override
  170.     public int getAge() {
  171.         return this.age;
  172.     }
  173.  
  174.     @Override
  175.     public String getGender() {
  176.         return this.gender.toString();
  177.     }
  178.  
  179.     @Override
  180.     public String getDoctor() {
  181.         return this.doctor;
  182.     }
  183.  
  184.     @Override
  185.     public String toString() {
  186.         return "PatientFromDB1{" +
  187.                 "code=" + code +
  188.                 ", name='" + name + '\'' +
  189.                 ", age=" + age +
  190.                 ", gender=" + gender +
  191.                 ", doctor='" + doctor + '\'' +
  192.                 '}';
  193.     }
  194.  
  195.     @Override
  196.     public int compareTo(PatientFromDB1 o) {
  197.         return Long.compare(this.code, o.code);
  198.     }
  199. }
  200.  
  201. public class HospitalSystemTest {
  202.     public static void main(String[] args) {
  203.         Scanner scanner = new Scanner(System.in);
  204.  
  205.         int db1Count = Integer.parseInt(scanner.nextLine());
  206.  
  207.         ArrayList<PatientFromDB1> patientsFromDB1 = IntStream.range(0, db1Count)
  208.                 .mapToObj(i -> PatientFromDB1.createPatientFromDB1(scanner.nextLine()))
  209.                 .collect(Collectors.toCollection(ArrayList::new));
  210.  
  211.         int db2Count = Integer.parseInt(scanner.nextLine());
  212.  
  213.         ArrayList<PatientFromDB2> patientsFromDB2 = IntStream.range(0, db2Count)
  214.                 .mapToObj(i -> PatientFromDB2.createPatientFromDB2(scanner.nextLine()))
  215.                 .collect(Collectors.toCollection(ArrayList::new));
  216.  
  217.         HospitalSystem<PatientFromDB1> hospital1System = new HospitalSystem<>(patientsFromDB1);
  218.         HospitalSystem<PatientFromDB2> hospital2System = new HospitalSystem<>(patientsFromDB2);
  219.  
  220.         System.out.println("--- FIRST HOSPITAL PATIENTS' INFORMATION ---");
  221.         hospital1System.printResults();
  222.  
  223.         System.out.println("--- SECOND HOSPITAL PATIENTS' INFORMATION ---");
  224.         hospital2System.printResults();
  225.     }
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement