1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to...

6
KING SAUD UNIVERSITY COLLEGE OF COMPUTER AND INFORMATION SCIENCES Computer Sciences Department CSC 111: Introduction to Programming I Tutorial Week 14 - OOP 2 2 nd Semester1436- 1437 Write a program for a system to manage the patients in hospital. The program should allow the following; to add patient to an array, retrieve information of a patient , arrange patients based on priority . Create a class Patient that has the following private instance attributes: name , ID , priority( 1 , 2 , 3 ) and a static attribute which is numberOfPatients. Patient provides a constructor that initializes the name, ID, priority and increments numberOfPatient by one. Write the following public methods that perform each of the following tasks: printPatientInfo() : Method prints the information(Name ,ID and priority) of a patient. Write an application class called Hospital with a main method that displays the menu: 1. Add new patient. 2. Retrieve patient’s information. 3. Arrange Patients based on priority. 4. Exit. Your code should include the following Methods: addPatient : that adds information of new patient ( name, ID, priority) to the end of the array. The method should check if the patient is already registered or not. This method will return true if the add operation was completed successfully. searchPatient : search for a specific patient. The search key of the search is ID. Method should return false for not found patient. 1

Transcript of 1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to...

Page 1: 1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to manage the patients in hospital. The program should allow the following; to add patient to

KING SAUD UNIVERSITYCOLLEGE OF COMPUTER AND INFORMATION SCIENCES

Computer Sciences DepartmentCSC 111: Introduction to

Programming I Tutorial Week 14 - OOP 22nd Semester1436- 1437

Write a program for a system to manage the patients in hospital. The program should allow the following; to add patient to an array, retrieve information of a patient, arrange patients based on priority.

Create a class Patient that has the following private instance attributes: name, ID , priority( 1 , 2 , 3 ) and a static attribute which is numberOfPatients.

Patient provides a constructor that initializes the name, ID, priority and increments numberOfPatient by one.

Write the following public methods that perform each of the following tasks:printPatientInfo() : Method prints the information(Name ,ID and priority) of a patient.

Write an application class called Hospital with a main method that displays the menu: 1. Add new patient.2. Retrieve patient’s information.3. Arrange Patients based on priority.4. Exit.

Your code should include the following Methods: addPatient : that adds information of new patient ( name, ID, priority) to the end of the

array. The method should check if the patient is already registered or not. This method will return true if the add operation was completed successfully.

searchPatient : search for a specific patient. The search key of the search is ID. Method should return false for not found patient.

ArrangePatients: arrange patients in a new array according to their priority, patients with priority of 1 are emergency cases and should be added to the beginning of the array, patients with priority of 2 are moderate cases and should follow. Finally patients with a priority of 3 are routine cases and should be placed at the end of the array.Your method should then print sorted array.

All input data should be provided by user.

1

Page 2: 1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to manage the patients in hospital. The program should allow the following; to add patient to

KING SAUD UNIVERSITYCOLLEGE OF COMPUTER AND INFORMATION SCIENCES

Computer Sciences DepartmentCSC 111: Introduction to

Programming I Tutorial Week 14 - OOP 22nd Semester1436- 1437

Solution: Copy the following code ina file called(hospital.java) and run it.import java.util.*;class patient{ private String name, ID; private int priority; public static int numberOfPatient; public patient(String n, String id, int pri) { name=n; ID=id; priority=pri; numberOfPatient++; } public String getID() { return ID; } public int getPriority() { return priority; } public void printPatientInfo() { System.out.println(" Patient Name : "+name+"\n ID :"+ID+"\n priority"+priority+"\n-----------------------" ); }}/////////////////////////////////////////////////////end class patint

public class hospital{ static Scanner read=new Scanner(System.in); static int size; static patient [] patients; public static void main(String args[]) { int choice,priority; String name,id; System.out.println("Enter the size of the array"); size=read.nextInt(); patients= new patient[size]; do{ System.out.println("please select your option \n1-Add new patient \n2-retrive patient's information \n3-Arrange Patient based on

2

Page 3: 1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to manage the patients in hospital. The program should allow the following; to add patient to

KING SAUD UNIVERSITYCOLLEGE OF COMPUTER AND INFORMATION SCIENCES

Computer Sciences DepartmentCSC 111: Introduction to

Programming I Tutorial Week 14 - OOP 22nd Semester1436- 1437

priority\n4-Exait"); choice=read.nextInt(); switch (choice){ case 1: System.out.println("enter patient name"); name=read.next(); System.out.println("enter patient ID"); id=read.next(); System.out.println("enter patient priority"); priority=read.nextInt(); if(addPatient(name,id,priority)) System.out.println("patient added successfully :) "); else System.out.println("failed to add patient!"); break; case 2: System.out.println("Enter patient ID"); String id1=read.next(); if(searchPatient(id1)) for(int i=0;i<patient.numberOfPatient;i++) if( patients[i].getID().equals(id1)) patients[i].printPatientInfo(); break; case 3: arrangePatient(); break; }//end switch }while(choice!=4); }//end main ///////////////////////////////////////////////////public static boolean addPatient(String name, String ID, int priority){ if(!searchPatient(ID)&&patient.numberOfPatient<size) { patients[patient.numberOfPatient]=new patient(name,ID,priority); return true; } return false;}//end addpatientpublic static boolean searchPatient(String ID){ for(int i=0;i<patient.numberOfPatient;i++) if(patients[i].getID().equals(ID)) return true; return false;}//end searchPatient

3

Page 4: 1st semester 1433-1434H CSC111_lab Quiz#1 file · Web viewWrite a program for a system to manage the patients in hospital. The program should allow the following; to add patient to

KING SAUD UNIVERSITYCOLLEGE OF COMPUTER AND INFORMATION SCIENCES

Computer Sciences DepartmentCSC 111: Introduction to

Programming I Tutorial Week 14 - OOP 22nd Semester1436- 1437

public static void arrangePatient(){ patient [] temp = new patient[patient.numberOfPatient]; int counter=0; for (int p=1;p<=3;p++) for(int i=0;i<patient.numberOfPatient; i++) if(patients[i].getPriority()==p) { temp[counter]=patients[i]; counter++; }//print sorted patient System.out.println("patient after arrange "); for(int i=0;i<temp.length; i++) temp[i].printPatientInfo(); }//end arrange ptient}//end class hospital

4