Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java help PLEASE! fix my code, and don\'t build your own code please.. And also

ID: 3663436 • Letter: J

Question

Java help PLEASE!

fix my code, and don't build your own code please..

And also provide me explanations of the code..

Thank you for your help!

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.

// 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());
}
  
  
public void setsaleforweekdayintersection(int week, int day, float f) {
  
  
}
}

//Franchise.java

public class Franchise {
   private Store stores[];

   public Franchise(int num) {
           stores = new Store[num];
           for(int i=0;i            stores[i] = new Store();
   }

   public Store getStores(int i) {
       return stores[i];
   }

   public void setStores(Store stores, int i) {
       this.stores[i] = stores;
   }
  
  
}

//FileIO.java

import java.io.*;
import java.util.StringTokenizer;

public class FileIO {
   // Franchise readData(String filename)
   Franchise readData(String filename, int numstores) {
       Franchise f1 = new Franchise(numstores);
       boolean DEBUG = true;
       int ctr = 0;
       // open the file
       // read the line
       // parse the line - get one value
       // and set it in the correct location in 2 d array
       try {
           FileReader file = new FileReader(filename);
           BufferedReader buff = new BufferedReader(file);
           boolean eof = false;
           while (!eof) {
               String line = buff.readLine();
               ctr++;
               if (line == null)
                   eof = true;
               else {
                   if (DEBUG)
                       System.out.println(line);
                   if (ctr > 1) {
                       StringTokenizer a = new StringTokenizer(line);
                       for (int week = 0; week < 5; week++) {
                           for (int day = 0; day < 7; day++) {
                               String l = a.nextToken();
                               float f = Float.parseFloat(l);
                               f1.getStores(ctr - 2)
                                       .setsaleforweekdayintersection(week,
                                               day, f);
                               if (DEBUG)
                                   System.out.print("f" + f + " ");
                           }
                       }
                   }
               }

           }
       } catch (IOException f2) {

       }
       return f1;
   }
}

//Driver.java


public class Driver
{
public static void main(String[] args)
{
   FileIO readdata = new FileIO();
   Franchise f1 = readdata.readData("C:/Users/Jamie/workspace/Assignment3/src/Salesdat.txt", 6);
   System.out.println("Data read");
  
}

}

// salesdat.txt (Sample)

// This pattern will be preserved to Day 35

Day1 Day2 Day3 Day4 Day5

2541.56 2258.96 2214 2256 2154

2041.56 1758.96 1714 1756 1654

3041.56 2758.96 2714 2756 2654

3563.54 3280.94 3235.98 3277.98 2547.21

2264.61 2219.65 2261.65 4040.55 3757.95

Explanation / Answer

import java.io.IOException;

The following error was corrected at the line number 184 in Franshise class:

for(int i=0; i<=num; i++) stores[i] = new Store();

Also

for testing purpose the file location is changed as follows:(from C drive to E drive)

//Franchise f1 = readdata.readData("C:/Users/Jamie/workspace/Assignment3/src/Salesdat.txt", 6);
   Franchise f1 = readdata.readData("E:/Files/Salesdat.txt", 6);
while running at the computer at the side of Jamie, the comments have to be swapped as follows:

Franchise f1 = readdata.readData("C:/Users/Jamie/workspace/Assignment3/src/Salesdat.txt", 6);
// Franchise f1 = readdata.readData("E:/Files/Salesdat.txt", 6);

package store;

import java.io.IOException;

public class Store {

    // Store.java

   private float salesByWeek[][];

   public Store() {
    
       salesByWeek = new float[5][7];
       // assign the array value at index 5, t to salesByWeek
   }


   public void setSaleForWeekDayIntersection(int week, int day, float sale) {
    
       salesByWeek[week][day] = sale;
       // store the sale value to SalesByWeek array at the index pointed to by week, day
       // for exaample, it can be week 2 and day 3 (Wednesday)
             
    
   }


   float[] getSalesForEntireWeek(int week) {
       // this will find the total sales for the whole week - all 5 days or 7 days including week ends Saturday and Sunday
    
       float[] sales = new float[7];
       // declare an array of type float and of size 7 - name the array as sales
    
           for (int d = 0; d < 7; d++)
               {

                   sales[d] = salesByWeek[week][d];
                   // the index d runs from 0 to 7
                
               }
           return sales;
        
   }


   float getSaleForWeekDayIntersection(int week, int day) {
    
       return salesByWeek[week][day];
       // the return value is the arraycontent pointed to by index week and day
    
   }

   float getTotalSalesForWeek(int week) {
    
       float total = 0;
    
           for (int d = 0; d < 7; d++)
           {
               total += salesByWeek[week][d];
             
               // increment total by adding the array content salesByWeek at index week, d ( if d is the day)
             
               }
        
           return total;
           // send the value of total back to the caller function
        
   }


   float getAverageSalesForWeek(int week) {
    
       return getTotalSalesForWeek(week) / 7;
       // divide the total sales for the whole week by 7 so that we get the average sales and return it
    
   }


