Question

In: Computer Science

In Java write an application that calculates total retail values for 5 different shoes entered by...

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

Solutions

Expert Solution

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:


Related Solutions

(JAVA) Write an application that reads three nonzero values entered by the user and determines and...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Enter three sizes, separated by spaces(decimals values are acceptable): 4.5·5.5·3.5 A triangle could measure 4.50, 5.50, by 3.50.
Write a Java application that prompts the user for an age. If the age entered is...
Write a Java application that prompts the user for an age. If the age entered is greater or equal to 65, display the statement "Age is greater than or equal to 65"; otherwise display the message "Age is less than 65". If the age entered is less than 18; display the statement "This person is a minor"; otherwise display the message "This person can legally vote. Do not create a class for this application. The code can be created in...
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
Write a program that calculates mean and standard deviation for four user entered values. The most...
Write a program that calculates mean and standard deviation for four user entered values. The most common measures of a sample are the mean and the standard deviation. the mean is the sum of the values divided by the number of elements in the data set. The dispersion - or spread in the values - is measured by the standard deviation The equation for the mean is x¯ = x1 + x2 + · · · + xn The equation...
JAVA - The Westfield Carpet Company has asked you to write an application that calculates the...
JAVA - The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor(width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs $8 per square foot would cost $960. (12 X 10...
How can we write an application that calculates connascence of an application?
How can we write an application that calculates connascence of an application?
Write a program in java which calculates the total price of an item you purchased which...
Write a program in java which calculates the total price of an item you purchased which includes sales tax. Create a Scanner object to read information from keyboard, Display prompts and get input, perform the calculation and display the Result of the program.
Write a Java application for a gas station to calculate the total payment for customer. Your...
Write a Java application for a gas station to calculate the total payment for customer. Your program should display a menu and get customer’s input: **************************** Welcome to XYZ gas station! Select type of gas (enter R, P, S) Regular - R. plus - P. Super -S: **************************** Enter the amount of gas in gallons: In your code, define regular price: $2.45, plus: $2.65, super $2.95 as constant. Use 7% as sale tax. Calculate gas subtotal = sale price *...
In C# Create a GUI application that calculates and displays the total travel expenses of a...
In C# Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: Number of days on the trip Amount of airfare, if any Amount of car rental fees, if any Number of miles driven, if a private vehicle was used Amount of parking fees, if any Amount of taxi charges, if any Conference or seminar registration fees, if any Lodging charges, per...
Create a GUI application in C# that calculates and displays the total travel expenses of a...
Create a GUI application in C# that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide: • Number of days on the trip • Amount of airfare, if any • Amount of car rental fees, if any • Number of miles driven, if a private vehicle was used • Amount of parking fees, if any • Amount of taxi charges, if any • Conference or seminar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT