HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

How is my quarterly sales statistics program?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
howprogramquarterlystatisticssales

Problem

Am I making good use of a 2-D array? Is this code inefficient? I am supposed to receive input from 6 departments for 4 quarterly periods in Java.

My assignment asks:


Quarterly Sales Statistics


Write a program that lets the user enter four quarterly sales figures
for six divisions of a company. The figures should be stored in a two-
dimensional array.

public static void main(String[] args)
{
    double test1;
    double test2;
    double test3;
    double test4;

    /* These are just test variable that will show output,
    to assure that my methods are summing my 2-D array */

    Scanner  keyboard = new Scanner(System.in);

    test1 = Quarterly1();
    System.out.println(" Test 1 = " + test1);

    test2 = Quarterly2();
    System.out.println(" Test 2 = " + test2);

    test3 = Quarterly3();
    System.out.println(" Test 3 = " + test3);

    test4 = Quarterly4();
    System.out.println(" Test 4 = " + test4 " /n ");

}


This is a method that will receive input for my first quarterly report and sum it up.

public static double Quarterly1()
{
    Scanner  keyboard = new Scanner(System.in);
    double [][] sales = new double [6][4];
    int num = 1;
    double total = 0;

    // this for statement will assign a row to every department
    for (int row = 0; row < 6; row++)
    {
        // This for statement is taking one column from the array in order to fill with input
        for (int col = 0; col < 4 - 3; col++)
        {
            // This will receive sale numbers from the six departments
            System.out.print("Enter a The First Quarterly Sales For Department [ "+num+" ]. . . . . $ ");

            sales[row][col] = keyboard.nextDouble();
            num++;

            // This for statement will sum each department's input
            for (int i = 0; i < sales.length; i++);
            total += sales[row][col];
        }
    }

    return total;
}


This is a method that will receive input for my second quarterly report a

Solution

I'm not really sure why you have 4 functions that are doing the exact same thing!?

The only difference seems to be the prompt text

I'm also not a big fan of the second for statement

// This for statement will sum each department's input

for (int i = 0; i < sales.length; i++);
total += sales[row][col];


what does that mean? My guess is:

// This for statement will sum each department's input
for (int i = 0; i < sales.length; i++){
total += sales[row][col];
}


but it still doesn't make sense here

I always suggest using {}'s it makes your code cleaner, easier to read and less error prone.

also why are you doing the 4-3? can't you just put 1?

Now my JAVA is a bit rusty but the point is still valid

public Enum Quarter{
   First,
   Second,
   Third,
   Forth
}

public class Quarterly{

     private final const ROW = 6;
     private final const COL = 4;
     private Scanner _scanner;

     public Quarterly(Scanner scanner){
          this._scanner = scanner;
     }
     private string GetMessage(Quarter quarter, int departmentNumber){
         switch(quarter){
             case First:
                return "Enter a The Third Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ "
             case Second:
                 ...

         }
         //May even want to use a template string
         //const string QuartlyTemplate = "Enter a The {0} Quarterly Sales for Department [{1}]. . . . . .$ "
         //then do a string format on the template to populate it
     }

     public double GetTotalForDepartments(Quarter quarter){
             double [][] sales = new double [ROW][COL];
             int num = 1;
             double total = 0;
            for (int row = 0; row < ROW ; row++)
            {
            // This for statement is taking one column from the array in order to fill with input
                 for (int col = 0; col < 4 - 3; col++)
                 {
                    // This will receive sale numbers from the six departments
                    System.out.print(message + "[" + num + " ]. . . . . $ ");
                    sales[row][col] = _scanner.nextDouble();
                    num++;
                    // This for statement will sum each department's input
                    for (int i = 0; i < sales.length; i++);
                    total += sales[row][col];
                }
        }

    return total;
}
 }


Now I know this may not compile right off the bat, because I don't have a java compiler handy, but it will be easier to read than having 4 functions doing the same exact work.

then your main will be

public static void main(String[] args)
 {
     double test1;
     double test2;
     double test3;
     double test4;

     Scanner  keyboard = new Scanner(System.in);
     Quarterly helper = new Quarterly(keyboard);

     test1 = helper.GetTotalForDepartments(Quarter.First);
     System.out.println(" Test 1 = " + test1);

     test2 = helper.GetTotalForDepartments(Quarter.Second);
     System.out.println(" Test 2 = " + test2);

     test3 = helper.GetTotalForDepartments(Quarter.Third);
     System.out.println(" Test 3 = " + test3);

     test4 = helper.GetTotalForDepartments(Quarter.Fourth);
     System.out.println(" Test 4 = " + test4 " /n ");
 }

Code Snippets

for (int i = 0; i < sales.length; i++);
total += sales[row][col];
// This for statement will sum each department's input
for (int i = 0; i < sales.length; i++){
total += sales[row][col];
}
public Enum Quarter{
   First,
   Second,
   Third,
   Forth
}

public class Quarterly{

     private final const ROW = 6;
     private final const COL = 4;
     private Scanner _scanner;

     public Quarterly(Scanner scanner){
          this._scanner = scanner;
     }
     private string GetMessage(Quarter quarter, int departmentNumber){
         switch(quarter){
             case First:
                return "Enter a The Third Quarterly Sales For Department [ " + departmentNumber+" ]. . . . . $ "
             case Second:
                 ...

         }
         //May even want to use a template string
         //const string QuartlyTemplate = "Enter a The {0} Quarterly Sales for Department [{1}]. . . . . .$ "
         //then do a string format on the template to populate it
     }


     public double GetTotalForDepartments(Quarter quarter){
             double [][] sales = new double [ROW][COL];
             int num = 1;
             double total = 0;
            for (int row = 0; row < ROW ; row++)
            {
            // This for statement is taking one column from the array in order to fill with input
                 for (int col = 0; col < 4 - 3; col++)
                 {
                    // This will receive sale numbers from the six departments
                    System.out.print(message + "[" + num + " ]. . . . . $ ");
                    sales[row][col] = _scanner.nextDouble();
                    num++;
                    // This for statement will sum each department's input
                    for (int i = 0; i < sales.length; i++);
                    total += sales[row][col];
                }
        }

    return total;
}
 }
public static void main(String[] args)
 {
     double test1;
     double test2;
     double test3;
     double test4;

     Scanner  keyboard = new Scanner(System.in);
     Quarterly helper = new Quarterly(keyboard);

     test1 = helper.GetTotalForDepartments(Quarter.First);
     System.out.println(" Test 1 = " + test1);

     test2 = helper.GetTotalForDepartments(Quarter.Second);
     System.out.println(" Test 2 = " + test2);

     test3 = helper.GetTotalForDepartments(Quarter.Third);
     System.out.println(" Test 3 = " + test3);

     test4 = helper.GetTotalForDepartments(Quarter.Fourth);
     System.out.println(" Test 4 = " + test4 " /n ");
 }

Context

StackExchange Code Review Q#33930, answer score: 3

Revisions (0)

No revisions yet.