Question

C++ program, inventory.cpp implementation

Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data clThe following diagram illustrates the data structure maintained by your Inventory class: Inventory Node Item head id item 300For all nodes in the list except the last node, the next field of the node at position pos will point to the next node. ThenePoints to the first node in the list Will be nullptr if the list is empty. head: Node Private Public Inventory( Inventory(O vPublic int stockIn(const int, const int) Find an item with the given id (first parameter), then increase its quantity by the

Mostly need the int load(istream&) function.

Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done in this class as well as find / add /
The following diagram illustrates the data structure maintained by your Inventory class: Inventory Node Item head id item 30001 DietCoke next name price 1.99 35 quantity item id 30002 CokeZero next name price 1.98 32 quantity item id 40001 Milk1Gal next name price 2.49 _quantity 30 nullptr The top left corner of the above figure shows the only private data member of the Inventory class: _head, a pointer to the first node in the linked list. The value is nullptr if the list is empty. Just to the right of these two data members is a column (in blue) of list nodes linked. Each node of the (singly) linked list has a next field, and an item field storing a pointer to an Item object. The Item object that the item field of each node points to are displayed (in green) to the right. The types of the four fields are int, string, double, and int, respectively
For all nodes in the list except the last node, the next field of the node at position pos will point to the next node. Thenext field of the last node will point to nullptr Note that the order of the items is last-come-first-out. That is, the most recently added node is at the head. We do not manage a tail pointer this time Files you will submit: stockmgmt.cpp item.h contains your main function contains the Item class declaration contains the Item class member function definitions contains the Node/Inventory class declaration contains the Node/Inventory class member function definitions item.cpp inventory.h inventory.cpp
Points to the first node in the list Will be nullptr if the list is empty. head: Node Private Public Inventory( Inventory(O void push_front ( Item* ) Initializeshead to nullptr. Free up (i.e., delete) all nodes in the list. Allocate a new Node with the parameter item, then insert it at the front of the list. Update_head to the new Node pointer. Returns the first item of the list (i.e., head's item). If there is no Node, return nullptr. Public Public Inventory Item* front () const Public bool pop_front() Public Deletes the first node. Return true if the delete was successful, false otherwise (i.e., no node)
Public int stockIn(const int, const int) Find an item with the given id (first parameter), then increase its quantity by the given value (second parameter). Return the updated quantity. If there is no item with the given id, return -1 Find an item with the given id (first parameter), then decrease its quantity by the given value (second parameter) If the given value is greater than the current quantity in stock, update the quantity as zero. Return how many items are withdrawn. (For example, if there was 20 in stock and 30 was asked, the updated quantity is 0 and 20 is returned.) Add a new item to the inventory, if there is no item with the same ID. Return true if the addition was successful. False Public int stockOut (const int, const int) addNewItem ( Item*) bool Public otherwise, that is, there is already an item with the same ID. Find an item with the given ID, then delete it. IMPORTANT] How to delete an item (1) Find the node with the item with the given ID (say A) (2) Switch A's item and head's item (3) Delete head (using pop head) Returns true if there are no nodes in the list, false bool delete Item Public const int) bool isEmpty () const otherwise. (Hint: check if head is nullptr) Reads records from an istream, and insert them to the inventory. Each line has the four fields for one item record Per record, an Item object is allocated and added to the inventory. Returns the number of records read. Note: There is no corrupt data. No validation needed Note 2: still, you need to check if there is a duplicate id. Note 3: Use addNewItem function int load(istream&)
0 0
Add a comment Improve this question Transcribed image text
Answer #1


int load(istream & s){
   std::string line = "";
   std::string word = "";
   int count = 0;
   while(s){
       s.getline(line);
       stringstream ss(line);
       Item *i = new Item();
       ss>>word;
       int id = std::stoi(word);
       i->setId(id);
       word = "";
       ss>>word;
       i->setName(word);
       word = "";
       ss>>word;
       double price = std::stod(word);
       i->setPrice(price);
       word = "";
       ss>>word;
       int q = std::stoi(word);
       i->setQuantity(q);
       line = "";
       if(!addNewItem(i)){   //if duplicate id present add its quantity.
           stockIn(i->getID(),i->getQuantity());
       }
       count++;
   }
   return count;
}

Add a comment
Know the answer?
Add Answer to:
C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

  • Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...

    Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario There are some classic data structures in computer science. One example of this that you've seen in the lectures is called the stack. In the next few exercises, we'll build yet another classic data structure, the linked list. To be exact, we'll be building the singly linked list. The building block of a singly linked list is a node, which consists of two parts:...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

  • 13. Given the following structure struct node { struct node *next; int id; }; Write a...

    13. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list as the last node of the linked list struct node *insertLast(stuct node **head, int newId) { } 14. Given the following structure struct node { struct node *next; int id; }; Write a code to insert a new node into the linked list after a node with the same id as the input parameter...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT