8 Replies - 669 Views - Last Post: 05 August 2013 - 02:35 PM Rate Topic: -----

#1 thoed_e   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-August 13

Payroll calculator trouble

Posted 05 August 2013 - 11:08 AM

Hello guys/gals,

I'm having an issue with my code summing workers salary. My code is in a do-while loop and when I enter more than one worker, it only adds the latest entry from the keyboard. I haven't been able to figure out what it is I'm missing, I've taken it out the loop and also tried two separate "if" loops. Suggestions?

if (reply == 'W')
		{
			System.out.print("Please enter the worker's pay rate: ");
			rate = myScanner.nextDouble();
			System.out.print("Please enter the number of hours worked: ");
			hours = myScanner.nextDouble();
			wnum++;
			wSalary = prc.addWorker(rate, hours);
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Payroll calculator trouble

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Payroll calculator trouble

Posted 05 August 2013 - 12:15 PM

Point to the line in the code you've posted (you can reference the line number) that is supposed to sum the worker's salary AND explain what that means. You either haven't shown that part, or I have no idea what you're talking about.
Was This Post Helpful? 0
  • +
  • -

#3 StrongJoshua   User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: Payroll calculator trouble

Posted 05 August 2013 - 01:01 PM

Yes, this code doesn't look incorrect. You need to post the entire problem (the entire loop) and anything else you think might be relevant.
Was This Post Helpful? 0
  • +
  • -

#4 thoed_e   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-August 13

Re: Payroll calculator trouble

Posted 05 August 2013 - 01:09 PM

import java.util.Scanner;

public class PayrollCalcd {

	double addWorker(double rate, double hours)
	{
		return hours*rate;
	}
	
	double addManager(double rate, double hours)
	{
		return hours*rate;
	}
	
	double addContractor(double amount)
	{
		return amount;
	}
	
	public static void main(String[] args) 
	{
		PayrollCalcd prc = new PayrollCalcd();
		Scanner myScanner = new Scanner(System.in);
		double hours, rate, amount = 0; 
		double wSalary = 0;
		double mSalary = 0;
		char reply;
		int wnum = 0;
		int cnum = 0;
		int mnum = 0;
	
	do{
		System.out.println("'W' - enter worker");
		System.out.println("'M' - enter a manager");
		System.out.println("'C' - enter a contractor");
		System.out.println("'D' - complete calculations and print results");
		reply = myScanner.findWithinHorizon(".", 0).charAt(0);
		

		if (reply == 'W')
		{
			System.out.print("Please enter the worker's pay rate: ");
			rate = myScanner.nextDouble();
			System.out.print("Please enter the number of hours worked: ");
			hours = myScanner.nextDouble();
			wnum++;
			wSalary = prc.addWorker(rate, hours);
		}

		
			
		if (reply == 'M')
		{
			System.out.print("Please enter the worker's pay rate: ");
			rate = myScanner.nextDouble();
			System.out.print("Please enter the number of hours worked: ");
			hours = myScanner.nextDouble();
			mnum++;
			mSalary = prc.addManager(rate, hours);
		}
		
		 if (reply == 'C')
		{
			System.out.print("Please enter the amount the worker is " +
							"owed: ");
			amount = myScanner.nextDouble();
			cnum++;
			amount = prc.addContractor(amount);
		}
		
		if (reply == 'D')
		{
			System.out.println("RESULTS:");
			System.out.print("Number of workers: ");
			System.out.println(wnum);
			System.out.print("Number of managers: ");
			System.out.println(mnum);
			System.out.print("Number of contractors: ");
			System.out.println(cnum);
			System.out.print("Total Gross Salaries: ");
			System.out.println(wSalary + mSalary + amount);
		}
	} while (reply != 'D');
	
	}

}


Here is the entire code; the issue I have comes when I try to input multiple worker/manager pay rate and hours. The program only calculates the last pay rate/hours I enter (lines 47, 59) instead of summing all entries (line 81.) If I'm still not being clear, I apologize for the confusion.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Payroll calculator trouble

Posted 05 August 2013 - 01:22 PM

Still a bit confused, but you're working with a single instance of the class PayrollCalcd, prc. Any change you make to prc, like calling prc.addWorker(), will change the existing values stored in the instance prc, hence causing the behavior you're observing.

If you want multiple instances of PayrollCalcd so that you can store different values, e.g. X workers and Y managers, then you should store multiple instances of PayrollCalcd in a collection of some kind, either an Array if you know how many or an ArrayList if you don't.

Make sense?
Was This Post Helpful? 1
  • +
  • -

#6 jjh08   User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Payroll calculator trouble

Posted 05 August 2013 - 01:28 PM

What is this line trying to do because I'm not an expert in Java?
reply = myScanner.findWithinHorizon(".", 0).charAt(0);

Also, your do-while loop seems unnecessarily complex IMHO because you have multiple if statements. I think there is probably a simpler way to do this but I'm not sure what you are trying to do with your code.
Was This Post Helpful? 1
  • +
  • -

#7 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Payroll calculator trouble

Posted 05 August 2013 - 02:12 PM

As I studied your code some more, perhaps a single instance of PayrollCalcd is enough, and you just need to add each new person's salary to the existing value. As it is, you're replacing the existing value with a new one each time through the loop. For example, instead of what you have, line 47 should become:
wSalary += prc.addWorker(rate, hours);

I also noticed there's not really any difference between addWorker() and addManager(). I'd condense those into a single method, appropriately named, if allowed.

You should also reduce repetition by moving repetitive code to methods. It won't make a huge difference in this small program, but it's a good habit to get into.
Was This Post Helpful? 2
  • +
  • -

#8 thoed_e   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-August 13

Re: Payroll calculator trouble

Posted 05 August 2013 - 02:29 PM

View Postjjh08, on 05 August 2013 - 01:28 PM, said:

What is this line trying to do because I'm not an expert in Java?
reply = myScanner.findWithinHorizon(".", 0).charAt(0);

Also, your do-while loop seems unnecessarily complex IMHO because you have multiple if statements. I think there is probably a simpler way to do this but I'm not sure what you are trying to do with your code.


It takes the keyboard input, that's how the book I was studying showed how to capture it.

View PostGregBrannon, on 05 August 2013 - 02:12 PM, said:

As I studied your code some more, perhaps a single instance of PayrollCalcd is enough, and you just need to add each new person's salary to the existing value. As it is, you're replacing the existing value with a new one each time through the loop. For example, instead of what you have, line 47 should become:
wSalary += prc.addWorker(rate, hours);

I also noticed there's not really any difference between addWorker() and addManager(). I'd condense those into a single method, appropriately named, if allowed.

You should also reduce repetition by moving repetitive code to methods. It won't make a huge difference in this small program, but it's a good habit to get into.


Thank you, I had been overlooking that this whole time. I'll condense the methods as well. Thanks again
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa   User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4213
  • View blog
  • Posts: 18,476
  • Joined: 24-September 08

Re: Payroll calculator trouble

Posted 05 August 2013 - 02:35 PM

View Postjjh08, on 05 August 2013 - 04:28 PM, said:

What is this line trying to do because I'm not an expert in Java?
reply = myScanner.findWithinHorizon(".", 0).charAt(0);

Also, your do-while loop seems unnecessarily complex IMHO because you have multiple if statements. I think there is probably a simpler way to do this but I'm not sure what you are trying to do with your code.


I just googled it myself, I think you put in a regex pattern as the first arg, then the horizon is like the outer limit? iirc, a '.' is just a single char. Idk, my regex sucks.

Also, OP, if you start to get a long list of if/elses like that, and you're really just checking for input or such, you can just use a switch. Also, reply = (char) myScanner.next().substring(0,0) should work too I think.

(I'm a fan of switches, can't you tell? :3)

This post has been edited by NeoTifa: 05 August 2013 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1