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

Using the input file I have already downloaded (sprocketorders.txt), write a Jav

ID: 3554331 • Letter: U

Question

Using the input file I have already downloaded (sprocketorders.txt), write a Java program to summarize the data from the input file and output a report. The report should appear in the output window when the program runs. I would like to use switch structure to solve this but if not, using a series of if-then statements would suffice.

The output report needs to look like this:

Spacely Sprockets

Taking Sprockets into the Future

Sales Summary Report

Sprocket Number   Total Quantity Sold

1 90

2 155

3 50

4 300

5 100

The data in the txt file appears as such:

3   50
2   20
2   100
5   15
1   90
5   85
4   300
2   35
3   100

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

class reportGenerator
{
   public static void main(String args[])
   {
       BufferedReader br = null;
       BufferedWriter bw = null;

       try
       {
           br = new BufferedReader(new FileReader("C:\sprocketorders.txt"));
           String line;
           String element[] = new String[2];
           int   sum[] = new int[5];
           File file = new File ("C:\sprocketordersreport.txt");

           while ((line = br.readLine()) != null)
           {
               element = line.split(" ");

               switch(Integer.parseInt(element[0]))
               {
                   case 1:
                       sum[0] = sum[0] + Integer.parseInt(element[1]);
                       break;
                   case 2:
                       sum[1] = sum[1] + Integer.parseInt(element[1]);
                       break;
                   case 3:
                       sum[2] = sum[2] + Integer.parseInt(element[1]);
                       break;
                   case 4:
                       sum[3] = sum[3] + Integer.parseInt(element[1]);
                       break;
                   case 5:
                       sum[4] = sum[4] + Integer.parseInt(element[1]);
                       break;
               }
           }

           if (!file.exists())
           {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           bw = new BufferedWriter(fw);
           bw.write("Spacely Sprockets Taking Sprockets into the Future Sales Summary Report Sprocket Number Total Quantity Sold ");
           for(int i = 0; i < sum.length; i++)
               bw.write((i + 1) + " " + sum[i] + " ");

       }
       catch (IOException e)
       {
           e.printStackTrace();

       }
       finally
       {
           try
           {
               if (br != null)
                   br.close();
               if (bw != null)
                   bw.close();
           }
           catch (IOException e)
           {
               e.printStackTrace();
           }
       }
   }
}