The file SalesData.txt, in this chapter\'s source code folder, contains the doll
ID: 3532465 • Letter: T
Question
The file SalesData.txt, in this chapter's source code folder, contains the dollar amount of sales that a retal 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 number are separeted by a comma. the following line is an EXAMPLE from the file:
2541.36,2956.88,1968.32,1845.23,7021.11,9652.74,1469.36
write a program that opens the file and processes its comments. 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
Explanation / Answer
Hi,
Tested on dataset:
2541.36,2956.88,1968.32,1845.23,7021.11,9652.74,1469.36
12312.34,234.34,12312.34,12312.45,466.67,7,8
with output :
Weekly Sale for Week 1 is $27455.0
Average Daily Sale for Week 1 is $3922.1428571428573
Weekly Sale for Week 2 is $37653.14
Average Daily Sale for Week 2 is $5379.0199999999995
Total Sale of All Weeks = $65108.14
Average Weekly Sales is = $21702.713333333333
Highest Sale of 37653.14 on Week: 2
Lowest Sale of 27455.0 on Week: 1
Please find the code :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
public class FileReadAndCalc
{
public static void main(String[] args)
{
try
{
BufferedReader br = new BufferedReader(new FileReader("E:\1.txt"));
LinkedHashMap<Integer,ArrayList<Double>> totalSale=new LinkedHashMap<Integer,ArrayList<Double>>();
LinkedList<Double> weeklySale=new LinkedList<Double>();
String sCurrentLine;
int x=1;
while ((sCurrentLine = br.readLine()) != null)
{
ArrayList<Double> week=new ArrayList<Double>();
String[] salesForWeek=sCurrentLine.split(",");
for(int i=0;i<salesForWeek.length;i++)
{
week.add(Double.parseDouble(salesForWeek[i]));
}
totalSale.put(x,week);
x++;
}
//TotalSale and Average Daily Sale for each week
int count_i=1;
Iterator<Entry<Integer, ArrayList<Double>>> it = totalSale.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
double weekSale=0;
for(double sale:(ArrayList<Double>)pairs.getValue())
{
weekSale+=sale;
}
System.out.println("Weekly Sale for Week "+count_i+" is $"+ weekSale);
System.out.println("Average Daily Sale for Week "+count_i+" is $"+ (weekSale/7));
weeklySale.add(weekSale);
count_i++;
}
//Total Sale of All Weeks
double grandTotalSale=0;
for(double sale:weeklySale)
{
grandTotalSale+=sale;
}
System.out.println("Total Sale of All Weeks = $"+grandTotalSale);
//Average Weekly Sale
double total=0;
for(double s:weeklySale)
{
total+=s;
}
System.out.println("Average Weekly Sales is = $"+(total/count_i));
Collections.sort(weeklySale);
System.out.println("Highest Sale of "+weeklySale.getLast()+" on Week: "+(1+weeklySale.indexOf(weeklySale.getLast())));
System.out.println("Lowest Sale of "+weeklySale.getFirst()+" on Week: "+(1+weeklySale.indexOf(weeklySale.getFirst())));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Happy to help :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.