Container Class

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

#include<iostream>
using namespace std;

 class item {
  private:
     unsigned long 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;
}
What is the purpose of this "container class"? Do you realize that you're already using a container?

Perhaps you should share the actual assignment text.


sorry, I thought I had it in there initially.

"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.
I'd start with "multiple modules to organize your code".
Topic archived. No new replies allowed.