A java program rewrite with solution

Anonymous
timer Asked: Aug 3rd, 2018
account_balance_wallet $15

Question description

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.*;
import java.util.*;
class FileIO 
{
        //Declaration and initialization
                private String fname = null;
                private boolean DEBUG = false;
        
        //Constructor that initializes the fname
                public FileIO(String fname) 
                {
                        this.fname = fname;
                }
        
        
        public Franchise readData() 
        {
                //Declaration and initialization
                        Franchise a1 = null;
                        int counter = 0;
                        
                //try block
                        try 
                        {
                                //Declaration and initialization
                                        FileReader file = new FileReader(fname);
                                        BufferedReader buff = new BufferedReader(file);
                                        String temp;
                                        boolean eof = false;
                                        
                                //while loop : parse the entire file
                                        while (!eof) 
                                        {
                                                //increment counter
                                                        counter++;
                                                
                                                //read the file line by line
                                                        String line = buff.readLine();
                                                        
                                                //if line is null, then re-initialize eof to true
                                                        if (line == null)
                                                                eof = true;
                                                
                                                //otherwise
                                                        else 
                                                        {
                                                                System.out.println("\n\t\t***************** Data of Store #"+counter+" ********************");
                                                                //if DEBUG is true, print line
                                                                        if (DEBUG)
                                                                                System.out.println("Reading.. " + line);
                                                                
                                                                //convert line to array of strings
                                                                        temp = line;
                                                                        String[] tempr=temp.split(" ");
                                                                        
                                                                //a1 is the object of Franchise class ; parameterized constructor is called
                                                                        a1 = new Franchise(tempr.length);
                                                                        
                                                                //function call : buildStore()
                                                                        int x = buildStore(a1, counter, line);
                                                                        
                                                                //if DEBUG is true, print statements
                                                                        if (DEBUG)
                                                                                                System.out.println("Reading Store # " + counter + " Number of weeks read = " + x);
                                                                        if (DEBUG) 
                                                                                        {
                                                                                                System.out.println("Data read:");
                                                                                                a1.getStores(counter).printdata(); 
                                                                                        } 
                                                                                        
                                                                //Function calls
                                                                        a1.getStores(counter).totalsalesforweek();
                                                                        a1.getStores(counter).avgsalesforweek();
                                                                        a1.getStores(counter).totalsalesforallweeks();
                                                                        a1.getStores(counter).averageweeklysales();
                                                                        a1.getStores(counter).weekwithhighestsaleamt();
                                                                        a1.getStores(counter).weekwithlowestsaleamt();
                                                        }
                                        }       //end of while
                                buff.close();
                        }       //end of try block
                
                //catch block
                        catch (Exception e) 
                        {
                                System.out.println("Error -- " + e.toString());
                        }
                //return object of Franchise class
                        return a1;
        }
        
        
        public int buildStore(Franchise a1, int counter, String temp)
        {
                //Declaration and initialization
                        Store tstore = new Store();
                        String s1 = "";
                        float sale = 0.0f;
                        int week = 0;
                        int day = 0; 
                        StringTokenizer st = new StringTokenizer(temp);
                        
                //while loop: loop through the entire line, for each store
                        while (st.hasMoreTokens()) 
                        {
                                //for loop: for each week
                                        for (day = 0; day < 7; day++) 
                                        {
                                                s1 = st.nextToken();
                                                //compute sale
                                                        sale = Float.parseFloat(s1);
                                                //function call : setsaleforweekdayintersection()
                                                        tstore.setsaleforweekdayintersection(week, day, sale);
                                        }
                                week++;
                        }       //end of while loop
                        
                //function call : setStores()
                        a1.setStores(tstore, counter);
                return week;
        }
}
class Franchise 
{
        //Declare array of type Store
                private Store stores[];
        
        //parameterized constructor
                public Franchise(int num) 
                {
                        stores = new Store[num];
                }
                
        //function getStores() returns object of type Store
                public Store getStores(int i) 
                {
                        return stores[i];
                }
                
        //function setStores() initializes stores array
                public void setStores(Store stores, int i) 
                {
                        this.stores[i] = stores;
                }
                
        //function numberofstores() returns the length of the stores array
                public int numberofstores() 
                {
                        return stores.length;
                }
                
}

class Store 
{
        //Declare array of type float to store sales data
                private float salesbyweek[][];
        
        //Constructor
                Store() 
                {
                        salesbyweek = new float[5][7];
                }
                
        //function setsaleforweekdayintersection() initializes salesbyweek array
                public void setsaleforweekdayintersection(int week, int day, float sale) 
                {
                        salesbyweek[week][day] = sale;
                } 
                
        //function printdata() prints the sales data week by week
                public void printdata() 
                {
                        for (int i = 0; i < 5; i++) 
                        {
                                for (int j = 0; j < 7; j++) 
                                {
                                        System.out.print(salesbyweek[i][j] + " ");
                                }
                                System.out.println("");
                        }
                }
        
