In C++. You need to design, implement, and test a grocery shopping list program.
ID: 3785391 • Letter: I
Question
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
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Item {
private:
string name;
enum unit_types {can, box, pounds, ounces} unit;
int quantity;
float unit_price;
float total_price;
public:
Item() {
cout<<"Enter item name: ";
cin>>name;
cout<<"Enter unit type: "<<endl<<"1. Can 2. Box 3. Pounds 4. Ounces: ";
int temp;
cin>>temp;
unit = (unit_types)temp;
cout<<"Enter quantity: ";
cin>>quantity;
cout<<"Enter unit price: ";
cin>>unit_price;
total_price = unit_price * quantity;
}
void print(void) {
cout<<"Number of items: "<<quantity<<endl;
cout<<"Unit of sale: "<<unit<<endl;
cout<<"Quantity: "<<quantity;
cout<<"Unit price: ";
switch(unit) {
case 0:
cout<<"Can";
break;
case 1:
cout<<"Box";
break;
case 2:
cout<<"Pounds";
break;
case 3:
cout<<"Ounces";
break;
}
cout<<endl;
cout<<"Total price: "<<total_price<<endl;
}
float getTotalPrice() {
return total_price;
}
string getName() {
return name;
}
};
class List {
private:
Item * list;
int size;
int capacity;
public:
List() {
list = (Item *)calloc(4, sizeof(Item));
size = 0;
capacity = 4;
}
void addItem() {
Item item;
if(size == capacity) {
capacity *= 2;
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++)
temp[i] = list[i];
list = temp;
}
list[size++] = item;
}
void removeItem(string name) {
Item * temp = (Item *)calloc(capacity, sizeof(Item));
for(int i=0; i<size; i++) {
if(name.compare(temp[i].getName()) != 0)
temp[i] = list[i];
}
list = temp;
size--;
}
void display() {
float total = 0;
cout<<"Size = "<<size<<endl;
for(int i=0; i<size; i++) {
cout<<"Item "<<(i+1)<<endl;
list[i].print();
cout<<endl;
total += list[i].getTotalPrice();
}
cout<<"Total price of all items: "<<total<<endl;
}
};
int main()
{
char ch = 'y';
while(ch == 'y') {
int choice;
cout<<"1. Add Item 2. Delete Item 3. Display list ";
cout<<"Enter choice: ";
cin>>choice;
List list;
string name;
switch (choice) {
case 1:
list.addItem();
break;
case 2:
cout<<"Enter item name to remove: ";
cin>>name;
list.removeItem(name);
break;
case 3:
cout<<"Displaying the list..."<<endl;
list.display();
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Do you wish to continue (y/n)? ";
cin>>ch;
}
return 0;
}