⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hospitalsysuos.java.old

📁 国外的数据结构与算法分析用书
💻 OLD
字号:
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 with his/her specified hospitalization number */
	PKeyedDictUos patNumToPatFile;
	
	/**	File storing the hospitalization number of patient for each doctor's name */
	PKeyedDictUos doctorToPatNumFile;
	
        /**	File storing the hospitalization number of patient for each drug */
	PKeyedDictUos drugToPatNumFile;
	
        /**	File storing each patient's name with his/her hospitalization number */
    	PKeyedDictUos patNameToPatNumFile;
	
        /**	File storing the hospitalization number of patient for each bed */
   	PKeyedDictUos bedToPatNumFile;
   	
   	BufferedReader input;
   	
   	/**	Initialize and run the system
   		Analysis: Time = O(numbers of the queries * time for each query) */
   	public HospitalSysUos()
   	{
   		String dataFileName ="";
   		System.out.println("\nWelcome to the hospital system\n");
   		patNumToPatFile = new BtreeFileUos(4,"./hospital_sys/patient_data_file", "./hospital_sys/patient_node_file", 1000, 2000);
   		//patNumToPatFile = new TwoThreePKeyedDictUos();
   		
   		/**	A doctor can have many patients */
   		doctorToPatNumFile = new BtreeFileUos(4, "./hospital_sys/doctor_data_file", "./hospital_sys/doctor_node_file", 1000, 2000);
   		//doctorToPatNumFile = new TwoThreePKeyedDictUos();
   		
   		/**	Many people can take the same drug */
   		drugToPatNumFile = new BtreeFileUos(4, "./hospital_sys/drug_data_file", "./hospital_sys/drug_node_file", 1000, 2000);
   		//drugToPatNumFile = new TwoThreePKeyedDictUos();
   		
   		/**	Assume no two people have the same name (first and last) */
   		patNameToPatNumFile = new BtreeFileUos(4, "./hospital_sys/name_data_file", "./hospital_sys/name_node_file", 1000, 2000);
   		//patNameToPatNumFile = new TwoThreePKeyedDictUos();
   		
   		/**	Only one person per bed */
   		bedToPatNumFile = new BtreeFileUos(4, "./hospital_sys/bed_data_file", "./hospital_sys/bed_node_file", 1000, 2000);
   		//bedToPatNumFile = new TwoThreePKeyedDictUos();
   		
   		input = new BufferedReader(new InputStreamReader(System.in));
   		run();
   	}
   	
   	/**	Run the system
   		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: 
   					outputFile();
   					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
		Analysis: Time = O(1) */
	public int menuQuery()
	{
		boolean validChoice = false;
		int result = 0;
		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 
		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()");
			newPatient = null;
		}
		
		/**	Create new record and add to the files */
		if (newPatient == null)
			;
		else
		{
			int hosp = newPatient.hospNum();
			int bed = newPatient.bedNum();
			if (patNumToPatFile.has(new Integer(hosp)))
				System.out.println("Can not insert the new patient due to the existed hospitilization number"); 
			if (bedToPatNumFile.has(new Integer(bed)))
				System.out.println("Can not insert the new patient due to the existed bed number");
			else
			{
				try{
				patNumToPatFile.insert(new Integer(hosp), newPatient);
				patNameToPatNumFile.insert(newPatient.name(), new Integer(hosp));
				bedToPatNumFile.insert(new Integer(bed), new Integer(hosp));
				}catch(Exception e){System.out.println("try1******************");}
				
				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
				{	try{
					doctorToPatNumFile.insert(docPatPair, null);
					docPatPair = new ComparablePairUos(doc, new Integer(hosp));
					doctorToPatNumFile.insert(docPatPair, null);
					}catch(Exception e){System.out.println("try2******************");}
				}
		
				
				String drug = newPatient.drugName();
				ComparablePairUos drugPatPair = new ComparablePairUos(drug, new Integer(-1));
				if (drugToPatNumFile.has(drugPatPair))
				{	try{
					drugPatPair = new ComparablePairUos(drug, new Integer(hosp));
					drugToPatNumFile.insert(drugPatPair, null);
					System.out.println(((BtreeFileUos)drugToPatNumFile).formatToString());
					}catch(Exception e){System.out.println("try3****************");}
				}
				else
				{	
					try{
					System.out.println(((BtreeFileUos)drugToPatNumFile).formatToString());
					drugToPatNumFile.insert(drugPatPair, null);
					System.out.println(((BtreeFileUos)drugToPatNumFile).formatToString());
					drugPatPair = new ComparablePairUos(drug, new Integer(hosp));
					System.out.println(drugPatPair);
					System.out.println(((BtreeFileUos)drugToPatNumFile).formatToString());
					drugToPatNumFile.insert(drugPatPair, null);
					System.out.println(((BtreeFileUos)drugToPatNumFile).formatToString());
					}catch(Exception e) {System.out.println("try5**************");}
				}
			}
		}
	}
	
	/**	Delete a user specified patient by hospitalization number
		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 in deleteAPatient()");
			hospNum = -1;
		}
		if (hospNum == -1)
			;
		else
		{
			patNumToPatFile.search(new Integer(hospNum));
			/**	Make sure that patient exists */
			if (patNumToPatFile.itemExists())
			{
				PatientUos deletionPatient = (PatientUos)patNumToPatFile.obtain(new Integer(hospNum));
				String deletionDoc = deletionPatient.docName();
				String deletionDrug = deletionPatient.drugName();
				patNumToPatFile.deleteItem();
			
				/**	Delete the doctor patient pair from the file */
				ComparablePairUos deleteDocPat = new ComparablePairUos(deletionDoc, new Integer(hospNum));
				doctorToPatNumFile.delete(deleteDocPat);
				
				/** Delete the drug patient pair from the file */
				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()));
			}
		}
	}
	
	/**	Search a user specified patient by patient's name
		Analysis: Time = O(search()) + O(obtain()) for patNameToPatNumFile */
	public void searchByPatName()
	{
		String tempString = null;
		System.out.print("Enter the patient's name : ");
		try{
			tempString = input.readLine();
		}catch (Exception e)
		{
			System.out.println("Invalid input");
		}
		if (tempString == null)
			;
		else
		{	
			patNameToPatNumFile.search(tempString);
			if (patNameToPatNumFile.itemExists())
			{
				Integer patNum = (Integer)patNameToPatNumFile.obtain(tempString);
				PatientUos pat = (PatientUos)patNumToPatFile.obtain(patNum);
				System.out.println(pat.toString());
			}
		}
	}
	
	/**	Search a list of user specified patients by doctor 's name
		Analysis: Time = O(search()) + O(obtain()) for doctorToPatNumFile */
	public void searchByDocName()
	{
		String tempString = null;
		System.out.print("Enter the doctor's name : ");
		try{
			tempString = input.readLine();
		}catch (Exception e)
		{
			System.out.println("Invalid input");
		}
		if (tempString == null)
			;
		else
		{	
			ComparablePairUos startDocPatNum = new ComparablePairUos(tempString, new Integer(-1));
			doctorToPatNumFile.search(startDocPatNum);
			/**	doctor has no patient. i.e. only has docName and -1 for this doctor */
			doctorToPatNumFile.goForth();
			ComparablePairUos nextDocPatNum = (ComparablePairUos)doctorToPatNumFile.itemKey();
			String docName = (String)(nextDocPatNum.firstItem());
			while(docName.equals(tempString))
			{
				Integer patNum = (Integer)(nextDocPatNum.secondItem());
				PatientUos pat = (PatientUos)patNumToPatFile.obtain(patNum);
				System.out.println(pat.toString());
				doctorToPatNumFile.goForth();
				if(doctorToPatNumFile.after())
					break;
				else
				{
					nextDocPatNum = (ComparablePairUos)doctorToPatNumFile.itemKey();
					docName = (String)(nextDocPatNum.firstItem());
				
				}
			}
		}
	}
	
	/**	Search a list of user specified patients by drug's name
		Analysis: Time = O(search()) + O(obtain()) for drugToPatNumFile */
	public void searchBydrug()
	{
		String tempString = null;
		System.out.print("Enter the drug name : ");
		try{
			tempString = input.readLine();
		}catch (Exception e)
		{
			System.out.println("Invalid input");
		}
		if(tempString == null)
			;
		else
		{
			ComparablePairUos startDrugPatNum = new ComparablePairUos(tempString, new Integer(-1));
			drugToPatNumFile.search(startDrugPatNum);
			/**	drug has no patient. i.e. only has drug and -1 for this drug */
			drugToPatNumFile.goForth();
			ComparablePairUos nextDrugPatNum = (ComparablePairUos)drugToPatNumFile.itemKey();
			String drug = (String)(nextDrugPatNum.firstItem());
			while(drug.equals(tempString))
			{
				Integer patNum = (Integer)(nextDrugPatNum.secondItem());
				PatientUos pat = (PatientUos)patNumToPatFile.obtain(patNum);
				System.out.println(pat.toString());
				drugToPatNumFile.goForth();
				if(drugToPatNumFile.after())
					break;
				else
				{
					nextDrugPatNum = (ComparablePairUos)drugToPatNumFile.itemKey();
					drug = (String)(nextDrugPatNum.firstItem());
				}
			}
		}
	}
	
	/**	Search a user specified patient by bed number
		Analysis: Time = O(search()) + O(obtain()) for bedToPatNumFile */
	public void searchBybed()
	{
		int tempInt = -1;
		System.out.print("Enter the bed number : ");
		try{
			tempInt = Integer.parseInt(input.readLine());
		}catch (Exception e)
		{
			System.out.println("Invalid input");
		}
		if (tempInt == -1)
			;
		else
		{
			bedToPatNumFile.search(new Integer(tempInt));
			if (bedToPatNumFile.itemExists())
			{
				Integer patNum = (Integer)bedToPatNumFile.obtain(new Integer(tempInt));
				PatientUos pat = (PatientUos)patNumToPatFile.obtain(patNum);
				System.out.println(pat.toString());
			}
		}
	}
	
	/**	Search a user specified patient by hospitalization number
		Analysis: Time = O(search()) + O(obtain()) for patNumToPatFile */
	public void searchByHospNum()
	{
		int tempInt = -1;
		System.out.print("Enter the hospitalization number : ");
		try{
			tempInt = Integer.parseInt(input.readLine());
		}catch (Exception e)
		{
			System.out.println("Invalid input");
		}
		if (tempInt == -1)
			;
		else
		{
			patNumToPatFile.search(new Integer(tempInt));
			if (patNumToPatFile.itemExists())
			{
				PatientUos pat = (PatientUos)patNumToPatFile.obtain(new Integer(tempInt));
				System.out.println(pat.toString());
		 	}
		 }
	}
	
	/**	Exit the system
		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 index file
		Analysis: Time = size of patNumToPatFile. */
	public void outputFile()
	{
		System.out.println("CURRENT PATIENTS");
		System.out.println("-----------------");
		System.out.println(((BtreeFileUos)patNumToPatFile).sequentialToString());
		//System.out.println(patNumToPatFile.toString());
	}
			
	/**	Main method of the class 
		Analysis: Time = O(numbers of the queries * time for each query) */
	public static void main(String[] args)
	{
		HospitalSysUos hsu = new HospitalSysUos();
	}	
}
				
				
				
				
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -