From 0ff3dccb52b7cd1a71cd21f2a218cbf17ef18dab Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Wed, 31 Mar 2021 21:43:32 -0400 Subject: [PATCH] Add cpp project --- cpp/project/ItemToPurchase.cpp | 20 ++++++++ cpp/project/ItemToPurchase.h | 28 ++++++++++ cpp/project/main.cpp | 93 ++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 cpp/project/ItemToPurchase.cpp create mode 100644 cpp/project/ItemToPurchase.h create mode 100644 cpp/project/main.cpp diff --git a/cpp/project/ItemToPurchase.cpp b/cpp/project/ItemToPurchase.cpp new file mode 100644 index 0000000..6767f16 --- /dev/null +++ b/cpp/project/ItemToPurchase.cpp @@ -0,0 +1,20 @@ +#include +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 */ diff --git a/cpp/project/ItemToPurchase.h b/cpp/project/ItemToPurchase.h new file mode 100644 index 0000000..80d546d --- /dev/null +++ b/cpp/project/ItemToPurchase.h @@ -0,0 +1,28 @@ +#ifndef ITEM_TO_PURCHASE_H +#define ITEM_TO_PURCHASE_H + +#include +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 diff --git a/cpp/project/main.cpp b/cpp/project/main.cpp new file mode 100644 index 0000000..0fc3855 --- /dev/null +++ b/cpp/project/main.cpp @@ -0,0 +1,93 @@ +#include +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; +}