import java.io.InputStreamReader; import java.util.Scanner; class tshirts { publ
ID: 3555940 • Letter: I
Question
import java.io.InputStreamReader; import java.util.Scanner; class tshirts { public static int getValidProductId(Scanner input){ int productId; while(true){ String s = input.next(); try { productId = Integer.parseInt(s); if(productId < 0) { System.out.println ("Please Enter Positive number for Product ID:"); continue; } } catch(NumberFormatException e) { System.out.println ("Please Enter Positive number for Product ID:"); continue; } break; } return productId; } public static double getValidProductPrice(Scanner input){ double productPrice; while(true){ String s = input.next(); try { productPrice = Double.parseDouble(s); if(productPrice < 0) { System.out.println ("Please Enter valid Positive floating point number for Product Price:"); continue; } } catch(NumberFormatException e) { System.out.println ("Please Enter valid Positive floating point number for Product Price:"); continue; } break; } return productPrice; } } public class Sales { static double computeAverageSales(){ System.out.println("Enter sales (space separated):"); double avg = 0; int numProducts = 0; Scanner sc = new Scanner(new InputStreamReader(System.in)); while(sc.hasNext()){ double num = sc.nextDouble(); if(num == -1) break; if(num >= 200.00){ System.out.println("Product no. "+ (numProducts+1)+" has produced a significant amount of revenue."); } numProducts++; avg += num; } return avg/numProducts; } static double computeShippingCharges(){ System.out.println("Enter Shipping Charges (space separated):"); double avg = 0; int numProducts = 0; Scanner sc = new Scanner(new InputStreamReader(System.in)); while(sc.hasNext()){ double num = sc.nextDouble(); if(num == -1) break; numProducts++; avg += num; } return avg/numProducts; } public static void main(String[] args) { int productId; String productName; double productPrice; /**After getting all of my variables declared,it's on to the new scanner*/ Scanner input=new Scanner(System.in); /** prompt user to enter the product ID*/ System.out.println ("Please Enter Product ID:"); productId = tshirts.getValidProductId(input); System.out.printf ("Product ID : %s ",productId); /** prompt user to enter product name*/ System.out.println ("Please Enter Product Name:" ); productName = input.next(); /** prompt system to display user entered information for verification*/ System.out.printf ("Product Name: %s ",productName); /** prompt user to enter Product Price*/ System.out.println ("Please Enter Product Price:"); productPrice = tshirts.getValidProductPrice(input); System.out.printf ("Product Price: %s ",productPrice); /** prompt system to display user entered information for verification*/ System.out.println("---------------------------------"); System.out.println("Product Details:"); System.out.printf ("Product ID : %s ",productId); System.out.printf ("Product Name: %s ",productName); System.out.printf ("Product Price: %s ",productPrice); double averageSales = computeAverageSales(); System.out.println("Average Sale: $"+averageSales); System.out.println(); double shippingCharges = computeShippingCharges(); System.out.println("Average Shipping Charges: $"+shippingCharges); } }
Explanation / Answer
EXPLANATION
If you see this program has 2 parts, in one part it is taking user input, in second it calculates average of Sales and Shipping Charges.
In first half :
It asks user to input product Details, product ID , product Name , product Price. And then it stores data in memory.
In second half :
It asks user to input space seperated sales value. Until user enter '-1' as sales value program keep saving sales data. Then once input is ended with an -1 then we take sum of all inputs and take average to show sales average.
Same way it does for shipping charge, means It asks user to input space seperated shipping value. Until user enter '-1' as shipping value program keep saving shipping data. Then once input is ended with an -1 then we take sum of all inputs and take average to show shipping average.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.