我有4个课程Township
,Society
,Flat
和Owner
。如果我输入所有者名称,我将如何从Owner
类获得所有数据?
public class TestSociety {
public static void main(String[] args) {
HashMap<Integer, TownShip> township = initializeTownShip();
Scanner scanner = new Scanner(System.in);
System.out.println("enter name of owner");
String name = scanner.nextLine();
}
private static HashMap<Integer, TownShip> initializeTownShip() {
// putting township into the hashmap
HashMap<Integer, TownShip> township = new HashMap<Integer, TownShip>();
for (int tid = 1; tid < 5; tid++) {
township.put(tid, new TownShip("topaz", getListOfSociety(tid)));
}
return township;
}
private static List<Society> getListOfSociety(int tid) {
// putting societies into the list
List<Society> listOfSociety = new ArrayList<Society>();
for (int societyId = 1; societyId < 5; societyId++) {
listOfSociety.add(new Society(societyId,
"society name" + societyId, getListofFlat(societyId)));
}
return listOfSociety;
}
private static List<Flat> getListofFlat(int societyId) {
// putting flats into the list
List<Flat> listOfFlat = new ArrayList<Flat>();
for (int flatId = 1; flatId <= 10; flatId++) {
listOfFlat.add(new Flat(flatId, flatId + 1, new Owner("firstname"
+ flatId, "lastname" + flatId, flatId * 1234)));
}
return listOfFlat;
}
}
答案 0 :(得分:0)
我刚尝试了这个,它会工作,实际上它的编码很糟糕,但我想把它做为虚拟代码,我只是创建了一个方法并通过每个循环使用3来迭代所有列表
private static void printOwnerAddressOnContainsStr(List<TownShip> townships, String fname) {
for (TownShip town : townships) {
/* System.out.println(town); */
List<Society> societies = town.getListOfSociety();
for (Society society : societies) {
List<Flat> flats = society.getListOfFlat();
// System.out.println(flats);
for (Flat flat : flats) {
Owner owner = flat.getOwner();
// System.out.println(owner);
if (owner.getFirstName().contains(fname)){
System.out.println("Address is");
System.out.println("Owners name :"+owner.getFirstName()+" "+owner.getLastName());
System.out.println("flat no: "+flat.getfNo()+" "+society.getsName()+" "+town.gettName());
System.out.println("");
/*System.out.println(town.gettName()+" "+society.getsName()+" "+flat.getfNo()+" "+owner);
//System.out.println(fname);
//System.exit(1);
*/ }
}
}
}
}