Type your question here Write a program that displays items and produces a purch
ID: 3539001 • Letter: T
Question
Type your question here
Write a program that displays items and produces a purchase bill when customer buys some. The
program will have some predefined items stored in an array. It will ask the customer to add items in the
cart and finally output a bill statement.
The Item class
Create an Item class that will hold item%u2019s name and price. As we did in previous labs, create two files:
Item.h and Item.cpp. Here%u2019s the class diagram:
Item
- m_name: string
- m_price: double
+ Item()
+ Item(name: string, price: double)
+ setName(name: string) : void
+ getName(): string
+ setPrice(price: double): void
+ getPrice(): double
Input
1. Create a
main.cpp file and a main() method.
2. Create an array of
Item objects. The size will be six. Put following items in the array:
Item name Price
Laptop 1250.00
Printer 499.99
Desktop 399.99
Monitor 125.50
Keyboard 45.00
Mouse 12.99
3. Print the item names and prices in the console.
Sample console output:
# Item name Price
1. Laptop 1250.00
2. Printer 499.99
[Print all the items in this manner]
7. Finish
4. Prompt for user input until he chooses finish (i.e. press 7). [Note: you may use a while loop.]
Output
Print the total purchase amount. Samp
Explanation / Answer
#ifndef ITEM_H
#define ITEM_H
#include<iostream>
using namespace std;
class Item
{
private:
string m_name;
double m_price;
public:
Item();
Item(string name,double price);
void setName(string name);
string getName();
void setPrice(double price);
double getPrice();
};
#endif
// ITEM.cpp file
#include "Item.h"
Item::Item()
{
m_name="";
m_price=0.0;
}
Item::Item(string name,double price):m_name(name),m_price(price)
{
}
void Item::setName(string name)
{
m_name = name;
}
string Item::getName()
{
return m_name;
}
void Item::setPrice(double price)
{
m_price = price;
}
double Item::getPrice()
{
return m_price;
}
#include<iostream>
#include "Item.h"
using namespace std;
int main()
{
Item item_object[6];
double total_price = 0.0;
item_object[0] = Item("Laptop",1250.00);
item_object[1] = Item("Printer",499.99);
item_object[2] = Item("Desktop",399.99);
item_object[3] = Item("Monitor",125.50);
item_object[4] = Item("Keyboard",45.00);
item_object[5] = Item("Mouse",12.99);
cout << " # Item name Price " << endl;
for(int i=0; i<6; i++)
{
cout << i << ". " << item_object[i].getName() << " " << item_object[i].getPrice() << endl;
}
int k=0,i=0;
int input;
while(k!=7)
{
cout << " how many " << item_object[i].getName() << "s you want? " << endl;
cin >> input;
total_price = total_price + input*item_object[i].getPrice();
i++;
if(i==6) break;
cout << " Enter 7. to finish program " << endl;
cin >> k;
}
cout << "total purchase amount is " << total_price << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.