-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
111 lines (85 loc) · 1.84 KB
/
program.cpp
File metadata and controls
111 lines (85 loc) · 1.84 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
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <fstream.h>
class itemStore {
struct item {
int id;
char name[50];
double price;
item *link;
}t;
item *top;
void initialize() {
fstream file("itemStore.dat", ios::binary|ios::in);
while(file.read((char*)&t, sizeof(t))) {
item *temp = new item;
temp->id = t.id;
strcpy(temp->name, t.name);
temp->price = t.price;
temp->link = top;
top = temp;
}
file.close();
}
void write(item *temp) {
fstream file("itemStore.dat", ios::binary|ios::app);
t.id = temp->id;
strcpy(t.name, temp->name);
t.price = temp->price;
t.link = temp->link;
file.write((char*)&t, sizeof(t));
file.close();
}
public:
itemStore() {
top = NULL;
initialize();
}
void addItem() {
item *temp = new item;
cout << "ENTER NEW ITEM DETAILS" << endl;
cout << "Product Id: ";
cin >> temp->id;
cout << endl;
cout << "Product Name: ";
gets(temp->name);
cout << endl;
cout << "Product Price: ";
cin >> temp->price;
cout << endl << endl;
write(temp);
temp->link = top;
top = temp;
}
void deleteItem() {
if (top == NULL) {
cout << "No items exist." << endl << endl;
}
else {
cout << "Item deleted: " << top->id << " " << top->name << " " << top->price << endl << endl;
item *temp = top;
top = temp->link;
delete temp;
}
}
void displayItem() {
if (top == NULL) {
cout << "No items exist." << endl << endl;
}
else {
for (item *temp = top; temp->link != NULL ; temp = temp->link) {
cout << temp->id << " " << temp->name << " " << temp->price << endl;
}
cout << temp->id << " " << temp->name << " " << temp->price << endl << endl;
}
}
};
void main() {
itemStore i;
i.displayItem();
i.addItem();
i.addItem();
i.displayItem();
}