Requesting help for payroll program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycg0771
    New Member
    • Dec 2006
    • 5

    #1

    Requesting help for payroll program.

    I'm trying to modify the following 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. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated.
    Here is my latest code...

    Code:
    // Payroll program that calculates an employee's weekly pay.
    
    import java.util.Scanner; // program uses class Scanner
    
    class EmployeeData {
    	EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
    		name = newName;  hourlyRate = newHourlyRate;  hoursWorked = newHoursWorked;
    	}
    
    	public String getName() { return name; }
    	public float getWeeklyPay() { return hourlyRate * hoursWorked; }
    
    	private String name;
    	private float hourlyRate, hoursWorked;
    } // end of EmployeeData class
     
    public class Payroll3
    {
       private String EmployeeData; // employee information for this Payroll
    	   
       // main method begins execution of java application
       public static void main( String args[] )
       {
    		boolean stop = false; // This flag will control whether we exit the loop below
     
    		// Loop until user types "stop" as the employee name:
    		while (!stop)
    		{     
    			// create scanner to obtain input from command window
    			Scanner input = new Scanner ( System.in );
    			
    			System.out.println();  // outputs a blank line
    			System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
    			String empName = input.nextLine(); // read employee name
    			
    			if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
    			{
    				System.out.println( "Program ended." );
    				stop = true;
    			}
     			else
     			{
       				// User did not indicate to stop, so continue reading info for this iteration:
    				EmployeeData employee;
    				float hourlyRate; // first number to multiply
    				float hoursWorked; // second number to multiply
    				float weeklyPay; // product of hourlyRate and hoursWorked
    				
    				System.out.print( "Please enter hourly rate: $" ); // prompt
    				hourlyRate = input.nextFloat(); // read hourly rate from user
    				while (hourlyRate <= 0) // prompt until a positive value is entered
    		        {
    		        	System.out.print( "Hourly rate must be a positive value. " +
    		               "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
    		            hourlyRate = input.nextFloat(); // read hourly rate again
    		        }
    		 
              		System.out.print( "Please enter hours worked: " ); // prompt
              		hoursWorked = input.nextFloat(); // read number of hours worked from user
              		while (hoursWorked <= 0) // prompt until a positive value is entered
              		{
                 		System.out.print( "Hours worked must be a positive value. " +
                   			"Please enter the hours worked again: " ); // prompt for positive value for hours worked
                  		hoursWorked = input.nextFloat(); // read hours worked again
              		}
     
     				employee = new EmployeeData(empName, hourlyRate, hoursWorked);
              		///weeklyPay = hourlyRate * hoursWorked; // multiply
              
              		System.out.print( employee.getName() ); // display employee name
              		System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
              
            }
          }
       
       } // end method main
     
    } // end class Payroll3
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    What appears to be the problem, the EmployeeData class seems to do everything you have asked....

    I assume you need help with the main body of the program, but could you please specify what is the problem you are having....

    Comment

    • zaidalin79
      New Member
      • Nov 2006
      • 59

      #3
      I think we might be in the same class... I am trying to figure these things out too... I will try to help you as I get them...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        To store the Employers's data, you neen an ArrayList not a String.


        Code:
         
        // Payroll program that calculates an employee's weekly pay.
        import java.util.*; // program uses class Scanner 
        class EmployeeData {
         EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
          name = newName;
          hourlyRate = newHourlyRate;
          hoursWorked = newHoursWorked;
         }
         public String getName() {
          return name;
         }
         public float getWeeklyPay() {
          return hourlyRate * hoursWorked;
         }
         private String name;
         private float hourlyRate, hoursWorked;
        } // end of EmployeeData class
        public class Payroll3
        {
        
           // main method begins execution of java application
           public static void main( String args[] )
           {
        	 ArrayList employees = new ArrayList(); // employee information for this Payroll you need to be able to store many employees
          boolean stop = false; // This flag will control whether we exit the loop below
          // Loop until user types "stop" as the employee name:
          while (!stop)
          {
           // create scanner to obtain input from command window
           Scanner input = new Scanner ( System.in );
           System.out.println();  // outputs a blank line
           System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
           String empName = input.nextLine(); // read employee name
           if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
           {
        	System.out.println( "Program ended." );
        	stop = true;
           }
        	else
        	{
        	   // User did not indicate to stop, so continue reading info for this iteration:
        	EmployeeData employee;
        	float hourlyRate; // first number to multiply
        	float hoursWorked; // second number to multiply
        	float weeklyPay; // product of hourlyRate and hoursWorked
        	System.out.print( "Please enter hourly rate: $" ); // prompt
        	hourlyRate = input.nextFloat(); // read hourly rate from user
        	while (hourlyRate <= 0) // prompt until a positive value is entered
        		  {
        		   System.out.print( "Hourly rate must be a positive value. " +
        				 "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
        			  hourlyRate = input.nextFloat(); // read hourly rate again
        		  }
        			System.out.print( "Please enter hours worked: " ); // prompt
        			hoursWorked = input.nextFloat(); // read number of hours worked from user
        			while (hoursWorked <= 0) // prompt until a positive value is entered
        			{
        			   System.out.print( "Hours worked must be a positive value. " +
        				  "Please enter the hours worked again: " ); // prompt for positive value for hours worked
        				hoursWorked = input.nextFloat(); // read hours worked again
        			}
        	 employee = new EmployeeData(empName, hourlyRate, hoursWorked);
        			///weeklyPay = hourlyRate * hoursWorked; // multiply
        			System.out.print( employee.getName() ); // display employee name
        			System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
        			employees.add(employee);
        		}
        	  }
        	  //You can do additional stuff with the employees here
        
           } // end method main
        } // end class Payroll3

        Comment

        • ycg0771
          New Member
          • Dec 2006
          • 5

          #5
          Originally posted by r035198x
          To store the Employers's data, you neen an ArrayList not a String.


          Code:
           
          // Payroll program that calculates an employee's weekly pay.
          import java.util.*; // program uses class Scanner 
          class EmployeeData {
           EmployeeData(String newName, float newHourlyRate, float newHoursWorked) {
            name = newName;
            hourlyRate = newHourlyRate;
            hoursWorked = newHoursWorked;
           }
           public String getName() {
            return name;
           }
           public float getWeeklyPay() {
            return hourlyRate * hoursWorked;
           }
           private String name;
           private float hourlyRate, hoursWorked;
          } // end of EmployeeData class
          public class Payroll3
          {
          
             // main method begins execution of java application
             public static void main( String args[] )
             {
          	 ArrayList employees = new ArrayList(); // employee information for this Payroll you need to be able to store many employees
            boolean stop = false; // This flag will control whether we exit the loop below
            // Loop until user types "stop" as the employee name:
            while (!stop)
            {
             // create scanner to obtain input from command window
             Scanner input = new Scanner ( System.in );
             System.out.println();  // outputs a blank line
             System.out.print( "Please enter the employee name or 'stop' to end program: " ); // prompt
             String empName = input.nextLine(); // read employee name
             if ( empName.compareTo("stop") == 0) // Check whether user indicated to stop program
             {
          	System.out.println( "Program ended." );
          	stop = true;
             }
          	else
          	{
          	   // User did not indicate to stop, so continue reading info for this iteration:
          	EmployeeData employee;
          	float hourlyRate; // first number to multiply
          	float hoursWorked; // second number to multiply
          	float weeklyPay; // product of hourlyRate and hoursWorked
          	System.out.print( "Please enter hourly rate: $" ); // prompt
          	hourlyRate = input.nextFloat(); // read hourly rate from user
          	while (hourlyRate <= 0) // prompt until a positive value is entered
          		  {
          		   System.out.print( "Hourly rate must be a positive value. " +
          				 "Please enter the hourly rate again: $" ); // prompt for positive value for hourly rate
          			  hourlyRate = input.nextFloat(); // read hourly rate again
          		  }
          			System.out.print( "Please enter hours worked: " ); // prompt
          			hoursWorked = input.nextFloat(); // read number of hours worked from user
          			while (hoursWorked <= 0) // prompt until a positive value is entered
          			{
          			   System.out.print( "Hours worked must be a positive value. " +
          				  "Please enter the hours worked again: " ); // prompt for positive value for hours worked
          				hoursWorked = input.nextFloat(); // read hours worked again
          			}
          	 employee = new EmployeeData(empName, hourlyRate, hoursWorked);
          			///weeklyPay = hourlyRate * hoursWorked; // multiply
          			System.out.print( employee.getName() ); // display employee name
          			System.out.printf( "'s weekly pay is: $%,.2f\n", employee.getWeeklyPay() );  // display weekly pay
          			employees.add(employee);
          		}
          	  }
          	  //You can do additional stuff with the employees here
          
             } // end method main
          } // end class Payroll3
          Thank you, but this outputs a note of "uses unchecked or unsafe operations".

          Comment

          • ycg0771
            New Member
            • Dec 2006
            • 5

            #6
            Originally posted by zaidalin79
            I think we might be in the same class... I am trying to figure these things out too... I will try to help you as I get them...
            Thank you. I wasn't doing too bad in this class until this past week. I was ill for three days and I guess it threw me off track more than I ever imagined.

            Comment

            • ycg0771
              New Member
              • Dec 2006
              • 5

              #7
              Originally posted by DeMan
              What appears to be the problem, the EmployeeData class seems to do everything you have asked....

              I assume you need help with the main body of the program, but could you please specify what is the problem you are having....
              You are correct. I thought I had a problem here, but after going through it once again, step by step, the code works as it should. I hate to admit it, but my husband must be right when he tells me that I make things harder than what they really are. I apologize for wasting everyone's time here.
              Thank you.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by ycg0771
                You are correct. I thought I had a problem here, but after going through it once again, step by step, the code works as it should. I hate to admit it, but my husband must be right when he tells me that I make things harder than what they really are. I apologize for wasting everyone's time here.
                Thank you.
                No you did not waste anyone's time

                Comment

                • ycg0771
                  New Member
                  • Dec 2006
                  • 5

                  #9
                  Originally posted by r035198x
                  No you did not waste anyone's time
                  Thank you.

                  Comment

                  • deepslp
                    New Member
                    • Jan 2007
                    • 1

                    #10
                    Ok so I’m in this same class and your posts really helped allot. I just want to know how do I not plagiarize the work that was done by someone else if we all have to write the same application? I’m about to turn mine in and it’s almost a copy/paste of the code above even though I wrote it all out.

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Originally posted by deepslp
                      Ok so I’m in this same class and your posts really helped allot. I just want to know how do I not plagiarize the work that was done by someone else if we all have to write the same application? I’m about to turn mine in and it’s almost a copy/paste of the code above even though I wrote it all out.
                      If you've written the code by yourself, I wouldn't be terribly worried about it. Some simple programs have just about 1 solution, and every code solving that problem will look similar, if not identical. As long as your code works and you know you've written it yourself and thought the whole process through thoroughly, I think you'll be fine.

                      Comment

                      Working...