Detected code, here are some useful tools:
Formatted code
A grocery store wants to track products that are past their expiration date(no longer fresh).The store’s software engineer tackles the problem by writing the following code, but it has some problems that you’re going to fix : class NotFreshRecordHashMap {
HashMap<String, GroceryItem> notFreshMap;
public NotFreshRecordHashMap() {
notFreshMap = new HashMap<String, GroceryItem>();
}
void addGroceryItem(String name, GroceryItem item) {
notFreshMap.put(name, item);
}
GroceryItem getGroceryItem(String name) {
if (!notFreshMap.containsKey(name)) return null ;
return notFreshMap.get(name);
}
}
class ItemNotFoundException extends Exception {
String err;
public ItemNotFoundException(String err) {
this .err = err;
}
String getErr() {
return this .err;
}
}
a.Modify the above code so that it uses appropriate access modifiers on all fields and methods.b.Modify getGroceryItem so that instead of returningnull if the item is not in the HashMap, it implements exception handling using ItemNotFoundException.Youdo not need to modify any other methodsfor this step.c.Modify the above code to handle collisions(using a list structure).This may require modifying the HashMap itself and / or some method parameter andreturn types.Use getGroceryItem in other methodsif you need to get something from your hash map to modify.If getGroceryItemthrows an exception, respond by instantiating something.NOTE : The GroceryItemclass is not providedfor you, but you don't need it to solvethis problem.If you solve the problem using an IDE, you can stub out a GroceryItemclass to get your code to compile.