Advertisement
cperryoh

Untitled

Apr 17th, 2021 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1.  
  2. // inventorySystem.cpp : This file contains the 'main' function. Program execution begins and ends there.
  3. //
  4. #include <iostream>
  5. using namespace std;
  6. class InventoryItem {
  7. public:
  8.     int id;
  9.     float price;
  10.     InventoryItem() {
  11.         id = 0;
  12.         price = 0;
  13.     }
  14.     InventoryItem(int id_, float price_) {
  15.         id = id_;
  16.         price = price_;
  17.     }
  18.     float getPrice() {
  19.         return price;
  20.     }
  21.     int getId() {
  22.         return id;
  23.     }
  24.     virtual void displayItem(){
  25.         cout << "Id: " << id;
  26.         cout << "Price: " << price;
  27.     }
  28. };
  29. class FoodItem : public InventoryItem {
  30.     public:
  31.         FoodItem(int id, float price):InventoryItem(id,price){}
  32.         void displayItem() {
  33.             InventoryItem::displayItem();
  34.             cout << "Item type: Food item";
  35.         }
  36. };
  37. class ClothingItem : public InventoryItem {
  38.     public:
  39.         ClothingItem(int id, float price) :InventoryItem(id, price) {}
  40.         void displayItem(){
  41.             InventoryItem::displayItem();
  42.             cout << "Item type: Clothing item";
  43.         }
  44. };
  45.  
  46. void invalidInput(std::string invalidItem);
  47. int getId();
  48. float getPrice();
  49. FoodItem* getFoodItem();
  50. ClothingItem* getClothingItem();
  51.  
  52. void printItems(InventoryItem** items, int itemCount);
  53. int main()
  54. {
  55.     int ITEM_COUNT;
  56.     cout << "How many items? ";
  57.     cin >> ITEM_COUNT;
  58.     InventoryItem** items = new InventoryItem*[ITEM_COUNT];
  59.     //instructions for id and price entry
  60.     //id and price entry
  61.     for (int i = 0; i < ITEM_COUNT; i++) {
  62.         InventoryItem* item;
  63.         bool itemRecived;
  64.         cout << "Clothing item[c] or Food item[f]: ";
  65.         string input;
  66.         cin >> input;
  67.         if (input == "c") {
  68.             item = getClothingItem();
  69.             items[i] = item;
  70.         }else if (input == "f") {
  71.             item = getFoodItem();
  72.             items[i] = item;
  73.         }
  74.         else {
  75.             cout << "Invalid input, please try again.";
  76.         }
  77.     }
  78.     printItems(items, ITEM_COUNT);
  79.     for (int i = 0; i < ITEM_COUNT; i++) {
  80.         delete items[i];
  81.     }
  82.     delete[] items;
  83. }
  84.  
  85. float getPrice() {
  86.     float price;
  87.     cout << "Enter a price: ";
  88.     cin >> price;
  89.     while (price <= 0) {
  90.         invalidInput("price");
  91.         cout << "\nEnter a price: ";
  92.         cin >> price;
  93.     }
  94.     return price;
  95. }
  96. int getId() {
  97.     int id;
  98.     cout << "Enter a ID: ";
  99.     cin >> id;
  100.     while (id <= 0) {
  101.         invalidInput("id");
  102.         cout << "\nEnter a id: ";
  103.         cin >> id;
  104.     }
  105.     return id;
  106. }
  107. FoodItem* getFoodItem() {
  108.     int id = getId();
  109.     float price = getPrice();
  110.     return new FoodItem(id, price);
  111. }
  112. ClothingItem* getClothingItem() {
  113.     int id = getId();
  114.     float price = getPrice();
  115.     return new ClothingItem(id, price);
  116. }
  117. void printItems(InventoryItem** items, int itemCount) {
  118.     for (int i = 0; i < itemCount; i++) {
  119.         cout << (i + 1) << ") id: " << items[i]->getId() << " price: " << items[i]->getPrice() << "\n";
  120.     }
  121. }
  122. void invalidInput(std::string invalidItem) {
  123.     cout << "Error: The " << invalidItem << " that you have entered is invalid, please try again.\n";
  124. }
  125. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  126. // Debug program: F5 or Debug > Start Debugging menu
  127. // Tips for Getting Started:
  128. // 1. Use the Solution Explorer window to add/manage files
  129. // 2. Use the Team Explorer window to connect to source control
  130. // 3. Use the Output window to see build output and other messages
  131. // 4. Use the Error List window to view errors
  132. // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  133. // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement