-2

I am writing a Java program where the user enters in a sales amount and then they enter a code(1, 2, or a 3) and then the program calculates the commission based on the sales type(the code the user entered). It works for 1 and 3 but for some odd reason when I enter in a 2 for the code it does nothing, just closes even though the code is pretty much the same as 1 and 3. Any help would be appreciated.

public class TravelCommission extends Frame

{

static double dollars;
static double answer;
static int empCode;
static DecimalFormat df = new DecimalFormat("#.#");

public static void main(String[] args) 
{
    //Calling all methods
    dollars = getSales(0);
    empCode = getCode(0);
    answer = getCommission(dollars, empCode);
    output(dollars, answer);
    finish();

}
private static double getSales(double sales) 
{
    //Getting sales along with error checking and exception handeling
    try 
    {

        sales = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter in sales:", "Sales", JOptionPane.DEFAULT_OPTION));
        if(sales <= 0)
        {

            JOptionPane.showMessageDialog(null, "Please enter a value greater than 0.", "Error", JOptionPane.INFORMATION_MESSAGE);

        }
        else if(sales == JOptionPane.CANCEL_OPTION)
        {

            System.exit(0);

        }
        else
        {



        }

    } 
    catch (IllegalArgumentException e) 
    {

        JOptionPane.showMessageDialog(null, "Enter in a double value.", "Error!", JOptionPane.ERROR_MESSAGE);

    }

    return sales;

}
private static int getCode(int code)
{

    try 
    {

        code = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the code for the type of sale\n1 = Telephone\n2 = In-store\n3 = Outdoor:", "Sales", JOptionPane.DEFAULT_OPTION));
        if(code <= 0 || code > 3)
        {

            JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3.", "Error", JOptionPane.INFORMATION_MESSAGE);

        }
        else if(code == JOptionPane.CANCEL_OPTION)
        {

            System.exit(0);

        }
        else
        {



        }

    } 
    catch (IllegalArgumentException e) 
    {

        JOptionPane.showMessageDialog(null, "Please enter in a 1, 2, or 3.", "Error!", JOptionPane.ERROR_MESSAGE);

    }

    return code;
}
private static double getCommission(double dollars, int empCode) 
{
    //Determing the amount of commission made based off of the type of sale
    if(empCode == 1)
    {

        answer = dollars * 0.10;

    }
    else if(empCode == 2)
    {

        answer = dollars * 0.14;

    }
    else if(empCode == 3)
    {

        answer = dollars * 0.18;

    }

    return answer;
}
//Outputing the commission
public static int output(double dollars, double answer)
{

    JOptionPane.showMessageDialog(null, "Your total commission on sales of $" + dollars + " is $" + df.format(answer));

    return 0;
}
//Closes the program
public static void finish()
{

    System.exit(0);

    return;
}

}

  • 1
    You might want to check out what value `JOptionPane.CANCEL_OPTION` is. – OH GOD SPIDERS Apr 19 '17 at 16:29
  • 2
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Apr 19 '17 at 16:34

1 Answers1

0
else if (sales == JOptionPane.CANCEL_OPTION) {
 System.exit(0);
}

In JOptionPane class,

public static final int CANCEL_OPTION = 2;

Hence, value 2 will make you exit and frame closed.

Tony
  • 2,515
  • 14
  • 38
  • 71
  • 1
    Ahhh ok. I didn't think of that. Thanks! – Joshua G. Phillips Apr 19 '17 at 16:33
  • Are you using IDE ? You can check that by clicking `ctrl` + mouse click. – Tony Apr 19 '17 at 16:35
  • This also means that your attempt to check whether the user has pressed Cancel does not work. If I press Cancel, `showInputDialog()` returns `null` and `Integer.parseInt)` throws a `NumberFormatException` that you catch (since it is a subclass of `IllegalArgumentException`), and your program continues. – Ole V.V. Apr 19 '17 at 17:25