you will be creating a program to manage a car dealership. This will again be a
ID: 653748 • Letter: Y
Question
you will be creating a program to manage a car dealership. This will again be a menu driven system. The following is your menu:
Display Inventory
Add a vehicle
Update a vehicle
Delete a vehicle
Sort inventory by VIN
Search inventory by Model
Read inventory from file
Write inventory to file and exit
Your program will be class based with the following UML representing the class:
Vehicle
- VIN:string
- make:string
- model:string
- year:int
- price:double
+ Vehicle(iVIN:string, iMake:string, iModel:string, iYear:int, iPrice:double)
+ Vehicle( )
+ getVIN( ):string
+ getMake( ):string
+ getModel( ):string
+ getYear( ):int
+ getPrice( ):double
+ setVIN(input:string):void
+ setMake(input:string):void
+ setModel(input:string):void
+ setYear(input:int):void
+ setPrice(input:double):void
You will have four files for your program (I want these file names!): main.cpp, functions.h, vehicle.h, vehicle.cpp.
main.cpp: this will be your driver file.
functions.h: this will contain your global functions for each of the menu items (display, addVehicle, deleteVehicle, updateVehicle, etc).
vehicle.h: this will contain the class declaration.
vehicle.cpp: this will contain the class implementation.
You will be storing your vehicle objects in a vector. In the main function, you will create a vector of vehicles with an initial size of zero (0). When the menu options display, delete, edit, sort, or search are called, you will have to check your vector and ensure that it has a vehicle in it before the function continues (error checking).
You will not have any global variables.
Each menu item will have a corresponding function, and the definition of the function will be found in the file functions.h. Each function will only have one parameter, the vector. All I/O will be done in the functions and not in main (expect asking for what menu option the user wants).
The following are the details for each of your menu options:
You will display each vehicle, using the following format:
Vehicle: #
VIN: VVVV
Make: MMMM
Model: MMMM
Year: YYYY
Price: $D.CC
The bold face letters represent the values for the individual vehicle, and it doesnt have to be bolded. Notice the tab stop between the label and the value. Also notice that there is a dollar sign and decimal for the price. Lastly, for Vehicle: #, the pound sign represents the vehicle position number in the vector (starting at 1 not 0), which will be 1, 2, 3 and so on.
2. You will ask the user for all of the information to add the vehicle and then you will add it to the vector.
3. You will display the vehicles and then ask the user which vehicle to edit; they will give you the index number (starting at 1). Check to ensure they gave you a valid entry, then prompt for which item they want to edit (year, make, model, price, or VIN). Once they make their selection prompt for the new information, then return to the main menu.
4. You will display the vehicles and then ask the user which vehicle to delete: they will give you the index number (starting at 1). You will then check to ensure they gave you a valid entry and then remove that vehicle from the vector.
5. You will sort the vector by VIN number (when sort is done, the vector in main will be sorted by VIN number).
6. You will ask the user for a make, then search the vector for that vehicle. You will return the first matching entrys index number or an appropriate message if not found.
7. You will read the inventory from a file called inventory.dat, which is NOT provided for you (you will need to make your own to start with). The data is stored in the same order as listed above and it is newline delimited.
8. You will write the entire inventory out to a file called inventory.dat and then exit the program.
Explanation / Answer
vehicle.h
#include<string>
using namespace std;
class Vehicle{
private:
string VIN;
string make;
string model;
int year;
double price;
public:
Vehicle(string,string,string,int,double);
Vehicle();
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
void setVIN(string);
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);
};
vehicle.cpp
#include "vehicle.h"
Vehicle::Vehicle(string iVIN,string iMake,string iModel,int iYear,double iPrice){
VIN=iVIN;
make=iMake;
model=iModel;
year=iYear;
price=iPrice;
}
Vehicle::Vehicle(){
VIN="";
make="";
model="";
year=0;
price=0.0;
}
string Vehicle::getVIN(){
return VIN;
}
string Vehicle::getMake(){
return make;
}
string Vehicle::getModel(){
return model;
}
int Vehicle::getYear(){
return year;
}
double Vehicle::getPrice(){
return price;
}
void Vehicle::setVIN(string input){
VIN=input;
}
void Vehicle::setMake(string input){
make=input;
}
void Vehicle::setModel(string input){
model=input;
}
void Vehicle::setYear(int input){
year=input;
}
void Vehicle::setPrice(double input){
price=input;
}
main.cpp
#include<iostream>
#include "vehicle.cpp"
#include "functions.h"
#include<vector>
#include<cstdlib>
#include<conio.h>
using namespace std;
int main(){
vector<Vehicle> v;
int ch=0;
do{
system("cls");
cout<<"1.Display Inventory";
cout<<" 2.Add a vehicle";
cout<<" 3.Update a vehicle";
cout<<" 4.Delete a vehicle";
cout<<" 5.Sort inventory by VIN";
cout<<" 6.Search inventory by Model";
cout<<" 7.Read inventory from file";
cout<<" 8.Write inventory to file and exit";
cout<<" Enter choice: ";
cin>>ch;
switch(ch){
case 1:
displayInventory(v);
getch();
break;
case 2:
addVehicle(v);
getch();
break;
case 3:
updateVehicle(v);
getch();
break;
case 4:
deleteVehicle(v);
getch();
break;
case 5:
sortInventory(v);
getch();
break;
case 6:
searchInventory(v);
getch();
break;
case 7:
readFile(v);
getch();
break;
case 8:
writeFile(v);
getch();
break;
}
}while(ch!=8);
}
functions.h
#include<vector>
void displayInventory(vector<Vehicle> v){
for(int i=0;i<v.size();i++){
cout<<" Inventory "<<i+1;
cout<<" VIN: "<<v[i].getVIN();
cout<<" Make: "<<v[i].getMake();
cout<<" Model: "<<v[i].getModel();
cout<<" Year: "<<v[i].getYear();
cout<<" Price:"<<v[i].getPrice();
}
}
void addVehicle(vector<Vehicle> v){
string VIN,make,model;
int year;
double price;
Vehicle v1;
cout<<"Enter VIN:";
cin>>VIN;
v1.setVIN(VIN);
cout<<"Enter make:";
cin>>make;
v1.setMake(make);
cout<<"Enter model:";
cin>>model;
v1.setModel(model);
cout<<"Enter year:";
cin>>year;
v1.setYear(year);
cout<<"Enter Price:";
cin>>price;
v1.setPrice(price);
v.push_back(v1);
}
void updateVehicle(vector<Vehicle> v){
int num,ch,inp;
double pr;
string input;
cout<<"Enter inventory no:";
cin>>num;
cout<<" 1.Update VIN 2.Update Make 3.Update Model 4.Update year 5.Update price Enter choice:";
cin>>ch;
switch(ch){
case 1:
cout<<" Enter new VIN:";
cin>>input;
v[num].setVIN(input);
break;
case 2:
cout<<" Enter new Make:";
cin>>input;
v[num].setMake(input);
break;
case 3:
cout<<" Enter new Model:";
cin>>input;
v[num].setModel(input);
break;
case 4:
cout<<" Enter new year:";
cin>>inp;
v[num].setYear(inp);
break;
case 5:
cout<<" Enter new price:";
cin>>pr;
v[num].setPrice(pr);
break;
}
}
void deleteVehicle(vector<Vehicle> v){
int num;
cout<<"Enter inventory number to delete:";
cin>>num;
v.erase(num-1);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.