Question: Java help please Write a program that opens the salesdat.txt file and processes it contents. The program should display the following per store: The total
Java help please
Write a program that opens the salesdat.txt file and processes it contents. The program should display the following per store:
The total sales for each week. (Should print 5 values - one for each week).
The average daily sales for each week. (Should print 5 values - one for each week).
The total sales for all the weeks. (Should print 1 value)
The average weekly sales. (Should print 1 value)
The week with the highest amount in sales. (Should print 1 week #)
The week with the lowest amount in sales. (Should print 1 week #)
Here is my code, and I don't know why it's not working. Please check it for me..
And also could you explain what was wrong? Tnx in advance
// FileIO.java
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
public class FileIO { //Franchise readData(String filename) public static void main(String[] args) { try { FileReader file = new FileReader("Salesdat.txt"); BufferedReader buff = new BufferedReader(file); boolean eof = false; while (!eof) { String line = buff.readLine(); if (line == null) eof = true; else System.out.println(line); } buff.close(); } catch (IOException e) { System.out.println("Error -- " + e.toString()); } } }
//Store.java
public class Store { private float salesByWeek[][]; public Store() { salesByWeek = new float[5][7]; } public void setSaleForWeekDayIntersection(int week, int day, float sale) { salesByWeek[week][day] = sale; } float[] getSalesForEntireWeek(int week) { float[] sales = new float[7]; for (int d = 0; d < 7; d++) { sales[d] = salesByWeek[week][d]; } return sales; } float getSaleForWeekDayIntersection(int week, int day) { return salesByWeek[week][day]; } float getTotalSalesForWeek(int week) { float total = 0; for (int d = 0; d < 7; d++) { total += salesByWeek[week][d]; } return total; } float getAverageSalesForWeek(int week) { return getTotalSalesForWeek(week) / 7; } float getTotalSalesForAllWeeks() { float total = 0; for (int w = 0; w < 5; w++) { total += getTotalSalesForWeek(w); } return total; } float getAverageWeeklySales() { return getTotalSalesForAllWeeks() / 5; } int getWeekWithHighestSaleAmount() { int maxWeek = 0; float maxSale = -1; for (int w = 0; w < 5; w++) { float sale = getTotalSalesForWeek(w); if (sale > maxSale) { maxSale = sale; maxWeek = w; } } return maxWeek; } int getWeekWithLowestSaleAmount() { int minWeek = 0; float minSale = Float.MAX_VALUE; for (int w = 0; w < 5; w++) { float sale = getTotalSalesForWeek(w); if (sale < minSale) { minSale = sale; minWeek = w; } } return minWeek; } public void analyzeResults() { for (int w = 0; w < 5; w++) { System.out.printf("---- Week %d ---- ", w); System.out.printf(" Total sales: %.2f ", getTotalSalesForWeek(w)); System.out.printf(" Average sales: %.2f ", getAverageSalesForWeek(w)); } System.out.printf(" "); System.out.printf("Total sales for all weeks: %.2f ", getTotalSalesForAllWeeks()); System.out.printf("Average weekly sales: %.2f ", getAverageWeeklySales()); System.out.printf("Week with highest sale: %d ", getWeekWithHighestSaleAmount()); System.out.printf("Week with lowest sale: %d ", getWeekWithLowestSaleAmount()); } }
//Francise.java
public class Franchise { private Store stores[];
public Franchise(int num) { stores = new Store[num]; }
public Store getStores(int i) { return stores[i]; }
public void setStores(Store stores, int i) { this.stores[i] = stores; } }
//Driver.java
public class Driver { public static void main(String[] args) { Franchise f = FileIO.readData("Salesdat.txt"); for (int i = 0; i < 6; i++) { Store s = f.getStore(i); System.out.printf("==== Store %d ==== ", i); s.analyzeResults(); } } }
//salesdat.txt
// it preserves pattern to day 35
Day1 Day2 Day3 Day4 Day5 Day6 Day7 Day8 2541.56 2258.96 2214 2256 2154 2398 2597 2684 2041.56 1758.96 1714 1756 1654 1898 2097 2184 3041.56 2758.96 2714 2756 2654 2898 3097 3184 3563.54 3280.94 3235.98 3277.98 3175.98 3419.98 3618.98 3705.98 2547.21 2264.61 2219.65 2261.65 2159.65 2403.65 2602.65 2689.65 4040.55 3757.95 3712.99 3754.99 3652.99 3896.99 4095.99 4182.99
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
