Check Code to ensure correctness

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hammer45
    New Member
    • Feb 2008
    • 3

    Check Code to ensure correctness

    Modify payroll program so that it uses a class to store and retrive the employee's name, 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. Make sure the code is readable and well documented.

    This is what I have done is this correct, as per the parameters above:

    [CODE=java]import java.util.Array List;
    import java.util.Scann er; // program uses class Scanner

    class EmployeeData {
    EmployeeData(St ring 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 payroll4a
    {
    private String EmployeeData; // employee information for this Payroll

    private static int ArrayList;

    private static int i;

    // main method begins execution of java application
    public static void main( String args[] )
    {
    ArrayList employees = new ArrayList (); // employee information to be stored for all employees

    boolean stop = false; // This flag will control whether you 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.prin tln(); // outputs a blank line
    System.out.prin tln( "Please enter the employee name or 'stop' to end program: " ); // prompt
    String empName = input.nextLine( ); // read employee name

    if ( empName.compare To("stop") == 0) // Check whether user indicated to stop program
    {
    System.out.prin tln( "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
    // If we are at the end of input then NoSuchElement;
    // If there is still input left then InputMismatch
    {

    System.out.prin tln( "Please enter hourly rate: $" ); // prompt
    hourlyRate = input.nextFloat (); // read hourly rate from user
    if (hourlyRate <= 0) // prompt until a positive value is entered

    {
    System.out.prin tln( "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.prin tln( "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.prin tln( "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(em pName, hourlyRate, hoursWorked) {
    ///weeklyPay = hourlyRate * hoursWorked; // multiply

    };
    System.out.prin t( employee.getNam e() ); // display employee name
    System.out.prin tf( "'s weekly pay is: $%,.2f\n", employee.getWee klyPay() ); // display weekly pay





    }
    }

    } // end method main

    }} // end class Payroll4a[/CODE]
    Last edited by Ganon11; Feb 8 '08, 03:19 AM. Reason: Please use the [CODE] tags provided.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Is there any place in particular you're having problems? Is your code not doing what you want it to? Skimming it, I don't see anything that screams "WRONG!", though you never use employees anywhere. Also, if you're working in Java 1.5 or later (you should be, 1.6 is recommended), use an ArrayList<Emplo yeeData> so that you don't have to cast values accessed to EmployeeData.

    Comment

    • hammer45
      New Member
      • Feb 2008
      • 3

      #3
      Yes,
      As per the instructions store and retrieve name, hourly rate and the number of hours worked. Use a constructor to intialize the employee information.
      Does what I have written fit? It does compile and work........ Just want to ensure I have this done correctly

      Thank You
      Hammer45

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        It looks fine to me, though if you're going to declare an ArrayList to store employee data, you should put employee data into the list.

        Your constructor is fine as it is.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          If you are using a recent version of Java (>=5), you will want to use generics with collection classes:

          This collections Java tutorial describes interfaces, implementations, and algorithms in the Java Collections framework

          Comment

          • hammer45
            New Member
            • Feb 2008
            • 3

            #6
            Thank you both for your guidance.



            V/R
            Hammer

            Comment

            Working...