Write a Java Program Let’s think that Company XYZ produces t-shirts. Develop a J
ID: 3754134 • Letter: W
Question
Write a Java Program
Let’s think that Company XYZ produces t-shirts. Develop a Java application that will input sales revenue and total number products sold (both as integers) for each year. The program should calculate, and display selling price of product for each year and print the combined selling price of product (total sales revenue for all years/total number of products sold for all years) for all years up to this point. All calculations should produce floating-point results. Use class Scanner and sentinel-controlled iteration to obtain the data from user.
Sample input: For the first-year sales revenue is 1000, total number of products sold 10, for the second-year sales revenue is 2000, total number of products sold 20)
Sample output: Selling price of one product for this year is $100
Combined selling price is $10
Selling price of one product for this year is $200
Combined selling price is $30
Please provide the screenshot of the code snippet and also the output.
Explanation / Answer
As lons as i understand requiremnt uaser want to calculate for multiple years and not necessary for 2 yrs only please do correct me if something wrong will helpinh u out..
save the below cod as Sell.java
//start
import java.util.Scanner;
class Sale {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//asking number of year user want to check
String input = "";
int year_no = -1, temp = 0;
while (true) {
System.out.print("For how many year want to calculate : ");
input = sc.next();
year_no = validate_input(input);
if (year_no == -999) {
System.out.println("Invalid year count ...try again!");
} else {
break;
}
}
//created array for storing the values
int yr[] = new int[year_no];
int rev_by_yr[] = new int[year_no];
int prod_sold[] = new int[year_no];
int total_selling = 0;
int total_product_no = 0;
/*
* looping through years
* and asking for year , revenue and produce sold number
*/
for (int i = 0; i < year_no; i++) {
while (true) {
System.out.print("Enter the year : ");
input = sc.next();
temp = validate_input(input);
if (temp == -999) {
System.out.println("Invalid year ...");
} else {
yr[i] = temp;
break;
}
}
while (true) {
System.out.printf("Enter the revenue sale for %d year : ", i + 1);
input = sc.next();
temp = validate_input(input);
if (temp == -999) {
System.out.println("Invalid revenue...try again!");
} else {
rev_by_yr[i] = temp;
break;
}
}
while (true) {
System.out.printf("Enter the total number of products sold for %d year : ", i + 1);
input = sc.next();
temp = validate_input(input);
if (temp == -999) {
System.out.println("Invalid ");
} else {
prod_sold[i] = temp;
break;
}
}
}
System.out.println(" *************************************** ");
for(int i=0;i<yr.length;i++){
System.out.printf("Selling price of one product for year %d is $%.2f ",yr[i],(double)rev_by_yr[i]/prod_sold[i]);
total_selling += rev_by_yr[i];
total_product_no += prod_sold[i];
}
System.out.println(" *****************************************");
System.out.printf("Combined selling price is $%.2f ",(double)total_selling/(double)total_product_no);
sc.close();
}
/**
* helper method for validating sentinel value from user
* @param input
* @return
*/
private static int validate_input(String input) {
int v = -1;
try {
v = Integer.valueOf(input);
} catch (Exception e) {
return -999;
}
if (v < 0)
return -999;
return v;
}
}
//end
//OUTPUT
For how many year want to calculate : 3
Enter the year : 2015
Enter the revenue sale for 1 year : -190
Invalid revenue...try again!
Enter the revenue sale for 1 year : 1000
Enter the total number of products sold for 1 year : 10
Enter the year : 2016
Enter the revenue sale for 2 year : 2000
Enter the total number of products sold for 2 year : 20
Enter the year : 2018
Enter the revenue sale for 3 year : 3000
Enter the total number of products sold for 3 year : 20
***************************************
Selling price of one product for year 2015 is $100.00
Selling price of one product for year 2016 is $100.00
Selling price of one product for year 2018 is $150.00
*****************************************
Combined selling price is $120.00