PDA

View Full Version : Methods of Java


UrbanTwitch
01-21-2010, 11:07 PM
OK so My code works. But after the first popup box. I press OK and then it keeps poping up. It's suppose to put another pop up box.

/**
Program name: Comissions Calculator
Programmer: Dan Jasnowski
Date: January 21, 2010
**/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class comCalc
{

public static double getSales()
{
double sales = 0.0;
boolean done = false;


while(!done)
{
String answer = JOptionPane.showInputDialog(null, "Enter the sales amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");

if (answer == null) finish();


try
{
sales = Double.parseDouble(answer);
if (sales <= 0) throw new NumberFormatException();
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.", "Error!!", JOptionPane.INFORMATION_MESSAGE);
}
}
return sales;
}

public static void finish()
{
System.out.println("Goodbye!");
System.exit(0);
}

public static void output(double comission, double sales)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");

JOptionPane.showMessageDialog(null, "Your Comission on sales of "+ twoDigits.format(sales) + " is " + twoDigits.format(comission),"Comission Totals", JOptionPane.INFORMATION_MESSAGE);
}


public static double getComm(double employeeSales, int employeeCode)
{
double comission = 0.0;

switch(employeeCode)
{
case 1:
comission = .10 * employeeSales;
break;

case 2:
comission = .14 * employeeSales;
break;

case 3:
comission = .18 * employeeSales;
break;
}
return comission;
}



public static int getCode()
{
int code = 0;
boolean done = false;


try
{
String message = "Enter the Comission code:\n\n1) Telephone Sales \n2) In-Store Sales \n3) Outside Sales\n\n";

code = Integer.parseInt(JOptionPane.showInputDialog(null,message));

if (code < 1 || code > 3) throw new NumberFormatException();
else done = true;
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please enter 1, 2, or 3.", " Error", JOptionPane.INFORMATION_MESSAGE);
}

return code;
}

public static void main(String[] args)
{
double dollars, answer;
int empCode;

dollars = getSales();
empCode = getCode();
answer = getComm(dollars,empCode);
output(answer,dollars);
finish();

}
}

What is wrong with my code?

Thanks for reading!

Old Pedant
01-21-2010, 11:58 PM
???

You do
while(!done)

But you never change done to true, so you stay in that loop forever.

UrbanTwitch
01-22-2010, 12:32 AM
Whoops! By adding done = true; above return sales; it worked.

Thanks! :P