First I tried to fetch this program as close to pseudo code where I must follow all imported items, all method definitions, and all defined variables; anything else was my choice.
After I created working program that best represent the sample run of output we were provided in my homework documentation I decided to create checks for data input such as correct number input.
Now, is my code (sorry for expression) idiot proof? Meaning that whatever user inputs will not effect program's outputs?
What to do to ensure that this code will not fail because of user input?
In my program I am trying to use while loops as much as possible to let user to re-enter data if data that was entered is not appropriate. But is this enough?
Thank you in advance!
import java.io.*;
public class CalculatePayroll
{
public static final double TAX_SOCIAL = 0.10;
public static final double TAX_STATE = 0.10;
public static final double TAX_FEDERAL = 0.10;
public static void main(String[] args)
{
String inputName = null, mustBeNumeric = "[-+]?\\d+(\\.\\d+)?";
char inputStatus = 0, inputAskRepeat = 0;
double inputSalary = 0, inputRate = 0, inputHours = 0;
boolean boolRepeat = true;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Payroll Calculator");
while (boolRepeat == true)
{
boolean boolAskRepeat = true;
boolean boolAskStatus = true;
System.out.print("\nLast Name: ");
try {inputName = input.readLine(); }
catch (IOException e) { e.printStackTrace(); }
while (boolAskStatus == true)
{
System.out.print("Status (F or P): ");
try { inputStatus = input.readLine().toLowerCase().charAt(0); }
catch (IOException e) { e.printStackTrace(); }
if (inputStatus == "f".charAt(0))
{
boolAskStatus = false;
String stringCheckSalary = null;
boolean boolCheckSalary = true;
while (boolCheckSalary == true)
{
System.out.print("Salary: ");
try { stringCheckSalary = input.readLine(); }
catch (IOException e) { e.printStackTrace(); }
if (stringCheckSalary.matches(mustBeNumeric))
{
boolCheckSalary = false;
inputSalary = Double.parseDouble(stringCheckSalary);
}
else
boolCheckSalary = true;
}
outputData(inputName, inputStatus, calculateFullTimePay(inputSalary));
}
else if (inputStatus == "p".charAt(0))
{
boolAskStatus = false;
String stringCheckRate = null;
boolean boolCheckRate = true;
while (boolCheckRate == true)
{
System.out.print("Rate: ");
try { stringCheckRate = input.readLine(); }
catch (IOException e) { e.printStackTrace(); }
if (stringCheckRate.matches(mustBeNumeric))
{
boolCheckRate = false;
inputRate = Double.parseDouble(stringCheckRate);
}
else
boolCheckRate = true;
}
String stringCheckHours = null;
boolean boolCheckHours = true;
while (boolCheckHours == true)
{
System.out.print("Hours: ");
try { stringCheckHours = input.readLine(); }
catch (IOException e) { e.printStackTrace(); }
if (stringCheckHours.matches(mustBeNumeric))
{
boolCheckHours = false;
inputHours = Double.parseDouble(stringCheckRate);
}
else
boolCheckHours = true;
}
outputData(inputName, inputStatus, calculatePartTimePay(inputRate, inputHours));
}
else boolAskStatus = true;
}
while (boolAskRepeat == true)
{
System.out.print("Another payroll? (Y or N): ");
try { inputAskRepeat = input.readLine().toLowerCase().charAt(0); }
catch (IOException e) { e.printStackTrace(); }
if (inputAskRepeat == "n".charAt(0))
{
boolAskRepeat = false;
boolRepeat = false;
}
else if (inputAskRepeat == "y".charAt(0))
{
boolAskRepeat = false;
boolRepeat = true;
}
else boolAskRepeat = true;
}
}
System.out.print("\nProgram terminated...");
}
private static double calculateTaxes(double weeklyPay)
{
return weeklyPay * (TAX_SOCIAL + TAX_STATE + TAX_FEDERAL);
}
private static double calculatePartTimePay(double rate, double hours)
{
double pay = 0;
if (hours <= 40) pay = rate * hours;
else if (hours > 40) pay = (rate * 40) + ((hours - 40) * (rate * 1.5));
return pay;
}
private static double calculateFullTimePay(double salary)
{
return salary / 52;
}
private static void outputData(String name, char status, double pay)
{
if (status == "p".charAt(0))
{
System.out.println("-------------------------------------------------------");
System.out.println("Name\tStatus\t\tGross\tTaxes\tNet");
System.out.printf("%s\tPart Time\t%.2f\t%.2f\t%.2f\n\n",
name,
roundDouble(pay),
roundDouble(calculateTaxes(pay)),
roundDouble(pay - calculateTaxes(pay)));
}
else if (status == "f".charAt(0))
{
System.out.println("-------------------------------------------------------");
System.out.println("Name\tStatus\t\tGross\tTaxes\tNet");
System.out.printf("%s\tFull Time\t%.2f\t%.2f\t%.2f\n\n",
name,
roundDouble(pay),
roundDouble(calculateTaxes(pay)),
roundDouble(pay - calculateTaxes(pay)));
}
}
private static double roundDouble(double number)
{
return Double.parseDouble(String.valueOf((int)(number * 100))) / 100;
}
}
doubleorfloat, because they can't accurately represent most base 10 numbers. You should use eitherBigDecimal,intorlong, withintandlongrepresenting pennies, cents or their equivalents. More info here -> javapractices.com/topic/TopicAction.do?Id=13 – toofarsideways Sep 21 '12 at 10:33