In C You need to design implement and test a grocery shoppin
In C++. You need to design, implement, and test a grocery shopping list program. The program should maintain and display a list of items.
Item Class:
You will use a class for the Items. The class should have data elements for the following information: item name, unit (i.e. can, box, pounds, or ounces), number to buy, and unit price. Do you need any functions other than the constructor(s)? How do you calculate the extended price for the item (number to by times unit price? How do you print it to the screen?
List Class :
You will also need a List class. The List class will use a dynamic array to store Item objects. The dynamic array wshould start with a cpacity of 4 Item objects. As each item is entered an Item object must created and added to the array. What do you do if the array is full? One List object will have many Item objects. How do you print to the screen?
Operations:
Your program must perform the following activities: create a list, add items, and remove items. To add an item you should prompt the user to enter the name, unit of sale, the number needed, and the unit price.
Displaying the list:
The display should show: for each item in the list, the number of items, the unit of sale, the unit price, the extended price for each item; then the total price for all items
Solution
GroceryList.hpp
class Linked_Grocery{
private:
struct Grocery{
std::string name;
std::size_t quantity;
bool purchase;
Grocery*next;
};
Grocery* head;
std::size_t differentGroceries;
std::size_t totalGroceries;
public:
LinkedGroceryList();
void addItem(std::string const&,std::size_t);
void removeItem(std:: string const&,unsigned int);
bool findItem(std:: string const&) const;
void markAsPurchase(std::string const&);
void displayItems() const;
void clearItems();
std::size_t different() const{return differentItems;}
std::size_t total() const{return totalItems:}
bool empty() const { return differentItems==0;}
};
GroceryList.cpp
#include \"GroceryList.hpp\"
#include<iostream>
Linked_Grocery::Linked_Grocery()
;head(nullptr)
,differentItems(0)
,totalItems(0)
{}
Linked_Grocery::~Linked_Grocery()
{
clearItems();
}
void Linked_Grocery::addItem(std::string const& name, std::size_t quantity)
{
Grocery* newGrocery=new Grocery;
newGrocery->name=name;
newGrocery->quantity=quantity;
newGrocery->purchased=false;
newGrocery->next=nullptr;
if(!head)
{
head=newGrocery;
}
else
{
Grocery* groceryPtr=head;
while(groceryPtr->next)
groceryPtr=groceryPtr->next;
groceryPtr->next=newGrocery;
}
differentItems++;
totalGroceries+=quantity;
}
void Linked_Grocery::removeItem(std::string const& name, unsigned int quanity)
{
if(!head) return;
Grocery* groceryPtr;
if(head-> name == name)
{
groceryPtr=head;
head=groceryPtr->next;
totalItems-=quantity;
if(quantity== groceryPtr->quantity)
differentItems--;
else
{
groceryPtr->quantity-=quantity;
return;
}
delete groceryPtr;
}
else
{
Grocery* predPtr=nullptr;
groceryPtr=head;
while(groceryPtr && groceryPtr->name != name)
{
predPtr = groceryPtr;
groceryPtr=groceryPtr->next;
}
if(groceryPtr)
{
totalItems -= quantity;
if(quantity== groceryPtr->quantity)
differentItems--;
else
{
groceryPtr->quantity -= quantity;
return;
}
predPtr->next = groceryPtr->next;
delete groceryPtr;
}
}
}
bool Linked_Grocery::findItem(std:: string const& name) const
{
if(!head) return false;
if(head->name == name)
return true;
else
{
Grocery* groceryPtr== head->next;
while(groceryPtr)
{
if(groceryPtr->name == name)
return true;
groceryPtr = groceryPtr->next;
}
}
return false;
}
void Linked_Grocery::markAsPurchased(std::string const& name)
{
Grocery* groceryPtr = head;
while(groceryPtr)
{
if(groceryPtr->name == name)
groceryPtr-> purchased = true;
groceryPtr= groceryPtr->next;
}
}
void Linked_Grocery::displayItems() const
{
if(!head) return;
Grocery* groceryPtr = head;
while(groceryPtr)
{
std::cout<<< groceryPtr->name<<\",\";
std::cout<<groceryPtr->quantity<<\",\";
std::cout<<((groceryPtr->purchased) ? \"purchased\ \" : \"not purchased\ \");
groceryPtr= groceryPtr->next;
}
}
void Linked_Grocery::clearItems()
{
if(!head) return;
Grocery* predPtr = head;
Grocery* nextGrocery;
while(predPtr)
{
nextGrocery = predPtr->next;
delete predPtr;
predptr= nextGrocery;
}
head= nullptr;
differentItems=0;
totalItems=0;
}
Driver
#include \"GroceryList.hpp\"
#include<iostream>
int main()
{
Linked_Grocery Items;
Items.addItem(\"Sugar\",5);
Items.addItem(\"Tomato\",3);
Items.addItem(\"Flour\",2);
Items.displayItems();
std::cout<< \"\ Different Items:\"<<Items.different()<<\"\ \";
std::cout<<\"Total Items: \" <<Items.total();
std::cout<<\"\ sugar present?\"<<std::boolalpha<<Items.findItem(\"Sugar\");
std::cout<<\"\ Wheat present?\"<< std :: boolalpha << Items.findItem(\"Wheat\");
std::cout<<\"\ \ removing 1 tomato \ \ \";
Items.removeItem(\"tomato\",1);
Items.displayItems();
std:: cout<< \"\ Different Items:\" << Items.different() <<\"\ \";
std:: cout<< \"total Items: \" << Items.total();
std:: cout<< \"\ tomato present? \" << std:: boolalpha << Items.findItem(\"tomato\");
Items.markAsPurchased(\"Flour\");
std::cout<<\"\ \ flour has been purchased\ \ \";
Items.displayItems();
}





