package com.example.cosmosclient.yolp; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; public class Yolp { private static Yolp theInstance = null; private HashMap<Integer, SubCategory> codeToSubcategory = new HashMap<>(); private HashMap<String, Category> nameToCategory = new HashMap<>(); private Yolp() { // Yolp.getInstance().getCategoryByName("ショッピング").getSubCategories(); // for (Category c: Yolp.getInstance().getCategories()) { // } // for (String n: Yolp.getInstance().getCategoryNames()) { // } Category cat203 = new Category("家電・携帯電話"); cat203.addSubCategory(new SubCategory(203001, "電化製品")); cat203.addSubCategory(new SubCategory(203002,"家電量販店")); cat203.addSubCategory(new SubCategory(203003,"携帯電話")); cat203.addSubCategory(new SubCategory(203004, "パソコン")); putNameToCategory(cat203.getName(),cat203); Category cat205 = new Category("コンビニ・スーパー"); cat205.addSubCategory(new SubCategory(205001, "コンビニ")); cat205.addSubCategory(new SubCategory(205002, "スーパー")); putNameToCategory(cat205.getName(), cat205); } public static Yolp getInstance() { if (theInstance == null) { theInstance = new Yolp(); } return theInstance; } // 業種コードから業種名3 public SubCategory getSubCategoryByCode(int code) { return codeToSubcategory.get(code); } public HashMap<Integer, SubCategory> getCodeToSubcategory() { return codeToSubcategory; } public void setCodeToSubcategory(HashMap<Integer, SubCategory> codeToSubcategory) { this.codeToSubcategory = codeToSubcategory; } public void putCodeToSubCategory(int code, SubCategory subCategory) { codeToSubcategory.put(code, subCategory); } // 業種名2から業種名3 public String[] getCategoryNames() { return nameToCategory.keySet().toArray(new String[0]); } public Collection<Category> getCategories() { return nameToCategory.values(); } public Category getCategoryByName(String name) { return nameToCategory.get(name); } public void setNameToCategory(HashMap<String, Category> nameToCategory) { this.nameToCategory = nameToCategory; } public void putNameToCategory(String name, Category category) { nameToCategory.put(name, category); for (SubCategory s: category.getSubCategories()) { codeToSubcategory.put(s.getCode(), s); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Yolp yolp = (Yolp) o; return codeToSubcategory.equals(yolp.codeToSubcategory) || nameToCategory.equals(yolp.nameToCategory); } }