Building a category mapping
You are managing inventory for your electronics store, and you need to standardize the electronics categories in your inventory. Create a category mapping to help keep track of the products in your store.
The necessary packages such as EnumMap, and HashMap have been imported for you.
이 연습은 강의의 일부입니다
Cleaning Data in Java
연습 안내
- Create an
enumcalledProductCategory. - Look up an unknown category called
"Mystery category". - Create an unmodifiable map using
categoryMap.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class ElectronicsCategoryExample {
// Create an enum called ProductCategory
public ____ ____ {
ELECTRONICS,
HOME_APPLIANCES,
UNCATEGORIZED;
}
public static void main(String[] args) {
Map categoryMap = new HashMap<>();
categoryMap.put("Home Appliances", ProductCategory.HOME_APPLIANCES);
categoryMap.put("home appliances", ProductCategory.HOME_APPLIANCES);
categoryMap.put("HomeAppliances", ProductCategory.HOME_APPLIANCES);
categoryMap.put("Electronics", ProductCategory.ELECTRONICS);
categoryMap.put("electronics", ProductCategory.ELECTRONICS);
categoryMap.put("Electronic", ProductCategory.ELECTRONICS);
System.out.println("Electronics category:");
System.out.println(categoryMap.get("Electronics"));
// Look up an unknown category
ProductCategory unknownCategory = ____.____(
"Mystery category", ProductCategory.UNCATEGORIZED);
System.out.println("\nMystery category:");
System.out.println(unknownCategory);
// Create an unmodifiable map
Map immutableMap = ____.____(categoryMap);
System.out.println("\nAttempting to modify immutable map:");
try {
immutableMap.put("new", ProductCategory.ELECTRONICS);
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable map");
}
}
}