patternjavaMinor
Reporting quarterly changes for each department
Viewed 0 times
reportingeachquarterlychangesfordepartment
Problem
This code works 100%, but I am trying to see if I can use a
I have to show the differences between each quarter for every department.
My array has 6 Departments and 4 Quarters.
This is the only thing that I could come up with, since I am new to Java and 2-D arrays. I want to know if there is an easier way of getting the quarterly differences for each department. I was told I could use a nested
```
public void departmentdiff()
{
double dept1Qrt1 = sales [0] [0];
double dept2Qrt1 = sales [1] [0];
double dept3Qrt1 = sales [2] [0];
double dept4Qrt1 = sales [3] [0];
double dept5Qrt1 = sales [4] [0];
double dept6Qrt1 = sales [5] [0];
double dept1Qrt2 = sales [0] [1];
double dept2Qrt2 = sales [1] [1];
double dept3Qrt2 = sales [2] [1];
double dept4Qrt2 = sales [3] [1];
double dept5Qrt2 = sales [4] [1];
double dept6Qrt2 = sales [5] [1];
double dept1Qrt3 = sales [0] [2];
double dept2Qrt3 = sales [1
for-statement to make my second method less-cumbersome and more efficient instead of making so many variables. I have to show the differences between each quarter for every department.
My array has 6 Departments and 4 Quarters.
import java.util.*;
public void setQuarter()
{
final int DEPARTMENT = 6;
final int QUARTER = 4;
private static double [][] sales = new double [DEPARTMENT][QUARTER];
Scanner keyboard = new Scanner(System.in);
double total = 0;
for (int row = 0; row < DEPARTMENT ; row++)
{
for (int col = 0; col < QUARTER; col++)
{
// This will input value into my array
System.out.print(" \n What Were The Total Sales For Department : " + (row + 1 ) + " Quarter : " + (col + 1 ) + " = ");
sales[row][col] = keyboard.nextDouble();
}
}
}This is the only thing that I could come up with, since I am new to Java and 2-D arrays. I want to know if there is an easier way of getting the quarterly differences for each department. I was told I could use a nested
for-statement to subtract the elements in the 2-D array. However, I have tried but I can't seem to get one done. I am pretty sure the use of a nested for-statement would clean my code up quickly. Can anyone help me out with this?```
public void departmentdiff()
{
double dept1Qrt1 = sales [0] [0];
double dept2Qrt1 = sales [1] [0];
double dept3Qrt1 = sales [2] [0];
double dept4Qrt1 = sales [3] [0];
double dept5Qrt1 = sales [4] [0];
double dept6Qrt1 = sales [5] [0];
double dept1Qrt2 = sales [0] [1];
double dept2Qrt2 = sales [1] [1];
double dept3Qrt2 = sales [2] [1];
double dept4Qrt2 = sales [3] [1];
double dept5Qrt2 = sales [4] [1];
double dept6Qrt2 = sales [5] [1];
double dept1Qrt3 = sales [0] [2];
double dept2Qrt3 = sales [1
Solution
Refactoring should get you there
We're going to make it look worse for a moment...
Re arrange, so things line up better
Inject a variable to represent the quarter
Now it should be obvious where the for loop falls out; note the introduction of "quarterNames", which is a lookup table (aka array) to make sure the spellings are correct in the strings; you need to do something to ensure that you don't confuse 1-based humans by printing 0-based identifiers.
For more readability, use better variable names
Eliminate some magic numbers
At this point, it should be clear how to create a for loop that generates the report for each department.
BUT: more advanced programmers wouldn't do it this way. Part of the reason that this is such a mess, is that you are storing each piece of data independently, and then trying to reconstruct the data relationships.
In this example, there are three (simple) abstractions. There's the concept of a "quarter", th
total1 = dept1Qrt1 - dept1Qrt2;
total2 = dept1Qrt2 - dept1Qrt3;
total3 = dept1Qrt3 - dept1Qrt4;
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");We're going to make it look worse for a moment...
total1 = sales [0] [0]- sales [0] [1];
total2 = sales [0] [1]- sales [0] [2];
total3 = sales [0] [2]- sales [0] [3];
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");Re arrange, so things line up better
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
total1 = sales [0] [0]- sales [0] [1];
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
total2 = sales [0] [1]- sales [0] [2];
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
total3 = sales [0] [2]- sales [0] [3];
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");Inject a variable to represent the quarter
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
int quarter = 0;
total1 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
quarter = 1;
total2 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
quarter = 2;
total3 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");Now it should be obvious where the for loop falls out; note the introduction of "quarterNames", which is a lookup table (aka array) to make sure the spellings are correct in the strings; you need to do something to ensure that you don't confuse 1-based humans by printing 0-based identifiers.
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
for (int quarter = 0; quarter < 3; ++quarter {
double total = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : " + quarterNames[quarter] + " to Quarter : " + quarterNames[quarter+1] + " The Difference Is ( " + total + " ) ");
}For more readability, use better variable names
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
for (int thisQuarter = 0; thisQuarter < 3; ++thisQuarter {
int nextQuarter = 1 + thisQuarter;
double total = sales [0] [thisQuarter]- sales [0] [nextQuarter];
System.out.println( " \n From Quarter : " + quarterNames[thisQuarter] + " to Quarter : " + quarterNames[nextQuarter] + " The Difference Is ( " + total + " ) ");
}Eliminate some magic numbers
int thisDepartment = 0;
System.out.println( " \n The Quarterly Differences For Department-" + departmentId[thisDepartment] + " is :");
System.out.println( " ********************************************** ");
for (int thisQuarter = 0; thisQuarter < QUARTER; ++thisQuarter {
int nextQuarter = 1 + thisQuarter;
double total = sales [thisDepartment] [thisQuarter]- sales [thisDepartment] [nextQuarter];
System.out.println( " \n From Quarter : " + quarterNames[thisQuarter] + " to Quarter : " + quarterNames[nextQuarter] + " The Difference Is ( " + total + " ) ");
}At this point, it should be clear how to create a for loop that generates the report for each department.
BUT: more advanced programmers wouldn't do it this way. Part of the reason that this is such a mess, is that you are storing each piece of data independently, and then trying to reconstruct the data relationships.
In this example, there are three (simple) abstractions. There's the concept of a "quarter", th
Code Snippets
total1 = dept1Qrt1 - dept1Qrt2;
total2 = dept1Qrt2 - dept1Qrt3;
total3 = dept1Qrt3 - dept1Qrt4;
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");total1 = sales [0] [0]- sales [0] [1];
total2 = sales [0] [1]- sales [0] [2];
total3 = sales [0] [2]- sales [0] [3];
System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
total1 = sales [0] [0]- sales [0] [1];
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
total2 = sales [0] [1]- sales [0] [2];
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
total3 = sales [0] [2]- sales [0] [3];
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
int quarter = 0;
total1 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 1 to Quarter : 2 The Difference Is ( " + total1 + " ) ");
quarter = 1;
total2 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 2 to Quarter : 3 The Difference Is ( " + total2 + " ) ");
quarter = 2;
total3 = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : 3 to Quarter : 4 The Difference Is ( " + total3 + " ) ");System.out.println( " \n The Quarterly Differences For Department-1 is :");
System.out.println( " ********************************************** ");
for (int quarter = 0; quarter < 3; ++quarter {
double total = sales [0] [quarter]- sales [0] [quarter+1];
System.out.println( " \n From Quarter : " + quarterNames[quarter] + " to Quarter : " + quarterNames[quarter+1] + " The Difference Is ( " + total + " ) ");
}Context
StackExchange Code Review Q#35276, answer score: 8
Revisions (0)
No revisions yet.