In: Computer Science
CSCI 3000 Homework 5
In this homework, you will manage the car data of a dealer. First, you will design a C++ class called Car. A single Car variable (object) have five member variables (attributes): VIN, make, model, year, and price.
Car class attributes should be private, and set/get functions should be implemented to access and update the values. Also, you should have at least two constructor functions to initialize Car objects. Finally a print() function should be implemented to print single Car data on the screen.
Class description is given below:
Car |
|
Type |
Member Variables |
string |
VIN |
string |
make |
string |
model |
int |
year |
int |
price |
Return Type |
Member Function |
(constructor) |
Car() //default constructor |
(constructor) |
Car(string newVIN, string newMake, string newModel, int newYear, int newPrice) |
void |
setVIN(string newVIN) |
void |
setMake(string newMake) |
void |
setModel(string newModel) |
void |
setYear(int newYear) |
void |
setPrice(int newPrice) |
string |
getVIN() |
string |
getMake() |
string |
getModel() |
int |
getYear() |
int |
getPrice() |
void |
print() |
Once your class is ready, you will read the “cardata.txt” file, which includes all 250 cars in the dealer. A sample view of the file is given below:
…
Before you read file, you will create an array of cars having size of 250. This number is always 250 and does not change. In the file, each line contains a single car, for which attributes are separated by space character. So, you can read one line using a simple while loop such as:
While(inputFile >> VIN >> make >> model >> year >> price){
//create new car and send it to the corresponding array index
...
}
Once your car array is ready, you will provide following operations to the user in a menu.
You can write your code (including the car class definition and main function) in a single cpp file and submit that file. If you can separate the class files from the main program, you will get extra 10 points for the assignment. In that case, you will need to submit three files such as, “car.h”, “car.cpp”, and “main.cpp”. I will show you how to separate class files from main program in class.
On the top of your source code, please add your name and number, Course ID, HW number and the date using a comment block. Please see the example below,
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <bits/stdc++.h>
using namespace std;
class Car
{
private:
string model;
string make;
string VIN;
int year;
int price;
public:
Car();
Car(string, string, string, int, int);
void setVIN(string newVIN);
void setMake(string newMake);
void setModel(string newModel);
void setYear(int newYear);
void setPrice(int newPrice);
string getVIN();
string getMake();
string getModel();
int getYear();
int getPrice();
void print();
};
Car::Car(){}
Car::Car(string newVIN, string newMake, string newModel, int newYear, int newPrice)
{
VIN = newVIN;
make = newMake;
model = newModel;
year = newYear;
price = newPrice;
}
void Car::setVIN(string newVIN)
{
Car::VIN = newVIN;
}
void Car::setMake(string newMake)
{
Car::make = newMake;
}
void Car::setModel(string newModel)
{
Car::model = newModel;
}
void Car::setYear(int newYear)
{
Car::year = newYear;
}
void Car::setPrice(int newPrice)
{
Car::price = newPrice;
}
void Car::print()
{
cout<<"VIN="<<Car::VIN<<endl;
cout<<"make="<<Car::make<<endl;
cout<<"model="<<Car::model<<endl;
cout<<"year="<<Car::year<<endl;
cout<<"price="<<Car::price<<endl;
}
string Car::getModel()
{
return model;
}
string Car::getVIN()
{
return VIN;
}
string Car::getMake()
{
return make;
}
int Car::getYear()
{
return year;
}
int Car::getPrice()
{
return price;
}
void displayMenu()
{
cout <<"\n Car Menu " << endl;
cout << "-------------------------------" << endl;
cout << "A) Print all cars" << endl;
cout << "B) Print by Make" << endl;
cout << "C) Print by price" << endl;
cout << "D) Exit the program" << endl;
cout << "\nEnter your choice: ";
}
void printByMake(Car* arr, int count){
cout << "Enter the model name"<<endl;
string makeChoice;
cin >> makeChoice;
for(int i=0;i<count;i++){
if(arr[i].getMake() == makeChoice)
arr[i].print();
}
}
void printByPrice(Car* arr, int count){
cout << "Enter the price"<<endl;
int priceChoice;
cin >> priceChoice;
for(int i=0;i<count;i++){
if(arr[i].getPrice() < priceChoice)
arr[i].print();
}
}
int main()
{
ifstream file("input.txt");
string str;
Car arr[250];
int count=0;
while (std::getline(file, str)) {
istringstream ss(str);
string elements[5];
for(int i=0;i<5;i++) {
string word;
ss >> word;
elements[i] = word;
// cout << word << endl;
}
arr[count++] = Car(elements[0], elements[1], elements[2], stoi(elements[3]), stoi(elements[4]));
}
char choice;
do
{
displayMenu();
cin >> choice;
while(toupper(choice) < 'A' || toupper(choice) > 'D')
{
cout << "Please make a choice of A or B or C or D:";
cin >> choice;
}
switch (choice)
{
case 'a':
case 'A': cout << "Printing all cars";
for(int i=0;i<count;i++) arr[i].print();
break;
case 'b':
case 'B': printByMake(arr, count);
break;
case 'c':
case 'C': printByPrice(arr, count);
break;
}
}while (toupper(choice) != 'D');
return 0;
}