forked from lancertech6/Hactober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle.cpp
258 lines (246 loc) · 7.76 KB
/
vehicle.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
#include <iostream>
#include <fstream>
using namespace std;
class Vehicle
{
protected:
unsigned int capacity, mileage, id;
string vname, vtype, cname;
public:
void gatherData()
{
cout << "Name of the Vehicle: ";
cin >> vname;
cout << "\nType of the Vehicle: ";
cin >> vtype;
cout << "\nCompany Name of the Vehicle: ";
cin >> cname;
cout << "\nCapacity of the Vehicle(in cc): ";
cin >> capacity;
cout << "\nMileage of the Vehicle (in Kmpl): ";
cin >> mileage;
id++;
}
void displayData()
{
fstream file;
cout << "\nAll Vehicles";
file.open("vehicle.txt", ios::in);
if (!file)
cout << "\n\nFile Opening Error!";
else
{
cout << "\n\n\nV_ID\tName"
<< "\tCompany\tType"
<< "\tCapacity\tMileage\n\n";
file >> id >> vname;
file >> cname >> vtype;
file >> capacity >> mileage;
while (!file.eof())
{
cout << " " << id
<< "\t" << vname
<< "\t" << cname
<< "\t" << vtype
<< "\t" << capacity
<< "\t\t" << mileage
<< "\n\n";
file >> id >> vname;
file >> cname >> vtype;
file >> capacity >> mileage;
}
file.close();
}
}
void write()
{
ofstream write;
write.open("vehicle.txt", ios::app);
if (!write)
{
cout << "\nFile could not be found or File could not be created.";
}
else
{
cout << "\nWriting Data to File...\n";
write << id << " " << vname << " " << cname << " " << vtype << " " << capacity << " " << mileage << " "
<< "Abhishek" << endl;
write.close();
cout << "Data has been exported to file named vehicle.txt";
}
}
void updateData()
{
unsigned int capacity_c, mileage_c, vid, flag = 0, choice;
string vname_c, vtype_c, cname_c;
ofstream write;
ifstream read;
cout << "\n\nUpdate Vehicle Record";
write.open("temp.txt", ios::app);
read.open("vehicle.txt");
if (!read)
{
cout << "\nFile could not be found or File could not be created.";
}
else
{
cout << "\nVehicle ID: ";
cin >> vid;
read >> id;
read >> vname;
read >> cname;
read >> vtype;
read >> capacity;
read >> mileage;
while (!read.eof())
{
if (vid != id)
{
write << id << " " << vname << " " << cname << " " << vtype << " " << capacity << " " << mileage << endl;
}
else
{
cout << "\nVehicle Name ----->(1)";
cout << "\nVehicle Company Name ----->(2)";
cout << "\nVehicle Type ----->(3)";
cout << "\nCapacity ----->(4)";
cout << "\nMileage ----->(5)";
cout << "\nWhat would you like to modify? ";
cin >> choice;
if (choice == 1)
{
cout << "\nEnter New Vehicle Name: ";
cin >> vname_c;
write << id << " " << vname_c << " " << cname << " " << vtype << " " << capacity << " " << mileage << endl;
}
else if (choice == 2)
{
cout << "\nEnter New Vehicle Company Name: ";
cin >> cname_c;
write << id << " " << vname << " " << cname_c << " " << vtype << " " << capacity << " " << mileage << endl;
}
else if (choice == 3)
{
cout << "\nEnter New Vehicle Type: ";
cin >> vtype_c;
write << id << " " << vname << " " << cname << " " << vtype_c << " " << capacity << " " << mileage << endl;
}
else if (choice == 4)
{
cout << "\nEnter New Vehicle Capacity: ";
cin >> capacity_c;
write << id << " " << vname << " " << cname << " " << vtype << " " << capacity_c << " " << mileage << endl;
}
else if (choice == 5)
{
cout << "\nEnter New Vehicle Mileage: ";
cin >> vname_c;
write << id << " " << vname << " " << cname << " " << vtype << " " << capacity << " " << mileage_c << endl;
}
else
{
cout << "\nInvalid Input. Please Try Again.";
}
flag++;
}
read >> id;
read >> vname;
read >> cname;
read >> vtype;
read >> capacity;
read >> mileage;
}
if (flag == 0)
{
cout << "\nVehicle ID not found.";
}
}
cout << endl;
write.close();
read.close();
remove("vehicle.txt");
rename("temp.txt", "vehicle.txt");
}
void deleteData()
{
fstream file, file1;
int found = 0;
int VNum;
cout << "\nDelete Vehicle Details." << endl;
file.open("vehicle.txt", ios::in);
if (!file)
{
cout << "\nFile Not Found.";
file.close();
}
else
{
cout << "\nVehicle ID: ";
cin >> VNum;
file1.open("temp.txt", ios::app | ios::out);
file >> id >> vname >> cname >> vtype >> capacity >> mileage;
while (!file.eof())
{
if (VNum != id)
{
file1 << " " << id << " " << vname << " " << cname << " " << vtype << " " << capacity << " " << mileage << "\n";
}
else
{
found++;
cout << "\nVehicle Data has been deleted successfully.";
}
file >> id >> vname >> cname >> vtype >> capacity >> mileage;
}
if (found == 0)
{
cout << "\nVehicle ID not found.";
}
file1.close();
file.close();
remove("vehicle.txt");
rename("temp.txt", "vehicle.txt");
}
}
};
int main()
{
Vehicle execute;
int loop = 0, a;
while (loop == 0)
{
cout << "\n=-=-=-=-=-=TSDL Vehicle Database=-=-=-=-=-=";
cout << "\nAdd Data ----->(1)";
cout << "\nView Data ----->(2)";
cout << "\nModify Data ----->(3)";
cout << "\nDelete Data ----->(4)";
cout << "\nExit ----->(0)";
cout << "\nWhat would you like to do?: ";
cin >> a;
if (a == 1)
{
execute.gatherData();
execute.write();
}
else if (a == 2)
{
execute.displayData();
}
else if (a == 3)
{
execute.updateData();
}
else if (a == 4)
{
execute.deleteData();
}
else if (a == 0)
{
break;
}
else
{
cout << "\nInvalid Choice. Please Try again.";
}
}
}