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.
Este exercício faz parte do curso
Cleaning Data in Java
Instruções do exercício
- Create an
enumcalledProductCategory. - Look up an unknown category called
"Mystery category". - Create an unmodifiable map using
categoryMap.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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");
}
}
}