(Btw, I know the terms do not match in the main cpp)
Hello So I am heaving problems with this practice problem. Here are the details:
The objective is to create a receipt that could be stored for marketing purposes. However, we will simplify this problem a little bit to make it easier and doable for the level of the course.
To avoid using stringstreams, we will use the class called RItem (receipt item) that I am giving to you. This class has some methods that will help you with the construction of the receipt.
Constructor RItem(): Sets all the items to “unknown”.
Constructor RItem(string description, string quantity, string price):
Creates a receipt item with the given information.
void RItem::setName(string itemName): sets the name of the item
void RItem::setQuantity(string quantity): sets the quantity of items sold.
void RItem::setPrice(string Price): sets the price of the item.
double RItem::getPrice() const: returns the price as a double.
int RItem::getQuantity() const: returns the quantity of items sold as an integer.
string RItem::getName() const: returns the name of the item.
void RItem::info() const: displays the information of the items sold on the screen.
You need to create a vector that will contain all the RItems of the receipt. To make it more programmer- friendly, it would be better to embed the vector into a class that we will call Receipt. This class will contain the list of items that we have sold. Moreover, this class should have at least the following methods:
void Receipt::display() const: this method displays the final receipt.
void Receipt::addItem(parameters): this method should add an Item to the vector.
You may create other methods. However, the methods you create cannot ask for user input.
Please, do not create unnecessary methods (see penalties).
You should collect the items in the following order: Item Name, Quantity, and Price. After introducing an item, you will ask the user for more items. If the user’s answer is Yes, then you will ask for the information of a new item. If the user introduces No, then you will stop collecting items and you will display the receipt. Our computer will introduce the Name of the item, the quantity, the price and either “Yes” or “No” in this order. Make sure you follow this simple rule.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
//Here is the main CPP
#include "Receipt.h"
#include <iostream>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main(){
//Defining strings and prices.
string itm;
double itmPrice;
int itmQty;
double itmSumTotal;
double total = 0;
string areThereMore = "Yes";
// double totalItm;
while (areThereMore == "Yes"){
//First asking for the name of the items
cout << "Item: ";
getline(cin, itm);
//Asking user for number of items
cout<< "Quantity: ";
cin>>itmQty;
//Now asking the user the cost of the item
cout<< "Price of " << itm << ": ";
cin >> itmPrice;
//Establishing the total price of Item
double itmSumTotal= itmQty * itmPrice;
total += itmSumTotal;
cout << "Sum total: "<<itmSumTotal<<endl;
//Preparing Body of Receipt
cout<< "Are there any more items that you want to add?";
cin>> areThereMore;
cin.ignore();
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
//Here is the Receipt.h file
#ifndef Receipt_h
#define Receipt_h
#include "RItem.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Receipt{
private:
string itemName;
string itemQuantity;
string itemPrice;
public:
// Default constructor
Receipt();
// Overloaded constructor
Receipt(string name, string quantity, string price);
// methods
void setName(string name);
void setQuantity(string Quantity);
void setPrice(string price);
double getPrice() const;
string getName() const;
int getQuantity() const;
void info() const;
};
Receipt::Receipt(){
vector<RItem> rec;
}
void Receipt::addItem(RItem newItem){
rec.push_back(newItem);
}
void Receipt::display() const {
for (int num = 0; i<rec.size(); ++num){
cout << "Item Name is:" << rec[i].itemName << endl;
cout << "Item Qty is:" << rec[i].itemQuantity << endl;
cout << "Item Name is:" << rec[i].itemPrice << endl;
}
}
#endif /* Receipt_h */
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
//And here is the RItem.h file
#ifndef RITEM_H
#define RITEM_H
#include <string>
using namespace std;
class RItem{
private:
string itemName;
string itemQuantity;
string itemPrice;
public:
// Default constructor
RItem();
// Overloaded constructor
RItem(string name, string quantity, string price);
// methods
void setName(string name);
void setQuantity(string Quantity);
void setPrice(string price);
double getPrice() const;
string getName() const;
int getQuantity() const;
void info() const;
};
#endif
|