Program must be in C++ and meet the following guidelines: The program should dis
ID: 3555583 • Letter: P
Question
Program must be in C++ and meet the following guidelines:
The program should display a list of items and the location in the store. The user must be able to enter the items and the location in the store. Use arrays or vectors for the data. Keep the location simple, it can be something like 1-8 or dry goods, frozen, household goods, etc. The display should show the items by zone or location in the store. Display the output by location. The output should list all items for each zone or location together. You do not need to alphabetize the items. You will also need a test plan. Since this is a program with input and output, you shouldn't need driver functions. The test plan should provide coverage, i.e. many different items and using all the zones that you identify. You do not need to test for every possible item in a grocery store, just a reasonable number or varying lengths. Do Not use inheritance, polymorphism, templates, or linked data structures.
Explanation / Answer
import java.util.ArrayList;
import java.util.Iterator;
public class GroceryStore
{
ArrayList myItems;
/**
* Creates an initially empty grocery store
*/
public GroceryStore()
{
myItems = new ArrayList();
}
/**
* Changes the price of the item associated with itemName
*/
public void setPrice(String itemName, double price)
{
GroceryItem item = getItem(itemName);
if (item != null) {
( (GItem) item).price = price;
}
}
/**
* returns the item associated with itemName
*/
public GroceryItem getItem(String itemName)
{
return getGItem(itemName);
}
/**
* returns a (possibly empty) ArrayList of all
* the items in the specified category
*/
public ArrayList getAllItems(String category)
{
ArrayList list = new ArrayList();
for(int k=0; k < myItems.size(); k++) {
GItem item = (GItem) myItems.get(k);
if (item.getCategory().equals(category)) {
list.add(item);
}
}
return list;
}
/**
* precondition: names.length == prices.length
* changes the price of the item associated with names[k]
* to the price specified by prices[k]
*/
public void changePrices(String[] names, double[] prices)
{
for(int k=0; k < names.length; k++) {
setPrice(names[k],prices[k]);
}
}
private GItem getGItem(String itemName)
{
Iterator it = myItems.iterator();
while (it.hasNext()) {
GItem gitem = (GItem) it.next();
if (gitem.getName().equals(itemName)) {
return gitem;
}
}
return null;
}
private class GItem implements GroceryItem
{
String name;
double price;
String category;
int size;
public String getName() { return name; }
public double getPrice() { return price; }
public int getSize() { return size; }
public String getCategory() { return category; }
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.