   float getTotalSalesForAllWeeks() {
    
       float total = 0; // declare a total variable of type float and initialize to 0 ( zero)

           for (int w = 0; w < 5; w++)
           {
            
               total += getTotalSalesForWeek(w);
               // sum up the total for the whole week and store it to the total variable
            
           }

           return total;
          // return the sum computed above
   }


   float getAverageWeeklySales() {
    
       return getTotalSalesForAllWeeks() / 5;
       // AVERAGE for 5 days - just Monday to Friday only - excludes the week ends
    
   }


   int getWeekWithHighestSaleAmount() {
       // top performing sales in the whole week
    
       int maxWeek = 0;
       float maxSale = -1;

           for (int w = 0; w < 5; w++)
               // run the for loop from 0 to 5 in steps of 1
           {
            
               float sale = getTotalSalesForWeek(w);
               // first store the total sales in to the sale variable of type float
               if (sale > maxSale)
                   {    // if at all if we find any amount greater than the max sale then replace max sale with the new sale amount
                       // and also note down the contributor - in the sense that which w ( week) achieved top sales
                   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;
               }
           }
          // comments are same as the top sales except in reverse order
           // first store an arbitary minimum sale figure
           // then compare each running week's vaue with the lowest
           // if at all when we encounter any value lower than the preset value then replace it
           return minWeek;
          // finally return the minimum value in that week
   }

   public void analyzeResults() {
    
       for (int w = 0; w < 5; w++) // run the for loop from 0 to 5
       {
           System.out.printf("---- Week %d ---- ", w); // print a title decoration
           System.out.printf(" Total sales: %.2f ", getTotalSalesForWeek(w)); // display or print out the total sales summed earlier in called function
           System.out.printf(" Average sales: %.2f ", getAverageSalesForWeek(w)); // display the average sales figure
        
       }
       System.out.printf(" ");
       System.out.printf("Total sales for all weeks: %.2f ", getTotalSalesForAllWeeks());    // print sum of the sales for the entire week
       System.out.printf("Average weekly sales: %.2f ", getAverageWeeklySales()); // print weekly average sales
       System.out.printf("Week with highest sale: %d ", getWeekWithHighestSaleAmount());   // print highest performing or top sales
       System.out.printf("Week with lowest sale: %d ", getWeekWithLowestSaleAmount()); // print lowest sales or the struggling week
}


public void setsaleforweekdayintersection(int week, int day, float f) {


}
}

//Franchise.java

public class Franchise {
   private Store stores[];

   public Franchise(int num) { // now for a franchise store
           stores = new Store[num];   // instantiate an array object of type class Store
           // the class is Store
           // the objects are named as stores
         
           for(int i=0; i<=num; i++) stores[i] = new Store();
   }

   public Store getStores(int i) { // GETTER display or return values
       return stores[i];
   }

   public void setStores(Store stores, int i) { // setter assign values
       this.stores[i] = stores;
   }


}

//FileIO.java

import java.io.*;
import java.util.StringTokenizer;

public class FileIO {
   // Franchise readData(String filename)
   Franchise readData(String filename, int numstores) {
       Franchise f1 = new Franchise(numstores);
       boolean DEBUG = true;
       int ctr = 0;
       // open the file
       // read the line
       // parse the line - get one value
       // and set it in the correct location in 2 d array
       try {
           FileReader file = new FileReader(filename); // file is equivalent to a file pointer in c/c++
           BufferedReader buff = new BufferedReader(file); // buffered reader will read a chunk in to the variable buff
           boolean eof = false;
           while (!eof) {
               String line = buff.readLine();
               ctr++;
               if (line == null)
                   eof = true;
               else {
                   if (DEBUG)
                       System.out.println(line);
                   if (ctr > 1) {
                       StringTokenizer a = new StringTokenizer(line);
                       for (int week = 0; week < 5; week++) {
                           for (int day = 0; day < 7; day++) {
                               String l = a.nextToken();
                               float f = Float.parseFloat(l);     // parseFloat will store to variable f of type float
                               f1.getStores(ctr - 2)
                                       .setsaleforweekdayintersection(week,
                                               day, f);
                               if (DEBUG)
                                   System.out.print("f" + f + " ");
                           }
                       }
                   }
               }

           }
       } catch (IOException f2) {

       }
       return f1;
   }
}

//Driver.java


public class Driver
{
public static void main(String[] args)
{
   FileIO readdata = new FileIO();
   //Franchise f1 = readdata.readData("C:/Users/Jamie/workspace/Assignment3/src/Salesdat.txt", 6);
   Franchise f1 = readdata.readData("E:/Files/Salesdat.txt", 6);
   System.out.println("Data read");

}

}


    The code is added with comments above

Error corrections:

The following sub class needs to be created:

package store;


public class DriverImpl extends Driver {
  
}

  
  
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote