Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java - Employee Records

records

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 Rememo

Rememo

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 17 November 2012 - 03:01 PM

Hello,

I have just started learning Java... I am trying to do a task to get the hours worked to multiple the pay scale (1-5) to select hourly rate. Therefore if I type in 2 the hourly rate will appear as $5


package ok;

import java.util.Scanner;

public class NewClass1 {



static Scanner input = new Scanner(System.in);

			public static void main(String[] args)
			{
				String employeeName = "";  
				int numberofEmployees;
				String employeeNumber = "12345";	
				double hourlyPay = 0;
				int payScale = 0;			  
				double hoursWorked, regularTime, overtime;		
				double regularPay, overtimePay, totalPay;	


			   //
				final double PAY_SCALE1 = 4;
				final double PAY_SCALE2 = 5;
				final double PAY_SCALE3 = 6;
				final double PAY_SCALE4 = 7;
				final double PAY_SCALE5 = 8;
				final double OVERTIME_RATE = 1.5;
				final double OVERTIME_RATE2 = 2;




					System.out.print("Number of Employees: ");
					numberofEmployees = input.nextInt();

			   for(int employeeCounter = 0; employeeCounter < 3; employeeCounter++) {



					System.out.print("Enter Employee Number: ");
					employeeNumber = input.next();

					System.out.print("Enter Employee's First & Last Name: ");
					employeeName = input.next();



					// PayScale

				 System.out.println ("Type Pay Scale from 1 to 5" ) ;
				 payScale = input.nextInt() ;
				 switch (payScale)

					{  
					case 1: System.out.println ("Your Pay is:" + PAY_SCALE1);
					break;
					case 2: System.out.println ("Your Pay is:" + PAY_SCALE2);
					break;
					case 3: System.out.println ("Your Pay is:" + PAY_SCALE3);
					break;
					case 4: System.out.println ("Your Pay is:" + PAY_SCALE4);
					break;
					case 5: System.out.println ("Your Pay is:" + PAY_SCALE5);
					break;
					}


				System.out.print("Enter Hours Worked: ");
				hoursWorked = input.nextDouble();



				// Calculate   Pay
				if (hoursWorked <= 40)
				{
				  overtime = 0;
				  overtimePay = 0;
				  totalPay = hoursWorked * payScale ;
				}
				else if (hoursWorked >=41 && hoursWorked <=49)
				{

				  overtime = hoursWorked - 40;
				  overtimePay = payScale * (OVERTIME_RATE * 40);
				  totalPay = payScale + overtimePay;
				}
				else
				{

				  overtime = hoursWorked - 40;
				  overtimePay = payScale * (OVERTIME_RATE2 * 40);
				  totalPay = payScale + overtimePay;
				}


				System.out.println("======================");
				System.out.println("=//=  TASK =//=");
				System.out.println("==-=-= Employee =-=-==");
				System.out.println("-------------------------------------------");

				System.out.println("Employee #:	" + employeeNumber);
				System.out.println("Employee Name: " + employeeName);
				System.out.println("Hourly Pay: " + payScale);
				System.out.println("Hours Worked:   " + hoursWorked);
				System.out.println("Overtime Pay:  " + overtimePay);
				System.out.println("Total Pay:	" + totalPay);
				System.out.println("======================");
				}

				}
			}


I can't get the various Pay Scales to multiple the worked hours and add the overtime pay. Am I using the constants correctly as I don't know any other way to get the different pay scale to show the pay rates.... Any Help!

Edited by Roger, 21 November 2012 - 09:45 AM.
added code tags


#2 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts

Posted 18 November 2012 - 04:07 AM

I'm not actually too sure what you mean in what you're trying to do... Having a bit of trouble understanding but I think you're doing it right, just skipping a bit of logic. If I am correct in thinking what you're trying to do, try this out and let me know if it works:

//Calculate Pay
/* Here, you handle the basic case that would work for everyone and that is, calculate the pay for hours less than 40 */
overtime = 0;
overtimePay = 0;
/* What you're doing is:
totalPay = hoursWorked * payScale;
But you haven't set payScale to be anything, it is still 0. Modify your switch statement to set payScale based on the constant instead of out outputting to System.out. You're also not multiplying by the hourly rate. I'm unsure if this is intentional or not but I've added it here just in case */
totalPay = hoursWorked * payScale * hourlyRate;
/* Handle the case if the hoursWorked is >= 41 */
if (hoursWorked >= 41) {
/* Set the overtime */
overtime = hoursWorked - 40;
overtimePay = payScale * OVERTIME_RATE * 40);
totalPay += overtimePay;
} else if (hoursWorked > 49) {
/* Now, from your logic, any hours done over 40 is multiplied by OVERTIME_RATE2 so you don't calculate the old overtime rate, you use the new one */
overtime = hoursWorked - 40;
overtimePay = payScale * OVERTIME_RATE2 * 40;
totalPay += overtimePay;
}

I might be wrong in what you're trying to achieve but if you help me understand what you actually mean, I can help you :) For now though, make sure you set payscale in the switch statement. Read my comments for more details :)

EDIT: Sorry for the non-indentation of the code, it's time for bed so just opened TextWrangler to do it haha :)
I've returned...

#3 Rememo

Rememo

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 18 November 2012 - 06:17 AM

Thanks for the help.

How would I change the "switch statement to set payScale based on the constant instead of out outputting to System.out."

When I use the Switch statement does that not link it to ask "System.out.println ("Type Pay Scale from 1 to 5" ) ; meaning the case 1: System.out.println ("Your Pay is:" + PAY_SCALE1); therefore my pay would appear as $5, but then I could just show it as println($5) to show price but I am wanting to link them... Yeah true it does not calculate switch statement there but I am confused a bit at this point....




The only way I have found is to re-enter the pay Rate instead of it calculating from the payScale therefore on the Display it comes up

Enter Pay Scale 1 - 5: 2 (I enter)
Employee's Hourly Rate is: 5 (automatically appears from switch statement)
Enter Hourly Pay: 5 ( I have to retype this part)

I was just trying to find a way to calculate it using the switch statement without having to retype the pay. Below is what I changed to....





			final double PAY_SCALE1 = 4;
			final double PAY_SCALE2 = 5;
			final double PAY_SCALE3 = 6;
			final double PAY_SCALE4 = 7;
			final double PAY_SCALE5 = 8;


			 System.out.print("Number of Employees: ");
			 numberofEmployees = input.nextInt();

			 for(int employeeCounter = 0; employeeCounter < 3; employeeCounter++)
			   {
				  System.out.print("Enter Employee Number: ");
				  employeeNumber = input.next();

				  System.out.print("Enter Employee's First Name: ");
				  firstName = input.next();

				  System.out.print("Enter Employee's Surname: ");
				  lastName = input.next();

				  // PayScale
				  System.out.print("Enter Pay Scale 1 - 5: " ) ;
				  payScale = input.nextInt() ;
				  switch (payScale)
					 {  
					  case 1:  System.out.println ("Employee's Hourly Rate is: " + PAY_SCALE1);
					  break;
					  case 2:  System.out.println ("Employee's Hourly Rate is: " + PAY_SCALE2);
					  break;
					  case 3:  System.out.println ("Employee's Hourly Rate is: " + PAY_SCALE3);
					  break;
					  case 4:  System.out.println ("Employee's Hourly Rate is: " + PAY_SCALE4);
					  break;
					  case 5:  System.out.println ("Employee's Hourly Rate is: " + PAY_SCALE5);
					  break;
					  default: System.out.println( "Invalid number" ) ;
					  } // end of switch construct


				System.out.print("Enter Hourly Pay: ");
				hourlyPay = input.nextDouble();

				System.out.print("Enter Hours Worked: ");
				hoursWorked = input.nextDouble();

				// Calculate   Pay
				if (hoursWorked <= 40)
				{
				 totalPay = hoursWorked * hourlyPay;
				}
				else if (hoursWorked >=41 && hoursWorked <=49)
				{
				  regularTime = 40;
				  overtime = hoursWorked - 40;
				  regularPay = hourlyPay * 40;
				  overtimePay =  overtime * (hourlyPay * 1.5);
				  totalPay =  regularPay + overtimePay;
				}
				else
				{
				  regularTime = 40;
				  overtime = hoursWorked - 40;
				  regularPay = hourlyPay * 40;
				  overtimePay =  overtime * (hourlyPay * 2);
				  totalPay =  regularPay + overtimePay;
				}


				System.out.println("======================");
				System.out.println("=//=  TASK =//=");
				System.out.println("==-=-= Employee =-=-==");
				System.out.println("-------------------------------------------");

				System.out.println("Employee #:	" + employeeNumber);
				System.out.println("Employee Name: " + firstName + lastName);
				System.out.println("Hourly Pay: " + hourlyPay);
				System.out.println("Hours Worked:   " + hoursWorked);
				System.out.println("Overtime Pay:  " + overtimePay);
				System.out.println("Total Pay:	" + totalPay);
				System.out.println("======================");
				}

				}
			}


Just cant seem to get the over 50 hours to calculate if I go for say 55 hrs. Need to add normal rate + x1.5 rate (41-49) + x2 rate (over 50) Will continue to work on it. Thanks

