Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

here is the necessary file Problem B: Grocery checkout (10 points) Start with th

ID: 3713825 • Letter: H

Question

here is the necessary file

Problem B: Grocery checkout (10 points) Start with the checkout.cpp file provided. You must make a class that works with the existing mainO function that simulates checking out at a grocery store. You may assume fewer than 200 items will be 1. The addO function should add a checkout item to the items being bought, either increasing the 2. You may directly compare strings when adding, thus if the string is different at all it can be 3. The checkout) function should return a list of all items and their quantities, along with a final 4. Note that the item bought is the sum of the quantities, not the amount of different types of items checked out. Ensure that you class makes the provided mainO output match the examples exactly. amount by one if the item is already present or adding it to the list if it is new treated as a new item (so apples are not Apples) additional line saying how many items were bought. on the list. The order that you display the items is not important (but the order the examples use is probably the easiest to do) Hint: A easy way to get numbers into string is to use the overloaded operator for strings along with the int2str0 function provided, for example string x"; x +- int2str (2); x += "X Apples". cout

Explanation / Answer

#include<iostream>

#include<string>

#include<cstring>

#include<cmath>

#include<map>

using namespace std;

string int2str(int x);

class GroceryList

{

public:

//To store item in map using item name as key and counter as value

map<string, int> itemMap;

//For map iterator

map<string,int>::const_iterator it;

//Pointer for return of items with quantatiy

string * items;

//Finding map size

int mapSize;

public:

//Add item into map

void add(string item)

{

if (itemMap.find(item) == itemMap.end() )

{

itemMap[item] = 1;

}

else

{

itemMap[item]++;

}

}

//Return items details with quantity

string* checkout(int & size)

{

mapSize = itemMap.size();

items = new string[mapSize + 1];

size = 0;

int total = 0;

for (it = itemMap.begin(); it!= itemMap.end(); it++)

{

string x = "";

x += int2str(it->second);

x += "x " + it->first;

items[size] = x;

total += it->second;

size++;

}

string x = "";

x += int2str(total);

items[size++] = x;  

return items;

}

};

int main()

{

GroceryList g1;

string item;

getline(cin, item);

while(item != "!checkout")

{

g1.add(item);

getline(cin, item);

}

int size;

string *receipt = g1.checkout(size);

cout<< " Receipt: ";

int maxLen = 0;

for(int i = 0; i < size-1; i++)

{

cout<< receipt[i] <<endl;

if(static_cast<int>(receipt[i].length()) > maxLen)

{

maxLen = receipt[i].length();

}

}

for(int i = 0; i < maxLen; i++)

{

cout<<"-";

}

cout<< endl;

cout<< "Total items: " << receipt[size-1] << endl;

delete [] receipt;

  

return 0;

}

string int2str(int x)

{

string reverse = "";

int pos = abs(x);

while(pos > 0)

{

reverse += ('0'+pos%10);

pos /= 10;

}

if(reverse.length() == 0)

{

return "0";

}

string result;

if(x < 0)

{

result += "-";

}

for(int i = 0; i < static_cast<int>(reverse.length()); i++)

{

result += reverse[reverse.length()-1-i];

}

return result;

}