I need to create a C++ program that recreates a car dealership, with 3 different
ID: 3744733 • Letter: I
Question
I need to create a C++ program that recreates a car dealership, with 3 different classes: vehicle, showroom, and dealership. I already have main.cpp, I just need the 3 classes.
The vehicle class will need to store data including the make of the vehicle, the model of the vehicle, the year, the price, and mileage. It needs to have these constructors and the functions defined. If dynamic memory is used, there should also be a destructor to clean up any memory allocated by the class.
Here's main.cpp:
#include "Vehicle.h"
#include "Showroom.h"
#include "Dealership.h"
#include <iostream>
#include <limits>
#include <memory>
#include <iomanip>
using namespace std;
void TestOne(Vehicle *v);
void TestTwo(Vehicle *v);
int main()
{
// Initialize some data. It's hard-coded here, but this data could come from a file, database, etc
Vehicle vehicles[] =
{
Vehicle("Ford", "Mustang", 1973, 9500, 113000),
Vehicle("Mazda", "CX-5", 2017, 24150, 5900),
Vehicle("Dodge", "Charger", 2016, 18955, 9018),
Vehicle("Telsa", "Model S", 2018, 74500, 31),
Vehicle("Toyota", "Prius", 2015, 17819, 22987),
Vehicle("Nissan", "Leaf", 2016, 12999, 16889),
Vehicle("Chevrolet", "Volt", 2015, 16994, 12558),
};
int testNum;
cin >> testNum;
if (testNum == 1)
TestOne(vehicles);
else if (testNum == 2)
TestTwo(vehicles);
return 0;
}
void TestOne(Vehicle *vehicles)
{
// Showrooms to store the vehicles
Showroom showroom("Primary Showroom", 3);
showroom.AddVehicle(&vehicles[0]);
showroom.AddVehicle(&vehicles[1]);
//showroom.AddVehicle(&vehicles[2]);
Showroom secondary("Fuel-Efficient Showroom", 4);
secondary.AddVehicle(&vehicles[3]);
secondary.AddVehicle(&vehicles[4]);
secondary.AddVehicle(&vehicles[5]);
secondary.AddVehicle(&vehicles[6]);
// A "parent" object to store the Showrooms
Dealership dealership("COP3503 Vehicle Emporium", 2);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.ShowInventory();
}
void TestTwo(Vehicle *vehicles)
{
// Showrooms to store the vehicles
Showroom showroom("Primary Showroom", 3);
showroom.AddVehicle(&vehicles[0]);
showroom.AddVehicle(&vehicles[1]);
Showroom secondary("Fuel-Efficient Showroom", 4);
secondary.AddVehicle(&vehicles[4]);
secondary.AddVehicle(&vehicles[5]);
Showroom third("Fuel-Efficient Showroom", 4);
third.AddVehicle(&vehicles[3]);
// A "parent" object to store the Showrooms
Dealership dealership("COP3503 Vehicle Emporium", 3);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.AddShowroom(&third);
cout << "Using just the GetAveragePrice() function ";
cout << "Average price of the cars in the dealership: $" << std::fixed << std::setprecision(2);
cout << dealership.GetAveragePrice();
}
Explanation / Answer
Vehicle.h
#ifndef VEHCILE_H
#define VEHCILE_H
#include <string>
using namespace std;
class Vehicle
{
private:
string Make;
string Model;
int Year;
float Price;
int Mileage;
public:
Vehicle();
Vehicle(string Make1,string Model1,int Year1,float Price1,int Mileage1);
void Display() const;
string GetYearMakeModel() const;
float GetPrice() const;
};
#endif
Vehicle.cpp
#include "Vehicle.h"
#include <iostream>
#include <string>
using namespace std;
Vehicle :: Vehicle() // default constructor
{
Make="";
Model="";
Year=0;
Price=0;
Mileage=0;
}
Vehicle :: Vehicle(string Make1,string Model1,int Year1,float Price1,int Mileage1) // parametric constructor
{
Make=Make1;
Model=Model1;
Year=Year1;
Price=Price1;
Mileage=Mileage1;
}
void Vehicle :: Display() const
{
cout<<Make<<" "<<Model<<" "<<Year<<" "<<Price<<" "<<Mileage<<endl;
}
string Vehicle :: GetYearMakeModel() const
{
string year = to_string(Year);
return year + Make + Model;
}
float Vehicle :: GetPrice() const
{
return Price;
}
Showroom.h
#ifndef SHOWROOM_H
#define SHOWROOM_H
#include "Vehicle.h"
#include <string>
using namespace std;
class Showroom
{
private:
int maxSize; // the max size of vehicles
int currentSize; // the current number of vehicles
Vehicle* vehiclelist;
string Name;
public:
Showroom();
Showroom(string n,int cur);
Showroom(const Showroom &t);
~Showroom();
Showroom& operator=(const Showroom &t);
void AddVehicle(const Vehicle *v);
void ShowInventory() const;
const Vehicle *GetVehicleList() const;
unsigned int GetCapacity() const;
unsigned int GetCount() const;
const char * GetName() const;
};
#endif
Showroom.cpp
#include "Showroom.h"
#include "Vehicle.h"
#include <iostream>
#include <string>
using namespace std;
Showroom :: Showroom()
{
maxSize = 20; // change 20 to increase the maximum no of vehicles
currentSize = 0;
vehiclelist = new Vehicle[maxSize];
Name = "";
}
Showroom :: Showroom(string n,int cur)
{
maxSize = 20; // change 20 to increase the maximum no of vehicles
currentSize = cur;
vehiclelist = new Vehicle[maxSize];
Name = n;
}
Showroom :: Showroom(const Showroom &t)
{
maxSize = t.maxSize;
currentSize =t.currentSize;
vehiclelist =t.vehiclelist;
Name = t.Name;
}
Showroom :: ~Showroom()
{
delete [] vehiclelist;
}
Showroom& Showroom ::operator=(const Showroom &t)
{
}
void Showroom :: AddVehicle(const Vehicle *v)
{
vehiclelist[currentSize++]=v;
}
void Showroom :: ShowInventory() const
{
if (currentSize == 0)
{
cout << " Current Inventory is empty. ";
return;
}
for (int i = 0; i < currentSize; i++) // For each entry,
vehiclelist[i].Display(); // send it to output
}
const Vehicle Showroom :: *GetVehicleList() const
{
if (currentSize == 0)
{
cout << " Current Showroom is empty. ";
return nullptr;
}
else
{
return vehiclelist;
}
}
unsigned int Showroom :: GetCapacity() const
{
return maxSize;
}
unsigned int Showroom :: GetCount() const
{
return currentSize;
}
const char Showroom :: * GetName() const
{
return Name;
};
Dealership.h
#ifndef DEALERSHIP_H
#define DEALERSHIP_H
#include "Showroom.h"
#include "Vehicle.h"
#include <string>
using namespace std;
class Dealership
{
private:
int maxSize; // the max size of vehicles
int currentSize; // the current number of vehicles
Showroom* showroomlist;
string Name;
public:
Dealership(string n,int cur);
Dealership(const Dealership &t);
~Dealership();
Dealership& operator=(const Dealership &t);
unsigned int GetCapacity() const;
unsigned int GetCount() const;
const char * GetName() const;
void AddShowroom(const Showroom *v);
void ShowInventory() const;
void GetAveragePrice() const;
};
#endif
Dealership.cpp
#include "Vehicle.h"
#include "Showroom.h"
#include "Dealership.h"
#include <iostream>
#include <string>
using namespace std;
Dealership :: Dealership(string n,int cur)
{
maxSize = 20; // change 20 to increase the maximum no of vehicles
currentSize = cur;
showroomlist = new Showroom[maxSize];
Name = n;
}
Dealership :: Dealership(const Dealership &t)
{
maxSize = t.maxSize;
currentSize =t.currentSize;
showroomlist =t.showroomlist;
Name = t.Name;
}
Dealership :: ~Dealership()
{
delete [] showroomlist;
}
Dealership& Dealership :: operator=(const Dealership &t)
{
}
void Dealership :: AddShowroom(const Showroom *v)
{
showroomlist[currentSize++]=v;
}
void Dealership :: ShowInventory() const
{
if (currentSize == 0)
{
cout << " Current Dealership is empty. ";
return;
}
for (int i = 0; i < currentSize; i++) // For each entry,
showroomlist[i].ShowInventory();
}
unsigned int Dealership :: GetCapacity() const
{
return maxSize;
}
unsigned int Dealership :: GetCount() const
{
return currentSize;
}
const char Dealership :: * GetName()
{
return Name;
}
void GetAveragePrice() const
{
Vehicle* vehicle;
float price=0;
int i,j,n=0;
if (currentSize == 0)
{
cout << " Current Dealership is empty. ";
return 0;
}
for (i = 0; i < currentSize; i++) // For each entry,
{
vehicle = showroomlist[i].GetVehicleList();
for(j=0; j< showroomlist[i].GetCount(); j++)
{
price+= vehicle[j].GetPrice();
++n;
}
}
return price/n;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.