write a c++ program with the Vehicle in a class . For now, you\'ve decided to ju
ID: 3552816 • Letter: W
Question
write a c++ program with the Vehicle in a class. For now, you've decided to just store the following attributes of a vehicle: price, year and model. Write a computer program in C++ that stores Vehicles in an array and that can add, remove, print and list cars. The commands for adding, removing, printing and listing will come from a file specified on the command-line (Note: STDIN will NOT be used for this lab). Commands are as follows (fields are separated by tabs):
You can assume that the price and year values are integer numbers and that the model is a single word (or at least that do not contain whitespace(s)). Additionally, if a remove or print command is requested but can not be fulfilled, print out an appropriate error statement (see the example output file below for exact syntax for each case) and continue processing commands. Here are some example commands files: this is what is contained in lab3-hybridCommands.tab
Explanation / Answer
Dropbox Link:
"vehicle.h" file: https://dl.dropboxusercontent.com/u/41585969/vehicle.h
"vehicle_driver.cpp" file: https://dl.dropboxusercontent.com/u/41585969/vehicle_driver.cpp
"input.txt": https://dl.dropboxusercontent.com/u/41585969/input.txt
"vehicle.h" file:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
//Vehicle Class
class Vehicle {
protected:
string model; // model
int price;
int year; // year
public:
Vehicle(); //Default constructor
Vehicle(string, int, int); // Non-default constructor
//mutator and accessor functions
void setModel(string);
void setYear(int);
void setPrice(int);
string getModel();
int getYear();
int getPrice();
void print();
bool compare(int year, int price, string model);
};
//Sets to default values
Vehicle::Vehicle() {
model = " ";
year = 0;
price = 0;
}
Vehicle::Vehicle(string model, int year, int price) {
Vehicle::model = model;
Vehicle::year = year;
Vehicle::price = price;
}
void Vehicle::setModel(string model) {
Vehicle::model = model;
}
void Vehicle::setYear(int year) {
Vehicle::year = year;
}
void Vehicle::setPrice(int price) {
Vehicle::price = price;
}
string Vehicle::getModel() {
return model;
}
int Vehicle::getYear() {
return year;
}
int Vehicle::getPrice() {
return price;
}
void Vehicle::print(){
cout << "Price: " << price << endl;
cout << "Year: " << year << endl;
cout << "Model: " << model << endl;
cout << endl << endl;
}
bool Vehicle::compare(int p, int y, string m){
if(year==y && price==p && model.compare(m)==0)
return true;
else return false;
}
---------------------------------------------------------------------------------------------------
"vehicle_driver.cpp":
#include "vehicle.h"
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int tempInt;
string temp;
int menu=0;
int current_vehicle=0;
int tmpInt=0;
// My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
Vehicle *myVehicles[100];
int curr = 0;
std::ifstream infile("input.txt");
std::string line;
while (std::getline(infile, line))
{
bool flag = false;
std::istringstream iss(line);
char a;
int year, price;
string model;
iss >> a;
if(a == 'L'){
int count = 0;
for (int i=0; i< curr; i++){
Vehicle* v = myVehicles[i];
if(v!=NULL){
count++;
}
}
if(count!=0){
cout << "Listing all vehicles:" << endl;
cout <<"---------------------------------" << endl;
for (int i=0; i< curr; i++){
Vehicle* v = myVehicles[i];
if(v!=NULL){
cout << "Vehicle #" << (i+1) <<":"<< endl;
cout << "-------------------" << endl;
cout << "Price: " << v->getPrice() << endl;
cout << "Year: " << v->getYear() << endl;
cout << "Model: " << v->getModel() << endl;
cout << endl << endl;
}
}
cout << endl;
}
}
if(a == 'A'){
iss >> price >> year >> model;
myVehicles[curr] = new Vehicle(model, year, price);
curr++;
}
if(a == 'R'){
iss >> price >> year >> model;
for (int i=0; i< curr; i++){
Vehicle* v = myVehicles[i];
if(v!=NULL && v->compare(price, year, model)){
myVehicles[i] = NULL;
flag = true;
}
}
if(!flag){
cout << "Unable to find (and therefore remove) the car with the following specifications: "
<< price << ", " << year << ", " << model << endl << endl;
}
}
if(a == 'P'){
iss >> price >> year >> model;
for (int i=0; i< curr; i++){
Vehicle* v = myVehicles[i];
if(v!=NULL && v->compare(price, year, model)){
cout << "Found vehicle:" << endl;
cout << "-------------" << endl;
v->print();
flag = true;
}
}
if(!flag){
cout << "Unable to find (and therefore print) the car with the following specifications: "
<< price << ", " << year << ", " << model << endl << endl;
}
}
}
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
Input.txt:
A 7989 2008 Prius
A 8993 2005 Civic
A 8998 2006 Insight
R 8993 2005 Civic
L
A 9800 2007 Prius
A 9995 2010 Insight
R 8998 2006 Insight
R 9995 2010 Insight
A 9998 2008 Prius
A 10000 2008 Prius
A 10000 2010 Insight
A 10000 2008 Prius
R 10000 2010 Insight
L
R 9999 2005 Civic
P 7989 2008 Prius
P 9998 2008 Prius
P 10000 2008 Prius
P 8993 2005 Civic
P 10000 2010 Insight
L
R 10000 2008 Prius
R 9998 2008 Prius
R 9800 2007 Prius
R 7989 2008 Prius
L
------------------------------------------------------------------------------------------------------------------------
Output:
Listing all vehicles:
---------------------------------
Vehicle #1:
-------------------
Price: 7989
Year: 2008
Model: Prius
Vehicle #3:
-------------------
Price: 8998
Year: 2006
Model: Insight
Listing all vehicles:
---------------------------------
Vehicle #1:
-------------------
Price: 7989
Year: 2008
Model: Prius
Vehicle #4:
-------------------
Price: 9800
Year: 2007
Model: Prius
Vehicle #6:
-------------------
Price: 9998
Year: 2008
Model: Prius
Vehicle #7:
-------------------
Price: 10000
Year: 2008
Model: Prius
Vehicle #9:
-------------------
Price: 10000
Year: 2008
Model: Prius
Unable to find (and therefore remove) the car with the following specifications: 9999, 2005, Civic
Found vehicle:
-------------
Price: 7989
Year: 2008
Model: Prius
Found vehicle:
-------------
Price: 9998
Year: 2008
Model: Prius
Found vehicle:
-------------
Price: 10000
Year: 2008
Model: Prius
Found vehicle:
-------------
Price: 10000
Year: 2008
Model: Prius
Unable to find (and therefore print) the car with the following specifications: 8993, 2005, Civic
Unable to find (and therefore print) the car with the following specifications: 10000, 2010, Insight
Listing all vehicles:
---------------------------------
Vehicle #1:
-------------------
Price: 7989
Year: 2008
Model: Prius
Vehicle #4:
-------------------
Price: 9800
Year: 2007
Model: Prius
Vehicle #6:
-------------------
Price: 9998
Year: 2008
Model: Prius
Vehicle #7:
-------------------
Price: 10000
Year: 2008
Model: Prius
Vehicle #9:
-------------------
Price: 10000
Year: 2008
Model: Prius
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.