-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
318 lines (251 loc) · 8.34 KB
/
code.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include<iostream>
#include<fstream>
#include<string.h>
#include<cstdio> //for rename() and remove()
#include<conio.h>
using namespace std;
/* char pass[5],passc[5]="@Mit";*/
/* void confor() //checking for verified user's password
{
cout<<"Hello! We need confirmation! Password Please:- ";
cin>>pass;
int n=strcmp(pass,passc); //user confirmation
if(n==0)
{
cout<<"\n\t\t\t\t\t\tWelcome Amit!!"<<endl;
return;
}
else
{
cout<<"Sorry! You are not granted the access!!";
exit(1);
}
}*/
/* confor() for the authentication for the console screen*/
int confor()
{
char pass[5];
int i;
string passwd;
cout<<"Enter the password"<<endl;
for(i=0;i<4;i++)
{
pass[i]=getch(); /* taking the inputs and displaying '*' as a password does*/
cout<<"*";
}
pass[i]='\0'; /*to make the input string*/
/*system("cls");*/ /*used for clearing the screen, but not using*/
passwd=pass;
if(passwd=="@Mit")
return 1; /* returning 1 to the if statement call*/
else
return 0;
}
/*the processor(metaphor) of the data the data are stores in the blocks of the objects*/
class shop{
char pname[26];
char pcode[10];
float pcost;
int pq;
public:
void input();
void display();
int checkpc(char ac[]) //accessor function
{ return strcmp(pcode,ac);
}
void modify();
}s,sh;
/*input() member function defined outside the class*/
void shop::input()
{
cout<<"Enter the name of product: "<<endl;
cin>>pname;
cout<<"Enter the product code: "<<endl;
cin>>pcode;
cout<<"Enter the product cost: "<<endl;
cin>>pcost;
cout<<"Enter the product quantity: "<<endl;
cin>>pq;
}
/*display() member function defined outside the class*/
void shop::display()
{
cout<<"\n\t\tName of the product:-"<<pname<<endl;
cout<<"\t\tProduct code of product:-"<<pcode<<endl;
cout<<"\t\tCost of the product:-"<<pcost<<endl;
cout<<"\t\tProduct Quantity in stock:-"<<pq<<endl;
}
/* modify() member function defined outside the class*/
void shop::modify()
{
cout<<"\t\t\tProduct Name: "<<pname<<endl;
cout<<"\t\t\tProduct Code: "<<pcode<<endl;
cout<<"\t\t\tProduct Cost: "<<pcost<<endl;
cout<<"\t\t\tProduct Quantity: "<<pq<<endl;
char pn[10]=" ",pc[10]=" ";
float pcs; int pqq;
cout<<"\t\tNew Product Name:(Enter '.' to retain old one)";
cin>>pn;
cout<<"\t\tNew Product code:(Enter '.' to retain old one)";
cin>>pc;
cout<<"\t\tNew Product cost:(Enter '0' to retain old one)";
cin>>pcs;
cout<<"\t\tNew Product quantity:(Enter '0' to retain old one)";
cin>>pqq;
if(strcmp(pn,".")!=0)
strcpy(pname,pn);
if(strcmp(pc,".")!=0)
strcpy(pcode,pc);
if(pcs!=0)
pcost=pcs;
if(pqq!=0)
pq=pqq;
}
/*function to add a items details to the file*/
void add()
{
ofstream fo;
fo.open("items.dat",ios::app|ios::binary);
char ans='y';
while((ans=='y')||(ans=='Y'))
{
s.input();
fo.write((char*)&s,sizeof(s));
cout<<"Item added to file!!\n";
cout<<"Want to Add more items(y/n):- ";
cin>>ans;
}
fo.close();
}
/*funtion for modification of the product from the file*/
void firmod()
{
fstream fio;
fio.open("items.dat",ios::in|ios::out|ios::binary);
char mpc[10];
long pos; char found='f';
cout<<"\nEnter the Product code to be modified:-";
cin>>mpc;
while(!fio.eof())
{
pos=fio.tellg(); //before reading a record its begining pos is determined with tellg()
fio.read((char*)&s,sizeof(s));
if(s.checkpc(mpc)==0)
{
s.modify();
fio.seekg(pos);
fio.write((char*)&s,sizeof(s));
found='t';
break;
}
}
if(found=='f')
cout<<"\nRecord not found!!\n";
fio.close();
}
/*function to delete the product from the file*/
void delt()
{
ifstream fio("items.dat",ios::binary);
ofstream file("temp.dat",ios::out|ios::binary);
char mpc1[10];
char found='f',confirm='n';
cout<<"\nEnter the Product code to be Deleted:-";
cin>>mpc1;
while(!fio.eof())
{
fio.read((char*)&s,sizeof(s));
if(s.checkpc(mpc1)==0)
{
s.display();
found='t';
cout<<"Are you sure , you want to delete this record(y/n):.. ";
cin>>confirm;
if(confirm=='n')
file.write((char*)&s,sizeof(s));
}
else
file.write((char*)&s,sizeof(s));
}
if(found=='f')
cout<<"\nRecord not found!!\n";
fio.close();
file.close();
remove("items.dat");
rename("temp.dat","items.dat"); //old file is removed and temp file is renamed
}
/*funtion to display the whole data from the file*/
void dis()
{
cout<<"\nThe file contains:\n";
ifstream fio("items.dat");
while(!fio.eof())
{
fio.read((char*)&sh,sizeof(sh));
if(fio.eof())
break;
sh.display();
}
fio.close();
}
/*function for displaying the particular item from the data*/
void pshow()
{
ifstream fio("items.dat",ios::binary);
char pc1[10];
char found='f',confirm='n';
cout<<"\nEnter the Product code to be Shown:-";
cin>>pc1;
while(!fio.eof())
{
fio.read((char*)&s,sizeof(s));
if(s.checkpc(pc1)==0)
{
s.display();
found='t';
}
}
if(found=='f')
cout<<"\nRecord not found!!\n";
fio.close();
}
/* the heart of the program the main()*/
int main()
{
cout<<"****************************Welcome to Amit's Shop**************************\n";
cout<<"*****************All Products available here***********************************\n";
if(confor())
{
int c;
while(c!=6)
{
cout<<"\nEnter your choice: \n";
cout<<"\t\t\t1. Add Items"<<endl;
cout<<"\t\t\t2. Modify the Data"<<endl;
cout<<"\t\t\t3. Delete an item"<<endl;
cout<<"\t\t\t4. Show Data of all items:"<<endl;
cout<<"\t\t\t5. Show Data of particular item:"<<endl;
cout<<"\t\t\t6. Exit"<<endl;
cin>>c;
switch(c)
{
case 1: add();
break;
case 2: firmod();
break;
case 3: delt();
break;
case 4: dis();
break;
case 5: pshow();
break;
case 6: exit(1);
break;
default: cout<<"\n\t\tSorry! Wrong input!!";
}
}
}
else
cout<<"\n\t\tYou are not granted the access!! Wrong Password!!";
return 0;
}