【问题标题】:how i will get data from owner我将如何从所有者那里获取数据
【发布时间】:2015-07-16 12:51:42
【问题描述】:

我有 4 个班级 TownshipSocietyFlatOwner。如果我输入所有者名称,我将如何从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;
    }
}

【问题讨论】:

  • 请发布相关代码(您的课程)。
  • 您也应该发布您的TownshipFlat 课程。
  • 也发布OwnerOwner 包含您想要获取的哪些数据? Owners 是如何填充/初始化的?对于初学者,您可能希望实现一种方法来检索 Owner 的实例以“获取所有数据”(注意:根据您的用例,您可能需要辅助工具来选择 Owner 如果重名是可能的)。
  • 谢谢你们,抱歉代码不完整

标签: java arraylist collections hashmap


【解决方案1】:

我刚试过这个,它会起作用,实际上它的编码很糟糕,但我想把它作为虚拟代码来做,我只是创建了一个方法并通过每个循环使用 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);
*/                  }
                }

            }

        }
    }

【讨论】:

    猜你喜欢
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2019-10-31
    相关资源
    最近更新 更多