In: Computer Science
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
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: