This will be a c++ menu driven system. The following is your menu: Display Inven
ID: 658200 • Letter: T
Question
This will be a c++ 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 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).
. 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 entry
Explanation / Answer
// Vehicle.cpp class
#include <iostream>
using namespace std;
class Vehicle{
private:
string VIN;
string make;
string model;
int year;
double price;
public:
Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice){
VIN = iVIN;
make = iMake;
model = iModel;
year = iYear;
price = iPrice;
}
Vehicle(){
VIN = "unknown";
make = "unknown";
model = "unknown";
year = 0;
price = 0;
}
string getVIN(){
return VIN;
}
string getMake(){
return make;
}
string getModel(){
return model;
}
int getYear(){
return year;
}
double getPrice(){
return price;
}
void setVIN(string input){
VIN = input;
}
void setMake(string input){
make = input;
}
void setModel(string input){
model = input;
}
void setYear(int input){
year = input;
}
void setPrice(double input){
price = input;
}
};
// functions.h class
#include <iostream>
#include <fstream>
#include <vector>
#include "Vehicle.cpp"
using namespace std;
void display(vector<Vehicle> &vec){
for(int i = 0; i < vec.size(); i++){
cout << "VIN: " << vec[i].getVIN() << ", Make: " << vec[i].getMake() << ", Model: " << vec[i].getModel() << ", Year: " << vec[i].getYear() << ", Price: " << vec[i].getPrice() << " ";
}
cout << " ";
}
void addVehicle(vector<Vehicle> &vec){
string id, make, model;
int year;
double price;
cout << "Enter the vehicle ID: ";
cin >> id;
cout << "Enter the make: ";
cin >> make;
cout << "Enter the new model: ";
cin >> model;
cout << "Enter the new year: ";
cin >> year;
cout << "Enter the new price: ";
cin >> price;
vec.push_back(Vehicle(id, make, model, year, price));
}
void updateVehicle(vector<Vehicle> &vec){
string id, input;
int index = -1;
cout << "Enter the vehicle ID which you want to update: ";
cin >> id;
for(int i = 0; i < vec.size(); i++){
if(vec[i].getVIN().compare(id) == 0){
index = i;
break;
}
}
if(index > -1){
cout << "Enter the new make: ";
cin >> input;
vec[index].setMake(input);
cout << "Enter the new model: ";
cin >> input;
vec[index].setModel(input);
int y;
cout << "Enter the new year: ";
cin >> y;
vec[index].setYear(y);
double p;
cout << "Enter the new price: ";
cin >> p;
vec[index].setPrice(p);
}
else{
cout << "Vehicle with VID " << id << " does not exist" << " ";
}
}
void deleteVehicle(vector<Vehicle> &vec){
string id;
int index = -1;
cout << "Enter the vehicle ID which you want to delete: ";
cin >> id;
for(int i = 0; i < vec.size(); i++){
if(vec[i].getVIN().compare(id) == 0){
index = i;
break;
}
}
if(index > -1){
vec.erase(vec.begin() + index);
}
else{
cout << "Vehicle with VID " << id << " does not exist" << " ";
}
}
void sortVehicle(vector<Vehicle> &vec){
Vehicle temp;
int n = vec.size();
for(int c = 0 ; c < (n - 1); c++){
for(int d = 0 ; d < n - c - 1; d++){
if (vec[d].getVIN().compare(vec[d + 1].getVIN()) > 0){
temp = vec[d];
vec[d] = vec[d + 1];
vec[d + 1] = temp;
}
}
}
}
void searchVehicle(vector<Vehicle> &vec){
int index = -1;
string model;
cout << "Enter the vehicle model which you want to search: ";
cin >> model;
for(int i = 0; i < vec.size(); i++){
if(vec[i].getModel().compare(model) == 0){
index = i;
break;
}
}
if(index > -1){
cout << "VIN: " << vec[index].getVIN() << ", Make: " << vec[index].getMake() << ", Model: " << vec[index].getModel() << ", Year: " << vec[index].getYear() << ", Price: " << vec[index].getPrice() << " ";
}
else cout << "Vehicle not found ";
}
void readInventory(vector<Vehicle> &vec){
char fileName[100];
cout << "Enter the inventory file name: ";
cin >> fileName;
ifstream inFile(fileName);
string id, make, model;
int year;
double price;
while(inFile >> id >> make >> model >> year >> price){
vec.push_back(Vehicle(id, make, model, year, price));
}
}
void writeInventory(vector<Vehicle> &vec){
char fileName[100];
cout << "Enter the output file name: ";
cin >> fileName;
ofstream outFile(fileName);
for(int i = 0; i < vec.size(); i++){
outFile << vec[i].getVIN() << " " << vec[i].getMake() << " " << vec[i].getModel() << " " << vec[i].getYear() << " " << vec[i].getPrice() << " ";
}
}
// a.cpp
#include <iostream>
#include <vector>
#include "functions.h"
using namespace std;
int main(){
vector<Vehicle> object;
while(true){
int choice;
cout << "Menu: ";
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 your choice number: ";
cin >> choice;
cout << " ";
switch(choice){
case 1:
display(object);
break;
case 2:
addVehicle(object);
break;
case 3:
updateVehicle(object);
break;
case 4:
deleteVehicle(object);
break;
case 5:
sortVehicle(object);
break;
case 6:
searchVehicle(object);
break;
case 7:
readInventory(object);
break;
case 8:
writeInventory(object);
return 0;
default:
"invalid choice. ";
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.