For Programming Assignment 3 you will be creating a c progra

For Programming Assignment 3 you will be creating a c++ program to manage cars in a dealership.   This will again be a menu driven system. The following is your menu: (please include comments of what you are doing)

1.Display Inventory

2.Add a vehicle

3.Update a vehicle

4.Delete a vehicle

5.Sort inventory by VIN

6.Search inventory by Model

7.Read inventory from file

8.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:

1.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

Solution

vehicle.h

#include <string>
#include <fstream>
#include <ostream>

using namespace std;

class Dealer{
private:
   string dealerName, dealerAddress;
public:
   Dealer(){dealerName = \"\";dealerAddress = \"\";} // Default constructor as asked
   Dealer(string iName){dealerName = iName;dealerAddress = \"\";} // parameter constructor to initialize dealer name
   //method stub
   void setName(string);
   void setAddress(string);
   string getName();
   string getAddress();
};

class Vehicle{
private:
   string VIN, make, model;
   int year;
   double price;
public:
   // as asked in the program just simply declare it
   Dealer * DealerPtr;
  
   //default constructor
   Vehicle(){
       VIN = \"\";
       make = \"\";
       model = \"\";
       year = 0;
       price = 0.0;
   }
   //parameter constructor
   Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice){  
       VIN = iVIN;
       make = iMake;
       model = iModel;
       year = iYear;
       price = iPrice;
   }
   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);
   friend ostream & operator << (ostream &, Vehicle );
};

vehicle.cpp

#include \"vehicle.h\"

//Definition of all the Dealer public methods
void Dealer::setName(string name){
   this->dealerName = name;
}
void Dealer::setAddress(string address){
   this->dealerAddress = address;
}

string Dealer::getName(){
   return this->dealerName;
}

string Dealer::getAddress(){
   return this->dealerAddress;
}


//Definition of all the Vehicle class public methods

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 VIN){
   this->VIN = VIN;
}
void Vehicle::setMake(string make){
   this->make = make;
}
void Vehicle::setModel(string model){
   this->model = model;
}
void Vehicle::setYear(int year){
   this->year = year;
}
void Vehicle::setPrice(double price){
   this->price = price;
}

//overloading the << outstream operator to get the output in desired form.
//beware of using the dot and arrow notation.
ostream & operator << (ostream& out, Vehicle car1){
   out << \"VIN: \"<< car1.getVIN()<< \"\ \" << \"Make: \" << car1.getMake() << \"\ \"
   << \"Model: \" << car1.getModel() << \"\ \" << \"Year: \"<< car1.getYear() << \"\ \" << \"Price: $\" << car1.getPrice() << \"\ \"
   << \"Dealer Name: \"<< car1.DealerPtr->getName() <<\"\ \" << \"Dealer Address: \" << car1.DealerPtr->getAddress() << endl;

   return out;
}

function.h

#include <string>
#include <fstream>
#include <ostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;


//Just iterate through the inventory, its printing is done through the overriding << operator we have done for Vehicle class
void displayInventory(vector<Vehicle> cars){
   for(int i = 0;i < (unsigned)cars.size(); ++i){
       cout << \"Vehicle: \" << i + 1 << endl;
       cout << cars[i];
   }
}

void addVehicle(vector<Vehicle> &cars){
   string VIN, make, model, dealerName, dealerAddress, sPrice, sYear;
   int year;
   double price;
   getline(cin,VIN); // to just garbage the new line from command prompt

   //Take the input unless user enter -1 to stop
   while(getline(cin, VIN) && getline(cin, make) && getline(cin, model)
       && getline(cin, sYear) && getline(cin, sPrice)
       && getline(cin, dealerName) && getline(cin, dealerAddress)){

       istringstream iss(sYear), iss1(sPrice);
       iss >> year;
       iss1 >> price;
       Vehicle car(VIN, make, model, year, price);

       //creating the new Object of Dealer so that we can add its details
       car.DealerPtr = new Dealer();
       car.DealerPtr->setName(dealerName);
       car.DealerPtr->setAddress(dealerAddress);
       cars.push_back(car);
       cout << \"Enter -1 to stop adding vehicle\" << endl;
       int d;
       cin >> d;
       if(d == -1)break;
   }
}

void updateVehicle(vector<Vehicle> &cars){
   int index;
   cin >> index;
   --index;

   //just check whether index is in between 0 and size - 1 of car vector
   if(index >= cars.size()){
       cout << \"Index is out of bound\ \";
       return;
   }
   string VIN, make, model, dealerName, dealerAddress, sPrice, sYear;
   int year;
   double price;
   getline(cin,VIN); // to just garbage the new line from command prompt

   getline(cin, VIN);getline(cin, make);getline(cin, model);
   getline(cin, sYear); getline(cin, sPrice);
   getline(cin, dealerName);getline(cin, dealerAddress);
   stringstream iss(sYear), iss1(sPrice);
   iss >> year;
   iss1 >> price;
   Vehicle car(VIN, make, model, year, price);
   car.DealerPtr = new Dealer();
   car.DealerPtr->setName(dealerName);
   car.DealerPtr->setAddress(dealerAddress);
   cars[index] = car;
}

void deleteVehicle(vector<Vehicle> &cars){
   int index;
   cin >> index;
   --index;
   //just check whether index is in between 0 and size - 1 of car vector

   if(index >= cars.size()){
       cout << \"Index is out of bound\ \";
       return;
   }

   // used standard library method of vector
   cars.erase(cars.begin() + index);
}

