New Text Document
New Text Document
#include<vector>
#include<fstream>
using namespace std;
struct Time
{
int hh;
int mm;
int ss;
char col1;
char col2;
};
struct Date
{
int day;
int month;
int year;
char sym1;
char sym2;
};
class Vehicle
{
string pltno;
int type;
Date dt;
Time arrive;
Time departure;
public:
void addVehicle();
void deleteVehicle();
void printVehicle(Vehicle v);
void show();
int getHashIndex(const string& key, int size);
};
vector<Vehicle> veh(100);
int static totalvehicle = 0, totalcar = 0, totalbike = 0, totalamt = 0, i = 0;
fstream file;
void Vehicle::addVehicle()
{
Vehicle *v = new Vehicle;
cout << "Enter vehicle type (1 for Car/2 for Bike): ";
cin >> v->type;
cout << "Enter vehicle number: ";
cin >> v->pltno;
cout << "Enter arrival time in hours, minutes, and seconds: ";
cin >> v->arrive.hh >> v->arrive.col1 >> v->arrive.mm >> v->arrive.col2 >> v-
>arrive.ss;
cout << "Enter date in day, month, and year: ";
cin >> v->dt.day >> v->dt.sym1 >> v->dt.month >> v->dt.sym2 >> v->dt.year;
veh.at(i).pltno = v->pltno;
veh.at(i).type = v->type;
veh.at(i).arrive.hh = v->arrive.hh;
veh.at(i).arrive.mm = v->arrive.mm;
veh.at(i).arrive.ss = v->arrive.ss;
veh.at(i).dt.day = v->dt.day;
veh.at(i).dt.month = v->dt.month;
veh.at(i).dt.year = v->dt.year;
i++;
totalvehicle++;
if (v->type == 1)
{
totalcar++;
}
else
{
totalbike++;
}
return t3.hh;
}
void Vehicle::deleteVehicle()
{
string pno;
int typ;
Time depart;
int time_diff;
int charge = 0;
cout << "Enter vehicle type (1 for Car/2 for Bike): ";
cin >> typ;
cout << "Enter vehicle number: ";
cin >> pno;
cout << "Enter departure time in hours, minutes, and seconds: ";
cin >> depart.hh >> depart.col1 >> depart.mm >> depart.col2 >> depart.ss;
if (veh.at(j).type == 1)
{
totalcar--;
if (time_diff < 2)
{
charge = 20;
}
else
{
if ((time_diff > 2) && (time_diff < 5))
{
charge = 40;
}
else
{
charge = 50;
}
}
}
else
{
totalbike--;
if (time_diff < 2)
{
charge = 5;
}
else
{
if ((time_diff > 2) && (time_diff < 5))
{
charge = 10;
}
else
{
charge = 20;
}
}
}
cout << "\nVehicle with number " << veh.at(j).pltno << " has left the
parking after paying Rs. " << charge << endl;
file.open("./parkingDatabase.txt", ios::app); // Update the file path
if (!file)
{
cerr << "Error: file could not be opened" << endl;
exit(1);
}
file << veh.at(j).type << "\t\t\t" << veh.at(j).pltno << "\t\t\t" <<
veh.at(j).dt.day << "/" << veh.at(j).dt.month << "/" << veh.at(j).dt.year << "\t\t\
t" << veh.at(j).arrive.hh << ":" << veh.at(j).arrive.mm << ":" <<
veh.at(j).arrive.ss << "\t\t\t" << veh.at(j).departure.hh << ":" <<
veh.at(j).departure.mm << ":" << veh.at(j).departure.ss << endl;
file.close();
veh.erase(veh.begin() + j);
i--;
totalvehicle--;
totalamt = totalamt + charge;
break;
}
if (j == i)
{
cout << "\nWrong Entry, Try Again" << endl;
cout << "Departure: " << endl;
deleteVehicle();
}
}
}
void Vehicle::printVehicle(Vehicle v)
{
cout << v.type << "\t\t\t" << v.pltno << "\t\t\t" << v.dt.day << "/" <<
v.dt.month << "/" << v.dt.year << "\t\t\t" << v.arrive.hh << ":" << v.arrive.mm <<
":" << v.arrive.ss << endl;
}
void Vehicle::show()
{
cout << "Vehicle Type\t\tVehicle Number\t\t\tDate\t\t\tArrival Time" << endl;
for (int j = 0; j < i; j++)
{
printVehicle(veh[j]);
}
}
void totalVehicles()
{
cout << "Total number of vehicles parked: " << totalvehicle << endl;
cout << "Total number of cars parked: " << totalcar << endl;
cout << "Total number of bikes parked: " << totalbike << endl;
}
void totalAmount()
{
cout << "Total collection till now: " << totalamt << endl;
}
int Vehicle::getHashIndex(const string &key, int size)
{
return hashFunction(key, size);
}
int main()
{
int choice;
int size = 100;
char ans;
system("clear"); // Use "clear" command instead of "cls"
do
{
system("clear"); // Use "clear" command instead of "cls"
cout <<
"********************************************************************" << endl;
cout << " VEHICLE PARKING SYSTEM USING SEQUENTIAL FILE APPROACH
" << endl;
cout << "1. Arrival of a vehicle" << endl
<< "2. Total number of vehicles parked" << endl
<< "3. Departure of vehicle" << endl
<< "4. Total amount collected" << endl
<< "5. Display" << endl
<< "6. Exit" << endl
<<
"********************************************************************" << endl
<< "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
system("clear"); // Use "clear" command instead of "cls"
cout << "Add: " << endl;
veh.at(i).addVehicle();
break;
case 2:
system("clear"); // Use "clear" command instead of "cls"
totalVehicles();
break;
case 3:
system("clear"); // Use "clear" command instead of "cls"
cout << "Departure: " << endl;
veh.at(i).deleteVehicle();
break;
case 4:
system("clear"); // Use "clear" command instead of "cls"
totalAmount();
break;
case 5:
system("clear"); // Use "clear" command instead of "cls"
veh.at(i).show();
break;
case 6:
exit(0);
}
cout << "\nDo you want to continue? (y/n): " << endl;
cin >> ans;
if (ans == 'n')
{
break;
}
else
{
continue;
}
} while (1);
return 0;
}