-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItemList.cpp
169 lines (152 loc) · 3.5 KB
/
ItemList.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include "ItemList.h"
#include "Item.h"
using namespace std;
//Constructor--Initial allocation of the class object
ItemList::ItemList()
{
curAllocation=5;
itemList=new Item[curAllocation];
count=0;
}
//Destructor
ItemList::~ItemList()
{
delete[] itemList;
}
//Dynamic memory allocation
void ItemList::addItem(Item& obj)
{
if(count < curAllocation)
{
itemList[count]=obj;
++count;
}
else
{
Item *temp = itemList;
itemList = new Item[curAllocation*2];
curAllocation=curAllocation*2;
for(int i=0; i<count; i++)
{
itemList[i]=temp[i];
}
delete[] temp;
}
setCount(count);
// cout<<"\nThe Item details are: \n"<<endl;
// cout<<"Item ID: "<<obj.getId()<<endl;
// cout<<"Item Name: "<<obj.getName()<<endl;
// cout<<"Item Cost: "<<obj.getCost()<<endl;
// cout<<"Item Quantity: "<<obj.getQuantity()<<endl;
}
//Function to print items calling Item class
void ItemList::printItem(Item& obj, int& cnt)
{
cout<<"calling item function"<<endl;
cout<<cnt;
for(int i=0; i<cnt; ++i)
{
itemList[i].printItems(obj);
}
}
//To find by ID
void ItemList::findItemID(Item& obj)
{
int id;
cout<<"Enter the Item ID to be found: "<< endl;
cin>>id;
for(int i=0; i<count; i++)
{
if(itemList[i].getId()==id)
{
cout<<"The Item details are: \n";
itemList[i].printItems(obj);
break;
}
else if (i >= count -1 && itemList[i].getId() != id)
{
cout<<"Error: Item not Found!"<<endl;
break;
}
}
}
//To find by name
void ItemList::findItemName(Item& obj)
{
string name;
cout<<"Enter the name to be searched: "<<endl;
getline(cin>>ws, name);
for(int i=0; i<count;i++)
{
if(itemList[i].getName()==name)
{
cout<<"The Item details are: \n";
itemList[i].printItems(obj);
break;
}
else if(i >= count -1 && itemList[i].getName() != name)
{
cout<<"Error: Item not found!"<<endl;
break;
}
}
}
//Writing to file
void ItemList::writeFile()
{
ofstream myOut;
myOut.open("InventoryList.txt", ios::app);
for (int i=0; i<count; ++i)
{
myOut<<"hello";
cout<<itemList[i].getId();
myOut<<itemList[i].getId()<<endl;
myOut<<itemList[i].getName()<<endl;
myOut<<itemList[i].getCost()<<endl;
myOut<<itemList[i].getQuantity()<<endl;
}
myOut.close();
}
//Read all the details from the file
void ItemList::readFile(Item pobj, ItemList pitem)
{
unsigned short tid;
string tname;
float tcost;
int tquantity;
ifstream myOut;
myOut.open("InventoryList.txt", ios::in);
if (!myOut.is_open())
{
cout<<"Error: File not found"<<endl;
}
else
{
for(;;)
{
myOut>>tid;
pobj.setId(tid);
myOut.ignore();
getline(myOut, tname);
pobj.setName(tname);
myOut>>tcost;
pobj.setCost(tcost);
myOut>>tquantity;
pobj.setQuantity(tquantity);
if (myOut.good())
{
Item(tid, tname, tcost, tquantity);
pitem.addItem(pobj);
}
else if(myOut.eof())
{
break;
}
}
}
myOut.close();
}