Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Someone who can help me with my PAYROLL PROGRAM

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Someone who can help me with my PAYROLL PROGRAM

    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()));
            }
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Someone who can help me with my PAYROLL PROGRAM

    UP

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Someone who can help me with my PAYROLL PROGRAM

    Start with an error counter = 0, say numberOfErrors = 0, and check each user entry. If the user's entry is an error, the increment the error counter. Then check if the error counter > 3 and if so, let the user know they've errored out and exit.

    This approach requires the use of methods which you don't seem too familiar with. If you were, you wouldn't be repeating the same code over and over and over and over and . . . over again in what you posted.

    As for only accepting String inputs, since all alphanumeric inputs can be String objects, I assume you mean only allowing alpha characters. You can build your own character checker, you could use a regular expression, and there are probably a dozen other possible approaches.

Similar Threads

  1. employe payroll
    By daim9996 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2013, 08:35 AM
  2. Payroll Program Only Returning Value Of 0
    By starterkit123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2013, 10:17 AM
  3. Trouble with Java payroll program
    By Shawn Wray in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2012, 12:57 AM
  4. [SOLVED] Please help me with this payroll program
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 14th, 2011, 06:47 PM
  5. Payroll
    By Kesh486 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 6th, 2010, 06:48 PM