Question

In: Computer Science

PLEASE PROVIDE COMMENTS TO BE ABLE TO UNDERSTAND CODE C++ Inventory Class Create an inventory class....

PLEASE PROVIDE COMMENTS TO BE ABLE TO UNDERSTAND CODE

C++

Inventory Class

Create an inventory class. The class will have the following data members,

const int size = 100;
int no_of_products; // the total number of products in the inventory. char name_array[size][15] ; // stores name of products
double array [size][2]; // In the first column quantity of the product and in the second column the price per item of the product is stored.

  • name_array stores the name of products, it is assumed that the length of the product name is at most 14 characters and each name is null terminated ( See Table 1. for an example )

  • For each product, the two-dimensional array will store two pieces of information, the current stock in number of pieces and the per unit price. We note that the information for a given product will be stored in the same row in the one-dimensional and two-dimensional arrays ( See Table 2. for an example).

    The class should provide the following functions.

  • Constructor function: The function receives no parameters but initializes the elements of the double array to zero and the first element of each row of the char array to a null character.

  • int row_no( char *product name): Function receives the name of the product and returns the number of the row that contains the information for this product in the two-dimensional arrays. This function may be used by other functions when needed.

  • int get_stock ( char *product_name): Function receives the name of the product and returns the current amount of the product in the stock.

  • double order( char *product_name, int quantity): The function will check if the requested quantity of the stock is available. If the requested quantity is available drop from the stock and return the total cost of the order, otherwise order cannot be met and return zero.

  • bool new_product( char *product_name, int quantity, double price): If there is an empty entry available in the arrays, then, a new product is added to the inventory. The new product should be added to the first available entries in the two-dimensional arrays. After that the function increases the number of different products in the inventory by one and returns true. If no space is available in the arrays, then, product can not be added to the inventory and the function returns false.

  • void discontinued_product( char *product_name); // If a product is discontinued, the function should set the entry of this item in the two- dimensional array zero and the first element of the row for this product in the two-dimensional name_array to a null character. This entry becomes available for adding new product to the inventory. Reduce the number of different products in the inventory by one.

    Write the implementation of this class as well as test it with a driver program.

    Note : In this problem you are not allowed to use string class or any of the string library functions.

    c

    a

    m

    e

    r

    a

    \0

      

    t

    e

    l

    e

    v

    i

    s

    i

    o

    n

    \0

    \0

    c

    o

    m

    p

    u

    t

    e

    r

    \0

    \0

    r

    e

    f

    r

    i

    g

    e

    r

    a

    t

    o

    r

    \0

    o

    v

    e

    n

    \0

    \0

    \0

    \0

    Table 1. Stores the product names

    12

    625.00

    17

    410.00

    0

    0

    8

    750.00

    0

    0

    6

    975.00

    9

    550.00

    0

    0

    0

    0

    0

    0

    Table 2. First column stores the stock in the inventory and second

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Main.cpp

#include<iostream>
#include"Inventory.h"
using namespace std;

void main(){
   Inventory inv;
   inv.new_product("Product1",4,5);
   inv.new_product("Product2",5,7);
   cout<<"Product 2 Stock:"<<inv.get_stock("Product2")<<endl;
   cout<<"\n=====================================\nOrder large quantity :";
   cout<<inv.order("Product2",6)<<endl;
   cout<<"\n=====================================\nOrder limited quantity :"<<inv.order("Product2",3)<<endl;
   cout<<"\n=====================================\nDiscontinue product\n";
   inv.discontinued_product("Product2");
   cout<<"Stock of discontinued product:"<<inv.get_stock("Product2")<<endl;
   system("pause");
}

inventory.h

#pragma once
#include<iostream>
using namespace std;
class Inventory
{
private:
   static const int size = 100;
   int no_of_products; // the total number of products in the inventory.
   char name_array[size][15] ; // stores name of products
   double array [size][2]; // In the first column quantity of the product and in the second column the price per item of the product is stored.
public:
   //function prototypes as asked in question
   Inventory(void);
   int row_no( char *);
   int get_stock( char *);
   double order( char *, int);
   bool new_product( char *, int, double);
   void discontinued_product( char *);
};

Inventory.cpp

#include "Inventory.h"


Inventory::Inventory(void)
{
   no_of_products=0; // the total number of products in the inventory.
   name_array[size][15]; // stores name of products
   array [size][2]=0;//by default double will set values to 0
   for(int i=0;i<size;i++){//setting first character of each value to \0
       name_array[i][0]='\0';
   }
}

int Inventory::row_no(char *product_name){
   int row=0;  
   int j=0;
   char temp;
   //looping through all elements
   for(int i=0;i<size;i++){
       j=0;
       temp=product_name[j];//first character of product to be find
       while(temp!='\0'){ //loop untill full string is searched
           if(temp==name_array[i][j]){//if value is matching continue with while loop
               j++;//updating index
               temp=product_name[j];//updating temp
           }else{//when the first different character is found break the loop
               break;
           }
       }
       //if temp is \0 that means full string matched
       if(temp=='\0'){
           return i;//return row number
       }
   }
   return -1; //if not found returning negative index
}
int Inventory::get_stock( char *product_name){
   int row=row_no(product_name); //getting row number by product name
   if(row!=-1){
       return array[row][0]; //return quantity of that row
   }else{//if product not in array then print message
       cout<<"Product not found\n";
   }
   return 0;
}

double Inventory::order( char *product_name, int quantity){
   int row=row_no(product_name);//getting row of product
   if(row!=-1){
       if(array[row][0]>=quantity){//if order is less than stock
           array[row][0]=array[row][0]-quantity; //update quantity
           return array[row][1]*quantity;//return cost
       }else{//if order is greater than stock return 0 with error message
           cout<<"Order cannot be met\n";
       }
   }else{
       cout<<"Product not found\n";
   }
   return 0;
}
bool Inventory::new_product( char *product_name, int quantity, double price){
   int j;
   char temp;
   for(int i=0;i<size;i++){
       if(name_array[i][0]=='\0'){//finding where there is empty space
           j=0;
           temp=product_name[j];
           while(temp!='\0'){ //copying product name to the empty name_array
               name_array[i][j]=product_name[j];
               j++;
               temp=product_name[j];
           }
           name_array[i][j]='\0';
           array[i][0]=quantity; //setting quantity
           array[i][1]=price; //setting price
           no_of_products++; //increasing number of products
           return true;
       }
   }
   return false;//if array is not free return false
}
void Inventory::discontinued_product(char *product_name){
       int row=row_no(product_name);//finding row value
       if(row!=-1){
           name_array[row][0]='\0';//setting value to null
           array[row][0]=0; //updating quantity
           array[row][1]=0; //updating price
           no_of_products--;
       }else{
           cout<<"Product not found.\n";
       }
}

Output:


Related Solutions

Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
please I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Understand the code and explain the code and answer the questions. Type your answers as comments....
Understand the code and explain the code and answer the questions. Type your answers as comments. #include #include using namespace std; // what is Color_Size and why it is at the end? enum Color {        Red, Yellow, Green, Color_Size }; // what is Node *next and why it is there? struct Node {        Color color;        Node *next; }; // explain the code below void addNode(Node* &first, Node* &last, const Color &c) {        if (first == NULL)...
I need this code in C++ form using visual studios please: Create a class that simulates...
I need this code in C++ form using visual studios please: Create a class that simulates an alarm clock. In this class you should: •       Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have separate private members for the alarm and the clock. Do not forget to have a character variable representing AM or PM.) •       Initialize the clock to a specified time. •       Allow the clock to increment to the...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT