In: Computer Science
In C++ write an application that calculates total retail values for 5 different shoes entered by the user.
Shoe 1 $15.50
Shoe 2 $27.30
Shoe 3 $34.50
Shoe 4 $42.11
Shoe 5 $54.25
Application must read shoe number and quantity sold for each
transaction stdin, compute the total value of that sale, and add
the value of that sale to a grand total for that shoe. After all
data has been entered, display the total value of each of the five
shoe items. Must have driver and a class, must use a switch
structure to which shoes sales to update, must use a sentinel
controlled loop to determine when the program should stop looping
and display result.
Data members
Product price
Total sales for product(in dollars)
Member Functions
One 1 argument constructor(representing the shoe price), set the
value of the data member using this argument. Getters and setters
for data member, and updateTotalSales method(uses one parameter
representing quantity sold and does not return a value) . must use
getter and setter methods to access the fields do not access them
directly. It computes the total value of the current sale by
multiplying the argument passed by the shoess price. It updates the
shoes total Sales data member by adding the value calculated in the
previous step to the shoes existing totalSales. Must use getter and
setter functions. Do not access the data members directly.
Main function
Instantiate 5 separate objects of the class Shoe, passing the
appropriate argument to each constructor. The Shoe class
constructor requires the price of that instance of the shoe. The
prices can be hard coded numbers.
Using Sentinel controlled loop
1. Read the shoe number and the quantity sold for one
transaction.
2. Use a switch statement to determine which Shoe
objects sales to update.
3. Call the appropriate Shoe objects updateTotalSales
function.
4. Continue loop until user enters 0 for the shoe
number.
5. After the loop is complete display each shoe objects
total sales, including the dollar sign, two decimal places with a
field width of 10.
Sample run
Shoe number: 2
Quantity sold: 4
Shoe number: 1
Quantity sold: 3
Shoe Number: 5
Quantity sold: 2
Shoe number: 1
Quantity sold: 2
Shoe number: 5
Quantity sold: 3
Shoe Number: 4
Quantity sold: 2
Shoe Number: 0
Total Sales
Shoe 1 : $77.50
Shoe 2: $109.20
Shoe 3: $0
Shoe 4: $84.22
Shoe 5: $271.25
#include <bits/stdc++.h>
using namespace std;
class Shoe
{
private:
double product_price, total_sale;
public:
Shoe(double price)
{
product_price = price;
total_sale = 0;
}
double getPrice()
{
return product_price;
}
void setPrice(double val)
{
product_price = val;
}
double getTotalSale()
{
return total_sale;
}
void setTotalSale(double val)
{
total_sale = val;
}
void updateTotalSale(int quantity)
{
double temp = getPrice() * quantity + getTotalSale();
setTotalSale(temp);
}
};
int main()
{
Shoe Shoe_1 = Shoe(15.50);
Shoe Shoe_2 = Shoe(27.30);
Shoe Shoe_3 = Shoe(34.50);
Shoe Shoe_4 = Shoe(42.11);
Shoe Shoe_5 = Shoe(54.25);
while (1)
{
int shoe_no, quantity;
cout << "Shoe number: ";
cin >> shoe_no;
if(shoe_no==0){
cout<<"Total Sale\n";
printf("Shoe 1 : $%.2f\n",Shoe_1.getTotalSale());
printf("Shoe 2 : $%.2f\n",Shoe_2.getTotalSale());
printf("Shoe 3 : $%.2f\n",Shoe_3.getTotalSale());
printf("Shoe 4 : $%.2f\n",Shoe_4.getTotalSale());
printf("Shoe 5 : $%.2f\n",Shoe_5.getTotalSale());
return 0;
}
cout << "Quantity sold: ";
cin >> quantity;
switch (shoe_no)
{
case 1:
Shoe_1.updateTotalSale(quantity);
break;
case 2:
Shoe_2.updateTotalSale(quantity);
break;
case 3:
Shoe_3.updateTotalSale(quantity);
break;
case 4:
Shoe_4.updateTotalSale(quantity);
break;
case 5:
Shoe_5.updateTotalSale(quantity);
break;
default:
cout << "Enter Valid Choice\n";
break;
}
}
}