Hi everyone.
I'm trying to write a program that calculates commissions. When my program asks for the total sales of the salesperson, I have code that validates the number is above 0. I want to catch the InputMismatchException for when people attempt to type alphabet/special characters. In the catch block I display an error message and set total sales to -1. Since the try/catch blocks are nested within a do-while loop, I thought this would circle back to ask for input. Instead the program just repeatedly displays the error message and I have to exit the cmd to get it to stop.
Why is this happening and how can I fix it?
Here's my code
I'm trying to write a program that calculates commissions. When my program asks for the total sales of the salesperson, I have code that validates the number is above 0. I want to catch the InputMismatchException for when people attempt to type alphabet/special characters. In the catch block I display an error message and set total sales to -1. Since the try/catch blocks are nested within a do-while loop, I thought this would circle back to ask for input. Instead the program just repeatedly displays the error message and I have to exit the cmd to get it to stop.
Why is this happening and how can I fix it?
Here's my code
public class Salesperson
{
private int fixedSalary;
private double totalSales;
private double percentCommission;
public Salesperson(int salary, double sales, double commission)
{
setFixedSalary( salary );
setTotalSales( sales );
setPercentCommission( commission );
}
public void setFixedSalary( int salary )
{
fixedSalary = salary;
}
public int getFixedSalary()
{
return fixedSalary;
}
public void setTotalSales( double sales )
{
totalSales = sales;
}
public double getTotalSales()
{
return totalSales;
}
public void setPercentCommission( double commission )
{
percentCommission = commission;
}
public double getPercentCommission()
{
return percentCommission;
}
public double totalCommission()
{
double totalCommission = totalSales * percentCommission;
return totalCommission;
}
}
import java.util.Scanner;
import java.util.InputMismatchException;
public class Compensation
{
public static void main( String args[])
{
Salesperson aSalesperson = new Salesperson(40_000, 0, .05);
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Annual Compensation Calculator!");
do
{
try
{
System.out.println("How much did you earn in total sales this year?");
aSalesperson.setTotalSales( input.nextDouble() );
if (aSalesperson.getTotalSales() <= 0)
System.out.println("Input Error. Please enter a positive number.");
}
catch (InputMismatchException e)
{
System.out.println("Please type your answer using number keys with no special characters.");
aSalesperson.setTotalSales( -1 );
}
} while (aSalesperson.getTotalSales() <= 0);
System.out.println();
System.out.println("You have a base salary of $" + aSalesperson.getFixedSalary() + ".");
System.out.println("Commissions are currently " + (aSalesperson.getPercentCommission() * 100) + "% of total sales.");
System.out.println();
System.out.println("You have earned $" + aSalesperson.totalCommission() + " in commissions.");
System.out.println("Your Annual Compensation is $" + (aSalesperson.getFixedSalary() + aSalesperson.totalCommission()) + ".");
}
}