I need help creating a container class for this code so I can then modularize it. Any help is greatly appreciated!! Below is what I have written so far without the container class:
#include<iostream>
usingnamespace std;
class item {
private:
unsignedlong id;
string name;
float cost;
int quantity;
int x;
public:
void additem(void);//add item to an inventory
void printitem(void);//iterate x for displaying the list
void printid(void);//take input id from user show the data
void printname(void);//take input item name and display it
void initial(void){x=0;};//initially x is equal to zero
int get_id(){return id;};
string get_name(){return name;};
};
void findid(item inventory[], int count);
void findname(item inventory[], int count);
void printall (item inventory[], int count);
void item::additem(void){//add item to an inventory//person container
cout<<"Enter the Item ID:";
cin>>id;
cout<<"Enter the Item Name:"<<endl;
cin>>name;
cout<<"Enter the Price of the Item:"<<endl;
cin>>cost;
cout<<"Enter The Item Quantity:"<<endl;
cin>>quantity;
x++;
cout<<endl;
}
void findid(item inventory[], int count){//find by ID//person
int i;
cout<<"Please Enter the Item ID:"<<endl;
cin>>i;
cout<<endl;
for(int j=0;j<count;j++){
if(inventory[j].get_id()==i){
inventory[j].printitem();
}
}
}
void findname(item inventory[], int count){//find by name//person
string strname;
cout<<"Please Enter the Item Name:"<<endl;
cin>>strname;
cout<<endl;
for(int j=0;j<count;j++){
if(inventory[j].get_name()==strname){
inventory[j].printitem();
}
}
}
void printall (item inventory[], int count){
for (int i = 0; i < count; i++)
inventory[i].printitem();
}
void item::printitem(){//print item found in system//personcontainer
if(x==0){
cout<<"No Items In Inventory";
}
cout<<endl;
cout<<"Item ID is:"<<id<<endl;
cout<<"Item Name is:"<<name<<endl;
cout<<"Item Price is:"<<cost<<endl;
cout<<"Item Quantity is:"<<quantity<<endl;
cout<<endl;
}
int main ()
{
int i;
item inventory[50];
int count = 0;
while(1)
{
cout<<endl;
cout<<"This is our Inventory Management System"<<endl;
cout<<"1: Add a new item."<<endl;
cout<<"2: Print Item list."<<endl;
cout<<"3: Find Item by ID."<<endl;
cout<<"4: Find Item by Name."<<endl;
cout<<"5: Quit"<<endl;
cout<<"Please choose a number:"<<endl;
cin>>i;
cout<<endl;
switch(i){
case 1:{
inventory[count].additem();
count++;
break;
}
case 2:{
printall(inventory, count);
break;
}
case 3:{
findid(inventory, count);
break;
}
case 4:{
findname(inventory, count);
break;
}
case 5:{
cout<<"You chose to quit!"<<endl;
cout<<endl;
break;
}
}
if (i==5)
break;
}
return 0;
}
"Modify your inventory management system created in previous assignment to use separate container class to main the items collection and multiple modules to organize your code".
The existing code has an array of 50 items and a count that indicates how many are actually valid. You can do several things with the collection: add an item, find an item by id, find by name, and print all items.
So you could create a collection class and put all the underlined things into the class. The parameters of the methods would change somewhat. Also, I'd use a vector<item> to store them instead of an array and count that you must maintain yourself.