-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
148 lines (126 loc) · 3.14 KB
/
Copy pathproject.cpp
File metadata and controls
148 lines (126 loc) · 3.14 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<iostream>
#include<string>
using namespace std;
class InventoryItem{
private:
int id;
string name;
int quantity;
string category;
public:
InventoryItem(){};
InventoryItem(int id, string name, int quantity, string category): id(id), name(name), quantity(quantity), category(category){}
int getId() const {return id;}
const string& getName() const { return name; }
int getQuantity() const { return quantity; }
const string& getCategory() const { return category; }
void setQuantity(int quantity) { this->quantity = quantity;}
};
class Inventory{
private:
InventoryItem* items;
int numItems;
int capacity;
Inventory(){}
int findItem(int id) const
{
for (int i=0;i<numItems;i++)
{
if(items[i].getId()==id)
{
return i;
}
}
return -1;
}
void ensureCapacity(int capacity)
{
if (this->capacity<capacity)
{
InventoryItem* newItems=new InventoryItem[capacity];
for (int i=0;i<numItems;i++)
{
newItems[i]=items[i];
}
delete[] items;
items=newItems;
this->capacity=capacity;
}
}
public:
Inventory():items(nullptr),numItems(0),capacity(0) {}
~Inventory()
{
delete[] items;
}
void addItem(int id,string name,int quantity,string category)
{
if(findItem(id)!=-1)
{
cout << "invalid" << id <<"Exist before" << endl;
return;
}
if (numItems==capacity) {
ensureCapacity(capacity*2);
}
items[numItems]=InventoryItem(id,name,quantity,category);
numItems++;
}
void update(int id, int quantity)
{
int index=findItem(id);
if(index==-1)
{
cout << "invalid"<< id<<"couldnt found"<< endl;
return;
}
items[index].setQuantity(quantity);
}
void getIteminfo(int id)const
{
int index=findItem(id);
if (index==-1)
{
cout << "Invalid"<<id<<"couldnot found" << endl;
return;
}
const InventoryItem& item=items[index];
cout << "Id :" << item.getId() << endl;
cout << "Name :" << item.getName() << endl;
cout << "Quantity :" << item.getQuantity() << endl;
cout << "Category :" << item.getCategory() << endl;
}
void getCount()const
{
int count[3]={0,0,0};
for (int i=0;i<numItems;i++) {
if(items[i].getCategory()=="Food")
{
count[0]++;
}
else if(items[i].getCategory()=="Tool")
{
count[1]++;
}
else if(items[i].getCategory()=="Equipment")
{
count[2]++;
}
}
cout<<"Count of food item :"<< count[0]<< endl;
cout<<"Count of tool item :"<< count[1]<< endl;
cout<<"Count of equipment :"<< count[2]<< endl;
}
};
int main()
{
// cout<<"INVENTORY MANAGAMENT SYSTEM";
Inventory i1;
i1.addItem(1,"Bread",5,"Food");
i1.addItem(2,"Hammer",3,"Tools");
i1.addItem(3,"Nails",3,"Equipment");
i1.update(1,3);
i1.getIteminfo(2);
i1.getCount();
return 0;
}