Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
244 lines (227 loc) · 6.07 KB
/
Copy pathmain.cpp
File metadata and controls
244 lines (227 loc) · 6.07 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
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
#include<iostream>
#include<fstream>
#include<cctype>
#include<iomanip>
using namespace std;
//Creating Vehicle Class
class vehicle
{
int year;
int id;
char OwnerName[50];
char model[50];
char type[50];
public:
void add_vehicle(); //Function to add a Vehicle to the Garage
void show_vehicle() const; //Function to display a Vehicle
void modify(); //Function to Update a Users Vehicle
void report() const; //Function to display Vehicle in a tabular method
int retid() const; //Function to return userID
};
void vehicle::add_vehicle()
{
cout<<"\n User ID :";
cin>>id;
cout<<"\nEnter Owner Name :";
cin.ignore();
cin.getline(OwnerName,50);
cout<<"\nEnter Make and Model of Car : ";
cin.ignore();
cin.getline(model,50);
cout<<"\nEnter Type of Vehicle: ";
cin.ignore();
cin.getline(type,50);
cout<<"\nEnter Year of Vehicle: ";
cin>>year;
cout<<"\n\nVehicle Added!";
}
void vehicle::show_vehicle() const
{
cout<<"\n User ID :"<<id;
cout<<"\nOwner : "<<OwnerName;
cout<<"\nCar Model : "<<model;
cout<<model;
cout<<"\nType of Vehicle : "<<type;
cout<<"\nYear : "<<year;
}
void vehicle::modify()
{
cout<<"\nOwner: "<<OwnerName;
cout<<"\nModify Owner Vehicle (Make/Model) : ";
cin>>model;
// cin.getline(model,50);
cout<<"\nVehicle Type : ";
cin>>type;
// cin.ignore();
// cin.getline(type,50);
cout<<"\nYear : ";
cin>>year;
}
int vehicle::retid() const
{
return id;
}
void vehicle::report() const
{
cout<<id<<"\t"<<OwnerName<<"\t "<<model<<"\t"<<type<<"\t"<<year<<endl;
}
void write_account(); //Function to write object record in binary file
void display_veh(int); //function to display vehicle details given by user
void modify_account(int); //function to modify record of file
void delete_account(int); //function to delete record of file
void display_all(); //function to display all account details
int main()
{
char ch;
int num;
cout<<"Welcome to the Garage Management System (GMS)";
do
{
cout<<"\n\n\n\tGarage Management System";
cout<<"\n\n\t1. New Driver";
cout<<"\n\n\t2. View Driver";
cout<<"\n\n\t3. All Drivers";
cout<<"\n\n\t4. Remove Driver";
cout<<"\n\n\t5. Modify Driver";
cout<<"\n\n\t6. Exit the System";
cout<<"\n\n\tSelect Your Option (1-6): ";
cin>>ch;
switch(ch)
{
case '1':
write_account();
break;
case '2':
cout<<"\n\n\tEnter the User's ID: "; cin>>num;
display_veh(num);
break;
case '3':
display_all();
break;
case '4':
cout<<"\n\n\tEnter the User's ID: "; cin>>num;
delete_account(num);
break;
case '5':
cout<<"\n\n\tEnter the User's ID: "; cin>>num;
modify_account(num);
break;
case '6':
cout<<"\n\n\tThanks for using the garage management system!";
break;
default :cout<<"\a";
}
cin.ignore();
cin.get();
}while(ch!='6');
return 0;
}
void write_account()
{
vehicle veh;
ofstream outFile;
outFile.open("garage.dat",ios::binary|ios::app);
veh.add_vehicle();
outFile.write(reinterpret_cast<char *> (&veh), sizeof(vehicle));
outFile.close();
}
void display_veh(int n)
{
vehicle veh;
bool flag=false;
ifstream inFile;
inFile.open("garage.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
return;
}
cout<<"\nVEHICLE DETAILS\n";
while(inFile.read(reinterpret_cast<char *> (&veh), sizeof(vehicle)))
{
if(veh.retid()==n)
{
veh.show_vehicle();
flag=true;
}
}
inFile.close();
if(flag==false)
cout<<"\n\nAccount number does not exist";
}
void modify_account(int n)
{
bool found=false;
vehicle veh;
fstream File;
File.open("garage.dat",ios::binary|ios::in|ios::out);
if(!File)
{
cout<<"File could not be open!\n Press any Key...";
return;
}
while(!File.eof() && found==false)
{
File.read(reinterpret_cast<char *> (&veh), sizeof(vehicle));
if(veh.retid()==n)
{
veh.show_vehicle();
cout<<"\n\nEnter New Details of Vehicle"<<endl;
veh.modify();
int pos=(-1)*static_cast<int>(sizeof(vehicle));
File.seekp(pos,ios::cur);
File.write(reinterpret_cast<char *> (&veh), sizeof(vehicle));
cout<<"\n\n\t User's Vehicle Updated";
found=true;
}
}
File.close();
if(found==false)
cout<<"\n\n Vehicle Not Found ";
}
void delete_account(int n)
{
vehicle veh;
ifstream inFile;
ofstream outFile;
inFile.open("garage.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open!\n Press any Key...";
return;
}
outFile.open("Temp.dat",ios::binary);
inFile.seekg(0,ios::beg);
while(inFile.read(reinterpret_cast<char *> (&veh), sizeof(vehicle)))
{
if(veh.retid()!=n)
{
outFile.write(reinterpret_cast<char *> (&veh), sizeof(vehicle));
}
}
inFile.close();
outFile.close();
remove("garage.dat");
rename("Temp.dat","garage.dat");
cout<<"\n\n\tRecord Deleted ..";
}
void display_all()
{
vehicle veh;
ifstream inFile;
inFile.open("garage.dat",ios::binary);
if(!inFile)
{
cout<<"File could not be open!\n Press any Key...";
return;
}
cout<<"\n\n\t\t Vehicles In Garage\n\n";
cout<<"=========================================================================\n";
cout<<"User Id Driver Make/Model Type Year\n";
cout<<"=========================================================================\n";
while(inFile.read(reinterpret_cast<char *> (&veh), sizeof(vehicle)))
{
veh.report();
}
inFile.close();
}