For Programming Assignment 3 you will be creating a program

For Programming Assignment 3 you will be creating a program to manage cars in a 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 classes: Dealer - DealerName: string - DealerAddress: string + setName( input:string): void + setAddress(input:string ): void + getName( ): string + getAddress( ): string + Dealer ( ); + Dealer ( iName:string) ; Vehicle - VIN:string - make:string - model:string - year:int - price:double + DealerPtr: Dealer * //Note this should be in the public area + 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 + friend operator << (out: ostream &, Vehicle: car 1):ostream & //Note: Don\'t forget to create a new Dealer if either the Vehicle constructor or default constructor is called. You will have four files for your program (Use these file names!): main.cpp, functions.h, vehicle.h, vehicle.cpp. Place into a file folder named LastnamePA3, the zip the content and hand in a zip file! 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 declarations for vehicle and dealer. vehicle.cpp: this will contain the class implementations for vehicle and dealer. 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 Dealer Name: DDDDDD Dealer Address: AAAAAAAAAA The bold face letters represent the values for the individual vehicle, and it doesn’t 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 model, then search the vector for that vehicle. You will return the first matching entry’s index number or an appropriate message if not found. 7. You will read the inventory from a file called inventory.txt, 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 is newline delimited (like the example below). 34ABC321B BMW 328xi 2016 41315.00 Schomp BMW Littleton, CO 8. You will write the entire inventory out to a file called inventory.out (using the << operator) and then exit the program. Can someone help me on this assignment?

Solution

main.cpp

//============================================================================

// Name : InventoryVehicles.cpp

// Author : Kaju

// Version :

// Copyright : This is just an example code

// Description : Hello World in C++, Ansi-style

//============================================================================

//============================================================================
// Name : InventoryVehicles.cpp
// Author : Kaju
// Version :
// Copyright : This is just an example code
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <fstream>
#include \"functions.h\"
#include <vector>
#include <string>
using namespace std;

void menu(vector<Vehicle> &vehicle)
{
int choice;
cout<<\"1. Display Inventory\ 2. Add a Vehicle\ 3. Update a Vehicle\ 4. Delete a Vehicle\ 5. Sort inventory by VIN\";
cout<<\"\ 6. Search inventory by Model\ 7. Read Inventory from file\ 8.Write inventory to file\ \";
cout<<\"Enter your choice: \";
cin>>choice;
switch(choice)
{
case 1: display(vehicle);
break;
case 2: addVehicle(vehicle);
break;
case 3: updateVehicle(vehicle);
break;
case 4: deleteVehicle(vehicle);
break;
case 5: sortVehiclesByVIN(vehicle);
break;
case 6: searchVehicleByModel(vehicle);
break;
case 7: ReadFromFile();
break;
case 8: WriteToFile(vehicle);
break;
default: cout<<\"Invalid option: Select again !\ \";
menu(vehicle);
}
}

//display Inventory function
void display(vector<Vehicle> &vehiclevec)
{
vector<Vehicle>::iterator itr = vehiclevec.begin();
while(itr != vehiclevec.end())
{
cout<<*itr<<endl;
itr++;
}
}


