a java program rewrite with solution 1
Please start with the source code.
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 #)
The file contains the dollars amount of sales that a retail store made each day for a number of weeks. Each line in the file contains thirty five numbers, which are sales numbers for five weeks. The number are separated by space. Each line in the file represents a separate store.
Please make sure that you:
a. Add a class diagram with your submission.
b. Add comments to your code in FileIO class.
c. Make sure you adequately test your code.
d. Provide a user-friendly interface (Console based).
———————————————————————–===================================
import java.io.*;_x000D_ import java.util.*;_x000D_ class FileIO _x000D_ {_x000D_ //Declaration and initialization_x000D_ private String fname = null;_x000D_ private boolean DEBUG = false;_x000D_ _x000D_ //Constructor that initializes the fname_x000D_ public FileIO(String fname) _x000D_ {_x000D_ this.fname = fname;_x000D_ }_x000D_ _x000D_ _x000D_ public Franchise readData() _x000D_ {_x000D_ //Declaration and initialization_x000D_ Franchise a1 = null;_x000D_ int counter = 0;_x000D_ _x000D_ //try block_x000D_ try _x000D_ {_x000D_ //Declaration and initialization_x000D_ FileReader file = new FileReader(fname);_x000D_ BufferedReader buff = new BufferedReader(file);_x000D_ String temp;_x000D_ boolean eof = false;_x000D_ _x000D_ //while loop : parse the entire file_x000D_ while (!eof) _x000D_ {_x000D_ //increment counter_x000D_ counter++;_x000D_ _x000D_ //read the file line by line_x000D_ String line = buff.readLine();_x000D_ _x000D_ //if line is null, then re-initialize eof to true_x000D_ if (line == null)_x000D_ eof = true;_x000D_ _x000D_ //otherwise_x000D_ else _x000D_ {_x000D_ System.out.println("ntt***************** Data of Store #"+counter+" ********************");_x000D_ //if DEBUG is true, print line_x000D_ if (DEBUG)_x000D_ System.out.println("Reading.. " + line);_x000D_ _x000D_ //convert line to array of strings_x000D_ temp = line;_x000D_ String[] tempr=temp.split(" ");_x000D_ _x000D_ //a1 is the object of Franchise class ; parameterized constructor is called_x000D_ a1 = new Franchise(tempr.length);_x000D_ _x000D_ //function call : buildStore()_x000D_ int x = buildStore(a1, counter, line);_x000D_ _x000D_ //if DEBUG is true, print statements_x000D_ if (DEBUG)_x000D_ System.out.println("Reading Store # " + counter + " Number of weeks read = " + x);_x000D_ if (DEBUG) _x000D_ {_x000D_ System.out.println("Data read:");_x000D_ a1.getStores(counter).printdata(); _x000D_ } _x000D_ _x000D_ //Function calls_x000D_ a1.getStores(counter).totalsalesforweek();_x000D_ a1.getStores(counter).avgsalesforweek();_x000D_ a1.getStores(counter).totalsalesforallweeks();_x000D_ a1.getStores(counter).averageweeklysales();_x000D_ a1.getStores(counter).weekwithhighestsaleamt();_x000D_ a1.getStores(counter).weekwithlowestsaleamt();_x000D_ }_x000D_ } //end of while_x000D_ buff.close();_x000D_ } //end of try block_x000D_ _x000D_ //catch block_x000D_ catch (Exception e) _x000D_ {_x000D_ System.out.println("Error -- " + e.toString());_x000D_ }_x000D_ //return object of Franchise class_x000D_ return a1;_x000D_ }_x000D_ _x000D_ _x000D_ public int buildStore(Franchise a1, int counter, String temp)_x000D_ {_x000D_ //Declaration and initialization_x000D_ Store tstore = new Store();_x000D_ String s1 = "";_x000D_ float sale = 0.0f;_x000D_ int week = 0;_x000D_ int day = 0; _x000D_ StringTokenizer st = new StringTokenizer(temp);_x000D_ _x000D_ //while loop: loop through the entire line, for each store_x000D_ while (st.hasMoreTokens()) _x000D_ {_x000D_ //for loop: for each week_x000D_ for (day = 0; day < 7; day++) _x000D_ {_x000D_ s1 = st.nextToken();_x000D_ //compute sale_x000D_ sale = Float.parseFloat(s1);_x000D_ //function call : setsaleforweekdayintersection()_x000D_ tstore.setsaleforweekdayintersection(week, day, sale);_x000D_ }_x000D_ week++;_x000D_ } //end of while loop_x000D_ _x000D_ //function call : setStores()_x000D_ a1.setStores(tstore, counter);_x000D_ return week;_x000D_ }_x000D_ }_x000D_ class Franchise _x000D_ {_x000D_ //Declare array of type Store_x000D_ private Store stores[];_x000D_ _x000D_ //parameterized constructor_x000D_ public Franchise(int num) _x000D_ {_x000D_ stores = new Store[num];_x000D_ }_x000D_ _x000D_ //function getStores() returns object of type Store_x000D_ public Store getStores(int i) _x000D_ {_x000D_ return stores[i];_x000D_ }_x000D_ _x000D_ //function setStores() initializes stores array_x000D_ public void setStores(Store stores, int i) _x000D_ {_x000D_ this.stores[i] = stores;_x000D_ }_x000D_ _x000D_ //function numberofstores() returns the length of the stores array_x000D_ public int numberofstores() _x000D_ {_x000D_ return stores.length;_x000D_ }_x000D_ _x000D_ }_x000D_ _x000D_ class Store _x000D_ {_x000D_ //Declare array of type float to store sales data_x000D_ private float salesbyweek[][];_x000D_ _x000D_ //Constructor_x000D_ Store() _x000D_ {_x000D_ salesbyweek = new float[5][7];_x000D_ }_x000D_ _x000D_ //function setsaleforweekdayintersection() initializes salesbyweek array_x000D_ public void setsaleforweekdayintersection(int week, int day, float sale) _x000D_ {_x000D_ salesbyweek[week][day] = sale;_x000D_ } _x000D_ _x000D_ //function printdata() prints the sales data week by week_x000D_ public void printdata() _x000D_ {_x000D_ for (int i = 0; i < 5; i++) _x000D_ {_x000D_ for (int j = 0; j < 7; j++) _x000D_ {_x000D_ System.out.print(salesbyweek[i][j] + " ");_x000D_ }_x000D_ System.out.println("");_x000D_ }_x000D_ }_x000D_ _x000D_ //function totalsalesforweek() computes and prints total sales for each week_x000D_ public void totalsalesforweek()_x000D_ {_x000D_ int total;_x000D_ System.out.print("nTotal Sales for week:");_x000D_ //for loop: loop through the sales data for each store_x000D_ for (int i = 0; i < 5; i++) _x000D_ {_x000D_ //initialize total to 0_x000D_ total=0;_x000D_ _x000D_ //for loop : loop through the sales data for each week_x000D_ for (int j = 0; j < 7; j++) _x000D_ {_x000D_ //total is the sum of sales data for each week _x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ //print total : total sales for each week_x000D_ System.out.print("t$"+total);_x000D_ }_x000D_ }_x000D_ _x000D_ _x000D_ //function avgsalesforweek() computes and prints average sales for each week_x000D_ public void avgsalesforweek()_x000D_ {_x000D_ float avg,total;_x000D_ System.out.print("nAverage Sales for week:");_x000D_ //for loop: loop through the sales data for each store_x000D_ for (int i = 0; i < 5; i++) _x000D_ {_x000D_ //initialize total and avg to 0_x000D_ total=0;_x000D_ avg=0;_x000D_ _x000D_ //for loop : loop through the sales data for each week_x000D_ for (int j = 0; j < 7; j++) _x000D_ {_x000D_ //total is the sum of sales data for each week _x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ //avg=(total sales in a week)/(number of days in a week)_x000D_ avg=total/7;_x000D_ //print avg : average sales for each week_x000D_ System.out.print("t$"+avg);_x000D_ }_x000D_ }_x000D_ _x000D_ _x000D_ //function totalsalesforallweeks() computes and prints total sales for all weeks_x000D_ public void totalsalesforallweeks()_x000D_ {_x000D_ //initialize total to 0_x000D_ int total=0;_x000D_ System.out.print("nTotal Sales for all weeks:");_x000D_ _x000D_ //for loop: loop through the sales data for each store_x000D_ for (int i = 0; i < 5; i++) _x000D_ {_x000D_ for (int j = 0; j < 7; j++) _x000D_ {_x000D_ //total is the sum of sales data for each store _x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ }_x000D_ //print total : total sales for all weeks_x000D_ System.out.print("t$"+total);_x000D_ }_x000D_ _x000D_ _x000D_ //function averageweeklysales() computes and prints average weekly sales._x000D_ public void averageweeklysales()_x000D_ {_x000D_ float avg,total=0,weekly_total=0;_x000D_ System.out.print("nAverage weekly sales:");_x000D_ _x000D_ //for loop: loop through the sales data for each store_x000D_ for (int i = 0; i < 5; i++) _x000D_ {_x000D_ //initialize total and avg to 0_x000D_ total=0; avg=0;_x000D_ _x000D_ //for loop : loop through the sales data for each week_x000D_ for (int j = 0; j < 7; j++) _x000D_ {_x000D_ //total is the sum of sales data for each week_x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ _x000D_ //avg=(total sales in a week)/(number of days in a week)_x000D_ avg=total/7;_x000D_ _x000D_ //weekly_total is the sum of avg_x000D_ weekly_total+=avg;_x000D_ }_x000D_ _x000D_ //avg=(weekly_total)/(number of weeks)_x000D_ avg=weekly_total/5;_x000D_ //print avg : average weekly sales_x000D_ System.out.print("t$"+avg);_x000D_ }_x000D_ _x000D_ _x000D_ _x000D_ //function weekwithhighestsaleamt() determines and prints week with the highest sale amount_x000D_ public void weekwithhighestsaleamt()_x000D_ {_x000D_ //declaration and initialization. Initialize max and week to 0_x000D_ int total,i,j,max=0,week=0;_x000D_ _x000D_ //for loop: loop through the sales data for each store_x000D_ for (i = 0; i < 5; i++) _x000D_ {_x000D_ //initialize total to 0 for each week_x000D_ total=0;_x000D_ _x000D_ for (j = 0; j < 7; j++)_x000D_ {_x000D_ //total is the sum of sales data for each week_x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ _x000D_ //initialize max with total sales amount of the 1st week and let week be 0 _x000D_ if(i==0)_x000D_ {_x000D_ max=total;_x000D_ week=0;_x000D_ }_x000D_ _x000D_ //if i is not 0_x000D_ else_x000D_ {_x000D_ //if total is greater than max, then initialize max to total and week to i_x000D_ if(total>max)_x000D_ {_x000D_ max=total;_x000D_ week=i;_x000D_ }_x000D_ }_x000D_ }_x000D_ System.out.print("n"+(week+1) + "th week has the highest sale with amount $"+max);_x000D_ } _x000D_ _x000D_ _x000D_ _x000D_ //function weekwithlowestsaleamt() determines and prints week with the lowest sale amount_x000D_ public void weekwithlowestsaleamt()_x000D_ {_x000D_ //declaration and initialization. Initialize min and week to 0_x000D_ int total,i,j,min=0,week=0;_x000D_ _x000D_ //for loop: loop through the sales data for each store_x000D_ for (i = 0; i < 5; i++) _x000D_ {_x000D_ //initialize total to 0 for each week_x000D_ total=0;_x000D_ _x000D_ for (j = 0; j < 7; j++) _x000D_ {_x000D_ //total is the sum of sales data for each week_x000D_ total+=salesbyweek[i][j];_x000D_ }_x000D_ _x000D_ //initialize min with total sales amount of the 1st week and let week be 0 _x000D_ if(i==0)_x000D_ {_x000D_ min=total;_x000D_ week=0;_x000D_ }_x000D_ _x000D_ //if i is not 0_x000D_ else_x000D_ {_x000D_ //if total is lesser than min, then initialize min to total and week to i_x000D_ if(total<min)_x000D_ {_x000D_ min=total;_x000D_ week=i;_x000D_ }_x000D_ }_x000D_ }_x000D_ System.out.println("n"+(week+1) + "th week has the lowest sale with amount $"+min);_x000D_ }_x000D_ _x000D_ }_x000D_ _x000D_ _x000D_ public class Driver _x000D_ {_x000D_ public static void main(String[] args) _x000D_ {_x000D_ // TODO Auto-generated method stub _x000D_ FileIO a1 = new FileIO("C:\Users\Ranjini\OneDrive\Documents\javaprogs\NON LIVE\Salesdat.txt");_x000D_ Franchise f = a1.readData();_x000D_ }_x000D_ }_x000D_