0% found this document useful (0 votes)
7 views6 pages

New Text Document

The document defines classes and functions for a vehicle parking system. It includes structs for storing time and date data, a Vehicle class with methods for adding, deleting, and displaying vehicle data, and a main function with a menu driven system for interacting with the parking system by adding, viewing, removing vehicles and collecting payment amounts. Vehicle data is stored in a vector and written to a sequential file.

Uploaded by

Speed Up
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

New Text Document

The document defines classes and functions for a vehicle parking system. It includes structs for storing time and date data, a Vehicle class with methods for adding, deleting, and displaying vehicle data, and a main function with a menu driven system for interacting with the parking system by adding, viewing, removing vehicles and collecting payment amounts. Vehicle data is stored in a vector and written to a sequential file.

Uploaded by

Speed Up
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream>

#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;
};

int hashFunction(const string& key, int size)


{
int sum = 0;
for (int i = 0; i < key.length(); i++)
{
char ch = key[i];
sum += ch;
}
return sum % size;
}

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++;
}

cout << "\nVehicle added successfully" << endl;


}

int computeTimeDifference(Time t1, Time t2)


{
int sec1, sec2, totalsec;
Time t3;
sec1 = t1.hh * 60 * 60 + t1.mm * 60 + t1.ss;
sec2 = t2.hh * 60 * 60 + t2.mm * 60 + t2.ss;

totalsec = sec2 - sec1;

t3.mm = totalsec / 60;


t3.hh = t3.mm / 60;
t3.mm = t3.mm % 60;
t3.ss = totalsec % 60;

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;

for (int j = 0; j <= i; j++)


{
if ((veh.at(j).pltno == pno) && (veh.at(j).type == typ))
{
veh.at(j).departure.hh = depart.hh;
veh.at(j).departure.mm = depart.mm;
veh.at(j).departure.ss = depart.ss;

time_diff = computeTimeDifference(veh.at(j).arrive, depart);

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;
}

You might also like