创建一个名为Hospital.java的类
package com.rais.hospital;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Hospital
{
private Integer hospitalId;
private String hospitalName;
private String hospitalAddress;
private Long contatNumber;
/**
* @param hospitalId
* @param hospitalName
* @param hospitalAddress
* @param contatNumber
*/
public Hospital(Integer hospitalId, String hospitalName, String hospitalAddress, Long contatNumber)
{
super();
this.hospitalId = hospitalId;
this.hospitalName = hospitalName;
this.hospitalAddress = hospitalAddress;
this.contatNumber = contatNumber;
}
/**
* @param hospitalId
* @param contatNumber
*/
public Hospital(Integer hospitalId)
{
super();
this.hospitalId = hospitalId;
}
/**
* @return the hospitalId
*/
public Integer getHospitalId()
{
return hospitalId;
}
/**
* @param hospitalId the hospitalId to set
*/
public void setHospitalId(Integer hospitalId)
{
this.hospitalId = hospitalId;
}
/**
* @return the hospitalName
*/
public String getHospitalName()
{
return hospitalName;
}
/**
* @param hospitalName the hospitalName to set
*/
public void setHospitalName(String hospitalName)
{
this.hospitalName = hospitalName;
}
/**
* @return the hospitalAddress
*/
public String getHospitalAddress()
{
return hospitalAddress;
}
/**
* @param hospitalAddress the hospitalAddress to set
*/
public void setHospitalAddress(String hospitalAddress)
{
this.hospitalAddress = hospitalAddress;
}
/**
* @return the contatNumber
*/
public Long getContatNumber()
{
return contatNumber;
}
/**
* @param contatNumber the contatNumber to set
*/
public void setContatNumber(Long contatNumber)
{
this.contatNumber = contatNumber;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((hospitalId == null) ? 0 : hospitalId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Hospital other = (Hospital) obj;
if (!hospitalId.equals(other.hospitalId))
return false;
return true;
}
}
创建一个类Doctor.java
package com.rais.hospital;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Doctor
{
private Integer id;
private String name;
private String address;
private String department;
/**
* @param id
* @param name
* @param address
* @param department
*/
public Doctor(Integer id, String name, String address, String department)
{
super();
this.id = id;
this.name = name;
this.address = address;
this.department = department;
}
/**
* @return the id
*/
public Integer getId()
{
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id)
{
this.id = id;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @param name the name to set
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return the address
*/
public String getAddress()
{
return address;
}
/**
* @param address the address to set
*/
public void setAddress(String address)
{
this.address = address;
}
/**
* @return the department
*/
public String getDepartment()
{
return department;
}
/**
* @param department the department to set
*/
public void setDepartment(String department)
{
this.department = department;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return "Doctor [id=" + id + ", name=" + name + ", address=" + address + ", department=" + department + "]";
}
}
现在在下面描述的客户端Client.java的帮助下访问所有医生列表
package com.rais.hospital;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Rais.Alam
* @project MyFirstProject
* @date Dec 24, 2012
*/
public class Client
{
private static Map<Hospital, List<Doctor>> repo = new HashMap<Hospital, List<Doctor>>();
/**
* @param args
*/
public static void main(String[] args)
{
// Displaying records for Hospital for HospitalA of Boston
createRepository();
List<Doctor> lst1 = getDoctorsList(new Hospital(101));
for (Doctor doctor : lst1)
{
System.out.println(doctor);
}
System.out.println("==================================");
// Displaying records for Hospital for HospitalB of Atlanta
List<Doctor> lst2 = getDoctorsList(new Hospital(201));
for (Doctor doctor : lst2)
{
System.out.println(doctor);
}
}
public static List<Doctor> getDoctorsList(Hospital hospital)
{
return repo.get(hospital);
}
public static void createRepository()
{
Hospital hospital1 = new Hospital(101, "HospitalA", "Street no, 101, Boston", 123456789l);
Hospital hospital2 = new Hospital(201, "HospitalB", "Street no, 102, Atlanta", 987654321l);
List<Doctor> list1 = new ArrayList<Doctor>();
List<Doctor> list2 = new ArrayList<Doctor>();
list1.add(new Doctor(1011, "Doctor-P", "Boston", "ENT"));
list1.add(new Doctor(1012, "Doctor-Q", "Boston", "ENT"));
list1.add(new Doctor(1013, "Doctor-R", "Boston", "ENT"));
list1.add(new Doctor(1014, "Doctor-S", "Boston", "ENT"));
list2.add(new Doctor(2011, "Doctor-A", "Atlanta", "Therapist"));
list2.add(new Doctor(2012, "Doctor-B", "Atlanta", "Therapist"));
list2.add(new Doctor(2013, "Doctor-C", "Atlanta", "Therapist"));
list2.add(new Doctor(2014, "Doctor-D", "Atlanta", "Therapist"));
repo.put(hospital1, list1);
repo.put(hospital2, list2);
}
}
您将看到如下所述的输出
Doctor [id=1011, name=Doctor-P, address=Boston, department=ENT]
Doctor [id=1012, name=Doctor-Q, address=Boston, department=ENT]
Doctor [id=1013, name=Doctor-R, address=Boston, department=ENT]
Doctor [id=1014, name=Doctor-S, address=Boston, department=ENT]
==================================
Doctor [id=2011, name=Doctor-A, address=Atlanta, department=Therapist]
Doctor [id=2012, name=Doctor-B, address=Atlanta, department=Therapist]
Doctor [id=2013, name=Doctor-C, address=Atlanta, department=Therapist]
Doctor [id=2014, name=Doctor-D, address=Atlanta, department=Therapist]