C++ You need to design, implement, and test a grocery shopping list program. The
ID: 3691142 • Letter: C
Question
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 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 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. As
each item is entered 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?
NOTE: A vector is not a dynamic array.
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 of sale, the number needed, and the unit price. The display should show:
each item in the list, the number of items, the unit of sale, 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. J 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 in correctly.
Explanation / Answer
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class item
{
public:
char itemName[20];
char unit[10];
int noToBuy;
float unitPrice;
item()
{
strcpy("",itemName);
strcpy("",unit);
noToBuy=0;
unitPrice=0.0;
}
};
class list
{
item it[4];
public:
void add(int);
void display();
};
void list::display()
{
for(int i=0;i<4;i++){
cout<<"Item name:"<<it[i].itemName<<endl;
cout<<"Item name:"<<it[i].unit<<endl;
cout<<"Item name:"<<it[i].noToBuy<<endl;
cout<<"Item name:"<<it[i].unitPrice<<endl;
cout<<"*************************";
}
}
//void read();
void list::add(int count)
{
cout<<"Enter item name: ";
cin>>it[count].itemName;
cout<<"Enter unit: ";
cin>>it[count].unit;
cout<<"Enter Number to Buy: ";
cin>>it[count].noToBuy;
cout<<"Enter unit price: ";
cin>>it[count].unitPrice;
}
int main()
{
char ch;
list ob;
int count=0;
do{
cout<<"****************Shopping List*****************";
cout<<"Press 'ctrl+E' to finish or exit ";
ob.add(count);
ob.display();
ch=getch();
count++;
}while(ch!=5);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.