Specialty and Ward Class in Java

Posted by Samath
  1328
May 18, 2015

 

Design and implement a class called Specialty that will store data about a ward’s specialty. Data for a specialty includes:
- specialtyName (e.g. “Cardiology”)
- numOfConsultants (e.g. 10)
- leadConsultant (e.g. “Mary Seacole”)

Your implementation should include:
a) A constructor which initializes all instance variables
b) Getter method for each instance attribute
c) A method called setLeadConsultant which accepts the name of a leading doctor and assigns the doctor to a specialty.
d) A toString() method which should return a string with the format:

Specialty of Ward:
           Name : <<insert specialty name>>
           Number of Consultants: ________
           Lead Consultant: _____

Specialty Class:

public class Specialty 
{
        private String specialtyName;
        private int numOfConsultants;
        private String leadConsultant;
        
        public Specialty(String sN, int nOc, String lC)
        {
            specialtyName = sN;
            numOfConsultants = nOc;
            leadConsultant = lC;
        }
        
        public String getSpecialtyName()
        {
            return specialtyName;
        }
                
        public int getNumOfConsultants()
        {
            return numOfConsultants;
        }
        
        public String getLeadConsultant()
        {
            return leadConsultant;
        }
        
        public void setLeadConsultant(String lC)
        {
            leadConsultant = lC;
        }
        
        public String toString()
        {
            String result = " ";
            
            result+="\n\t\tSpecialty of Ward: "+
                        "\n\t\t\tName : "+specialtyName+
                        "\n\t\t\tNumber of Consultants: "+numOfConsultants+
                        "\n\t\t\tLead Consultant: "+leadConsultant;
            
            return result;
        }
        
        
    
}

Design and implement a class called Ward that will store data about a hospital’s ward. Data includes:
- Ward number (e.g 12)
- Name of lead matron/nurse
- Number of beds
- Number of rooms
- Maximum number of patients allowed (note: should initially be set to the number of rooms available)
- Specialization

Your implementation should include:
a) A constructor which initializes all instance variables. All values (except the maximum number of patients allowed) should be accepted as parameters.
b) A toString()method which s should return a string with the format:
Ward Information:
           Ward Number : ____
           Lead Nurse/Matron: _______________
           Number of beds : ____
           Number of rooms: _____
                    Specialty of Ward:
                               Name : <<insert specialty name>>
                               Number of Consultants: ________
                               Lead Consultant: _____

In the Ward class, define the following methods that will set the maximum number of patients allowed:
void setMaxPatients()
          - Sets the maximum number of patients allowed to the number of beds on the ward.
void setMaxPatients (int numPerRoom)
          - Calculate the maximum number of patients allowed as number of rooms * number of patients per room.

Ward Class:

public class Ward 
{
    private int ward_number;
    private String name_of_lead_nurse;
    private int number_of_beds;
    private int number_of_rooms;
    private int maximum_number_of_patients;
    private Specialty Specialization;
    
    public Ward(int wN, String name_nurse, int num_of_bed, int num_of_room,Specialty Sp)
    {
        ward_number = wN;
        name_of_lead_nurse = name_nurse;
        number_of_beds = num_of_bed;
        number_of_rooms = num_of_room;
        Specialization = Sp;
    }
    
     public String toString()
        {
            String result = " ";
            
            result+="\n\n\tWard Information: "+
                        "\n\t\tWard Number: "+ward_number+
                        "\n\t\tLead Nurse/Matron: "+name_of_lead_nurse+
                        "\n\t\tNumber of beds: "+number_of_beds+
                        "\n\t\tNumber of rooms: "+number_of_rooms+
                        Specialization.toString();
            
            return result;
        }
     
     public void setMaxPatients()
     {
         maximum_number_of_patients = number_of_beds;
     }
     
     public void setMaxPatients (int numPerRoom)
     {
         maximum_number_of_patients = maximum_number_of_patients*number_of_rooms;
     }
    
    
    
}

Implement a driver class, called UWIHospital that will do the following:
a. Declare and initialize a list of wards.
b. Add up to 5 wards of your choice.
c. Use the first version of the setMaxPatients method to set the number of patients per ward for the third ward.
d. Use the second version of the setMaxPatients method to set the number of patients per ward for the fifth ward.
e. Use a while loop to display all wards.

UWIHospital Class:

import java.util.*;
public class UWIHospital 
{

    public static void main(String[] args) 
    {
       ArrayList<Ward> wList = new ArrayList<Ward>();
       
       Specialty sp1 = new Specialty("Cardiology", 10, "Mary Seacole");
       Specialty sp2 = new Specialty("Cardiology", 20, "Bill Clarke");
       
       Ward w1 = new Ward(12, "Joy Jones", 20, 20,sp1);
       Ward w2 = new Ward(18, "Joy Jones", 15, 15,sp2); 
       Ward w3 = new Ward(21, "Peter Smith", 13, 13,sp2);
       Ward w4 = new Ward(32, "Sam Kim", 20, 20,sp1);
       Ward w5 = new Ward(42, "John Jones", 20, 20,sp1);
       
       wList.add(w1);
       wList.add(w2);
       wList.add(w3);
       wList.add(w4);
       wList.add(w5);
       
       w3.setMaxPatients();
       w5.setMaxPatients(2);
       
       int count = 0;
       while(wList.size()!=count)
       {
           System.out.print(wList.get(count).toString());
           count++;
       }
       
    }
    
}