In: Computer Science
(1) Create three files to submit:
Build the ItemToPurchase class with the following specifications:
(2) In main(), prompt the user for two items and create two
objects of the ItemToPurchase class. Before prompting for the
second item, call cin.ignore() to allow the user
to input a new string. (2 pts)
Ex:
Item 1 Enter the item name: Chocolate Chips Enter the item price: 3 Enter the item quantity: 1 Item 2 Enter the item name: Bottled Water Enter the item price: 1 Enter the item quantity: 10
(3) Add the costs of the two items together and output the total
cost. (2 pts)
Ex:
TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13
_____________________________________________________________________________________________
given started code:
main.cpp:
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
int main() {
/* Type your code here */
return 0;
}
__________________________________________________________________________________________
ItemToPurchase.cpp
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
/* Type your code here */
___________________________________________________________________________________________
ItemToPurchase.h
#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H
#include <string>
using namespace std;
/* Type your code here */
#endif
//C++ Code
/*============ItemToPurchase.h=======================*/
#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H
#include <string>
using namespace std;
class ItemToPurchase {
private:
string itemName;
int itemPrice;
int itemQuantity;
public:
//default constructor
ItemToPurchase();
//mutators
void SetName(const string name);
void SetPrice(const int price);
void SetQuantity(const int quantity);
//accessors
string GetName()const;
int GetPrice() const;
int GetQuantity() const;
};
#endif
/*====================ItemToPurchase.cpp============*/
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
//default constructor
ItemToPurchase::ItemToPurchase()
{
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//mutators
void ItemToPurchase::SetName(const string name)
{
itemName = name;
}
void ItemToPurchase::SetPrice(const int price)
{
itemPrice = price;
}
void ItemToPurchase::SetQuantity(const int quantity)
{
itemQuantity = quantity;
}
//accessors
string ItemToPurchase::GetName()const
{
return itemName;
}
int ItemToPurchase::GetPrice() const
{
return itemPrice;
}
int ItemToPurchase::GetQuantity() const
{
return itemQuantity;
}
/*============main.cpp===================*/
#include <iostream>
#include<string>
using namespace std;
#include "ItemToPurchase.h"
int main() {
string name;
int price, quantity;
//Create two items
ItemToPurchase item1, item2;
cout << "Item 1\n";
cout << "Enter the item name: ";
getline(cin, name);
cout << "Enter the item price: ";
cin >> price;
cout << "Enter the item quantity: ";
cin >> quantity;
item1.SetName(name);
item1.SetPrice(price);
item1.SetQuantity(quantity);
cout << "Item 2\n";
cout << "Enter the item name: ";
cin.ignore();
getline(cin, name);
cout << "Enter the item price: ";
cin >> price;
cout << "Enter the item quantity: ";
cin >> quantity;
item2.SetName(name);
item2.SetPrice(price);
item2.SetQuantity(quantity);
int total = item1.GetQuantity() * item1.GetPrice()
+ item2.GetQuantity() * item2.GetPrice();
cout << "TOTAL COST: " << endl;
cout << item1.GetName() << " " <<
item1.GetQuantity() << " @ $" << item1.GetPrice()
<< " = " << item1.GetQuantity() * item1.GetPrice()
<< endl;
cout << item2.GetName() << " " <<
item2.GetQuantity() << " @ $" << item2.GetPrice()
<< " = " << item2.GetQuantity() * item2.GetPrice()
<< endl;
cout << "Total: $" << total <<
endl;
return 0;
}
//Output
//If you need any help regarding this solution.......... please leave a comment..... thanks