Not sure, if this is what you want, but you would need to update your total
within the loop to update the value in the Total Compensation
column.
Let’s start with the little things you should change first. The naming convention in Java is camel-case. You should change that accordingly. For example, rather than getsalesDouble
you should name your function getSalesDouble
– similar to your numFormat
.
Next, you should also change the names of your methods and parameters/variables. In order to do that, I like to ask myself what is this method going to do?. Once I know that I’d come up with an appropriate verb for it and that’s my method’s name. For example, your Calculator
should be named calculate
in order to have a verb. But this wouldn’t be self-explanatory, thus, another name has to take its place — ask youself what does it calculate? (e.g. calculateSalary
).
Next, your utilsCalculate
class should start with an uppercase letter (see link above). I’d prefer to name it Utils
, since there is only one utils class anyway. Additionally, the class should have only static methods, so you do not need to create an instance (see code below). You might want to create a private default constructor, to avoid other programmers in your team accidentally creating an instance of your Utils
class.
Now, that being said, you should avoid commenting like you do. It only clutters your code. For example, the end of a method is indicated by }
. So, there is no need to state that the end of the method is at the line where the }
occurs. If you find it hard to see where the closing }
of a method is, you might consider properly formatting your code. Usually your IDE can do that for you by pressing a shortcut or using the menu bar.
Last but not least, I take it, your code works for you, but the total
does not update (within your while loop
). This is, because you only calculate the total once and never re-calculate it with the new value of salesDouble
. You’ll have to call total = salesObject.Calculator();
after updating the salesDouble
value. Of course, this would require you to create a new utilsCalculate
instance. Otherwise, the value would always be the same, as you would never update the salesDouble
within your utils class. That is nothing I’d recommend you to do, but would solve your issue for the code as is.
Have this example, on how to do it properly*.
Utils
package utils;
import java.text.DecimalFormat;
public class Utils {
public final static double FIXED_SALARY = 30000;
/**
* Formats the given decimal number.
* @param dec The number to format
* @return The formatted number as string.
*/
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);
}
}
Main
package sales;
import java.util.Scanner;
import utils.Utils;
public class Person {
public static void main(String[] args) {
// Initialise scanner
Scanner input = new Scanner(System.in);
System.out.print("Please enter your annual sales:");
// Read sales from input & calculate salary
double annualSales = input.nextDouble();
double salary = Utils.calculateSalary(annualSales);
// Calculate commission bonus
double commissionBonus = 1.5 * annualSales;
// Print information for user
System.out.println("The total yearly salary is: " + Utils.numFormat(salary));
System.out.println("Total Sales \t\t Total Compensation");
while (annualSales <= commissionBonus) {
System.out.println(annualSales + " \t\t " + salary);
annualSales += 5000;
// Update salary according to annual sales
salary = Utils.calculateSalary(annualSales);
}
// Close scanner
input.close();
}
}
Output
Please enter your annual sales:320000
The total yearly salary is: 55.600,00
Total Sales Total Compensation
320000.0 55600.0
325000.0 56000.0
330000.0 56400.0
335000.0 56800.0
340000.0 57200.0
345000.0 57600.0
350000.0 58000.0
355000.0 58400.0
360000.0 58800.0
365000.0 59200.0
370000.0 59600.0
375000.0 60000.0
380000.0 60400.0
385000.0 60800.0
390000.0 61200.0
395000.0 61600.0
400000.0 70000.0
405000.0 70500.0
410000.0 71000.0
415000.0 71500.0
420000.0 72000.0
425000.0 72500.0
430000.0 73000.0
435000.0 73500.0
440000.0 74000.0
445000.0 74500.0
450000.0 75000.0
455000.0 75500.0
460000.0 76000.0
465000.0 76500.0
470000.0 77000.0
475000.0 77500.0
480000.0 78000.0
* Of course, there is still room for enhancement, but I think it should be sufficient for your problem.
Note that I did not check your equations, as I do not know how to calculate this – I assumed your calculations are correct.
10
solved Java issue using classes and accessing variables