Car Rental System This system tracks car records in a rental company. The car pl
ID: 662690 • Letter: C
Question
Car Rental System This system tracks car records in a rental company.
The car plate number is used as identification of the car and mobile number as identification of the customer.
program also tracks all renting transactions with customers.
program should use files to save the data permanently.
program should have the following menu:
1- Add new car When user selects 1, system asks for the plate number, car type (small, 4 wheel), manufacturer, model year.
2- Search available car by type When user selects 2, system asks for car type (s for small , w for 4 wheel), then system displays all cars available of that type (car number, model, manufacturer).
3- Search by year When user selects 3, system asks for model year , then system displays all cars available of that model year (car number, type, manufacturer).
4- Rent a car When user selects 4, system asks for car number, if the car is currently rented, system displays a message says
Explanation / Answer
//Car Rental tracking system
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
struct car //car structure
{
long int platenumber;
string cartype;
string manufacturer;
int modelyear;
};
struct customer //customer data structure
{
long int phone;
string name;
string rentingdate;
};
void saveCar(struct car c) //To save new car method
{
ofstream carfile;
carfile.open("cars.dat","w"); //open file for writing
carfile<<c.platenumber<<" "<<c.cartype<<" "<<c.manufacturer<<" "<<c.modelyear<<endl; //storing data with <<(insertion) operator to file stream
carfile.close();//close the file
}
car inputCar() //Taking car data from keyboard
{
car c;
cout<<"Enter plate number ";
cin>>c.platenumber;
cout<<"Enter car type(small, 4-wheeler) ";
cin>>c.cartype;
cout<<"Enter Manufacturer ";
cin>>c.manufacturer;
cout<<"Enter Model year ";
cin>>c.modelyear;
return c; //return input data
}
void searchCarType(char cartype) //Search car by type
{
car c;
ifstream carfile;
carfile.open("car.dat","r"); //open file for reading
if(cartype=='s') //if car type small
{
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
while(!carfile.eof())
{
if(strcmp(c.cartype,"small")==0) //print only cartype "small"
cout<<c.platenumber<<" "<<c.manufacturer<<" "<<c.modelyear<<endl;
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
}
}
else if(cartype=='w')//otherwise
{
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
while(!carfile.eof())
{
if(strcmp(c.cartype,"4-wheeler")==0) //print only cartype "4-wheel"
cout<<c.platenumber<<" "<<c.manufacturer<<" "<<c.modelyear<<endl;
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
}
}
carfile.close(); //close the file
}
void searchCarYear(int year) //searching car by manufacturing year
{
car c;
ifstream carfile;
carfile.open("car.dat","r"); //reading mode file
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear; //read file car.dat
while(!carfile.eof())
{
if(year==c.modelyear) //if model year found
cout<<c.platenumber<<" "<<c.manufacturer<<" "<<c.modelyear<<endl;
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
}
carfile.close(); //close file after searching
}
bool searchCarnumber(long int platenumber) //search by number
{
bool flag=false;
car c;
ifstream carfile;
carfile.open("car.dat","r"); //read mode file
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
while(!carfile.eof()) //until end of file
{
if(platenumber==c.platenumber) //if number plate matched
flag=true;
carfile>>c.platenumber>>c.cartype>>c.manufacturer>>c.modelyear;
}
carfile.close(); //close file
return flag; //return true or false
}
void saveCustomer(struct customer cust,long int platenumber) //saving customer data
{
ofstream custfile;
ifstream carfile;
custfile.open("customer.dat","w"); //writing file customer
carfile.open("car.dat","r"); //reading file car
if(searchCarnumber(platenumber)) //if plate number found
custfile<<platenumber<<" "<<cust.phone<<" "<<cust.name<<" "<<cust.rentingdate<<endl;
else
cout<<"No!such car number is found"; //otherwise error
carfile.close(); // close files
custfile.close();
}
bool checkRental(long int platenumber) //rental availability
{
bool flag=false;
long int tplate;
customer cust;
ifstream custfile;
custfile.open("cust.dat","r"); //reading file open
custfile>>tplatenumber>>cust.phone>>cust.name>>cust.rentingdate;
while(!custfile.eof()) //until end of file
{
if(tplatenumber==platenumber) //if number plate matched
flag=true;
custfile>>tplatenumber>>cust.phone>>cust.name>>cust.rentingdate;
}
custfile.close();
return flag;
}
void returnCar(struct customer cust,long int platenumber) //car return after usage
{
if(checkRental(platenumber)) //invalid car number
cout<<" Sorry! that car is not available";
else
saveCustomer(cust,platenumber); //equal car number
}
int main()
{
/// menu code here
///it is big solution to be coded
///lack of time i have been sending above function this might be very much useful
}