In: Computer Science
The program is to be called CarClub
The application will display menu as below
1. load cars
2. display all cars
3. search car
4 count cars older the 30 years
5 exit
Please enter your option:
Load car menu should read input data from text file name (car.txt)
Car class have 4 data members which are. Car make, car model, car year and car price
In addition
If both car year and price were not provided, year has a default value as 2020 and price has a default value as $20000
If only price was not provided, price should be set to a default value based on this table
2015<year<=2020 and price has a default value as $20000
2010<year<=2015 and price has a default value as $14000
1980<year<=2010 and price has a default value as $7000
2015<year<=2020 and price has a default value as $20000
year<=1980 and price has a default value as $40000
Using dynamic memory allocation to store Car Object
You should implement at least 1 custom excepetion and 1 file exception
You have to implement 1 operator overloading for operator "<<" to use as a print
WHY IS BUILD FAILED?????
#include
#include
using namespace std;
#include
class Ccar
{
private:
string make;
string model;
int year;
double price;
public:
Ccar(string="", string="", int=2020, double=20000);
~Ccar();
// setters
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);
// getters
string getMake();
string getModel();
int getYear();
double getPrice();
};
void Ccar:: setMake(string ma){
ma=make;
}
void Ccar :: setModel(string m){
m=model;
}
void Ccar :: setYear(int y){
y=year;
}
void Ccar :: setPrice(double p){
p=price;
}
// getters
string Ccar::getMake()
{
return make;
}
string Ccar::getModel()
{
return model;
}
int Ccar::getYear()
{
return year;
}
double Ccar::getPrice()
{
return price;
}
int main() {
int option, i, numberOffCars, year = 0;
string line, make, model, yearString, priceString;
double price;
bool loaded =false;
ifstream fin;
// file exception to be typed here
// num of cars counted
fin.open("cars.txt");
numberOffCars=0;
while (fin.good()) //while i have not reached the eof
{
getline(fin, line);
numberOffCars++;
}
fin.close();
//memory allocation
Ccar *carList = new Ccar[numberOffCars]; // Dynamic memory allocation
//menu
do{
cout <<"1. Load Cars"<
cout <<"2. Display all Cars"<
cout <<"3. Search Cars"<
cout <<"4. Count Cars older than 30 years"<
cout <<"5. Exit"<
cin>>option;
if (option <1 || option >5)
{
cout<<"error"<
else
{
switch (option)
{case 1: // load
loaded=true;
fin.open("car.txt");
i=0;
while (fin.good())
{
yearString="";
priceString="";
getline(fin, make,'$');
carList[i].setModel(model);
getline(fin, yearString,'$');
if (yearString!="")
year= stoi(yearString);
getline(fin, priceString,'$');
if (priceString!="")
price= stod(priceString);
if (yearString=="" && priceString =="")
{
carList[i].setYear(2020);
carList[i].setPrice(20000);
}
else if (priceString=="")
{
if (year>2015 && year<=2020)
carList[i].setPrice(20000);
else if (year>2010 && year<=2015)
carList[i].setPrice(14000);
else if (year>1980 && year<=2010)
carList[i].setPrice(7000);
else
carList[i].setPrice(400000);
}
i++; //next car
} //end of while
fin.close();
break;
case 2: // display
if (loaded)
{i=0;
while(i
{cout <<"carList[i]"<
i++; //next car
}
}
break;
case 3: // search
if (loaded)
{
cout << "Enter a car make: "<< endl;
cin>>make;
cout << "Enter a car model: "<< endl;
cin>>model;
// search for this make and model
}
else {
cout << "Load the cars first! "<< endl;
}
break;
case 4: // count the cars older than 30 years
break;
case 5: // exit
cout << "\n\n\t\t\t Thanks for using our aplication"<< endl;
default: // already handled by cust excep
cout << "Invalid option! Try again"<< endl;
break;
}}} // end of switch
while (option !=5); // 5 exit option
delete[] carList;
return 0;
}
Hey, i can see there are some mistakes in the program. It would be difficult for you to understand by mentioning one by one since there is no numbers for the code lines so I'll just type all the code :
include <iostream>
using namespace std;
#include<string>
#ifndef _CAR_
#define _CAR_
class Ccar
{
private:
string make;
string model;
int year;
double price;
public:
Ccar(string="", string="", int=2020, double=20000);
~Ccar();
// setters
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);
// getters
string getMake();
string getModel();
int getYear();
double getPrice();
friend ostream& operator<<(ostream& out, Ccar&
param);
};
#endif
#include "Ccar.h"
void Ccar:: setMake(string){
make=make;
}
void Ccar :: setModel(string){
model=model;
}
void Ccar :: setYear(int y){
y=year;
}
void Ccar :: setPrice(double p){
p=price;
}
// getters
string Ccar::getMake()
{
return make;
}
string Ccar::getModel()
{
return model;
}
int Ccar::getYear()
{
return year;
}
double Ccar::getPrice()
{
return price;
}
#include <exception>
using namespace std;
class myexception: public exception{
// exception implementation???
};
#include <fstream>
#include "Ccar.h"
int main() {
int option, i, numberOffCars, year = 0;
string line, make, model, yearString, priceString;
double price;
bool foundIt, loaded =false;
exception myex;
ifstream fin;
// file exception to be typed here
// num of cars counted
fin.open("car.txt");
numberOffCars=0;
while (fin.good()) //while i have not reached the eof
{
getline(fin, line);
numberOffCars++;
}
fin.close();
//memory allocation
try {
Ccar *carList = new Ccar[numberOffCars]; // Dynamic memory
allocation
//menu
do{
cout <<"1. Load Cars"<<endl;
cout <<"2. Display all Cars"<<endl;
cout <<"3. Search Cars"<<endl;
cout <<"4. Count Cars older than 30 years"<<endl;
cout <<"5. Exit"<<endl;
cin>>option;
try {
if (option <1 || option >5)
{
throw myex;
}
else
{
switch (option)
{case 1: // load
loaded=true;
fin.open("car.txt");
i=0;
while (fin.good())
{
yearString="";
priceString="";
getline(fin, make,'$');
carList[i].setModel(model);
getline(fin, yearString,'$');
if (yearString!="")
year= stoi(yearString);
getline(fin, priceString,'$');
if (priceString!="")
price= stod(priceString);
if (yearString=="" && priceString =="")
{
carList[i].setYear(2020);
carList[i].setPrice(20000);
}
else if (priceString=="")
{
if (year>2015 && year<=2020)
carList[i].setPrice(20000);
else if (year>2010 && year<=2015)
carList[i].setPrice(14000);
else if (year>1980 && year<=2010)
carList[i].setPrice(7000);
else
carList[i].setPrice(400000);
}
i++; //next car
} //end of while
fin.close();
break;
case 2: // display
if (loaded)
{i=0;
while(i <numberOffCars)
{cout <<carList[i]<<endl;
i++; //next car
}
}
break;
case 3: // search
if (loaded)
{
cout << "Enter a car make: "<< endl;
cin>>make;
cout << "Enter a car model: "<< endl;
cin>>model;
// search for this make and model
}
else {
cout << "Load the cars first! "<< endl;
}
break;
case 4: // count the cars older than 30 years
break;
case 5: // exit
cout << "\n\n\t\t\t Thanks for using our aplication"<<
endl;
default: // already handled by cust excep
cout << "Invalid option! Try again"<< endl;
break;
} // end of switch
} // end of try
} catch (exception& ex){
cout << ex.what() << endl;
}
}
while (option !=5); // 5 exit option
delete[] carList;
}
catch (bad_alloc&)
{
cout << "Error in allocating memory" <<endl;
}
return 0;
}
Hey, hope this helps u,if so give a thumbs up, it means a lot thanks:) In case of any problem, write a comment I'll sure be helping you. Have a good day:)