Add cpp project

This commit is contained in:
Logen Kain 2021-03-31 21:43:32 -04:00
parent a651d2c8f7
commit 0ff3dccb52
3 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase(){
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
void ItemToPurchase::SetName(std::string n){itemName = n;}
std::string ItemToPurchase::GetName() {return itemName;}
void ItemToPurchase::SetPrice(int p){itemPrice = p;}
int ItemToPurchase::GetPrice() {return itemPrice;}
void ItemToPurchase::SetQuantity(int q) { itemQuantity = q; }
int ItemToPurchase::GetQuantity(){ return itemQuantity;}
/* Type your code here */

View File

@ -0,0 +1,28 @@
#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H
#include <string>
using namespace std;
/* Type your code here */
class ItemToPurchase {
private:
std::string itemName;
int itemPrice;
int itemQuantity;
public:
ItemToPurchase();
void SetName(std::string name);
std::string GetName();
void SetPrice(int price);
int GetPrice();
void SetQuantity(int q);
int GetQuantity();
};
#endif

93
cpp/project/main.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
int main() {
/* Type your code here */
/*
prompt user for two items
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.
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
*/
std::string userInput;
ItemToPurchase item1;
ItemToPurchase item2;
int totalCost;
/* Get items from user */
cout << "Item 1" << endl << "Enter the item name:" << endl;
getline(cin, userInput);
item1.SetName(userInput);
cout << "Enter the item price:" << endl;
cin >> userInput;
item1.SetPrice(stoi(userInput));
cout << "Enter the item quantity:" << endl;
cin >> userInput;
item1.SetQuantity(stoi(userInput));
/* Item 2 */
cin.ignore(); // ???
cout << endl << "Item 2" << endl << "Enter the item name:" << endl;
getline(cin, userInput);
item2.SetName(userInput);
cout << "Enter the item price:" << endl;
cin >> userInput;
item2.SetPrice(stoi(userInput));
cout << "Enter the item quantity:" << endl;
cin >> userInput;
item2.SetQuantity(stoi(userInput));
/* Total cost */
cout << endl << "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;
totalCost = (item1.GetPrice() * item1.GetQuantity()) + (item2.GetPrice() * item2.GetQuantity());
cout << endl << "Total: $" << totalCost << endl;
return 0;
}