//Add a vehicle
void addVehicle(vector<Vehicle> &vehicle)
{
Vehicle veh;
string vin, make, model, dealer_name, dealer_addr;
int year;
double price;
cout<<\"Enter Vehicle VIN: \";
cin>>vin;
cout<<\"Enter Vehicle Make: \";
cin>>make;
cout<<\"Enter Vehicle Model: \";
cin>>model;
cout<<\"Enter Vehicle Year: \";
cin>>year;
cout<<\"Enter Vehicle Price: \";
cin>>price;
cout<<\"Enter dealer name: \";
cin>>dealer_name;
cout<<\"Enter dealer address: \";
char s[100];
while (!cin.fail()) {
cin.getline(s, 11, \'\ \');
if (!cin.fail()) {
dealer_addr = s;
}
}
int index = vehicle.size()+1;
vin = to_string(index) + \" \" + vin;
veh.setVIN(vin);
veh.setMake(make);
veh.setModel(model);
veh.setYear(year);
veh.setPrice(price);
vehicle.push_back(veh);
veh.DealrPtr->setName(dealer_name);
veh.DealrPtr->setAddress(dealer_addr);
}
//Update vehicle information
void updateVehicle(vector<Vehicle> &vehicle)
{
display(vehicle);
string index;
cout<<\"Enter the index number to update: \";
cin>>index;
vector<Vehicle>::iterator itr = vehicle.begin();
while(itr != vehicle.end())
{
Vehicle veh= *(itr);
string indexString = veh.getVIN();
std::string token = indexString.substr(0, indexString.find(\" \"));

if(index == indexString)
{
int choice;
cout<<veh;
cout<<\"1. VIN\ 2. Make\ 3. Model\ 4. Year\ 5. Price\ \";
cout<<\"Enter the your choice to Update: \";
cin>>choice;
string vin,make, model;
int year, pos;
double price;
switch(choice)
{
case 1:
cout<<\"Enter the new VIN: \";
cin>>vin;
pos = indexString.find(\" \");
indexString.erase(pos + 1, indexString.length());
veh.setVIN(indexString + \" \" + vin);
break;
case 2:
cout<<\"Enter the new Make: \";
cin>>make;
veh.setMake(make);
break;
case 3:
cout<<\"Enter the new Model: \";
cin>>model;
veh.setModel(model);
break;
case 4:
cout<<\"Enter the new year: \";
cin>>year;
veh.setYear(year);
break;
case 5:
cout<<\"Enter the new price: \";
cin>>price;
veh.setPrice(price);
break;
default: cout<<\"Enter a valid number\ \";
updateVehicle(vehicle);

}
*(itr) = veh;
}
itr++;
}
}

//delete vehicle from vector
void deleteVehicle(vector<Vehicle> &vehicle)
{
display(vehicle);
int index;
cout<<\"Enter the index number to delete: \";
cin>>index;
vector<Vehicle>::iterator itr = vehicle.begin();
while(itr != vehicle.end())
{
Vehicle veh= *(itr);
string indexString = veh.getVIN();
std::string token = indexString.substr(0, indexString.find(\" \"));

if(to_string(index) == indexString)
{
vehicle.erase(std::remove(vehicle.begin(), vehicle.end(), index),vehicle.end());
break;
}
itr++;
}
}

//search vehicle information
void searchVehicleByModel(vector<Vehicle> &vehicle)
{
display(vehicle);
string model;
cout<<\"Enter the model to search: \";
cin>>model;
vector<Vehicle>::iterator itr = vehicle.begin();
while(itr != vehicle.end())
{
Vehicle veh= *itr;
string indexString = veh.getVIN();
std::string token = indexString.substr(0, indexString.find(\" \"));

if(model == veh.getModel())
{
cout<<\"The index Number is : \"<<token;
}
else
cout<<\"The model was not found\";
itr++;
}
}

//sorting algorithm for vehicles
bool sorter(const vector<Vehicle>& left, const vector<Vehicle>& right)
{
//go through each column
for(int i=0; i<left.size() && i<right.size(); i++) {
Vehicle leftSize = left[i];
Vehicle rightSide = right[i];
// if left is \"more\" return that we go higher
if( leftSize.getVIN() > rightSide.getVIN())
return true;
// if left is \"less\" return that we go lower
else if (leftSize.getVIN() < rightSide.getVIN())
return false;
}
// if left is longer, it goes higher
if (left.size() > right.size())
return true;
else //otherwise, left go lower
return false;
}
//sort vehicle information
void sortVehiclesByVIN(vector<Vehicle> &vehicle)
{
std::sort(vehicle.begin(), vehicle.end(), sorter);
display(vehicle);
}

//Reading from a file
void ReadFromFile()
{
string line;
ifstream myfile (\"inventory.txt\");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << \'\ \';
}
myfile.close();
}

else cout << \"Unable to open file\";
}

void WriteToFile(vector<Vehicle> &vehicle)
{
ofstream myfile (\"inventory.out\");
if (myfile.is_open())
{
vector<Vehicle>::iterator itr = vehicle.begin();
while(itr != vehicle.end())
{
Vehicle vehicle1 = *itr;
myfile << vehicle1;
itr++;
}
myfile.close();
}
else cout << \"Unable to open file\ \";
}

int main() {
vector<Vehicle> vehicles;
char continueCh;
do{
menu(vehicles);
cout<<\"Do you wish to continue? Y or N: \";
cin>>continueCh;
}while(continueCh == \'Y\' || continueCh == \'y\');
return 0;
}

functions.h

/*
* functions.h
*
* Created on: 30-Oct-2016
* Author: kaju
*/

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include \"Vehicle.h\"
#include <vector>
using namespace std;
//function to add vehicle
void addVehicle(vector<Vehicle> &vehicle);
//function for displaying menu
void display(vector<Vehicle> &vehicle);
//function for deleting vehicle
void deleteVehicle(vector<Vehicle> &vehicle);
//function for updating vehicle information
void updateVehicle(vector<Vehicle> &vehicle);
//function for sorting a vehicle
void sortVehiclesByVIN(vector<Vehicle> &vehicle);
//function for searchingVehicles
void searchVehicleByModel(vector<Vehicle> &vehicle);
// function for displaying the menu
void menu(vector<Vehicle> &vehicle);
void ReadFromFile();
void WriteToFile(vector<Vehicle> &vehicle);
#endif /* FUNCTIONS_H_ */

Vehicle.h

/*
* Vehicle.h
*
* Created on: 30-Oct-2016
* Author: kaju
*/

#ifndef VEHICLE_H_
#define VEHICLE_H_
#include <string>
using namespace std;
//Dealer class declaration
class Dealer
{
public:
string DealerName;
string DealerAddress;

//functions for the dealer class
Dealer(); // constructor
Dealer(string iName); //constructor with parameter
void setName(string name); // function for setting the name
void setAddress(string address); // function for setting the address
string getName(); // function for getting the name
string getAddress(); // function for getting the address

};

//Vehicle class declaration
class Vehicle
{
public:
string VIN;
string make;
string model;
int year;
double price;
Dealer *DealrPtr;

//function of vehicle class
Vehicle(); //constructor
Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice); //constructor with parameters
//get functions
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
//set functions
void setVIN(string iVIN);
void setMake(string iMake);
void setModel(string iModel);
void setYear(int iYear);
void setPrice(double iPrice);
//operator overloading- ostream
friend ostream & operator <<(ostream & out, Vehicle car1);
};


#endif /* VEHICLE_H_ */


Vehicle.cpp

/*
* Vehicle.cpp
*
* Created on: 30-Oct-2016
* Author: kaju
*/

#include \"Vehicle.h\"
#include <iostream>
// functions of dealer class

Dealer::Dealer()
{
DealerName = \"\";
DealerAddress = \"\";
}

Dealer::Dealer(string iName)
{
DealerName = iName;
}

string Dealer::getName()
{
return DealerName;
}

string Dealer::getAddress()
{
return DealerAddress;
}

void Dealer::setName(string name)
{
DealerName = name;
}

void Dealer::setAddress(string address)
{
DealerAddress = address;
}

//functions of vehicle class
Vehicle::Vehicle()
{
DealrPtr = new Dealer();
VIN = \"\";
make = \"\";
model = \"\";
year = 0;
price = 0.0;
}

Vehicle::Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice)
{
DealrPtr =new Dealer();
VIN = iVIN;
make = iMake;
model = iModel;
year = iYear;
price = iPrice;
}

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 iVIN)
{
VIN = iVIN;
}

void Vehicle::setMake(string iMake)
{
make = iMake;
}

void Vehicle::setModel(string iModel)
{
model = iModel;
}

void Vehicle::setYear(int iYear)
{
year = iYear;
}

void Vehicle::setPrice(double iPrice)
{
price = iPrice;
}

ostream& operator <<(ostream &out, Vehicle car1)
{
out<<\"Vehicle: \"<<\"# VIN:\\t\"<<car1.getVIN()<<\"\\tMake:\\t\"<<car1.getMake()<<\"\\tModel:\\t\"<<car1.getModel();
out<<\"\\tYear:\\t\"<<car1.getYear()<<\"\\tPrice:\\t$\"<<car1.getPrice();
out<<\"\\tDealer Name:\\t\"<<car1.DealrPtr->getName()<<\"\\tDealer Address:\\t\"<<car1.DealrPtr->getAddress()<<endl;
return out;
}

For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:
For Programming Assignment 3 you will be creating a program to manage cars in a dealership. This will again be a menu driven system. The following is your menu:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site