// created a comparator function to sort the value by VIN value, beware of that it is doing ascii comparision so
// small letter and capital letter will have difference
bool ValueCmp(Vehicle & a, Vehicle & b){
return a.getVIN() < b.getVIN();
}

// just use library function to sort the method taking our own comparator
void sortByVIN(vector<Vehicle> &cars){
   if(cars.size() == 0){
       cout << \"Inventory is empty so can\'t be sorted\" << endl;
       return;
   }
   sort(cars.begin(), cars.end(), ValueCmp);
}

//return the index if the model is present in the inventory otherwise -1
int searchByModel(vector<Vehicle> cars){
   string model;
   getline(cin, model);
   for (int i = 0; i < cars.size(); ++i)
   {
       if(cars[i].getModel() == model){
           return i;
       }
   }
   return -1;
}

// Read the inventory through file
void readInventory(vector<Vehicle> &cars){
   ifstream fin;// create ifstream object
   fin.open(\"inventory.txt\"); //open the readonly file
   //check if file is present or absent
   if(!fin){
       cout << \"Error in reading the file\";
       exit(1);
   }

   string VIN, make, model, dealerName, dealerAddress, sPrice, sYear;
   int year;
   double price;

   //used getline method so that even if the string contain name having space it work
   while(getline(fin, VIN) && getline(fin, make) && getline(fin, model)
       && getline(fin, sYear) && getline(fin, sPrice)
       && getline(fin, dealerName) && getline(fin, dealerAddress)){

       stringstream iss(sYear), iss1(sPrice);
       iss >> year;
       iss1 >> price;
       Vehicle car(VIN, make, model, year, price);
       car.DealerPtr = new Dealer();
       car.DealerPtr->setName(dealerName);
       car.DealerPtr->setAddress(dealerAddress);
       cars.push_back(car);
       cout << \"..\ \";

   }
   fin.close();
   cout << \"File Reading completed\" << endl;
}


//writing to the output.txt file.You can change it your default or take input from user.

void writeInventoryToFile(vector<Vehicle> &cars){
   ofstream fout;
   fout.open(\"output.txt\");
   if(!fout){
       cout << \"Error in creating the file\";
       exit(1);
   }
   for (int i = 0; i < cars.size(); ++i)
   {
       fout << \"Vehicle: \" << i + 1 <<endl;
       fout << cars[i];
       cout << \"..\ \";
   }

   cout << \"Writing of File completed\ \";
   fout.close();

}

main.cpp

#include \"vehicle.cpp\"
#include \"function.h\"
#include <iostream>
using namespace std;

int main(){
   vector<Vehicle> cars;
   while(1){
       int k, index;
       cout << \"Please enter the menu\ \"
               \"1.Display Inventory\ \"
               \"2.Add a vehicle\ \"
               \"3.Update a vehicle\ \"
               \"4.Delete a vehicle\ \"
               \"5.Sort inventory by VIN\ \"
               \"6.Search inventory by Model\ \"
               \"7.Read inventory from file\ \"
               \"8.Write inventory to file and exit\ \";
       cin >> k;
       switch(k){
           case 1:
               if(cars.size() == 0){
                   cout << \"Invetory is empty\" << endl;
               }else{
                   displayInventory(cars);
               }
               break;  
           case 2:
               cout << \"Enter the information to add the vehicle\" << endl;
               addVehicle(cars);
               break;
           case 3:
               cout << \"Enter the index you want to update\" << endl;
               updateVehicle(cars);
               break;
           case 4:
               cout << \"Enter the index to delete a vehicle\" << endl;
               deleteVehicle(cars);
               break;
           case 5:
               cout << \"Sorting the inventory........\" << endl;
               sortByVIN(cars);
               break;
           case 6:
               cout << \"Enter model name to search inventory\" << endl;
               index = searchByModel(cars);
               if(index != -1){
                   cout << \"This model is present in \" << index << \" location\ \";
               }else{
                   cout << \"Model not found\ \";
               }
               break;
           case 7:
               cout << \"Reading from the file.....\" << endl;
               readInventory(cars);
               break;
           case 8:
               if(cars.size() == 0){
                   cout << \"Inventory is empty to write\ \";
                   break;
               }
               cout << \"Writing to the file\" << endl;
               writeInventoryToFile(cars);
               return 0;
           default:
               cout << \"Wrong input\ \";

       }
   }
   return 0;
}

input for this problem has been read from file -

BMK236
BMW
MZ
2006
29922.21
AshleyStores
SanJose
WEWE6
Mercedez
Zseries
1992
32922.21
Ford Corp
Paulo Alto
KOl291
Ferrari
jui
2020
1000000.21
Mars colony
pluto

output written to file is

Vehicle: 1
VIN: BMK236
Make: BMW
Model: MZ
Year: 2006
Price: $29922.2
Dealer Name: AshleyStores
Dealer Address: SanJose
Vehicle: 2
VIN: WEWE6
Make: Mercedez
Model: Zseries
Year: 1992
Price: $32922.2
Dealer Name: Ford Corp
Dealer Address: Paulo Alto
Vehicle: 3
VIN: KOl291
Make: Ferrari
Model: jui
Year: 2020
Price: $1e+06
Dealer Name: Mars colony
Dealer Address: pluto

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site