        //function totalsalesforweek() computes and prints total sales for each week
                public void totalsalesforweek()
                {
                        int total;
                        System.out.print("\nTotal Sales for week:");
                        //for loop: loop through the sales data for each store
                                for (int i = 0; i < 5; i++) 
                                {
                                        //initialize total to 0
                                                total=0;
                                                
                                        //for loop : loop through the sales data for each week
                                                for (int j = 0; j < 7; j++) 
                                                {
                                                        //total is the sum of sales data for each week 
                                                                total+=salesbyweek[i][j];
                                                }
                                        //print total : total sales for each week
                                                System.out.print("\t$"+total);
                                }
                }
        
        
        //function avgsalesforweek() computes and prints average sales for each week
                public void avgsalesforweek()
                {
                        float avg,total;
                        System.out.print("\nAverage Sales for week:");
                        //for loop: loop through the sales data for each store
                                for (int i = 0; i < 5; i++) 
                                {
                                        //initialize total and avg to 0
                                                total=0;
                                                avg=0;
                                                
                                        //for loop : loop through the sales data for each week
                                                for (int j = 0; j < 7; j++) 
                                                {
                                                        //total is the sum of sales data for each week 
                                                                total+=salesbyweek[i][j];
                                                }
                                        //avg=(total sales in a week)/(number of days in a week)
                                                avg=total/7;
                                        //print avg : average sales for each week
                                                System.out.print("\t$"+avg);
                                }
                }
        
        
        //function totalsalesforallweeks() computes and prints total sales for all weeks
                public void totalsalesforallweeks()
                {
                        //initialize total to 0
                                int total=0;
                        System.out.print("\nTotal Sales for all weeks:");
                        
                        //for loop: loop through the sales data for each store
                                for (int i = 0; i < 5; i++) 
                                {
                                        for (int j = 0; j < 7; j++) 
                                        {
                                                //total is the sum of sales data for each store 
                                                total+=salesbyweek[i][j];
                                        }
                                }
                        //print total : total sales for all weeks
                                System.out.print("\t$"+total);
                }
        
        
        //function averageweeklysales() computes and prints average weekly sales.
                public void averageweeklysales()
                {
                        float avg,total=0,weekly_total=0;
                        System.out.print("\nAverage weekly sales:");
                        
                        //for loop: loop through the sales data for each store
                                for (int i = 0; i < 5; i++) 
                                {
                                        //initialize total and avg to 0
                                                total=0; avg=0;
                                                
                                        //for loop : loop through the sales data for each week
                                                for (int j = 0; j < 7; j++) 
                                                {
                                                        //total is the sum of sales data for each week
                                                                total+=salesbyweek[i][j];
                                                }
                                                
                                                //avg=(total sales in a week)/(number of days in a week)
                                                        avg=total/7;
                                                        
                                                //weekly_total is the sum of avg
                                                        weekly_total+=avg;
                                }
                        
                        //avg=(weekly_total)/(number of weeks)
                                avg=weekly_total/5;
                        //print avg : average weekly sales
                                System.out.print("\t$"+avg);
                }
        
        
        
        //function weekwithhighestsaleamt() determines and prints week with the highest sale amount
                public void weekwithhighestsaleamt()
                {
                        //declaration and initialization. Initialize max and week to 0
                                int total,i,j,max=0,week=0;
                                
                        //for loop: loop through the sales data for each store
                                for (i = 0; i < 5; i++) 
                                {
                                        //initialize total to 0 for each week
                                                total=0;
                                                
                                        for (j = 0; j < 7; j++)
                                        {
                                                //total is the sum of sales data for each week
                                                        total+=salesbyweek[i][j];
                                        }
                                                
                                        //initialize max with total sales amount of the 1st week and let week be 0 
                                                if(i==0)
                                                {
                                                        max=total;
                                                        week=0;
                                                }
                                                
                                        //if i is not 0
                                                else
                                                {
                                                        //if total is greater than max, then initialize max to total and week to i
                                                                if(total>max)
                                                                {
                                                                        max=total;
                                                                        week=i;
                                                                }
                                                }
                                }
                        System.out.print("\n"+(week+1) + "th week has the highest sale with amount $"+max);
                }       
        
        
        
        //function weekwithlowestsaleamt() determines and prints week with the lowest sale amount
                public void weekwithlowestsaleamt()
                {
                        //declaration and initialization. Initialize min and week to 0
                                int total,i,j,min=0,week=0;
                                
                        //for loop: loop through the sales data for each store
                                for (i = 0; i < 5; i++) 
                                {
                                        //initialize total to 0 for each week
                                                total=0;
                                                
                                        for (j = 0; j < 7; j++) 
                                        {
                                                //total is the sum of sales data for each week
                                                        total+=salesbyweek[i][j];
                                        }
                                                
                                        //initialize min with total sales amount of the 1st week and let week be 0 
                                                if(i==0)
                                                {
                                                        min=total;
                                                        week=0;
                                                }
                                                
                                        //if i is not 0
                                                else
                                                {
                                                        //if total is lesser than min, then initialize min to total and week to i
                                                                if(total<min)
                                                                {
                                                                        min=total;
                                                                        week=i;
                                                                }
                                                }
                                }
                        System.out.println("\n"+(week+1) + "th week has the lowest sale with amount $"+min);
                }
                
        }

        
public class Driver 
{
        public static void main(String[] args) 
        {
                // TODO Auto-generated method stub 
                                FileIO a1 = new FileIO("C:\\Users\\Ranjini\\OneDrive\\Documents\\javaprogs\\NON LIVE\\Salesdat.txt");
                                Franchise f = a1.readData();
        }
}

Tutor Answer

GitHub
School: Purdue University

Hello buddy, i have f...

flag Report DMCA
Review

Anonymous
Totally impressed with results!! :-)

Similar Questions
Hot Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors