mentorCoder mentorCoder -4 years ago 100
Java Question

Java sales program using either array or arraylist to find difference in sales made not making correct calculation

I am in a Java class and I have a sales program as a project. My code will run but will not accept a second user's name as necessary in the code. It then makes a comparison between the two salaries. But my second calculation is not working correctly. Here is what I have:

package sales2;

import java.util.Scanner;

public class Sales2 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Initialise scanner

String[] empName;
empName = new String[2];

double[] annualSales;
annualSales = new double[2];

System.out.printf("Please enter the employees name:"); // Question and input
empName[0] = input.nextLine();

System.out.printf("Please enter your annual sales:");
annualSales[0] = Double.parseDouble(input.nextLine());

double salary1 = Utils2.calculateSalary(annualSales[0]); // Read sales from input & calculate salary


System.out.println(empName[0] + "'s total yearly salary is: " + Utils2.numFormat(salary1)); // Print information for user

input.nextLine();

System.out.printf("Please enter the employees name:");
empName[1] = input.nextLine();

System.out.printf("Please enter your annual sales:");
annualSales[0] = Double.parseDouble(input.nextLine());

double salary2 = Utils2.calculateSalary(annualSales[1]); // Read sales from input & calculate salary


System.out.println(empName[1] + "'s total yearly salary is: " + Utils2.numFormat(salary2)); // Print information for user

if (salary1 > salary2){
System.out.println(empName[0] + " has a higher total annual compensation.");
System.out.println(empName[1] + " will need to increase their sales to match or exceed "
+ empName[0] + ", here is how much :" + (salary1 - salary2) );
}else if (salary1 < salary2){
System.out.println(empName[1] + " has a higher total annual compensation.");
System.out.println(empName[0] + " will need to increase their sales to match or exceed "
+ empName[1] + ", here is how much :" + (salary2 - salary1) );
}else {
System.out.println("\nBoth Salespersons have equal total annual compensation.");
}
}
}


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sales2;

/**
*
* @author etw11
*/
import java.text.DecimalFormat;

public class Utils2 {
public final static double FIXED_SALARY = 30000;
/**
* @param dec
* @return
*/
public static String numFormat(double dec) {
return new DecimalFormat("##,##0.00").format(dec);
}

/**
* Calculates the salary based on the given sales.
* @param sales The annual sales
* @return The calculated salary.
*/
public static double calculateSalary(double sales) {
double commissionRate = 0.10d;

if (sales < 320000) {
commissionRate = 0.00d;
} else if (sales >= 320000 && sales < 400000) {
commissionRate = 0.08d;
}

// System.out.println("The current commission is " + (int)
(commissionRate * 100) + "% of total sales.");

return FIXED_SALARY + (sales * commissionRate);
}
}

Answer Source

Found my issue. In this line:

annualSales[0] = Double.parseDouble(input.nextLine());

It should be:

annualSales[1] = Double.parseDouble(input.nextLine());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download