Need help with payroll program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbailey006
    New Member
    • Jan 2007
    • 3

    #1

    Need help with payroll program.

    I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I have.

    // Calculate.java
    // Calculate weekly pay.

    import java.util.Scann er;


    public class calculate
    {
    // main method begins execution of java application
    public static void main( String args[] )
    {
    Scanner input = new Scanner ( System.in );
    double hourlyrate;
    double hours;
    double pay;

    //boolean stop = false; // Exit the loop below

    //loop until user enters "stop" as the employee name:

    // while (!stop)

    // create scanner to obtain data from user


    System.out.prin t( "Please enter employee name or stop to exit program: " );
    String name = input.nextLine( );

    while (name != "stop"){

    System.out.prin tln("Please enter hourly rate: $" );}

    hourlyrate = input.nextDoubl e();

    if (hourlyrate >= 0){

    System.out.prin tln("hourly rate is: " + hourlyrate);}

    if (hourlyrate <= 0){

    System.out.prin tln("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

    hourlyrate = input.nextDoubl e();

    System.out.prin tln("Please enter hours worked: ");

    hours = input.nextDoubl e();

    if (hours >=0){

    System.out.prin tln("hours is: " + hours);}

    if (hours <=0){

    System.out.prin tln("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

    hours = input.nextDoubl e();

    pay = hourlyrate * hours;

    input.nextLine( );

    System.out.prin tf("%s pay is $ %.2f\n",name, pay);




    // Display employee information


    } // end method main

    } // end class Calculate
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by jbailey006
    I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I have.

    // Calculate.java
    // Calculate weekly pay.

    import java.util.Scann er;


    public class calculate
    {
    // main method begins execution of java application
    public static void main( String args[] )
    {
    Scanner input = new Scanner ( System.in );
    double hourlyrate;
    double hours;
    double pay;

    //boolean stop = false; // Exit the loop below

    //loop until user enters "stop" as the employee name:

    // while (!stop)

    // create scanner to obtain data from user


    System.out.prin t( "Please enter employee name or stop to exit program: " );
    String name = input.nextLine( );

    while (name != "stop"){

    System.out.prin tln("Please enter hourly rate: $" );}

    hourlyrate = input.nextDoubl e();

    if (hourlyrate >= 0){

    System.out.prin tln("hourly rate is: " + hourlyrate);}

    if (hourlyrate <= 0){

    System.out.prin tln("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

    hourlyrate = input.nextDoubl e();

    System.out.prin tln("Please enter hours worked: ");

    hours = input.nextDoubl e();

    if (hours >=0){

    System.out.prin tln("hours is: " + hours);}

    if (hours <=0){

    System.out.prin tln("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

    hours = input.nextDoubl e();

    pay = hourlyrate * hours;

    input.nextLine( );

    System.out.prin tf("%s pay is $ %.2f\n",name, pay);




    // Display employee information


    } // end method main

    } // end class Calculate
    Hey, the question says you should write the Employee class. Write code for that and post it and we will be able to help where you are wrong.

    Comment

    • jbailey006
      New Member
      • Jan 2007
      • 3

      #3
      Originally posted by r035198x
      Hey, the question says you should write the Employee class. Write code for that and post it and we will be able to help where you are wrong.
      public class Employee
      {

      private String employeeName; // name for wages
      private double hourlyrate;
      private double hours;

      // method to set the name
      public void setEmployeeName ( String name )
      {
      employeeName = name; // store name
      } // end method setEmployeeName

      // method to retrieve the employee name
      public String getEmployeeName ()
      {
      return employeeName;
      } // end method getEmployeeName

      // display employee name
      public void displayMessage( )
      {
      // this statement calls getEmployeeName
      System.out.prin tf( "Employee's name is: %s\n",
      getEmployeeName () );
      } // end method displayMessage
      public void setHourlyrate( double hourlyrate )
      {
      hourlyrate = hourlyrate;
      }

      public double getHourlyrate() {
      return hourlyrate;
      }
      public void setHours( double hours )
      {
      hours = hours;
      }

      public double getHours(){
      return hours;
      }
      public double getPay(){
      return hourlyrate * hours;
      }

      } // end class Employee

      // Calculate.java
      // Calculate weekly pay.

      import java.util.Scann er;


      public class calculate
      {
      // main method begins execution of java application
      public static void main( String args[] )
      {
      Scanner input = new Scanner ( System.in );

      double hourlyrate;
      double hours;
      double pay;

      //boolean stop = false; // Exit the loop below

      //loop until user enters "stop" as the employee name:

      // while (!stop)

      // create scanner to obtain data from user

      Employee aEmployee = new Employee();

      System.out.prin tln( "Please enter employee name or stop to exit program: " );
      String name = input.nextLine( );
      aEmployee.setEm ployeeName( name );
      System.out.prin tln();
      aEmployee.displ ayMessage();

      while (name != "stop"){

      System.out.prin tln( "Program Ended" );

      System.out.prin tln("Please enter hourly rate: $" );}

      hourlyrate = input.nextDoubl e();

      if (hourlyrate >= 0){

      System.out.prin tln("hourly rate is: " + hourlyrate);}

      if (hourlyrate <= 0){

      System.out.prin tln("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

      hourlyrate = input.nextDoubl e();

      System.out.prin tln("Please enter hours worked: ");

      hours = input.nextDoubl e();

      if (hours >=0){

      System.out.prin tln("hours is: " + hours);}

      if (hours <=0){

      System.out.prin tln("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

      hours = input.nextDoubl e();

      pay = hourlyrate * hours;

      input.nextLine( );

      System.out.prin tf("%s pay is $ %.2f\n",name, pay);




      // Display employee information


      } // end method main

      } // end class Calculate

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by jbailey006
        public class Employee
        {

        private String employeeName; // name for wages
        private double hourlyrate;
        private double hours;

        // method to set the name
        public void setEmployeeName ( String name )
        {
        employeeName = name; // store name
        } // end method setEmployeeName

        // method to retrieve the employee name
        public String getEmployeeName ()
        {
        return employeeName;
        } // end method getEmployeeName

        // display employee name
        public void displayMessage( )
        {
        // this statement calls getEmployeeName
        System.out.prin tf( "Employee's name is: %s\n",
        getEmployeeName () );
        } // end method displayMessage
        public void setHourlyrate( double hourlyrate )
        {
        hourlyrate = hourlyrate;
        }

        public double getHourlyrate() {
        return hourlyrate;
        }
        public void setHours( double hours )
        {
        hours = hours;
        }

        public double getHours(){
        return hours;
        }
        public double getPay(){
        return hourlyrate * hours;
        }

        } // end class Employee

        // Calculate.java
        // Calculate weekly pay.

        import java.util.Scann er;


        public class calculate
        {
        // main method begins execution of java application
        public static void main( String args[] )
        {
        Scanner input = new Scanner ( System.in );

        double hourlyrate;
        double hours;
        double pay;

        //boolean stop = false; // Exit the loop below

        //loop until user enters "stop" as the employee name:

        // while (!stop)

        // create scanner to obtain data from user

        Employee aEmployee = new Employee();

        System.out.prin tln( "Please enter employee name or stop to exit program: " );
        String name = input.nextLine( );
        aEmployee.setEm ployeeName( name );
        System.out.prin tln();
        aEmployee.displ ayMessage();

        while (name != "stop"){

        System.out.prin tln( "Program Ended" );

        System.out.prin tln("Please enter hourly rate: $" );}

        hourlyrate = input.nextDoubl e();

        if (hourlyrate >= 0){

        System.out.prin tln("hourly rate is: " + hourlyrate);}

        if (hourlyrate <= 0){

        System.out.prin tln("Hourly rate must be a positive value. " + "Please enter the hourly rate again: $");}

        hourlyrate = input.nextDoubl e();

        System.out.prin tln("Please enter hours worked: ");

        hours = input.nextDoubl e();

        if (hours >=0){

        System.out.prin tln("hours is: " + hours);}

        if (hours <=0){

        System.out.prin tln("Hours worked must be a positive value. " + "Please enter the hours worked again: ");}

        hours = input.nextDoubl e();

        pay = hourlyrate * hours;

        input.nextLine( );

        System.out.prin tf("%s pay is $ %.2f\n",name, pay);




        // Display employee information


        } // end method main

        } // end class Calculate
        Going in the right direction but I'm still not interested yet. How do we create the Epmloyees if you haven't added a construcor for that.

        Add two constructors, indent the code and post it using code tags.

        Comment

        • jbailey006
          New Member
          • Jan 2007
          • 3

          #5
          Originally posted by r035198x
          Going in the right direction but I'm still not interested yet. How do we create the Epmloyees if you haven't added a construcor for that.

          Add two constructors, indent the code and post it using code tags.
          I think I figured it out. I have another project involving an Inventory program. I can see from previous post that it's a popular topic.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by jbailey006
            I think I figured it out. I have another project involving an Inventory program. I can see from previous post that it's a popular topic.
            Good. I always like to see people find the solutions themselves. The inventory thing seems to be the thing of the moment.

            Comment

            Working...