In: Computer Science
As a programmer, you have been asked to write a Java
application, using OOP concepts, for a Hospital with the following
requirements:
• The Hospital has several employees and each one of them has an ID
(int), name (string), address (string), mobile phone number
(string), email (string) and salary (double) with suitable data
types. • The employees are divided into:
o Administration staff: who have in addition to the previous
information their position (string). o Doctor: who have also a rank
(string) and specialty (string).
The project requirements: • You will need to create all the needed
Java classes with all the required information. • You have to apply
all the OOP (Object Oriented Programming) concepts that we have
covered in this
module (i.e. inheritance, polymorphism, interface and
collections)
• Include a Microsoft Word document (name it as readme.doc) that
includes several screenshots of
your Netbeans IDE application GUI interface as well as the output
screenshot results. Provide all
assumptions that you have made during design and
implementation.
• Include the whole Netbeans Java project with all source codes.
Write as much comments as possible
At the end, you will need to create a testing class (e.g.
Hospital.java) with the static main() method with the following
requirements:
• It must have an initial fixed collection of working staff (at
least 3 administration staffs and 2
doctors)
• The program will print a selection screen where the user can
choose the operation
he/she wants to perform. The selection screen will be repeated
after each selection until the staff
type the number 4 to completely exit from the program:
1. Add an administration staff (by providing all her/his
information) to the list of all
administration staff
2. Add a doctor (by providing all her/his information) to the list
of all doctors
3. Print all working staff (remember to differentiate between
administration staff and doctors
in the output printout
4. Exit the program
package inheritance;
import java.util.*;
// Defines class Employees
class Employees
{
// Instance variables to store employee
information
private int ID;
private String name;
private String address;
private String mobileNumber;
private String email;
private double salary;
// Default constructor to assign default values to
instance variables
public Employees()
{
ID = 0;
name = address = mobileNumber =
email = null;
salary = 0.0;
}
// Parameterized constructor to assign parameter
values to instance variables
public Employees(int ID, String name, String address,
String mobileNumber,
String email,
double salary)
{
this.ID = ID;
this.name = name;
this.address =address;
this.mobileNumber =
mobileNumber;
this.email = email;
this.salary = salary;
}
// Getter method
public int getID()
{
return ID;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getMobileNumber()
{
return mobileNumber;
}
public String getEmail()
{
return email;
}
public double getSalary()
{
return salary;
}
// Setter method
public void setID(int ID)
{
this.ID = ID;
}
public void setName(String name)
{
this.name = name;
}
public void setAddress(String address)
{
this.address = address;
}
public void setMobileNumber(String mobileNumber)
{
this.mobileNumber =
mobileNumber;
}
public void setEmail(String email)
{
this.email = email;
}
public void setSalary(double salary)
{
this.salary = salary;
}
// Overrides method toString() to return employee
information
public String toString()
{
return "\n Name: " + name + "\n
Address: " + address +
"\n Mobile Number: " + mobileNumber + "\n Email:
" + email +
"\n Salary: $" + salary;
}
}// End of class Emoloyees
// Defines derived class AdministrationStaff extends super class
Employees
class AdministrationStaff extends Employees
{
// Instance variable to store position
private String position;
// Default constructor to assign default value to
instance variable position
public AdministrationStaff()
{
// Calls base class default
constructor
super();
position = "";
}
// Parameterized constructor to assign parameter
values to instance variables
public AdministrationStaff(int ID, String name, String
address, String mobileNumber,
String email,
double salary, String position)
{
// Calls base class parameterized
constructor
super(ID, name, address,
mobileNumber, email, salary);
this.position = position;
}
// Getter method to return position
public String getPosition()
{
return position;
}
// Setter method to change position
public void setPosition(String position)
{
this.position = position;
}
// Overrides method toString() to return
administration staff information
public String toString()
{
return super.toString() + "\n
Position: " + position;
}
}// End of class AdministrationStaff
//Defines derived class Doctor extends super class
Employees
class Doctor extends Employees
{
// Instance variable to store rank and
speciality
private String rank;
private String specialty;
// Default constructor to assign default value to
instance variable position
public Doctor()
{
// Calls base class default
constructor
super();
rank = specialty = "";
}
// Parameterized constructor to assign parameter
values to instance variables
public Doctor(int ID, String name, String address,
String mobileNumber,
String email,
double salary, String rank, String specialty)
{
// Calls base class parameterized
constructor
super(ID, name, address,
mobileNumber, email, salary);
this.rank = rank;
this.specialty = specialty;
}
// Getter method to return rank
public String getRank()
{
return rank;
}
// Getter method to return speciality
public String getSpeciality()
{
return specialty;
}
// Setter method to change rank
public void setRank(String rank)
{
this.rank = rank;
}
// Setter method to change specialty
public void setSpeciality(String speciality)
{
this.specialty = speciality;
}
// Overrides method toString() to return doctor
information
public String toString()
{
return super.toString() + "\n Rank:
" + rank + "\n Speciality: " + specialty;
}
}// End of class Doctor
// Driver class definition
public class Hospital
{
// Declares an array list to store employee of type
doctor
ArrayList<Employees> doctors;
// Declares an array list to store employee of type
admin
ArrayList<Employees> administrators;
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Method to display menu, accept user choice and
returns it
int menu()
{
// Displays menu, accepts user
choice converts it to integer
System.out.print("\n\n
***************** MENU *****************");
System.out.print("\n\t 1 - Add an
administration staff " +
"\n\t 2 - Add a doctor \n\t 3 - Print all
working staff " +
"\n\t 4 - Exit the program. \n\t\t What is your
choice? ");
// Returns user choice
return sc.nextInt();
}// End of method
// Default constructor
Hospital()
{
// Creates array lists
doctors = new
ArrayList<Employees>();
administrators = new
ArrayList<Employees>();
}// End of default constructor
// Method to accept administration staff
information.
// Creates an AdministrationStaff class object
// Returns AdministrationStaff class object
AdministrationStaff getAdministrator()
{
// Local variable to store the data
entered by the user
int ID;
String name;
String address;
String mobileNumber;
String email;
double salary;
String position;
// Accepts data
System.out.print("\n Enter
Administrator Information \n");
System.out.print("\n Enter ID:
");
ID = sc.nextInt();
System.out.print("\n Enter Name:
");
name = sc.next();
System.out.print("\n Enter Address:
");
address = sc.next();
System.out.print("\n Enter Mobile
Number: ");
mobileNumber = sc.next();
System.out.print("\n Enter Email:
");
email = sc.next();
System.out.print("\n Enter Salary:
$");
salary = sc.nextDouble();
System.out.print("\n Enter
Position: ");
position = sc.next();
// Creates an AdministrationStaff
class object using parameterized constructor
AdministrationStaff admin = new
AdministrationStaff (ID, name, address,
mobileNumber, email, salary, position);
// Returns the object
return admin;
}// End of method
// Method to accept doctor information.
// Creates an Doctor class object
// Returns Doctor class object
Doctor getDoctor()
{
// Local variable to store the data
entered by the user
int ID;
String name;
String address;
String mobileNumber;
String email;
double salary;
String rank;
String specialty;
// Accepts data
System.out.print("\n Enter
Administrator Information \n");
System.out.print("\n Enter ID:
");
ID = sc.nextInt();
System.out.print("\n Enter Name:
");
name = sc.next();
System.out.print("\n Enter Address:
");
address = sc.next();
System.out.print("\n Enter Mobile
Number: ");
mobileNumber = sc.next();
System.out.print("\n Enter Email:
");
email = sc.next();
System.out.print("\n Enter Salary:
$");
salary = sc.nextDouble();
System.out.print("\n Enter Rank:
");
rank = sc.next();
System.out.print("\n Enter
Speciality: ");
specialty = sc.next();
// Creates an Doctor class object
using parameterized constructor
Doctor doctor = new Doctor(ID,
name, address, mobileNumber, email, salary,
rank, specialty);
// Returns object
return doctor;
}// End of method
// Method to display all doctors and
administrators
void showRecords()
{
// Checks if number of doctors size
is 0 then display error message
if(doctors.size() == 0)
System.out.println("\n No Dcotor record available to
display.");
// Otherwise record available
else
{
System.out.print("\n\n **************** Doctor Information
**************** ");
// Loops till
number of doctors
for(int c = 0; c
< doctors.size(); c++)
// Displays each doctor information
System.out.println(doctors.get(c));
}// End of else
// Checks if number of
administrator staff size is 0 then display error message
if(administrators.size() ==
0)
System.out.println("\n No Administrator record available to
display.");
// Otherwise record available
else
{
System.out.print("\n\n **************** Administrator Information
**************** ");
// Loops till
number of administrators
for(int c = 0; c
< administrators.size(); c++)
// Displays each administrator information
System.out.println(administrators.get(c));
}// End of else
}// End of method
// main method definition
public static void main(String[] ss)
{
// Calls the default
constructor
Hospital hospital = new
Hospital();
// Loops till user choice is not
4
do
{
// Calls the
method to accept user choice
// Calls
appropriate method based on user choice
switch(hospital.menu())
{
case 1:
// Calls the method to return
AdministrationStaff class object
// adds the returned object
to administrator array list
hospital.administrators.add(hospital.getAdministrator());
break;
case 2:
// Calls the method to return
Doctor class object
// adds the returned object
to doctors array list
hospital.doctors.add(hospital.getDoctor());
break;
case 3:
hospital.showRecords();
break;
case 4:
System.out.println("\n\t\t
Thanks. Visig Again.");
System.exit(0);
default:
System.out.println("\n\t\t
Invalid Choice!!");
}// End of
switch - case
}while(true);// End of do - while
loop
}// End of main method
}// End of driver class
Sample Output:
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 3
No Dcotor record available to display.
No Administrator record available to display.
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 1
Enter Administrator Information
Enter ID: 111
Enter Name: Pyari
Enter Address: BBSR
Enter Mobile Number: 1479631597
Enter Email: PP@YY
Enter Salary: $98000.00
Enter Position: Principal
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 3
No Dcotor record available to display.
**************** Administrator Information ****************
Name: Pyari
Address: BBSR
Mobile Number: 1479631597
Email: PP@YY
Salary: $98000.0
Position: Principal
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 2
Enter Administrator Information
Enter ID: 222
Enter Name: Manvi
Enter Address: Bangalore
Enter Mobile Number: 7893692581
Enter Email: MM@SS
Enter Salary: $90000.00
Enter Rank: PG
Enter Speciality: Gynic
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 3
**************** Doctor Information ****************
Name: Manvi
Address: Bangalore
Mobile Number: 7893692581
Email: MM@SS
Salary: $90000.0
Rank: PG
Speciality: Gynic
**************** Administrator Information ****************
Name: Pyari
Address: BBSR
Mobile Number: 1479631597
Email: PP@YY
Salary: $98000.0
Position: Principal
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 9
Invalid Choice!!
***************** MENU *****************
1 - Add an administration staff
2 - Add a doctor
3 - Print all working staff
4 - Exit the program.
What is your choice? 4
Thanks. Visig Again.