I was out sick for a couple weeks and this lab was assigned but I can\'t figure
ID: 3834691 • Letter: I
Question
I was out sick for a couple weeks and this lab was assigned but I can't figure out how to do this. Here are all the instructions and links to the input file and expected results as given to us by the instructor. The end result is supposed to look like what I have in the link down below for the expected results.
LAB10/11, Annual Sales Analysis Points: 50
OBJECTIVE
To demonstrate the use of two-dimensional arrays
To demonstrate processing two-dimensional arrays
To demonstrate varied uses of portions of an array
To demonstrate printing two-dimensional arrays row-wise and column-wise
REQUIRED
A report of your work, in a flat pocket folder, in the following order:
1. Grade form Lab11GradeForm.doc
2. This program description
3. Problem Analysis Lab11ProblemAnalysis.doc
4. The listing of the source program Lab11.java
5. Listing of the input files Lab11Input.txt
6. Listing of the expected results (already created) Lab11ExpectedResults.xlsx
7. Listing of the output files Lab11Report.txt
SPECIFICATIONS
See Lab11ExpectedResults for a visual description of this array and for expected results.
Write a program to produce a Sales Report for products for the four quarters of the year.
There are 8 products. So the dimensions of the array are 4 columns; 8 rows.
HOWEVER, we can:
use one col to represent the product numbers (for example: col 0)
use one col to represent the product totals (for example: col 5)
use one row to represent the quarter totals (for example: row 8)
So the dimensions of the array are 9 rows and 6 columns;
1. The input file consists of one record for each product.
The first field is the product code, followed by the four quarter sales figures
2. Read the data from the input file and place the values into the 2-D array
The product # goes in col 0 and the sales figures go in col 1- 4
Each new product goes in a new row
3. Go through the array and add up the sales for each product (row) and place in last column
This could be done as you are reading in the data
4. Go through the array and add up the sales for each quarter (column) and place in last row
This could be done as you are reading in the data
5. Calculate the total sales - for all products for all four quarters and place in last row, last col
This could be done as you are reading in the data
6. Produce two different reports
The % is calculated by dividing the total for a product or quarter by the total sales
Report #1 is Sales by quarter
Report1- Sales by Product
Product Qtr1 Qtr2 Qtr3 Qtr4 Total %
#1 999 999 999 999 9999 99%
#2 999 999 999 999 9999 99%
:
#8 999 9999 999 9999 9999 99%
TOTAL 999 999 999 999 9999 100%
Report2- Sales by Quarter
Quarter #1 #2 #3 #4 #5 #6 #7 #8 Total %
Qtr1 999 999 999 999 999 999 999 999 9999 99%
Qtr2 999 999 999 999 999 999 999 999 9999 99%
Qtr3 999 999 999 999 999 999 999 999 9999 99%
Qtr4 999 999 999 999 999 999 999 999 9999 99%
TOTAL 999 999 999 999 999 999 999 999 9999 100%
Here's the link to the input files.--> https://pastebin.com/Eh3nnw5p
Here's a link to the expected results.--> https://pastebin.com/0Cpwjna3
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
public class annualSaleAnalysis {
private static final String FILENAME = "D:\Eh3nnw5p.txt";// Give the path of the file.
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
FileReader fr = null;
String salesInfo[][]= new String[9][6];
int j=0;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
//While loop to put the data from file to 2-D Array
while ((sCurrentLine = br.readLine()) != null) {
String temp[]=sCurrentLine.split(" ");
for(int i=0;i<temp.length;i++)
{
salesInfo[j][i]=temp[i];
}
j++;
}
salesInfo[8][0]="Total";
//For loop for counting total coloumn value
for (int r=0;r<8;r++)
{
Integer total=0;
for(int c=1;c<5;c++)
{
total+=new Integer(salesInfo[r][c]);
}
salesInfo[r][5]=total.toString();
}
//For loop for counting total Row value
for (int c=1;c<=5;c++)
{
Integer total=0;
for(int r=0;r<8;r++)
{
total+=new Integer(salesInfo[r][c]);
}
salesInfo[8][c]=total.toString();
}
Integer TotalSales=new Integer(salesInfo[8][5]);
DecimalFormat numberFormat = new DecimalFormat("#.00");// for displaying only 2 fraction digits
//Display format for Sales Report by Product
System.out.println("Sales Report by Product ");
System.out.println("product Qtr1 Qtr2 Qtr3 Qtr4 Total %%");
for (int r=0;r<9;r++)
{
for(int c=0;c<6;c++)
{
System.out.print(salesInfo[r][c]+" ");
}
System.out.print(numberFormat.format(((new Float(salesInfo[r][5]))/TotalSales)*100)+"%");
System.out.print(" ");
}
//Display format for Sales Report by Quarter
System.out.println(" Sales Report by Quarter ");
System.out.print("Quarter ");
for (int c=0;c<6;c++)
{ if(c>0&&c<5)
System.out.print("Qtr"+(c)+" ");
if(c==5)
System.out.print("Total ");
for(int r=0;r<9;r++)
{
System.out.print(salesInfo[r][c]+" ");
}
if(c==0)
System.out.print("%%");
if(c!=0)
System.out.print(numberFormat.format(((new Float(salesInfo[8][c]))/TotalSales)*100)+"%");
System.out.print(" ");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.