6
\$\begingroup\$

This code works 100%, but I am trying to see if I can use a 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] [2];
        double dept3Qrt3 = sales [2] [2];
        double dept4Qrt3 = sales [3] [2];
        double dept5Qrt3 = sales [4] [2];
        double dept6Qrt3 = sales [5] [2];
        double dept1Qrt4 = sales [0] [3];
        double dept2Qrt4 = sales [1] [3];
        double dept3Qrt4 = sales [2] [3];
        double dept4Qrt4 = sales [3] [3];
        double dept5Qrt4 = sales [4] [3];
        double dept6Qrt4 = sales [5] [3];
        double total1 = 0;
        double total2 = 0;
        double total3= 0;

        // Instructs User To Press Enter To Continue
        System.out.println(" \n \n \n Please Press Enter To Display The Department's Difference In Each Quarter ");
        // Scanner Type Waits For the "Enter" Input Before Proceeding
        new Scanner(System.in).nextLine();

        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 = dept2Qrt1 - dept2Qrt2;
        total2 = dept2Qrt2 - dept2Qrt3;
        total3 = dept2Qrt3 - dept2Qrt4;
        System.out.println( " \n \n The Quarterly Differences For Department-2 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 = dept3Qrt1 - dept3Qrt2;
        total2 = dept3Qrt2 - dept3Qrt3;
        total3 = dept3Qrt3 - dept3Qrt4;
        System.out.println( " \n \n The Quarterly Differences For Department-3 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 = dept4Qrt1 - dept4Qrt2;
        total2 = dept4Qrt2 - dept4Qrt3;
        total3 = dept4Qrt3 - dept4Qrt4;
        System.out.println( " \n \n The Quarterly Differences For Department-4 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 = dept5Qrt1 - dept5Qrt2;
        total2 = dept5Qrt2 - dept5Qrt3;
        total3 = dept5Qrt3 - dept5Qrt4;
        System.out.println( " \n \n  Quarterly Differences For Department-5 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 = dept6Qrt1 - dept6Qrt2;
        total2 = dept6Qrt2 - dept6Qrt3;
        total3 = dept6Qrt3 - dept6Qrt4;
        System.out.println( " \n \n The Quarterly Differences For Department-6 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 + " ) ");

    }
\$\endgroup\$
8
\$\begingroup\$

Refactoring should get you there

    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", there's the concept of a department - which has sales for each quarter, and there is the concept of a report, which tracks changes per quarter.

class Quarter {
    // this would normally be a date range, or a year/fiscal quarter pair, or something
    // for your simple example, it could even be an enum.
    int id;

    // These two methods, because it's important to get key semantics correct.
    @Override
    boolean equals(Object other);

    @Override
    int hashCode();
}

class Department {
    // Not going to define Sales.  In your example, it could just be a Double.
    Map<Quarter, Sales> = salesResults;
}

class Report {
    Quarter start;
    Quarter end;
}

List<Department> departments = new ArrayList();
List<Report> reports = new ArrayList();

for(Department department : departments) {
    for(Report report : reports) {
        double total = department.salesResults.get(report.start) - department.salesResults.get(report.end);
        // do something with the total
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.