Also in my code above when I do counter and enter 3 employees how do I validate or error out if someone types in 18 employees?

#4 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts

Posted 18 November 2012 - 03:09 PM

Oh, my apologies, I did read that wrong - you are setting payscale so there is no need to change your switch statement. If you were guaranteed to put in a value that would be one of PAY_SCALE1... PAY_SCALE5, you could remove the switch statement and just do this:
System.out.println("Employee's Hourly Rate is: " + payScale);

With how to calculate the pay, you need to think about it. Since you are using if else conditions, only one of the three blocks will run. If you want all three to run based on the conditions, you'll have to do something like I did earlier. I've added two solutions, one with a function to do it and another without it. Read the comments for why it's structured why it is. Basically, if you do if, else if, else, it will only run one of them three. But you might want it to execute all three and this should do that - it's not tested so there may be a few problems :)
/* Need to calculate the normal rate everytime for at least 40 hours */
if (hoursWorked < 41) {
totalPay = hoursWorked * hourlyPay;
} else {
/* It's greater than 40 but still need to work out normal pay for 40 hours */
totalPay = 40 * hourlyPay;
/* Now work out the first overtime pay */
if (hoursWorked < 50) {
  /* Already know it's > 40, so just check < 50
   * Now work out overtime for the hours left */
  overtime = hoursWorked - 40;
  overtimePay = overtime * hourlyPay * 1.5;
  /* Add the new overtime pay to the total pay */
  totalPay += overtimePay;
} else {
  /* Greater than 50 but still work out overtime pay for 10 hours */
  totalPay += 10 * hourlyPay * 1.5;
  /* Now work out for what is remaining at the higher rate */
  overtime = hoursWorked - 50;
  overtimePay = overtime * hourlyPay * 2;
  totalPay += overtimePay;
}
}

/* Calculate pay at a rate */
private double calculatePayAtRate (int hours, int payRate, int multiplier, double currentTotal) {
return currentTotal + hours * rate * multiplier;
}
if (hoursWorked < 41) {
totalPay = calculatePayAtRate(hoursWorked, hourlyPay, 1, totalPay);
} else {
totalPay = calculatePayAtRate(40, hourlyPay, 1, totalPay);
if (hoursWorked < 50) {
  totalPay = calculatePayAtRate(hoursWorked - 40, hourlyPay, 1.5, totalPay);
} else {
  totalPay = calculatePayAtRate(10, hourlyPay, 1.5, totalPay);
  totalPay = calculatePayAtRate(hoursWorked - 50, hourlyPay, 2, totalPay);
}
}

I've returned...

#5 Rememo

Rememo

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 19 November 2012 - 10:33 AM

Awesome just needed a few altercations and now works. Starting out in java and calculating involves thinking :) even for what seems simple.

if (hoursWorked <= 40)
			 {
				 totalPay = hoursWorked * hourlyPay;
			 }
			 else if (hoursWorked >=41 && hoursWorked <=49)
			 {
				 regularTime = 40;
				 overtime = hoursWorked - 40;
				 regularPay = hourlyPay * 40;
				 overtimePay = overtime * (hourlyPay * 1.5);
				 totalPay = regularPay + overtimePay;
			 }
			 else
			 {
				 regularTime = 40;
				 overtime = hoursWorked - 50;
				 regularPay = hourlyPay * 40;
				 overtimePay = overtime * (hourlyPay * 2) + (hourlyPay * 9 * 1.5);
				 totalPay = regularPay + overtimePay;
			 }


If I set it for 3 people, I can display figures for each person but can't get the total paid to all 3. I thought I would just do
System.out.println(totalEmployees * totalPay); outside the FOR statement brackets.....

I initialised it at the top to something like this - final int totalEmployees = 3; just to see if I could get the total for the 3....

#6 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts

Posted 19 November 2012 - 08:53 PM

Most definitely! Java is a good language to get you thinking and an example like this will actually teach you lots :) It's sometimes good to make mistakes too, even if they are intentional :)

Well, to help you with your new problem, each employee might have a different pay so multiplying the last user's `totalPay` by the `totalEmployees` is not very scalable. How I would achieve something like this would actually to create a global variable (outside the main method) called `totalOfEmployeePay` and renaming the current `totalPay` variable to `employeePay`. Just to have more clear variable names :)

Now at the end of the for loop, you would do something like:
totalOfEmployeesPay += employeePay;

That means at any state of the loop, totalOfEmployeesPay will be equal to the sum of `i` employee's pays :)
I've returned...

#7 Rememo

Rememo

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 20 November 2012 - 11:28 AM

Thanks




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download