Greetings ,
I have a problem with my payroll program .my problem is, how can I add error messages in my program.For example , if the user commit error 3 times quit the program after the 3rd message ,,another example,i need to ask a user to input his/her name in STRING only , if he/she input a number the program will ask to input again , another problem, user will input for no. of hours work should be numeric , if the input is character show the error message, then ask again to enter the no. of hours. In every error that the user might be committed give her/ her with appropriate message .
here's my code .. this program is payroll for monthly salary .
import java.text.DecimalFormat; import java.util.Scanner; public class EmployeePayroll { public static void main(String[] args) { Scanner dataIn = new Scanner(System.in); String firstName; String middleName; String lastName; int trials = 0; int choice = 0; String again; int hours = 0; int overtime = 0; int undertime = 0; DecimalFormat twoDigits = new DecimalFormat("P0.00"); EmployeeName epName = new EmployeeName(); EmployeeManager epM = new EmployeeManager(); EmployeeSupervisor epS = new EmployeeSupervisor(); EmployeeClerk epC = new EmployeeClerk(); System.out.println(" EMPLOYEES PAYROLL FOR MONTHLY SALARY"); System.out.println(); System.out.println(" Maximum of 3 ERRORS"); System.out.println("Note: If you've reach the maximum ERRORS the program will automatically exit."); System.out.println(); System.out.print("Employees First Name: "); firstName = dataIn.nextLine(); //input first name System.out.print("Employees Middle Name: "); middleName = dataIn.nextLine(); //input middle name System.out.print("Employees Last Name: "); lastName = dataIn.nextLine(); //input last name System.out.println(" [1]Manager [2]Supervisor [3]Clerk [4]Exit"); System.out.println(" -------------------------------------------------"); System.out.print(" What is your choice? : "); choice = dataIn.nextInt(); //input choice if (choice == 4) { //if choice is 4 , exit the program System.out.println(" Thank You for using the Payroll Program "); System.exit(0); } System.out.print("Number of Hours Work: "); hours = dataIn.nextInt(); //input hours if (hours > 176) { int OT = (int) (hours - 176); System.out.println(" Your number of hours work is " + hours ); System.out.println(" To get the overtime, substract the number of hours to 176" ); System.out.print("Enter the Overtime Hours: "); overtime = dataIn.nextInt(); while (overtime != OT) { System.out.println("Invalid Input!!"); System.out.print("Enter Again The Overtime Hours: "); overtime = dataIn.nextInt(); System.out.println(); } } if (hours < 176) { int UT = (int) (176 - hours); System.out.println(" Your number of hours work is " + hours ); System.out.println(" To get the overtime, substract 176 to the number of hours" ); System.out.print("Enter the Undertime Hours: "); undertime = dataIn.nextInt(); while (undertime != UT) { System.out.println("Invalid Input!!"); System.out.print("Enter Again The Undertime Hours: "); undertime = dataIn.nextInt(); System.out.println(); } } if (choice == 1) { System.out.println(); epName.setName(firstName, middleName, lastName); epM.setHours(hours); epM.setOt(overtime); epM.setUt(undertime); System.out.println(" -PAYROLL FOR MANAGER-"); System.out.println(); System.out.println("Name: " + epName.getName()); // for SSS if(hours > 176){ System.out.println("SSS:................." + twoDigits.format(epM.getSSSOM())); } else if (hours <176){ System.out.println("SSS:................." + twoDigits.format(epM.getSSSUM())); } else{ System.out.println("SSS:................." + twoDigits.format(epM.getSSSTM())); } //for PAGIBIG if(hours > 176){ System.out.println("PAGIBIG:............." + twoDigits.format(epM.getPagibigOM())); } else if (hours <176){ System.out.println("PAGIBIG:............." + twoDigits.format(epM.getPagibigUM())); } else{ System.out.println("PAGIBIG:............." + twoDigits.format(epM.getPagibigTM())); } //for PHILHEALTH if(hours > 176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epM.getPhilhealthOM())); } else if (hours <176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epM.getPhilhealthUM())); } else{ System.out.println("PHILHEALTH:.........." + twoDigits.format(epM.getPhilhealthTM())); } //for TAX if(hours > 176){ System.out.println("Tax:................." + twoDigits.format(epM.getTaxOM())); } else if (hours <176){ System.out.println("Tax:................." + twoDigits.format(epM.getTaxUM())); } else{ System.out.println("Tax:................." + twoDigits.format(epM.getTaxTM())); } //DEDUCTION System.out.println("Total Deduction:....." + twoDigits.format(epM.getDeductM())); System.out.println("Gross Income:........" + twoDigits.format(epM.getGrossM())); if (hours > 176) { System.out.println("Total Overtime Pay:.." + twoDigits.format(epM.getTotalOM())); } else { System.out.println("Total Overtime Pay:..P0.00"); } if (hours < 176) { System.out.println("Total Undertime Pay:.." + twoDigits.format(epM.getTotalUM())); } else { System.out.println("Total Undertime Pay:.P0.00"); } System.out.println("NET INCOME:.........." + twoDigits.format(epM.getNetM())); } if (choice == 2) { System.out.println(); epName.setName(firstName, middleName, lastName); epS.setHours(hours); epS.setOt(overtime); epS.setUt(undertime); System.out.println(" -PAYROLL FOR SUPERVISOR-"); System.out.println(); System.out.println("Name: " + epName.getName()); // for SSS if(hours > 176){ System.out.println("SSS:................." + twoDigits.format(epS.getSSSOS())); } else if (hours <176){ System.out.println("SSS:................." + twoDigits.format(epS.getSSSUS())); } else{ System.out.println("SSS:................." + twoDigits.format(epS.getSSSTS())); } //for PAGIBIG if(hours > 176){ System.out.println("PAGIBIG:............." + twoDigits.format(epS.getPagibigOS())); } else if (hours <176){ System.out.println("PAGIBIG:............." + twoDigits.format(epS.getPagibigUS())); } else{ System.out.println("PAGIBIG:............." + twoDigits.format(epS.getPagibigTS())); } //for PHILHEALTH if(hours > 176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epS.getPhilhealthOS())); } else if (hours <176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epS.getPhilhealthUS())); } else{ System.out.println("PHILHEALTH:.........." + twoDigits.format(epS.getPhilhealthTS())); } //for TAX if(hours > 176){ System.out.println("Tax:................." + twoDigits.format(epS.getTaxOS())); } else if (hours <176){ System.out.println("Tax:................." + twoDigits.format(epS.getTaxUS())); } else{ System.out.println("Tax:................." + twoDigits.format(epS.getTaxTS())); } //DEDUCTION System.out.println("Total Deduction:....." + twoDigits.format(epS.getDeductS())); System.out.println("Gross Income:........" + twoDigits.format(epS.getGrossS())); if (hours > 176) { System.out.println("Total Overtime Pay:.." + twoDigits.format(epS.getTotalOS())); } else { System.out.println("Total Overtime Pay:..P0.00"); } if (hours < 176) { System.out.println("Total Undertime Pay:.." + twoDigits.format(epS.getTotalUS())); } else { System.out.println("Total Undertime Pay:.P0.00"); } System.out.println("NET INCOME:.........." + twoDigits.format(epS.getNetS())); } if (choice == 3) { System.out.println(); epName.setName(firstName, middleName, lastName); epC.setHours(hours); epC.setOt(overtime); epC.setUt(undertime); System.out.println(" -PAYROLL FOR CLERK-"); System.out.println(); System.out.println("Name: " + epName.getName()); // for SSS if(hours > 176){ System.out.println("SSS:................." + twoDigits.format(epC.getSSSOC())); } else if (hours <176){ System.out.println("SSS:................." + twoDigits.format(epC.getSSSUC())); } else{ System.out.println("SSS:................." + twoDigits.format(epC.getSSSTC())); } //for PAGIBIG if(hours > 176){ System.out.println("PAGIBIG:............." + twoDigits.format(epC.getPagibigOC())); } else if (hours <176){ System.out.println("PAGIBIG:............." + twoDigits.format(epC.getPagibigUC())); } else{ System.out.println("PAGIBIG:............." + twoDigits.format(epC.getPagibigTC())); } //for PHILHEALTH if(hours > 176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epC.getPhilhealthOC())); } else if (hours <176){ System.out.println("PHILHEALTH:.........." + twoDigits.format(epC.getPhilhealthUC())); } else{ System.out.println("PHILHEALTH:.........." + twoDigits.format(epC.getPhilhealthTC())); } //for TAX if(hours > 176){ System.out.println("Tax:................." + twoDigits.format(epC.getTaxOC())); } else if (hours <176){ System.out.println("Tax:................." + twoDigits.format(epC.getTaxUC())); } else{ System.out.println("Tax:................." + twoDigits.format(epC.getTaxTC())); } //DEDUCTION System.out.println("Total Deduction:....." + twoDigits.format(epC.getDeductC())); System.out.println("Gross Income:........" + twoDigits.format(epC.getGrossC())); if (hours > 176) { System.out.println("Total Overtime Pay:.." + twoDigits.format(epC.getTotalOC())); } else { System.out.println("Total Overtime Pay:..P0.00"); } if (hours < 176) { System.out.println("Total Undertime Pay:.." + twoDigits.format(epC.getTotalUC())); } else { System.out.println("Total Undertime Pay:.P0.00"); } System.out.println("NET INCOME:.........." + twoDigits.format(epC.getNetC())); } } }