Question: Write a program that calculates the total and average sales over a period of years. The program will prompt the user to input the number

Write a program that calculates the total and average sales over a period of years. The program will prompt the user to input the number of years and the sales data for each month of each year. After collecting and processing the data, the program will print the average monthly sales and total annual sales.
Requirements:
User Input:
Prompt the user to enter the number of years they want to calculate sales for.
For each year, prompt the user to enter the sales amount for each month (12 months per year).
Input Validation:
Ensure the number of years entered is at least 1. If not, display an error message and prompt the user to enter a valid number.
Ensure that all sales amounts are non-negative. If a negative value is entered, display an error message and prompt the user to re-enter the value.
Calculations:
Calculate the total sales for each year.
Calculate the average monthly sales over the entire period.
Methods:
Value-Returning Method 1: Create a method that takes the total sales and the number of months as parameters, and returns the average sales.
Value-Returning Method 2: Create a method that validates user input (for years and sales amounts) and returns the validated value.
Non-Value-Returning Method: Create a method that prints the total sales and average sales in a formatted manner.
Output:
Format all dollar amounts to two decimal places.
Print the total sales for the entire period.
Print the average monthly sales.
Sample Output:
Run 1:
Enter the number of years of sales to calculate:
1
Enter the sales, in dollars for each month.
Year 1 month 1:
432.23
Year 1 month 2:
343.43
Year 1 month 3:
34.54
Year 1 month 4:
456.65
Year 1 month 5:
789.23
Year 1 month 6:
343.23
Year 1 month 7:
334.32
Year 1 month 8:
45.65
Year 1 month 9:
578.76
Year 1 month 10:
356.65
Year 1 month 11:
342.23
Year 1 month 12:
32.23
Number of months: 12.0
Total Sales: $4089.15
Average annual sales: $340.76
Run 2:
Enter the number of years of sales to calculate:
-1
At least one year is needed to perform the calculation. Please try again.
Implementation Details:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!