question archive Sales Analysis The file SalesData
Subject:Computer SciencePrice:3.87 Bought7
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
Answer:
Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SalesData {
public static void main(String[] args) {
processesInputFile("SalesData.txt");
}
public static void processesInputFile(String fileName) {
Scanner sacnnerReader = null;
try {
File file = new File(fileName);
if (file.exists()) {
int weekCount = 0;
sacnnerReader = new Scanner(file);
while (sacnnerReader.hasNextLine()) {
sacnnerReader.nextLine();
weekCount++;
}
sacnnerReader.close();
double weeklySales[][] = new double[weekCount][7];
sacnnerReader = new Scanner(file);
weekCount = 0;
while (sacnnerReader.hasNextLine()) {
double dailySales[] = new double[7];
String line = sacnnerReader.nextLine();
String lineArray[] = line.split(",");
for (int i=0;i<lineArray.length;i++) {
dailySales[i] = Double.parseDouble(lineArray[i].trim());
}
weeklySales[weekCount] = dailySales;
weekCount++;
}
sacnnerReader.close();
displayData(weeklySales);
} else {
System.out.println("Input File not found");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sacnnerReader != null) {
try {
sacnnerReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void displayData(double weeklySales[][]) {
double totalSales = 0;
double avgWeeklySales = 0;
double totalWeeklySales[] = new double[weeklySales.length];
for (int i=0;i<weeklySales.length;i++) {
double totalWeeklySale = 0;
double avgDailySales = 0;
for (int j=0;j<weeklySales[i].length;j++) {
totalWeeklySale = totalWeeklySale + weeklySales[i][j];
}
totalWeeklySales[i] = totalWeeklySale;
totalSales = totalSales + totalWeeklySale;
avgDailySales = totalWeeklySale/7;
System.out.println("The total sales for "+(i+1)+" week: "+String.format("%.2f", totalWeeklySale));
System.out.println("The average daily sales for "+(i+1)+" week: "+String.format("%.2f", avgDailySales));
System.out.println();
}
avgWeeklySales = totalSales/weeklySales.length;
System.out.println("The total sales for all of the weeks: "+String.format("%.2f", totalSales));
System.out.println("The average weekly sales: "+String.format("%.2f", avgWeeklySales));
System.out.println();
double highestSale = totalWeeklySales[0];
int highestSaleWeek = 0;
double lowestSale = totalWeeklySales[0];
int lowestSaleWeek = 0;
for (int i=0;i<totalWeeklySales.length;i++) {
if (totalWeeklySales[i] > highestSale) {
highestSale = totalWeeklySales[i];
highestSaleWeek = i;
}
if (totalWeeklySales[i] < lowestSale) {
lowestSale = totalWeeklySales[i];
lowestSaleWeek = i;
}
}
System.out.println("The week "+(highestSaleWeek+1)+" that had the highest amount of sales "+String.format("%.2f", highestSale));
System.out.println("The week "+(lowestSaleWeek+1)+" that had the lowest amount of sales "+String.format("%.2f", lowestSale));
}
}
****** Input File (SalesData.txt) *******
1965.36,2965.88,1965.32,1845.23,7021.11,9652.74,1845.36
2241.36,1265.88,1965.32,4322.23,7221.11,1965.74,2965.36
2541.36,2541.88,9652.32,1845.23,2965.11,4322.74,4322.36
9652.36,4322.88,1965.32,4322.23,7021.11,1965.74,2541.36
1965.36,2541.88,1965.32,1845.23,1021.11,4322.74,1469.36
2541.36,2215.88,4322.32,1845.23,7021.11,9652.74,9652.36
9652.36,2965.88,4322.32,2965.23,7021.11,4322.74,1469.36
2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
******** Output *******
The total sales for 1 week: 27261.00
The average daily sales for 1 week: 3894.43
The total sales for 2 week: 21947.00
The average daily sales for 2 week: 3135.29
The total sales for 3 week: 28191.00
The average daily sales for 3 week: 4027.29
The total sales for 4 week: 31791.00
The average daily sales for 4 week: 4541.57
The total sales for 5 week: 15131.00
The average daily sales for 5 week: 2161.57
The total sales for 6 week: 37251.00
The average daily sales for 6 week: 5321.57
The total sales for 7 week: 32719.00
The average daily sales for 7 week: 4674.14
The total sales for 8 week: 27461.00
The average daily sales for 8 week: 3923.00
The total sales for all of the weeks: 221752.00
The average weekly sales: 27719.00
The week 6 that had the highest amount of sales 37251.00
The week 5 that had the lowest amount of sales 15131.00