public class Tester
{
// Fields (Always static in main method of starting class)
static float oldAnnualSalary; // Current annual salary
// 0 not valid or unchanged
// .01 - 1000000.00
static float newAnnualSalary; // New annual salary
// 0 not valid or unchanged
// .01 - 2000000.00
static float percentIncrease; // Percent increase as whole
// percent
// ex 10.5, 1, .03, 100
static int msgTypeQ =
JOptionPane.QUESTION_MESSAGE; // showInputDialog message type
// in variable to shorten code
public static void main(String[]args)
{
// Get oldAnnualSalary from user
inputOldAnnualSalary();
// oldAnnualSalary not valid
if (oldAnnualSalary != 0)
{
// Get percentIncrease from user
inputPercentIncrease();
// percentIncrease not valid
if (percentIncrease != 0)
{
if (canCompute())
{
// Compute new salary
newAnnualSalary = oldAnnualSalary *
(1 + percentIncrease / 100); // Convert whole
// percent to
// decimal
// and add one.
// Multiply by
// old salary
// Display salary change report
System.out.println("Your old salary is " + oldAnnualSalary);
System.out.println("Percent increase is " + percentIncrease);
System.out.println("Your new salary is " + newAnnualSalary);
}
}
}
}
static void inputOldAnnualSalary()
{
String returnValue; // showInputDialog return value
String prompt; // showInputDialog prompt argument
String title; // showInputDialog title argument
oldAnnualSalary = 0; // 0 not valid or unchanged
// Prompt and get annual salary
prompt = "Enter your annual salary";
title = "Attention";
returnValue = JOptionPane.showInputDialog(null, prompt, title, msgTypeQ);
// Salary was entered - convert to float
if (returnValue != null && !returnValue.equals(""))
{
// Convert returnValue to float
oldAnnualSalary = Float.parseFloat(returnValue);
// oldAnnualSalary is not from .01 to 1000000.00
if ( oldAnnualSalary < 0.01 || oldAnnualSalary > 1000000.00 )
{
System.out.println("Salary entered was not from 0.01 to 1000000.00.");
oldAnnualSalary = 0; // Set to failed value
}
}
else
{
System.out.println("Salary was not entered");
}
}
static void inputPercentIncrease()
{
String returnValue; // showInputDialog return value
String prompt; // showInputDialog prompt argument
String title; // showInputDialog title argument
percentIncrease = 0; // 0 not valid or unchanged
// Prompt and get percentage increase
prompt = "Enter percent increase (.01 - 100) ex 10.5 for 10.5% : ";
title = "Attention";
returnValue = JOptionPane.showInputDialog(null, prompt, title, msgTypeQ);
// Percentage increase was entered - convert to float
if (returnValue != null && !returnValue.equals(""))
{
// Convert returnValue to float
percentIncrease = Float.parseFloat(returnValue);
// precentIncrease is not 0.0 - 100.0
if (percentIncrease < 0.01 || percentIncrease > 100.00)
{
System.out.println("Percent increase is not .01 to 100.");
percentIncrease = 0;
}
}
else
{
System.out.println("Percent increase was not entered.");
}
}
static boolean canCompute()
{
boolean isValid = true; // Can compute state
// Cannot compute
if ( oldAnnualSalary == 0 || percentIncrease == 0)
{
isValid = false; // Cannot compute state
}
return isValid;
}
}