Goals Develop a class from given requirements Implement that
Goals
  Develop a class from given requirements.
  Implement that class using object-oriented programming techniques.
  Overload an operator for a class.
 In this project, using C++ you need to design, implement, and test a grocery shopping list program. The program should maintain and display a list of items.
 You will design an Item class. The class should have data elements for the following information: item name, unit (i.e. can, box, pound, or ounce), quantity to buy, and unit price. Consider following things: do you need any functions other than the constructor? How do you calculate the extended price for the item (quantity to buy times unit price)? How do you print it to the screen?
 You will also need a List class. You will store Item objects in your List object. When you enter a new item, an Item object must be created and added to the List object. Use a dynamic array to hold the Item objects. The dynamic array should start with a capacity of 4 Item objects. Do you need a print function in this class? Think about it.
 Your program must perform the following activities: create a list, add items, remove items, and display the shopping list. To add an item, you should prompt the user to enter the name, unit, quantity to buy, and the unit price. The display should show: each item name in the list, item unit, the quantity to buy, the unit price, the extended price for each item, and the total price for all items. Oregon doesn’t have a sales tax so you can ignore that. Debug and test your program.
 Once you have the List and Item classes working correctly, test if an item is already in your List before adding it. Overload the == operator to perform the test. There is a simple example to overload this operator in the book. Keep it simple. How will you compare items? You can assume that the user will type the information correctly and compare them by the item names.
You should also test your program to make sure it works properly. Since this is a program with input and output, you should not need any driver functions. You do NOT need to test for every possible item in a grocery store, just a reasonable number. How do you handle spaces in names? Are the extended prices calculated correctly? Is the total amount correct?
 
 GRADING
  Programming style and documentation (10%)
  Create the list class and object (15%)
  Create the item class and objects (10%)
  Add and remove an item object to the list (15%)
  Resize the dynamic array correctly when the number of item objects exceeds the capacity (5%)
  Display the list to include the following: item name, unit, quantity to buy, unit price, extended price for that item. Then at the bottom of the display, indicate the total cost for that trip to the store (20%)
  Overload the equality operator (= =) to prevent including duplicate item objects in the shopping list (10%)
Solution
//main.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include \"List.h\"
#include \"Item.h\"
using namespace std;
int main()
{
cout << fixed << setprecision(2);
string name;
string line;
line.assign(50, \'-\');
int unit, total, choice;
char choice2;
double price;
List bag;
cout << \"Welcome to the shop\" << endl;
cout << line << endl;
do
{
cout << \"What options do you want to make\" << endl;
cout << \"\\t 1. Add Items\" << endl;
cout << \"\\t 2. Remove Items\" << endl;
cout << \"\\t 3. View Current Items\" << endl;
cout << \"\\t 4. Exit\" << endl;
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
{
cout << endl << \"What is the name of item you want to add: \";
getline(cin, name);
cout << \"What is the unit for this item: \";
cin >> unit;
cout << \"What is the total/quantity for this item: \";
cin >> total;
cout << \"What is cost of this item: \";
cin >> price;
Item product(name, unit, total, price);
if (bag.operator==(product))
{
cout << \"Sorry that item is already in the list\" << endl;
}
else
{
bag.addItem(product);
}
break;
}
case 2:
{
cout << endl << \"Which item do you want to remove from the list (Enter Item Name): \";
getline(cin, name);
Item product2;
product2.setName(name);
bag.removeItem(product2);
break;
}
case 3:
{
cout << endl << \"Here are the items you currently have in your list/bag\" << endl;
bag.printList();
break;
}
case 4:
{
cout << endl << \"Thank you for shopping, here is the report\" << endl;
bag.displayReport();
break;
}
default:
cout << endl << \"Invalid input\" << endl;
}
cout << endl;
} while (choice != 4);
return 0;
}
======================================================================
//List.cpp
#include \"List.h\"
#include <iostream>
using namespace std;
// Add Item Function
void List::addItem(Item itemTemp)
{
items.push_back(itemTemp);
}
// Remove Item Function
void List::removeItem(Item itemTemp)
{
int size = items.size();
int index = 0;
bool found=false;
for (int x = 0; x < size; x++)
{
if (itemTemp.getName() == items[x].getName())
{
index = x;
found = true;
}
}
if (size == 0)
{
cout << \"There are no items to remove from the list\" << endl;
}
else if (found)
{
items.erase(items.begin() + index);
cout << \"The item, \" << itemTemp.getName() << \" was removed from the list\" << endl;
}
else
{
cout << \"The item, \" << itemTemp.getName() << \" is not on the list\" << endl;
}
}
// Print List method, will only show the item names to the user
void List::printList()
{
int size = items.size();
for (int x = 0; x < size; x++)
{
cout << x + 1 << \". \" << items[x].getName() << endl;
}
}
// Display Report Function, this will display all the information to the user
void List::displayReport()
{
double total = 0;
int size = items.size();
for (int x = 0; x < size; x++)
{
cout << \"Name: \" << items[x].getName() << endl;
cout << \"Unit: \" << items[x].getUnit() << endl;
cout << \"Quantity: \" << items[x].getQuantity() << endl;
cout << \"Price : $\" << items[x].getPrice() << endl << endl;
total += items[x].totalPrice();
}
cout << \"The total price of all the items in the list is $\" << total << endl;
}
// == Operator Overload Function
bool List::operator==(Item b)
{
int size = items.size();
for (int x = 0; x < size; x++)
{
if (items[x].getName() == b.getName())
{
return true;
}
}
return false;
}
==================================================================
//List.h
#ifndef LIST_H
#define LIST_H
#include <vector>
#include \"Item.h\"
class List
{
private:
vector <Item> items;
public:
void addItem(Item);
void removeItem(Item);
void printList();
void displayReport();
bool operator==(Item);
};
#endif
====================================================================
//Item.cpp
#include \"Item.h\"
// Default Constructor
Item::Item()
{
name = \"\";
unit = quantity = price = 0;
}
// Constructor Overload
Item::Item(string n, int u, int total, double cost)
{
name = n;
unit = u;
quantity = total;
price = cost;
}
// Setter Functions
void Item::setName(string n)
{
name = n;
}
void Item::setUnit(int u)
{
unit = u;
}
void Item::setQuantity(int total)
{
if (total < 0)
{
quantity = 0;
}
else
{
quantity = total;
}
}
void Item::setPrice(double cost)
{
price = cost;
}
// Getter Functions
string Item::getName()
{
return name;
}
int Item::getUnit()
{
return unit;
}
int Item::getQuantity()
{
return quantity;
}
double Item::getPrice()
{
return price;
}
// Total cost function
double Item::totalPrice()
{
return quantity * price;
}
===================================================================
//Item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
using namespace std;
class Item
{
private:
string name;
int unit;
int quantity;
double price;
public:
Item();
Item(string, int, int, double);
void setName(string);
void setUnit(int);
void setQuantity(int);
void setPrice(double);
string getName();
int getUnit();
int getQuantity();
double getPrice();
double totalPrice();
};
#endif









