Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By KevinWorkman

Thread: Problem with Taxes in Payroll Calc

  1. #1
    thoed_e is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Problem with Taxes in Payroll Calc

    The problem I keep getting is when I try to enter the if loop to calculate the taxes (lines 68-87). I don't know if its because of the number of if loops I have consecutively or not, but the calculation of taxes are wrong when I add more than one if loop; When I use just use one it works fine. I can't figure it out, what am I not getting? Thanks for suggestions!

    Java Code:
    import java.util.Scanner;
    
    public class PayrollCalcd {
    
    	public static final double TAXES_LOW = .05;
    	public static final double TAXES_MID = .0785;
    	public static final double TAXES_HIGH = .08975;
    	
    	double addWorker(double rate, double hours)
    	{
    		return hours*rate;
    	}
    	
    	double addManager(double rate, double hours)
    	{
    		return hours*rate;
    	}
    	
    	double addContractor(double amount)
    	{
    		return amount;
    	}
    	
    	double addOvertime(double hours, double overtime, double rate)
    	{
    		return overtime = (hours-40)*(rate*1.5);
    	}
    	
    	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;
    		double cSalary = 0;
    		double overtime = 0;
    		char reply;
    		int wnum = 0;
    		int cnum = 0;
    		int mnum = 0;
    		double deduction = 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++;
    			if (hours > 40)
    			{
    				wSalary += (rate*40) + prc.addOvertime(hours, overtime, rate);
    			}
    		
    			else{
    				wSalary += prc.addWorker(rate, hours);
    			}
    			
    		        if (wSalary <= 10000)
    			{
    				deduction = wSalary * TAXES_LOW;
    				wSalary -= deduction;
    			
    			}
    			
    			if (wSalary <= 20000)
    			{
    				deduction = wSalary * TAXES_MID;
    				wSalary -= deduction;
    			
    			}
    			
    			if (wSalary > 20000)
    			{
    				deduction = wSalary * TAXES_HIGH;
    				wSalary -= deduction;
    	
    			} 
    			
    		}
    
    		
    			
    		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++;
    			if (hours > 40)
    				
    				mSalary += (rate*40) + prc.addOvertime(hours, overtime, rate);
    			
    			else
    			mSalary += prc.addManager(rate, hours);
    		}
    		
    		 if (reply == 'C')
    		{
    			System.out.print("Please enter the amount the worker is " +
    							"owed: ");
    			amount = myScanner.nextDouble();
    			cnum++;
    			cSalary += 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 + cSalary);
    			System.out.print("Total Taxes Withheld: ");
    			System.out.println(deduction);
    		}
    	} while (reply != 'D');
    	
    	}
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Problem with Taxes in Payroll Calc

    If the salary is less than 10,000, you know it's less than 20,000. Looks like a job for an if/else statement.

    By the way, there's no such thing as an if loop.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with Taxes in Payroll Calc

    If wSalary <= 10000 then most certainly wSalary <= 20000; do you want to execute the body of both if-statements? ('if' is a statement, not a loop ).

    kind regards,

    Jos

    edit: darn, who's the slowest old sod again?
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    thoed_e is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with Taxes in Payroll Calc

    Ah, I see. Thanks guys. Sorry for the waste of a post.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Problem with Taxes in Payroll Calc

    It's not a waste. You figured out the problem and learned something in the process, right? That's exactly what these forums are for!
    gimbal2 and DarrylBurke like this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. could not find or load main class calc.Calc
    By doomking517 in forum New To Java
    Replies: 5
    Last Post: 06-12-2013, 09:45 PM
  2. need help postfix calc
    By b3222 in forum Advanced Java
    Replies: 1
    Last Post: 03-12-2012, 11:05 PM
  3. calculating taxes in java
    By KAM0002 in forum New To Java
    Replies: 16
    Last Post: 12-08-2010, 11:41 AM
  4. Investment Calc
    By rfore1 in forum New To Java
    Replies: 13
    Last Post: 02-28-2010, 06:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •