📄 hospital.java
字号:
package hospital;
import java.util.*;
public class Hospital {
private Map patients = new HashMap();
private Map doctors = new HashMap();
public void addPatient(String first, String last, String ssn) {
Patient p = new Patient(first, last, ssn);
patients.put(ssn, p);
}
public void addDoctor(String first, String last, int id) {
Doctor d = new Doctor(first, last, id);
doctors.put(new Integer(id), d);
}
public Patient getPatient(String ssn) throws NoSuchPatient {
Patient p = (Patient) patients.get(ssn);
if (p == null)
throw new NoSuchPatient();
return p;
}
public Doctor getDoctor(int id) throws NoSuchDoctor {
Doctor d = (Doctor) doctors.get(id);
if (d == null)
throw new NoSuchDoctor();
return d;
}
public void assignPatientToDoctor(String ssn, int id) throws NoSuchPatient, NoSuchDoctor {
Doctor d = getDoctor(id);
Patient p = getPatient(ssn);
d.addPatient(p);
p.setDoctor(d);
}
public void printPatientsOfDoctor(int id) throws NoSuchDoctor {
Doctor d = getDoctor(id);
Collection c = d.getPatients();
for (Iterator i = c.iterator();i.hasNext();) {
System.out.println(i.next());
}
}
public void printDoctorOfPatient(String ssn) throws NoSuchPatient {
Patient p = getPatient(ssn);
Doctor d = p.getDoctor();
System.out.println(d);
}
public static void main(String[] args) {
Hospital hospital = new Hospital();
hospital.addPatient("Alice", "Green", "ALCGRN");
hospital.addPatient("Robert", "Smith", "RBTSMT");
hospital.addPatient("Steve", "Moore", "STVMRE");
hospital.addPatient("Susan", "Madison", "SSNMDS");
hospital.addDoctor("George", "Sun", 14);
hospital.addDoctor("Kate", "Love", 86);
try {
hospital.assignPatientToDoctor("SSNMDS", 86);
hospital.assignPatientToDoctor("ALCGRN", 14);
hospital.assignPatientToDoctor("RBTSMT", 14);
hospital.assignPatientToDoctor("STVMRE", 14);
hospital.printDoctorOfPatient("SSNMDS");
hospital.printPatientsOfDoctor(14);
hospital.printDoctorOfPatient("AAAAAA");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
hospital.printPatientsOfDoctor(-1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -