(a) Write a program to keep track of cars on a small car lot. This program will
ID: 3533042 • Letter: #
Question
(a) Write a program to keep track of cars on a small car lot.
This program will use a
class called Car with the following data
members:
make (Honda), model (Civic), year (of manufacture),
color
(exterior), (number of) doors and (suggested retail)
price
The class will need a default constructor and destructor,
a display method
(to display vehicle data in an appropriate
format) and
accessor and mutator methods for each data member.
The methods should be public access; the data members should be
private access. You will need an array of Car objects, call itcars;
allow 20 elements.
(b) You will also need methods outside the class:db_read()
(parameters: cars, ifstream reference to a file;
returns the number of vehicles read into cars) which will read
data about the cars on the lot into the cars database. Remember
to read through the newlines at the end of each line, otherwise
you may not get the EOF markersearch()
(parameters: cars, the number of cars on the lot, the
make and model of the car to be found; returns the element number
of the car requested or -1, if not found) which is a
binary
search
routine on make.
(c) The main routine will declare variables (including the array
of Car), open the database file (call it:
cardata.txt - cars
should be in alphabetic order by make; remember to check for a
good open), call db_read() to read the data into cars, then
prompt the user for the make and model they wish information
about; then it will call search(); if the vehicle is found,
display is called, if not an error message is displayed and the
user prompted as to whether they want to search for another car.
(d) The methods for the Car class will all be quite simple and
you will use the
get methods (accessors) in display() and the set
methods (mutators) in db_read(). You will also use get_make() and
get_model() in search().
Note: in the get and set routines avoid
using the same names for your parameters as for the data values
of the class: if you use
make for the data member, then use
something like
mke for the parameter - using the same name for
these can confuse the compiler.
(e) One fine point: the models will not be in alphabetic order.
So, once search() has found a car with the proper make, simply
check to see if it is the right model. This being a small car lot
we will assume that there is no more than one model of any make.
Obviously, this is not realistic, but we don't need any more
complications at this point.
(f) The data in the carsdata.txt file should have the data about
one vehicle, separated by spaces, on each line.
(g) Don't forget your
documentation.
(h) The
progra m will be due on Monday, April 2 2 and should
include theDev-C++ project file and the .cpp's and .h files -
the simple way is to compress the project directory and upload
it
Explanation / Answer
// car.h
//----------------------------------------------------------
#ifndef CAR_H
#define CAR_H
#include<fstream>
using namespace std;
class Car
{
private:
char make[20];
char model[20];
char year[20];
char color[20];
int doors;
double price;
public:
Car();
char* getMake();
char* getModel();
char* getYear();
char* getColor();
int getDoors();
double getPrice();
void setMake(char* m);
void setModel(char* m);
void setYear(char* y);
void setColor(char* c);
void setDoors(int d);
void setPrice(double p);
};
int db_read(Car c[],ifstream& in);
void display(Car c[],int n);
int search(Car c[],int n,char* make,char* model);
#endif
// car.cpp
//-----------------------------------------------------------------------------
#include"car.h"
#include<iostream>
Car::Car()
{
strcpy(make,"");
strcpy(model,"");
strcpy(year,"");
strcpy(color,"");
doors=0;
price=0;
}
char* Car::getMake()
{
return make;
}
char* Car::getModel()
{
return model;
}
char* Car::getYear()
{
return year;
}
char* Car::getColor()
{
return color ;
}
int Car::getDoors()
{
return doors;
}
double Car::getPrice()
{
return price;
}
void Car::setMake(char* m)
{
strcpy(make,m);
}
void Car::setModel(char* m)
{
strcpy(model,m);
}
void Car::setYear(char* y)
{
strcpy(year,y);
}
void Car::setColor(char* c)
{
strcpy(color,c);
}
void Car::setDoors(int d)
{
doors=d;
}
void Car::setPrice(double p)
{
price=p;
}
int db_read(Car c[],ifstream& in)
{
char make[20];
char model[20];
char year[20];
char color[20];
int doors;
double price;
int i=0;
if(!in)
{
cout<<"cnnot open file";
exit(0);
}
else
{
while(!in.eof())
{
in>>make;
in>>model;
in>>year;
in>>color;
in>>doors;
in>>price;
c[i].setMake(make);
c[i].setModel(model);
c[i].setColor(color);
c[i].setYear(year);
c[i].setDoors(doors);
c[i].setPrice(price);
i++;
if(in.eof())
break;
}
}
return i-1;
}
void display(Car c[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"Make: "<<c[i].getMake()<<endl;
cout<<"Model: "<<c[i].getModel()<<endl;
cout<<"Color: "<<c[i].getColor()<<endl;
cout<<"Year: "<<c[i].getYear()<<endl;
cout<<"Doors: "<<c[i].getDoors()<<endl;
cout<<"Price: "<<c[i].getPrice()<<endl;
cout<<endl;
}
}
int search(Car c[],int n,char* make,char* model)
{
int found=-1;
for(int i=0;i<n;i++)
{
if((strcmp(c[i].getMake(),make)==0)&&(strcmp(c[i].getModel(),model)==0))
found=i;
}
return found;
}
// main.cpp
//---------------------------------------------------------------------------
#include<iostream>
#include"car.h"
int main()
{
Car c[30];
char inputFile[30];
int noOfCars=0;
char make[20];
char model[20];
int found;
cout<<"enter the input file name: ";
cin>>inputFile;
ifstream in(inputFile);
noOfCars=db_read(c,in);
display(c,noOfCars);
cout<<"enter the car maufacturer that you want to search :-"<<endl;
cin>>make;
cout<<"enter the car model that you want to search :-"<<endl;
cin>>model;
found=search(c,noOfCars,make,model);
if(found!=-1)
cout<<" car has been found at "<<found+1<<" postion"<<endl;
else
cout<<"car not found"<<endl;
cin>>found;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.