Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // inventorySystem.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- using namespace std;
- class InventoryItem {
- public:
- int id;
- float price;
- InventoryItem() {
- id = 0;
- price = 0;
- }
- InventoryItem(int id_, float price_) {
- id = id_;
- price = price_;
- }
- float getPrice() {
- return price;
- }
- int getId() {
- return id;
- }
- virtual void displayItem(){
- cout << "Id: " << id;
- cout << "Price: " << price;
- }
- };
- class FoodItem : public InventoryItem {
- public:
- FoodItem(int id, float price):InventoryItem(id,price){}
- void displayItem() {
- InventoryItem::displayItem();
- cout << "Item type: Food item";
- }
- };
- class ClothingItem : public InventoryItem {
- public:
- ClothingItem(int id, float price) :InventoryItem(id, price) {}
- void displayItem(){
- InventoryItem::displayItem();
- cout << "Item type: Clothing item";
- }
- };
- void invalidInput(std::string invalidItem);
- int getId();
- float getPrice();
- FoodItem* getFoodItem();
- ClothingItem* getClothingItem();
- void printItems(InventoryItem** items, int itemCount);
- int main()
- {
- int ITEM_COUNT;
- cout << "How many items? ";
- cin >> ITEM_COUNT;
- InventoryItem** items = new InventoryItem*[ITEM_COUNT];
- //instructions for id and price entry
- //id and price entry
- for (int i = 0; i < ITEM_COUNT; i++) {
- InventoryItem* item;
- bool itemRecived;
- cout << "Clothing item[c] or Food item[f]: ";
- string input;
- cin >> input;
- if (input == "c") {
- item = getClothingItem();
- items[i] = item;
- }else if (input == "f") {
- item = getFoodItem();
- items[i] = item;
- }
- else {
- cout << "Invalid input, please try again.";
- }
- }
- printItems(items, ITEM_COUNT);
- for (int i = 0; i < ITEM_COUNT; i++) {
- delete items[i];
- }
- delete[] items;
- }
- float getPrice() {
- float price;
- cout << "Enter a price: ";
- cin >> price;
- while (price <= 0) {
- invalidInput("price");
- cout << "\nEnter a price: ";
- cin >> price;
- }
- return price;
- }
- int getId() {
- int id;
- cout << "Enter a ID: ";
- cin >> id;
- while (id <= 0) {
- invalidInput("id");
- cout << "\nEnter a id: ";
- cin >> id;
- }
- return id;
- }
- FoodItem* getFoodItem() {
- int id = getId();
- float price = getPrice();
- return new FoodItem(id, price);
- }
- ClothingItem* getClothingItem() {
- int id = getId();
- float price = getPrice();
- return new ClothingItem(id, price);
- }
- void printItems(InventoryItem** items, int itemCount) {
- for (int i = 0; i < itemCount; i++) {
- cout << (i + 1) << ") id: " << items[i]->getId() << " price: " << items[i]->getPrice() << "\n";
- }
- }
- void invalidInput(std::string invalidItem) {
- cout << "Error: The " << invalidItem << " that you have entered is invalid, please try again.\n";
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 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
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement