In: Computer Science
You are developing an application that prints out invoice information of customers and cars from dealers. The invoice contains: dealer name, customer name, customer’s phone number, car maker, building year, and total price. Please follow below instructions to write a program.
Part 1 create data types
1. Create an enumerated data type that holds 7 car colors, which are: red, black, white, blue, yellow, gray, and silver;
2. Create a structure for car that holds 4 members: car maker, building year, car color, and total price;
3. Create another structure for customer that holds 2 members: customer name, and customer phone number;
4. Create a third structure for invoice that holds 3 members: dealer name, customer information and car information. The car information is a pointer.
Part 2 define functions
1. Define a function that returns price rate base on the car color. If the car’s color is red, black or white, then the rate is 1. If the color is blue or yellow, then the rate is 1.5. If the color is gray or silver, then the rate is 2;
2. Define a function that copy each member in an invoice variable (e.g., var1) and assign these members to another invoice variable (e.g., var2). Then return this copied invoice variable (var2);
3. Define a function that display invoice information, which includes:
• Dealer name
• Customer name
• Customer phone number
• Car maker
• Car building year
• Car total price
Part 3 manipulate data
A car inventory of cars contains information of three cars: 2018 white Honda, 2019 blue BMW, 2018 silver Tesla. The initial price of each car starts at the price of $30,000. The color base price is $300, and it is varied by multiplying the price rate.
1. Define three pointers that hold char array values for car maker: Honda, BMW, Tesla;
2. Define a C++ string array that holds values of model year;
3. Define a pointer of the car’s type (car pointer) and dynamic allocate an array of size 3;
4. Define a pointer (price pointer) that dynamic allocate an array of size 3. The values pointed by the price pointer and its offsets are total price for each car;
5. Base on color information and price rate, calculate total prices and store them into price pointer and its offsets: total price = initial price + (color base price * price rate);
6. According to above information, store makers, model years, car color, and total price to the car array that pointed by car pointer.
A customer named Tom Smith comes to Carfax to buy the 2019 blue BMW. His phone number I (408)123-4567.
1. Create a variable of invoice type (invoice variable) and store dealer, customer, and car information;
2. Call the display function to print out invoice information.
Later, Tom Smith goes to KBB and buy the 2018 silver Tesla.
1. Create another variable of invoice type (copied invoice variable) and copy contents from invoice variable;
2. Modify contents in copied invoice variable to match the second transaction information;
3. Call the display function to print out this invoice information.
Screenshot
Program
#include <iostream>
#include<string>
using namespace std;
//Part1
//Create an enumerated data type that holds 7 car colors,
//which are: red, black, white, blue, yellow, gray, and
silver;
enum Colors {
red, black, white, blue, yellow, gray, silver
};
//Create a structure for car that holds 4 members: car maker,
building year, car color, and total price;
struct Car {
string maker;
string year;
Colors color;
float price;
};
//Create another structure for customer that holds 2 members:
customer name, and customer phone number;
struct Customer {
string name;
string phoneNum;
};
//Create a third structure for invoice that holds 3 members:
//dealer name, customer information and car information.
//The car information is a pointer.
struct Invoice {
string dealerName;
Customer customer;
Car* car;
};
//Part2
//Function prototypes
float priceRate(Colors color);
Invoice copy(Invoice invoice);
void displayInvoice(Invoice invoice);
int main()
{
//Part3
//Define three pointers that hold char array values
for car maker
char maker1[] = "Honda",maker2[] = "BMW", maker3[] =
"Tesla";
char* m1;
char*m2;
char*m3;
m1 = maker1;
m2 = maker2;
m3 = maker3;
//Define a C++ string array that holds values of model
year;
string modelsYear[] = { "2018","2019","2018" };
// Define a pointer of the car’s type (car pointer)
and dynamic allocate an array of size 3;
Car* cars;
cars = new Car[3];
cars[0].color = white;
cars[1].color = blue;
cars[2].color = silver;
//Define a pointer(price pointer) that dynamic
allocate an array of size 3.
//The values pointed by the price pointer and its
offsets are total price for each car;
float* price;
price = new float[3]{ 30000,30000,30000 };
//Add into car array
for (int i = 0; i < 3; i++) {
if (i == 0) {
cars[i].maker =
m1;
}
else if (i == 1) {
cars[i].maker =
m2;
}
else {
cars[i].maker =
m3;
}
cars[i].year = modelsYear[i];
cars[i].price = price[i] +( 300 *
priceRate(cars[i].color));
}
// Create a variable of invoice type (invoice
variable) and store dealer,
//customer, and car information;
Invoice invoice;
invoice.dealerName ="Carfax";
Customer customer = { "Tom Smith", "(408)123-4567"
};
invoice.customer = customer;
invoice.car = &cars[1];
cout << "Display invoice before copy:-" <<
endl;
//Display invoice
displayInvoice(invoice);
//Create second invoice
Invoice invoice2;
invoice2.dealerName = "KBB";
customer = { "Tom Smith", "(408)123-4567" };
invoice2.customer = customer;
invoice2.car = &cars[2];
//Copy
invoice=copy(invoice2);
//Display
cout << "\nDisplay invoice after copy:-"
<< endl;
displayInvoice(invoice);
}
//Define a function that returns price rate base on the car
color.
//If the car’s color is red, black or white, then the rate is
1.
//If the color is blue or yellow, then the rate is 1.5.
//If the color is gray or silver, then the rate is 2;
float priceRate(Colors color) {
if (color == red || color == black || color == white)
{ return 1; }
else if (color == blue || color == yellow) { return
1.5; }
else{ return 2; }
}
//Define a function that copy each member in an invoice variable
(e.g., var1)
//and assign these members to another invoice variable (e.g.,
var2).
//Then return this copied invoice variable (var2);
Invoice copy(Invoice invoice) {
Invoice newInvoice;
newInvoice.customer = invoice.customer;
newInvoice.dealerName = invoice.dealerName;
newInvoice.car = invoice.car;
return newInvoice;
}
// Define a function that display invoice information
void displayInvoice(Invoice invoice) {
cout << "Dealer name: " <<
invoice.dealerName << endl;
cout << "Customer name: " <<
invoice.customer.name << endl;
cout << "Customer phone number: " <<
invoice.customer.phoneNum << endl;
cout << "Car make: " <<
invoice.car->maker << endl;;
cout << "Car building year: " <<
invoice.car->year << endl;
cout << "Car total price: " <<
invoice.car->price << endl;
}
---------------------------------------------------------------------------------------
Output
Display invoice before copy:-
Dealer name: Carfax
Customer name: Tom Smith
Customer phone number: (408)123-4567
Car make: BMW
Car building year: 2019
Car total price: 30450
Display invoice after copy:-
Dealer name: KBB
Customer name: Tom Smith
Customer phone number: (408)123-4567
Car make: Tesla
Car building year: 2018
Car total price: 30600