📄 hospitalsysuos.java
字号:
package hospital_sys;
import java.io.*;
import dslib.file.*;
import dslib.dictionary.PKeyedDictUos;
import dslib.base.*;
/** This class provides a simple hospital system that maintains information
about patients */
public class HospitalSysUos
{
final int MENU_EXIT = 9;
/** File storing each patient by his/her specified hospitalization number. */
PKeyedDictUos patNumToPatFile;
/* The following files are inverted lists that provide access to the hospitlization
numbers of patients by doctor's name, drug name, patient name, and bed number. */
/** File storing the hospitalization numbers of patients for each doctor's name.
Note that a doctor can have many patients */
PKeyedDictUos doctorToPatNumFile;
/** File storing the hospitalization numbers of patients using each drug.
Note that many people can take the same drug */
PKeyedDictUos drugToPatNumFile;
/** File storing with each patient's name, his/her hospitalization number.
Assume that no two people have the same name (first and last) */
PKeyedDictUos patNameToPatNumFile;
/** File storing the hospitalization number of the patient in each bed */
PKeyedDictUos bedToPatNumFile;
BufferedReader input;
/** Initialize and run the system. <br>
Analysis: Time = O(numbers of the queries * time for each query) */
public HospitalSysUos()
{
System.out.println("\nWelcome to the hospital system\nInitially the system is empty.\n");
patNumToPatFile = new BtreeFileUos(4,"./hospital_sys/patient_data_file", "./hospital_sys/patient_node_file", 1000, 2000);
doctorToPatNumFile = new BtreeFileUos(4, "./hospital_sys/doctor_data_file", "./hospital_sys/doctor_node_file", 1000, 2000);
drugToPatNumFile = new BtreeFileUos(4, "./hospital_sys/drug_data_file", "./hospital_sys/drug_node_file", 1000, 2000);
patNameToPatNumFile = new BtreeFileUos(4, "./hospital_sys/name_data_file", "./hospital_sys/name_node_file", 1000, 2000);
bedToPatNumFile = new BtreeFileUos(4, "./hospital_sys/bed_data_file", "./hospital_sys/bed_node_file", 1000, 2000);
input = new BufferedReader(new InputStreamReader(System.in));
run();
}
/** Run the system. <br>
Analysis: Time = O(numbers of the queries * time for each query) */
public void run()
{
int response =0;
while (response != MENU_EXIT)
{
response = menuQuery();
switch(response)
{
case 1:
addAPatient();
break;
case 2:
deleteAPatient();
break;
case 3:
outputPatientList();
break;
case 4:
searchByPatName();
break;
case 5:
searchByDocName();
break;
case 6:
searchByDrug();
break;
case 7:
searchByBed();
break;
case 8:
searchByHospNum();
break;
case 9:
exitSys();
break;
default:
System.out.println("Invalid choice input");
break;
}
}
}
/** Display the main menu and return user's choice. <br>
Analysis: Time = O(1) */
public int menuQuery()
{
boolean validChoice = false;
int result = -1;
while (!validChoice)
{
System.out.println("\nMAIN MENU");
System.out.println("---------");
System.out.println("1. Add a patient");
System.out.println("2. Delete a patient");
System.out.println("3. View all patient records");
System.out.println("4. Search for patient by patient's name");
System.out.println("5. Search for patient(s) by doctor's name");
System.out.println("6. Search for patient(s) by drug");
System.out.println("7. Search for patient by bed number");
System.out.println("8. Search for patient by hospitalization number");
System.out.println("9. Exit system");
System.out.print("\nChoice : ");
try
{
result = Integer.parseInt(input.readLine());
}catch (Exception e)
{
System.out.println("Invalid input in menuQuery()");
result = -1;
}
validChoice = (result >=1) && (result <= MENU_EXIT);
}
return result;
}
/** Create a patient from user data and insert it into the
index file and update the inverted lists. <br>
Analysis: Time = O(has()) + O(insert()) for each of the files:
patNumToPatFile, bedToPatNumFile, doctorToPatNumFile, and
drugToPatNumFile. */
public void addAPatient()
{
PatientUos newPatient = null;
try{
System.out.print("Patient's name : ");
String name = input.readLine();
System.out.print("Patient's doctor : ");
String doc = input.readLine();
System.out.print("Drug : ");
String drug = input.readLine();
System.out.print("Hospitalization number : ");
int hn = Integer.parseInt(input.readLine());
System.out.print("Bed number : ");
int bn = Integer.parseInt(input.readLine());
newPatient = new PatientUos(name, bn, doc, drug, hn);
}catch (Exception e)
{
System.out.println("Valid input in addPatient()");
return;
}
/** Create new record and add to the files */
int hosp = newPatient.hospNum();
int bed = newPatient.bedNum();
if (patNumToPatFile.has(new Integer(hosp)))
System.out.println("Can not insert the new patient as the hospitilization number is already in use");
else if (bedToPatNumFile.has(new Integer(bed)))
System.out.println("Can not insert the new patient as the bed number is already in use");
else if (patNameToPatNumFile.has(newPatient.name))
System.out.println("Can not insert the new patient as another patient has the name");
else
{
patNumToPatFile.insert(new Integer(hosp), newPatient);
patNameToPatNumFile.insert(newPatient.name(), new Integer(hosp));
bedToPatNumFile.insert(new Integer(bed), new Integer(hosp));
/* As the doctor-hosp number file is a PKeyedDictUos, each entry must have
a unique key. Use the (doctor, hosp number) pair as the key and a null item.
Store (doctor, -1) as the first pair for each doctor. */
String doc = newPatient.docName();
ComparablePairUos docPatPair = new ComparablePairUos(doc, new Integer(-1));
if (doctorToPatNumFile.has(docPatPair))
{
docPatPair = new ComparablePairUos(doc, new Integer(hosp));
doctorToPatNumFile.insert(docPatPair, null);
}
else
{
doctorToPatNumFile.insert(docPatPair, null);
docPatPair = new ComparablePairUos(doc, new Integer(hosp));
doctorToPatNumFile.insert(docPatPair, null);
}
/* As the drug-hosp number file is a PKeyedDictUos, each entry must have
a unique key. Use the (durg, hosp number) pair as the key and a null item.
Store (drug, -1) as the first pair for each drug. */
String drug = newPatient.drugName();
ComparablePairUos drugPatPair = new ComparablePairUos(drug, new Integer(-1));
if (drugToPatNumFile.has(drugPatPair))
{
drugPatPair = new ComparablePairUos(drug, new Integer(hosp));
drugToPatNumFile.insert(drugPatPair, null);
}
else
{
drugToPatNumFile.insert(drugPatPair, null);
drugPatPair = new ComparablePairUos(drug, new Integer(hosp));
drugToPatNumFile.insert(drugPatPair, null);
}
}
}
/** Delete a user specified patient by hospitalization number. <br>
Analysis: Time = O(search()) + O(deleteItem()) for each of the files:
patNumToPatFile, doctorToPatNumFile, drugToPatNumFile,
patNameToPatNumFile, and bedToPatNumFile. */
public void deleteAPatient()
{
int hospNum = 0;
boolean deleted;
try{
System.out.print("Enter hospitalization number : ");
hospNum = Integer.parseInt(input.readLine());
}catch (Exception e)
{
System.out.println("Invalid input for a number");
return;
}
/** Make sure that patient exists */
patNumToPatFile.search(new Integer(hospNum));
if (patNumToPatFile.itemExists())
{
PatientUos deletionPatient = (PatientUos)patNumToPatFile.item();
patNumToPatFile.deleteItem();
/** Delete the doctor patient pair from the file */
String deletionDoc = deletionPatient.docName();
ComparablePairUos deleteDocPat = new ComparablePairUos(deletionDoc, new Integer(hospNum));
doctorToPatNumFile.delete(deleteDocPat);
/** Delete the drug patient pair from the file */
String deletionDrug = deletionPatient.drugName();
ComparablePairUos deleteDrugPat = new ComparablePairUos(deletionDrug, new Integer(hospNum));
drugToPatNumFile.delete(deleteDrugPat);
/** Delete the patient record from the file */
patNameToPatNumFile.delete(deletionPatient.name());
/** Delete the bed record from the file */
bedToPatNumFile.delete(new Integer(deletionPatient.bedNum()));
}
else
System.out.println("Invalid hospitalization number");
}
/** Output a user specified patient by patient's name. <br>
Analysis: Time = O(search()) + O(obtain()) for patNameToPatNumFile */
public void searchByPatName()
{
String patName = null;
System.out.print("Enter the patient's name : ");
try{
patName = input.readLine();
}catch (Exception e)
{
System.out.println("Invalid input");
return;
}
patNameToPatNumFile.search(patName);
if (patNameToPatNumFile.itemExists())
{
Integer patNum = (Integer) patNameToPatNumFile.item();
PatientUos pat = (PatientUos) patNumToPatFile.obtain(patNum);
System.out.println("The desired patient is\n" + pat.toString());
}
else
System.out.println("No patient by the name " + patName + " in the system");
}
/** Output the list of patients by user specified doctor 's name. <br>
Analysis: Time = O(search()) + O(obtain()) for doctorToPatNumFile */
public void searchByDocName()
{
String docName = null;
System.out.print("Enter the doctor's name : ");
try{
docName = input.readLine();
}catch (Exception e)
{
System.out.println("Invalid input");
return;
}
/* Move to the first pair for this doctor, the pair with hospitalization number -1. */
ComparablePairUos firstDocPatNumPair = new ComparablePairUos(docName, new Integer(-1));
doctorToPatNumFile.search(firstDocPatNumPair);
if (!doctorToPatNumFile.itemExists())
System.out.println("There is no doctor by the name " + docName);
else
{
doctorToPatNumFile.goForth();
System.out.println("The patients for Doctor " + docName + " are");
ComparablePairUos nextDocPatNumPair = (ComparablePairUos) doctorToPatNumFile.itemKey();
while(docName.equals(nextDocPatNumPair.firstItem()))
{
Integer patNum = (Integer) (nextDocPatNumPair.secondItem());
PatientUos pat = (PatientUos) patNumToPatFile.obtain(patNum);
System.out.println(pat.toString());
doctorToPatNumFile.goForth();
if(doctorToPatNumFile.after())
break;
else
nextDocPatNumPair = (ComparablePairUos)doctorToPatNumFile.itemKey();
}
}
}
/** Ouput the list of patients by user specified drug's name. <br>
Analysis: Time = O(search()) + O(obtain()) for drugToPatNumFile */
public void searchByDrug()
{
String drugName = null;
System.out.print("Enter the drug name : ");
try{
drugName = input.readLine();
}catch (Exception e)
{
System.out.println("Invalid input");
return;
}
/* Move to the first pair for this drug, the pair with hospitalization number -1. */
ComparablePairUos firstDrugPatNumPair = new ComparablePairUos(drugName, new Integer(-1));
drugToPatNumFile.search(firstDrugPatNumPair);
if (!drugToPatNumFile.itemExists())
System.out.println("There is no drug by the name " + drugName);
else
{
drugToPatNumFile.goForth();
System.out.println("The patients for drug " + drugName + " are");
ComparablePairUos nextDrugPatNumPair = (ComparablePairUos) drugToPatNumFile.itemKey();
while(drugName.equals(nextDrugPatNumPair.firstItem()))
{
Integer patNum = (Integer) (nextDrugPatNumPair.secondItem());
PatientUos pat = (PatientUos) patNumToPatFile.obtain(patNum);
System.out.println(pat.toString());
drugToPatNumFile.goForth();
if(drugToPatNumFile.after())
break;
else
nextDrugPatNumPair = (ComparablePairUos)drugToPatNumFile.itemKey();
}
}
}
/** Output the patient by user specified bed number. <br>
Analysis: Time = O(search()) + O(obtain()) for bedToPatNumFile */
public void searchByBed()
{
int bedNum = -1;
System.out.print("Enter the bed number : ");
try{
bedNum = Integer.parseInt(input.readLine());
}catch (Exception e)
{
System.out.println("Invalid input for a number");
return;
}
bedToPatNumFile.search(new Integer(bedNum));
if (bedToPatNumFile.itemExists())
{
Integer patNum = (Integer) bedToPatNumFile.item();
PatientUos pat = (PatientUos) patNumToPatFile.obtain(patNum);
System.out.println("The desired patient is \n" + pat.toString());
}
else
System.out.println("There is no patient in the bed with number " + bedNum);
}
/** Output the patient by user specified hospitalization number. <br>
Analysis: Time = O(search()) + O(obtain()) for patNumToPatFile */
public void searchByHospNum()
{
int hospNum = -1;
System.out.print("Enter the hospitalization number : ");
try{
hospNum = Integer.parseInt(input.readLine());
}catch (Exception e)
{
System.out.println("Invalid input for a number");
return;
}
patNumToPatFile.search(new Integer(hospNum));
if (patNumToPatFile.itemExists())
{
PatientUos pat = (PatientUos) patNumToPatFile.item();
System.out.println("The desired patient is \n" + pat.toString());
}
else
System.out.println("There is no patient with hospitalization number " + hospNum);
}
/** Exit the system. <br>
Analysis: Time = O(1) */
public void exitSys()
{
((BtreeFileUos)patNumToPatFile).closeFile();
((BtreeFileUos)doctorToPatNumFile).closeFile();
((BtreeFileUos)drugToPatNumFile).closeFile();
((BtreeFileUos)patNameToPatNumFile).closeFile();
((BtreeFileUos)bedToPatNumFile).closeFile();
System.out.println("\nSystem terminated by user");
}
/** Display the entire list of patients. <br>
Analysis: Time = size of patNumToPatFile. */
public void outputPatientList()
{
System.out.println("CURRENT PATIENTS");
System.out.println("-----------------");
for (patNumToPatFile.goFirst(); !patNumToPatFile.after(); patNumToPatFile.goForth())
System.out.println(patNumToPatFile.item());
}
/** Main method of the class. <br>
Analysis: Time = O(numbers of the queries * time for each query) */
public static void main(String[] args)
{
new HospitalSysUos();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -