Hi, I am currently working on a java program for class and I cannot figure out what I am doing wrong. Each week I have been creating this java program that calculates sales, total compensation, etc. Well for my final team assignment my group has to take a piece of someone's program and replace it with someone else's and make it work. We have gotten the program to where it will ask for the persons name and the sales that person had. Well after that it should compare the 2 individuals and display the total sales, highest sales, and then "the salesperson with the lower compensation must increase their sales by the minimum of $" Well the total sales, highest sales and salesperson with lower compensation .... is appearing with all zero's. I don't know what is wrong with the code that is making it appear as $0.00

Here is the code:

//establishing the package the program will be under

import java.text.DecimalFormat;
  import java.util.Scanner;
  //This establishes that we will need an input from the keyboard

    public class Assignment5W5
    //The program is establishing what the class will be called
    {
      public static void main(String[]args)
      //This establishes that this is the main controlling program.                 
      {
//---------------------------------------------------------------------------------------------------        
        String [] thename = new String[10];
        int arrnum = 0;
        //Creating an array for the salespersons names, and establishing the counter at 0.

        double [] thenumber = new double[10];
        //Creating an array for the sales of the saleperson.

        Scanner input = new Scanner(System.in);
        //Calling input as the variable that the user will enter values
//---------------------------------------------------------------------------------------------------    
  //Using the class SalesPerson to establish the names of the salesperson.      
        SalesPerson salepername = new SalesPerson();
        //Creates object salepername

        arrnum = 1;
        //Setting the array at the number 1 so the number of the sales person is established
        //when the question is asked to enter the name.

          while (arrnum <= 2 )
          //The loop is based on the numbers in the array.  
          {
            System.out.printf("what is the full name of salesperson %s\n",arrnum);
            String salesname = input.nextLine();
            //Taking in the sales persons name
            salepername.setSalesname(salesname);
            //Assinging the name of the salesperson in SalesPerson.java
            thename[arrnum] = salesname;
            //Placing the name in the array
            arrnum = arrnum + 1;
            //Adding 1 to the counter.
          }
//---------------------------------------------------------------------------------------------------
  //Using the class Sales to establish the sales of the salesperson.        
        Sales number = new Sales();
        //Creates object number

        arrnum = 1;
        //Reseting the counter.

          while (arrnum <=2)
          //The loop is based on the numbers in the array.    
          {    
            System.out.println();    
            System.out.printf("What were the sales of %s\n", thename[arrnum]);
            //Print out of the salespersons name and the entry number.
            double salesnumber = input.nextDouble();
            //Taking in the sales of the salesperson in the print line.
            number.setSalesnumber(salesnumber);
            //Assigning the sales of the salesperson in Sales.java
            thenumber[arrnum] = salesnumber;
            //placing the sales into the array.
            arrnum = arrnum + 1;
            //Adding 1 to the counter.
          }
//---------------------------------------------------------------------------------------------------
  //Calculations to get the sales numbers.     

        double num;
        double num2;
        double total;
        //Variables needed for calculations.

        num2 = thenumber[1];
        num = thenumber[2];
        //Setting the variables to the numbers in the array

          if (num2 <= num)
          //Conditional statement that compares the two numbers.  The higher of the two numbers
          //will be subtracted by the lower number.  It is done this way to eliminate the 
          //negative number.
          {
            total = num - num2;
            //Calculation to come up with the difference between the 2 salespeople.
            System.out.printf(thename[1]+" is $%.2f", total);
            System.out.printf(" short of "+thename[2]);
            //Printing out the salespersons name that is lower than the other salesperson
            //and by how much.
            System.out.println();
            //Spacing
          }
          else
          {
            total = num2 - num;
             //Calculation to come up with the difference between the 2 salespeople.
            System.out.printf(thename[2]+" is $%.2f", total);
            System.out.printf(" short of "+thename[1]);
            //Printing out the salespersons name that is lower than the other salesperson
            //and by how much.
            System.out.println();
            //Spacing
          }

           final int ANNUAL_COMPENSATION = 2; // Number of array elements

 // Create an array to hold the sales numbers per year.

double[] sales2 = new double[ANNUAL_COMPENSATION];

 // Get the annual compensation and store it
 // in the sales array.
//SalesData it = new SalesData(sales2);

 // Create a SalesData object initialized with the
 // sales array.
 SalesData year = new SalesData(sales2);

 // Create a DecimalFormat object for output formatting.
 DecimalFormat dollar = new DecimalFormat("#,##0.00");

 // Display the highest
 // sales amounts for the year.
 System.out.println();
 System.out.println("The totals sales were $"
 + dollar.format(year.getTotals()));
  System.out.println("The highest sales were $"
 + dollar.format(year.getHighest()));
System.out.println("The salesperson with the lower compensation must increase their sales by the minimum of $"
 + dollar.format(year.getHighest()-(year.getLowest()))); 

       }

    }

** And this is the end result:**

    run:
what is the full name of salesperson 1
Bob
what is the full name of salesperson 2
Sally

What were the sales of Bob
321654

What were the sales of Sally
123456
Sally is $198198.00 short of Bob

The totals sales were $0.00
The highest sales were $0.00
The salesperson with the lower compensation must increase their sales by the minimum of $0.00
BUILD SUCCESSFUL (total time: 15 seconds)

Any help would be great! Thank you!

~Lindsey

What does the SalesData class do? Where does the getTotals() method get the value it returns?

When is any data put in the sales2 array?

This is my first programming class so I am confused by what you are asking. The team created a "sales" class, "salesdata" class, and "salesperson" class. Are you asking what is listed in each of those?

When I run the program it will ask for the individuals name, and the sales of each individual. It should then compare the individuals but it keeps coming back with $0.00. So I'm super confused. I sort of understood it and then as soon as we combined the programs I was totally lost again.

What happens in the SalesData class? You didn't post the code for it. The zero amounts are coming from methods in that class.

Also when is data put into the sales2 array?

Oh ok! Here is the code for "SalesData" :

package assignment5w5;


  /**
 * This class keeps the sales figures 
 * in an array and provides methods for getting
 * the total and the highest 
 * amounts of sales.
 */

 public class SalesData
  {
public double[] sales2; // References the sales data

 /**
 * The constructor accepts an array as an argument.
 * The elements in the argument array are copied
 * to the sales array.
 */

 public SalesData(double[] s)
 {
 // Create a new array the same length as s.
 sales2 = new double[s.length];

 // Copy the values in s to sales.
 for (int arrnum = 0; arrnum < s.length; arrnum++)
 sales2[arrnum] = s[arrnum];
 }

 /**
 * The getTotal method returns the total of the
 * elements in the sales array.
 */

 public double getTotals()
 {
 double totals = 0.0; // Accumulator

 // Add up all the values in the sales array.
 for (double salesnumber : sales2)
 totals += salesnumber;

 // Return the total.
 return totals;
 }

 /**
 * The getHighest method returns the highest value
 * stored in the sales array.
 */

 public double getHighest()
 {
 // Store the first value in the sales array in
 // the variable highest.
 double highest = sales2[0];

 // Search the array for the highest value.
 for (int arrnum = 1; arrnum < sales2.length; arrnum++)
 {
 if (sales2[arrnum] > highest)
 highest = sales2[arrnum];
 }

 // Return the highest value.
 return highest;
 }
/**
 * The getLowest method returns the lowest value
 * stored in the sales array.
 */

 public double getLowest()
 {
 // Store the first value in the sales array in
 // the variable highest.
 double lowest = sales2[0];

 // Search the array for the lowest value.
 for (int arrnum = 1; arrnum < sales2.length; arrnum++)
 {
 if (sales2[arrnum] < lowest)
 lowest = sales2[arrnum];
 }

 // Return the lowest value.
 return lowest;
 }
//get the sales increases needed to meet or exceed highest

}

And here is the "Sales" class code

package assignment5w5;
//establishing the package the program will be under

  public class Sales 
  //The program is establishing what the class will be called
  {
    private double salesnumber; 
    //setting up the instance of salenumber for this class

      public void setSalesnumber (double number)
      //constructor   
      {
        salesnumber = number;

      }
        public double getSalesnumber()
        //returns the value for salesnumber
        {
          return salesnumber;
          //sends the value to the method
        }
  }

"SalesPerson Code

package assignment5w5;


//establishing the package the program will be under

  public class SalesPerson
  //The program is establishing what the class will be called
  {
    private String salesname;
    //setting up the instance of salename for this class

      public void setSalesname (String name)
      //constructor     
      {
        salesname = name;
      }

        public String getSalesname()
        //returns the value for salesnumber
        {
          return salesname;
          //sends the name to the method
        }
  }

You still have not answered this question:

Where does the sales2 array (defined on line 110 and used on line 118) get any values?

Where does the SalesData class get the values that it returns in the getTotals() method?

I am not sure. I thought the values and the totals were determined by the number amount that was put in for the sales of each individual.

My apologies for not being more helpful this is completely confusing for me. But I do thank you for helping me.

**One of our team members said he fixed it. Said he just combined 2 complete programs but that doesn't seem right to me? I feel like we got lucky on that one lol. But I would like to figure this way out just for learning experiences, if you don't mind. :)

There needs to be data in the sales2 array for the SalesData class to work with.

Where is the sales data that should be in that array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.