import java.io.*; public class Employee { //class members declared to store values private static String EmpName; //to store name private static double hourlyRate; // to store hourly rate private static double hoursWorked; // to store hours worked private static String ErrMessage; //this is used to display error message //its not used right now, just declared public Employee(String EName,double hRate,double hWorked) { EmpName = EName; hourlyRate = hRate; hoursWorked = hWorked; } //Get Employee name public static String GetName() { return EmpName; } //Set Employee name public static void SetName(String EName) { EmpName = EName; } //Get Hourly Rate public static double GetHourlyRate() { return hourlyRate; } //Set Hourly Rate public static void SetHourlyRate(double hRate) { hourlyRate = hRate; } //Get Hours Worked public static double GethoursWorked() { return hoursWorked; } //Set Hours Worked public static void SethoursWorked(double hWorked) { hoursWorked = hWorked; } //Method to Calculate weekly payroll which returns double public static double CalculatePayRoll() { return (GetHourlyRate() * GethoursWorked()); } //This is a Method which accepts message and displays on screen to read input from console public static String GetUserInput(String Message) { //Class which reads input from console BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); System.out.print(Message); //Displaying the message to console try { return console.readLine(); //returning the value entered on the screen } catch (Exception e) // handling exceptions if any.. { e.printStackTrace(); return ""; } } //Method to validate input depending on hourly rate, hours worked and loops until valid value is entered. public static String GetValidateInput(String userInputPromtMessage, String validationName) { boolean isValid = false; String userInput = ""; try { do //loop until valid value is entered { userInput = GetUserInput(userInputPromtMessage); // prompting input from console //if it is hourly rate we should check with negative numbers and any characters if (validationName.equals("HourlyRate")){ isValid = Employee.isValidDouble(userInput,"Hourly Rate"); //function which validates whether it is valid input or not } //if it is hours worked we should check with negative numbers and any characters if (validationName.equals("HoursWorked")){ isValid = Employee.isValidDouble(userInput,"Hours Worked"); } if (!isValid) System.out.println(ErrMessage); // if its invalid outputs the message and prompts another time } while (!isValid); return userInput; } catch (Exception e) //handling exceptions if any.. { e.printStackTrace(); return ""; } } //This method is used to validate Employee name whether it is "stop" or not (which is used to control the flow) public static boolean ValidateEmpName() { if (EmpName.equalsIgnoreCase("stop")) return false; else return true; } //Method validating whether the value entered is double or not public static boolean isValidDouble(String Val,String Msg) { try { double validNum = Double.parseDouble(Val); //converting the value to double if (validNum < 0) //if less than zero will return false which prompts user to enter again { ErrMessage = Msg + " Should Not be Negative"; return false; } else return true; } catch (Exception e) { ErrMessage = "Invalid Input, should be numbers"; // if the value entered is any invalid (alphabet or alpha numberic) returns false return false; } } //Outputs message to screen public static void DisplayOutput() { System.out.println("\n\nEmployee Name..............:" + GetName()); System.out.println("Hourly Rate................:" + GetHourlyRate()); System.out.println("Hours Worked...............:" + GethoursWorked()); System.out.println("Weekly Payroll.............:" + CalculatePayRoll()); } }