Question Write a program that opens the file and processes its contents. The pro
ID: 3764450 • Letter: Q
Question
Question
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 sale
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
public class Sales {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Scanner data = new Scanner(new File("SalesData.txt"));
//variables for overall total
double total = 0; //Dollar amount of all sales
double avg = 0; //total average
//variables for weeks
double weekTotal = 0;
double weekAvg = 0;
String;
String[] tokens;
String[] tokenize;
while (data.hasNext())//will read file while it still has data to read
{
for (int i = 0; i < 3; i++) {
in one line
tokens = oneLine.split(",");//splits up text
for(String weekSales : tokens){//to get total sales for each week
weekTotal += Double.parseDouble(weekSales);
}
weekAvg = weekTotal/7;//gets average of each week
System.out.println("Week "+(i+1)+" sales: $" + String.format("%.2f",weekTotal) +
" Average daily sales for week #"+(i+1)+": $"+String.format("%.2f",weekAvg));
}
tokenize = oneLine.split(",");
for (String str : tokenize) {
total += Double.parseDouble(str);
}
avg = total / tokenize.length;//length is number of groups to divide by
}//end of while loop
data.close();//closes file
System.out.println("Total sales: $" + String.format("%.2f", total));
System.out.println("Average weekly sales: $" + String.format("%.2f", avg));
System.out.println("The highest sales were made during week #");
System.out.println("The lowest sales were made during week #");
}
}
Need this to be the output
Expected output:
Week #1 sales:12,092.75 Average daily sales for week #1: $1,727.54
Week #2 sales:27,461.00 Average daily sales for week #2: $3,923.00
Week #3 sales:12,058.34 Average daily sales for week #3: $1,722.62
Total sales for weeks: $51,612.09
Average weekly sales: $17,204.03
The highest sales were made during week #2.
The lowest sales were made during week #3.
NOT THIS
Week 1 sales: $12092.75 Average daily sales for week #1: $1727.54
Week 2 sales: $39553.75 Average daily sales for week #2: $5650.54
Week 3 sales: $51612.09 Average daily sales for week #3: $7373.16
Total sales: $12058.34
Average weekly sales: $1722.62
Explanation / Answer
Program:
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
public class Sales {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
Scanner data = new Scanner(new File("SalesData.txt"));
//variables for overall total
double total = 0; //Dollar amount of all sales
double avg = 0; //total average
//variables for weeks
double weekTotal = 0;
double weekAvg = 0;
String;
String[] tokens;
String[] tokenize;
double[] avgsales=new double[3];
int highest=0;
int lowest=0;
while (data.hasNext())//will read file while it still has data to read
{
for (int i = 0; i < 3; i++) {
oneLine = data.nextLine();//reads in one line
tokens = oneLine.split(",");//splits up text
for(String weekSales : tokens){//to get total sales for each week
weekTotal += Double.parseDouble(weekSales);
}
weekAvg = weekTotal/7;//gets average of each week
avgsales[i]=weekTotal;
System.out.println("Week "+(i+1)+" sales: $" + String.format("%.2f",weekTotal) +
" Average daily sales for week #"+(i+1)+": $"+String.format("%.2f",weekAvg));
}
tokenize = oneLine.split(",");
for (String str : tokenize) {
total += Double.parseDouble(str);
}
avg = total / tokenize.length;//length is number of groups to divide by
}//end of while loop
data.close();//closes file
for(int i=1;i<3;i++)
{
if(avgsales[highest]<avgsales[i])
{
highest=i;
}
if(avgsales[lowest]>avgsales[i])
{
lowest=i;
}
}
System.out.println("Total sales: $" + String.format("%.2f", total));
System.out.println("Average weekly sales: $" + String.format("%.2f", avg));
System.out.println("The highest sales were made during week "+(highest+1));
System.out.println("The lowest sales were made during week "+(lowest+1));
}
}
SalesData.txt
34,34,67,23,78,1213,34
46,21,56,123,67,12,56
67,45,23,67,23,868,12
Result:
Week 1 sales: $1483.00 Average daily sales for week #1: $211.86
Week 2 sales: $1864.00 Average daily sales for week #2: $266.29
Week 3 sales: $2969.00 Average daily sales for week #3: $424.14
Total sales: $1105.00
Average weekly sales: $157.86
The highest sales were made during week 3
The lowest sales were made during week 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.