Question: In C++ Implement a class 14. Inventory Bins p. Define the operators <<, >>, =.
Inventory Bins Write a program that simulates inventory bins in a warehouse. Each bin holds a number of the same type of parts. The program should use a structure that keeps the following data: Description of the part kept in the bin Number of parts in the bin The program should have an array of 10 bins, initialized with the following data
Solution:
#include<iostream>
using namespace std;
class InventoryBin{
string desc;
int noOfPart;
public:
InventoryBin(){
desc = “”;
noOfPart = 0;
}
InventoryBin(string des){
desc = des;
noOfPart = 0;
}
InventoryBin(string des,int n){
desc = des;
if(n > 0 && n <= 30){
noOfPart = n;
}
}
void addPart(int n){
if(n > 0 && 30 >= noOfPart+n){
noOfPart += n;
}
else
cout<<“\n count may be negative or it add to become above 30 “;
}
void removePart(int n){
if(n > 0 && 0 <= noOfPart-n){
noOfPart -= n;
}
else
cout<<“\n count is negative or it will became negative “;
}
friend ostream& operator<<(ostream &out,const InventoryBin &oth){
out<<oth.desc<<” \t\t\t “<<oth.noOfPart;
return out;
}
void operator=(const InventoryBin &oth){
desc = oth.desc;
noOfPart = oth.noOfPart;
}
friend istream& operator>>(istream &in,InventoryBin &oth){
in>>oth.desc;
in>>oth.noOfPart;
return in;
}
};
void printInventory(InventoryBin in[10]){
system(“cls”);
cout<<“—————————————————————————–“;
cout<<“\nslNo. \t Part Description \t Number Of Parts in the Bin\n”;
cout<<“—————————————————————————–\n”;
for(int i = 0;i<10;i++){
cout<<i+1<<” \t “<<in[i]<<endl;
}
}
void select(InventoryBin &in){
char ch = ‘0’;
while(ch != ‘3’){
cout<<“\n 1. add “;
cout<<“\n 2. remove”;
cout<<“\n 3. back “;
cout<<“\nchoose : “;
cin>>ch;
if(ch == ‘1’){
cout<<“\n enter the no. of part to add : “;
int n;
cin>>n;
in.addPart(n);
}
if(ch == ‘2’){
cout<<“\n enter the no. of part to remove : “;
int n;
cin>>n;
in.removePart(n);
}
}
}
int main()
{
InventoryBin in[10];
in[0] = InventoryBin(“Value “,10);
in[1] = InventoryBin(“Bearing “,5);
in[2] = InventoryBin(“Bushing “,15);
in[3] = InventoryBin(“Coupling “,21);
in[4] = InventoryBin(“Flange “,7);
in[5] = InventoryBin(“Gear “,5);
in[6] = InventoryBin(“Gear Housing “,5);
in[7] = InventoryBin(“Vacuum Gripper”,25);
in[8] = InventoryBin(“Cable “,18);
in[9] = InventoryBin(“Rod “,12);
char ch = ‘ ‘;
while(ch != ‘q’){
printInventory(in);
cout<<“\n\nChoose from above sl no. to select the Inventory or press ‘q’ to quit the program”;
cin>>ch;
if(ch > ‘0’ && ch < ‘9’)
select(in[(int)(ch – ‘0’)-1]);
}
}