C++ PROGRAM Design an OOP program that can be used to create, update and read a
ID: 3682583 • Letter: C
Question
C++ PROGRAM
Design an OOP program that can be used to create, update and read a file, which contains car and service records/data.
Your program should create and use a Car class to store and process the car's data, such as the car model, license plate number and year when the car was made. The program should also use a Service class to store and process the service's data, such as the service description, number of months between two services, date of the last service and the date of the next service. The program should get all necessary data from the user except the date of the next service which will be calculated. The Service class should contain a function that will compute the date of the next service using the date of the last service and the number of months between two services. Please note that you may design and use additional class(s) as well.
The program should enable the user to: -
- Create a file and store car and service records/data into the file
- Search for a specific car in the file using its licence plate number and display all its service records
- Search for a specific date and display all cars that have to be serviced on this date:
- Delete and/or edit car and service records in the file.
- Program requirements:
It is required to use the following C++ language constructs/tools:
1. File I/O
2. Inheritance or composition
3. Operator functions (at least two)
4. A static member function which displays the current system date and time
5. Constructor functions.
Explanation / Answer
Car.h
#include <string>
#include "Service.h"
using namespace std;
class Car
{
public:
Car();
Car(string m,string num,int year);
~Car();
string getModel(){return model;}
string getLicenceNumber(){return licenceNumber;}
int getMakeYear(){return makeYear;}
Service* getServiceInfo(){return serviceInfo;}
void setModel(const string& carModel ){model=carModel;}
void setLicenceNumber(const string& licNum){licenceNumber=licNum;}
void setMakeYear(int year){makeYear=year;}
void setServiceInfo(Service* s){serviceInfo=s;}
private:
string model;
string licenceNumber;
int makeYear;
Service* serviceInfo;
};
Car::Car()
{
}
Car::Car(string mod,string licNum,int mYear)
{
model=mod;
licenceNumber=licNum;
makeYear=mYear;
}
Car::~Car()
{
delete serviceInfo;
serviceInfo=nullptr;
}
Service.h
#include <string>
#include "Date.h"
using namespace std;
class Service
{
public:
Service();
Service(string desc,int months,Date* sDate);
~Service();
void nextService();
string getServiceDesc(){return serviceDesc;}
int getServiceMonths(){return serviceMonths;}
Date* getServiceDate(){return serviceDate;}
Date* getNextServiceDate(){return nextServiceDate;}
void setServiceDesc(const string& desc){serviceDesc=desc;}
void setServiceMonths(int m){serviceMonths=m;}
void setServiceDate(Date* date){serviceDate=date;}
private:
string serviceDesc;
int serviceMonths;
Date* serviceDate;
Date* nextServiceDate;
};
Service::Service()
{
}
Service::Service(string desc,int months,Date* sDate)
{
serviceDesc=desc;
serviceMonths=months;
serviceDate=sDate;
nextService();
}
Service::~Service()
{
delete serviceDate;
serviceDate=nullptr;
delete nextServiceDate;
nextServiceDate=nullptr;
}
void Service::nextService()
{
nextServiceDate=serviceDate->addMonthsToDate(serviceMonths);
}
Date.h
#include <iostream>
using namespace std;
class Date
{
public:
Date();
Date(int d,int m,int y);
int getDay(){return day;}
int getMonth(){return month;}
int getYear(){return year;}
void setDay(int d){day=d;}
void setMonth(int m){month=m;}
void setYear(int y){year=y;}
Date* addMonthsToDate(int months);
void print();
private:
int day;
int month;
int year;
};
Date::Date()
{
}
Date::Date(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
Date* Date::addMonthsToDate(int months)
{
Date* newDate;
int years=months/12;
newDate->year=this->year+years;
int monthsToAdd=months-12*years;
newDate->month=this->month+monthsToAdd;
return newDate;
}
void Date::print()
{
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
CarService.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Car.h"
using namespace std;
void addRecord();
void viewRecord();
void displayToBeServicedCars();
int main()
{
int choice=0;
while(true)
{
cout<<"1)Add Car Record"<<endl;
cout<<"2)View Record"<<endl;
cout<<"3)Display all cars which are to be serviced on input date"<<endl;
cout<<"4)Quit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
addRecord();
break;
case 2:
viewRecord();
break;
case 3:
displayToBeServicedCars();
break;
case 4:
return -1;
default:
cout<<"Invalid choice"<<endl;
break;
}
}
}
void addRecord()
{
string model,licNum;
int makeYear;
cout<<"Enter the car details:"<<endl;
cout<<"Enter the Car Model"<<endl;
cin>>model;
cout<<"Enter the licence number"<<endl;
cin>>licNum;
cout<<"Enter the make year"<<endl;
cin>>makeYear;
string serviceDesc;
int numMonths,day,month,year;
cout<<"Enter the car service detail"<<endl;
cout<<"enter the service description"<<endl;
getline(cin,serviceDesc);
cout<<"enter the number of months between two sevices"<<endl;
cin>>numMonths;
cout<<"enter the day,month,year of last service"<<endl;
cin>>day>>month>>year;
ofstream out("CarRecords.dat");
out<<model<<" "<<licNum<<" "<<makeYear<<" "<<serviceDesc<<" "<<numMonths<<" "<<day<<"/"<<month<<"/"<<year<<endl;
}
void viewRecord()
{
string licNum;
cout<<"enter the licence number of the car"<<endl;
cin>>licNum;
ifstream file("CarRecords.dat");
string line;
cout<<line<<endl;
while(std::getline(file, line))
{
stringstream linestream(line);
string model,carLicNum,serviceDesc,serviceDate;
int makeYear,serviceMonths,day,month,year;
// Read the integers using the operator >>
linestream >> model >> carLicNum>>makeYear>>serviceDesc>>serviceMonths>>serviceDate;
stringstream date(serviceDate);
date>>day>>month>>year;
Date* sdate=new Date(day,month,year);
Service* ser=new Service();
ser->setServiceDesc(serviceDesc);
ser->setServiceDate(sdate);
ser->setServiceMonths(serviceMonths);
Car car(model,carLicNum,makeYear);
car.setServiceInfo(ser);
if(licNum==carLicNum)
{
car.printDetails();
break;
}
}
}
void displayToBeServicedCars()
{
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.