Solution by: Abdun Mahmood, MSc, OTA ID#: 103644
Submitted on: August 30, 2006, 1:35 pm EDT
Computer Science, Data Structures and Algorithms
Year 2
java programming problem
--------------------------------------------------------------------------------
Solution
I have created the code and commented it so that you can understand how it is done.
Hope it helps. If you find it helpful, please leave comment and reserve future problems for me.
Thanks
OTA 103644
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* Created by
* User: OTA 103644
* Date: Aug 31, 2006
* Time: 12:40:00 AM
* To change this template use File | Settings | File Templates.
*/
public class payroll {
public static void main( String[] args){
InputStreamReader stdin = new InputStreamReader(System.in);
BufferedReader console = new BufferedReader(stdin);
String strEmpName,strHourlyRate;
double dblHourlyRate = 0; //initializing the variables
double dblWeeklyPay;
double dblHoursWorked = 0;
boolean loopAgain; //boolean flag to check if stop has been entered indicating end of outer loop
boolean error; //boolean flag to check if any of the numerical values are non-positive
//prompt and input
try{
do{
System.out.println("Please Enter the name of the Employee");
strEmpName = console.readLine();
// Check if the input name equals stop, ignoring case
if (strEmpName.equalsIgnoreCase("stop")){
break;
}
else loopAgain = true;
// Reading loop for Hourly Rate
error = true;
while (error){
error = false; //Setting this to false enables us to re-use the error variable
System.out.println("Please Enter the hourly rate");
strHourlyRate = console.readLine();
dblHourlyRate = Double.parseDouble(strHourlyRate);
if (dblHourlyRate<0){
System.out.println("Hourly Rate cannot be negative.");
error = true;
}
}
// Reading loop for Hours Worked
error = true;
while (error){
error = false; //Setting this to false enables us to re-use the error variable
System.out.println("Please Enter the working hours");
dblHoursWorked = Double.parseDouble(console.readLine());
if (dblHoursWorked<0){
System.out.println("Hours Worked cannot be negative.");
error = true;
}
}
// Calculate the Weekly Pay
dblWeeklyPay = dblHoursWorked * dblHourlyRate;
System.out.println("Employee: "+ strEmpName+ ", Weekly Pay: "+dblWeeklyPay);
}while (loopAgain); //End Do While
}
catch(IOException ioex)
{
System.out.println("Input error");
System.exit(1);
}
} //End Main
} //End payroll
