In: Computer Science
Write program for the exercise 11(Sales Analysis) as explained at the end of the chapter.
11. sales Analysis The file SalesData.txt, in this chapter’s source code folder, contains the dollar amount of sales that a retail store made each day for a number of weeks. Each line in the file contains seven numbers, which are the sales numbers for one week. The numbers are separated by a comma. The following line is an example from the file: 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 Write a program that opens the file and processes its contents. The program should display the following:
• The total sales for each week
• The average daily sales for each week
• The total sales for all of the weeks
• The average weekly sales
• The week number that had the highest amount of sales
• The week number that had the lowest amount of sales
Other Requirements:
The program file name must be: SalesAnalysisDemo.java
The data file that your program opens must be named as: SalesData.txt
Sample Data File attached here SalesData.txt
1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36 1867.24,3124.20,1301.09,1489.27,1822.45,2111.89,1425.56 3296.54,9147.88,1801.45,0866.77,2001.21,0096.24,1156.83 2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36
Test Case:
Weekly sales from week 1 is $12092.75
Average for week 1 is $1727.54
Weekly sales from week 2 is $27461.00
Average for week 2 is $3923.00
Weekly sales from week 3 is $12058.34
Average for week 3 is $1722.62
Weekly sales from week 4 is $13141.70
Average for week 4 is $1877.39
Weekly sales from week 5 is $18366.92
Average for week 5 is $2623.85
Weekly sales from week 6 is $12058.34
Average for week 6 is $1722.62
Total sale of all weeks = 95179.05
Average weekly sales = 15863.17
The week number with the highest amount of sales is: 2
The week number with the lowest amount of sales is: 3
Code:
====
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SalesAnalysisDemo {
public static void main(String[] args) throws
FileNotFoundException {
Scanner sc = new Scanner(new
File("SalesData.txt"));
String line = "";
String [] week_array = null;
double weekly_total = 0.0,
final_total = 0.0, min_val = 0.0, max_val = 0.0;
int week_num = 0, min_week = 0,
max_week=0;
while(sc.hasNextLine()) {
line
=sc.nextLine();
week_array =
line.split(",");
for(String
day_sale: week_array) {
weekly_total =
weekly_total+Double.parseDouble(day_sale);
final_total =
final_total+Double.parseDouble(day_sale);
}
System.out.println("Weekly sales from week " +(week_num+1)+" is $"+
Math.round(weekly_total*100)/100.0);
System.out.println("Average for week" +(week_num+1)+" is $"+
Math.round((weekly_total/7.0)*100)/100.0);
if(week_num==0)
{
min_val = weekly_total;
max_val = weekly_total;
}
else {
if(weekly_total<min_val) {
min_val = weekly_total;
min_week = week_num+1;
}
else if (weekly_total>max_val) {
max_val = weekly_total;
max_week = week_num+1;
}
}
weekly_total=0;
week_num++;
}
System.out.println("The week number
with the highest amount of sales is: "+max_week);
System.out.println("The week number
with the lowest amount of sales is: "+min_week);
sc.close();
}
}
output:
=====