Question

In: Computer Science

CSCI 3000 Homework 5 In this homework, you will manage the car data of a dealer....

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.

  1. Print All Cars: Print all information of all Cars
  2. Print by Make: Get the model name from user and print all cars only in that model.
  3. Print by Price: Get a price input from the user and print all cars that have less than given input.
  4. Exit: Exit from the program.

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,

Solutions

Expert Solution

#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;
}

Related Solutions

Homework 3: A car dealer pays $17,985 for each car purchased. The annual holding rate is...
Homework 3: A car dealer pays $17,985 for each car purchased. The annual holding rate is estimated at 25%, and the ordering cost is $7,558. The dealer is selling an average of 516 cars a year. 3. Note the new information: • The dealership can only park 45 cars in its small parking lot. Therefore, if needed, it will lease a nearby bigger parking lot, and use it to park access inventory. • The dealer can get a discount of...
You are negotiating to buy a new car with a car salesman at a local dealer....
You are negotiating to buy a new car with a car salesman at a local dealer. You have negotiated the price to $35,000. You have $5,000 to put towards the down payment and plan to get a loan for the rest. If you can get an annual interest rate of 11 percent APR (with monthly compounding) over a 5-year period, what would be your monthly payment? Round it to two decimal place (cents), e.g., 234.56.
You are negotiating to buy a new car with a car salesman at a local dealer....
You are negotiating to buy a new car with a car salesman at a local dealer. You have negotiated the price to $31,000. You have $3,000 to put towards the down payment and plan to get a loan for the rest. If you can get an annual interest rate of 5 percent APR (with monthly compounding) over a 5-year period, what would be your monthly payment? Round it to two decimal place (cents)
You need a new car and the dealer has offered you a price of $20,000, with...
You need a new car and the dealer has offered you a price of $20,000, with the following payment options: (a) pay cash and receive a $2000 rebate, or (b) pay a $5000 down payment and finance the rest with a 0% APR loan over 30 months. But having just quit your job and started an MBA program, you are in debt and you expect to be in debt for at least the next 2 1 2 years. You plan...
You need a new car and the dealer has offered you a price of $20,000​, with...
You need a new car and the dealer has offered you a price of $20,000​, with the following payment​ options: (a) pay cash and receive a $2,000 ​rebate, or​ (b) pay a $5,000 down payment and finance the rest with a 0% APR loan over 30 months. But having just quit your job and started an MBA​ program, you are in debt and you expect to be in debt for at least the next 2​ ½ years. You plan to...
1. You decide to buy a new car. You negotiate with the dealer and get the...
1. You decide to buy a new car. You negotiate with the dealer and get the car for $40,000. What will be your monthly payment if you finance the purchase through your bank with a 6-year, 11% auto loan (assume no down payment). a. What if you made a down payment of $4,000? What would be the monthly payment for the car?
You would like to buy a new car that costs $25,000. The dealer offers you a...
You would like to buy a new car that costs $25,000. The dealer offers you a 3-year loan at an 8% interest rate, and because that rate is higher than market rates they offer to lower the price by $2,000. Assuming you make the required payment, should you accept his offer or seek alternative financing at the 3% rate?
C# A car dealer wants an application that calculates the cost of a car. The GUI...
C# A car dealer wants an application that calculates the cost of a car. The GUI application should link the “BuildYourCar.accdb” database and display all the data in four different “ListBox” based on the category. Each “ListBox” should display all the items in that category. The user can only choose one item from each “ListBox” to add an item to a car. As each item is selected, the application displays the item in a separate “ListBox” to display. If user...
You buy a car with a down payment of $3000 and payments of $600 per month...
You buy a car with a down payment of $3000 and payments of $600 per month for 3 years. If the interest rate is 3.6% compounded monthly, what is the total cost of the car?
You are considering buying a sports car with a list price of $99,000. The dealer has...
You are considering buying a sports car with a list price of $99,000. The dealer has offered you two payment alternatives: - You can get a $9,000 discount if you pay cash today. - You can buy the car for the list price of $99,000. You need to make a down payment of $39,000. The remainder $60,000 is a "zero-interest loan" to be paid back in equal installment over 36 months. Alternatively, your bank is willing to give you a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT