In: Computer Science
In Java 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 product 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 two classes, 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.
Methods
One 1 argument constructor, getters and setters for each field, and
updateTotalSales method(uses one parameter representing quantity
sold and does not return a value) . must use getter and setter
methods t access the fields do not access them directly.
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 : $31.00
Shoe 2: $109.20
Shoe 3: $0
Shoe 4: $84.22
Shoe 5: $271.25
Hi,
Hope you are doing fine. I have coded the above question in java and have adhered to all the requirements asked in the question. However, there is one error in the Sample output provided in the question. As per the logic, the cost of all shoes except shoe number 1 is printed accurately. The cost of shoe number 1 must be $77.5 and not $31.00. Here is the logic, if we look at the input entered we see that number of shoe number 1 purchased are 3+2=5. And cost would be 15.50 * 5 =77.5. I have clearly explained the program using comments that have been highlighted in bold.
Program:
Class Shoes:
//Declaring class shoes
public class Shoes {
//shoe_num is an instance variable that stores
the shoe number of the object
int shoe_num;
//cost is an array of type double that stores
the total cost purchased for each shoe number
static double [] cost= {0,0,0,0,0};
//single parameter constructor that takes in
shoe_num as parameter and initializes the object
public Shoes(int shoe_num) {
this.shoe_num = shoe_num;
}
//getter for shoe_num
public int getShoe_num() {
return shoe_num;
}
//setter for shoe_num
public void setShoe_num(int shoe_num) {
this.shoe_num = shoe_num;
}
//getter for array cost[]
public static double[] getCost() {
return cost;
}
//setter for array cost[]
public static void setCost(double[] cost) {
Shoes.cost = cost;
}
//method to update the cost of each shoe
number purchased as per the quantity.
void updateTotalSales(int qty)
{
//switch case for shoe_num
of the instance
switch(this.shoe_num)
{
//if shoe_num ==1 then cost[0] is
updated
case 1:
cost[0]=cost[0]+(15.50*qty);
break;
//if shoe_num ==2 then cost[1] is
updated
case 2:
cost[1]=cost[1]+(27.30*qty);
break;
//if shoe_num ==3 then cost[2] is
updated
case 3:
cost[2]=cost[2]+(34.50*qty);
break;
//if shoe_num ==4 then cost[3] is
updated
case 4:
cost[3]=cost[3]+(42.11*qty);
break;
//if shoe_num ==5 then cost[4] is
updated
case 5:
cost[4]=cost[4]+(54.25*qty);
break;
}
}
}
Class Result:
import java.util.Scanner;
//class that contains main
public class Result {
//main method
public static void main(String[] args) {
// TODO Auto-generated method
stub
//declaring new scanner for
input
Scanner sc=new
Scanner(System.in);
//declaring integers
shoe_num and qty to store inputs from user
int shoe_num,qty;
//loop is always
executed
while(true)
{
//prompting user to enter shoe number
System.out.print("Shoe number: ");
//storing it in shoe_num
shoe_num=sc.nextInt();
//if
shoe_num==0 then we break out of the loop. This is the
sentinel
if
(shoe_num==0)
break;
//prompting user to enter quantity sold
System.out.print("Quantity sold: ");
//storing it in qty
qty=sc.nextInt();
//initializing object s for class Shoes
Shoes s=new
Shoes(shoe_num);
//calling updateTotalSales() method
s.updateTotalSales(qty);
}
//initializing new object
as the previous one was a local one
Shoes s=new Shoes(shoe_num);
System.out.println("Total
sales");
//declaring and
initializing arr to store the cost array from class
Shoes.
double [] arr=s.getCost();
//Printing elements of
array arr
for(int i=0;i<5;i++)
{
System.out.println("Shoe "+(i+1)+":$"+arr[i]);
}
}
}
Executable code snippets:
Class Shoes:
Class Result:
Output: