Answer to Question #328101 in C++ for reddy

Question #328101

You’ve been asked to write a small menu driven inventory management system for a small convenient store. Item ID – unsigned long Item name – string Item cost – float Quantity - int

Following are the list of administrative functionalities your application shall support:

1.Add new item to the inventory. This function will be used to add a single new item into the inventory management system.

2.Print all item information in the store - This function will be used to display all items in the inventory. When this option is selected system shall print Item ID, Item name, Item cost and quantity. 3.Find item by ID – This function will be used to search item using an ID. If item exist print item information. If not display an error indicating item not found.

4.Find item by name – This function will be used to search item using name. If item exist print item information. If not display an error indicating item not found.

Write a menu driven application in

C++.


1
Expert's answer
2022-04-13T09:18:49-0400
#include <iostream>
#include <string>
#include <vector>


class Item {
  private:
    unsigned long id;
    std::string name;
    float cost;
    int quantity;
  public:
    Item(unsigned long _id, std::string _name, float _cost, int _quantity)
      : id(_id), name(_name), cost(_cost), quantity(_quantity) {}
    unsigned long get_id() {
      return id;
    }
    std::string get_name() {
      return name;
    }
    float get_cost() {
      return cost;
    }
    int get_quantity() {
      return quantity;
    }
    void print() {
      std::cout << "ID:       " << id << '\n';
      std::cout << "Name:     " << name << '\n';
      std::cout << "Cost:     " << cost << '\n';
      std::cout << "Quantity: " << quantity << '\n';
    }
    ~Item() {}
};


class Store {
  private:
    std::vector<Item> items;
  public:
    Store() {}
    void add_item(unsigned long id, std::string name, float cost, int quantity) {
      items.push_back(Item(id, name, cost, quantity));
    }
    void print() {
      if (items.size() == 0) {
        std::cout << "There are no items in the store\n";
        return;
      }
      std::cout << "Items: \n--------------------\n";
      for (int i = 0; i < items.size(); ++i) {
        items[i].print();
        if (i < items.size() - 1) {
          std::cout << "--------------------\n";
        }
      }
    }
    int find_by_id(unsigned long id) {
      for (int i = 0; i < items.size(); ++i) {
        if (items[i].get_id() == id) {
          return i;
        }
      }
      return -1;
    }
    int find_by_name(std::string name) {
      for (int i = 0; i < items.size(); ++i) {
        if (items[i].get_name() == name) {
          return i;
        }
      }
      return -1;
    }
    Item get(int index) {
      return items[index];
    }
    ~Store() {}
};


int main() {
  Store store;
  std::cout << "Options:\n";
  std::cout << "print:              display all items\n";
  std::cout << "add:                create new item\n";
  std::cout << "get_by_id <id>:     get item with given if\n";
  std::cout << "get_by_name <name>: get item with given name\n";
  std::cout << "exit:               exit the program\n";
  while (1) {
    std::cout << "\nEnter your option: ";
    std::string option;
    std::cin >> option;
    if (option == "exit") {
      return 0;
    }
    if (option == "add") {
      unsigned long id;
      std::string name;
      float cost;
      int quantity;
      std::cout << "Enter item id: ";
      std::cin >> id;
      std::cin.ignore();
      std::cout << "Enter item name: ";
      std::cin >> name;
      std::cout << "Enter item cost: ";
      std::cin >> cost;
      std::cout << "Enter item quantity: ";
      std::cin >> quantity;
      store.add_item(id, name, cost, quantity);
    }
    else if (option == "print") {
      store.print();
    }
    else if (option == "get_by_id") {
      unsigned long id;
      std::cin >> id;
      std::cin.ignore();
      int index = store.find_by_id(id);
      if (index == -1) {
        std::cout << "There in so such item\n";
        continue;
      }
      Item item = store.get(index);
      std::cout << "Item:\n--------------------\n";
      item.print();
    }
    else if (option == "get_by_name") {
      std::string name;
      std::cin >> name;
      int index = store.find_by_name(name);
      if (index == -1) {
        std::cout << "There in so such item\n";
        continue;
      }
      Item item = store.get(index);
      std::cout << "Item:\n--------------------\n";
      item.print();
    }
  }
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS