Payroll calculator

Page 1 of 1

9 Replies - 22215 Views - Last Post: 14 November 2008 - 06:43 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71486&amp;s=9d8000238b976fe4fda0e563d24d49ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Joe Six-Pack

Reputation: 0
• Posts: 7
• Joined: 11-November 08

Payroll calculator

Posted 11 November 2008 - 04:50 PM

Ok. I have a programming project that I'm working on. I'm more than half way done with the class and haven't run intro trouble like this. I mean, I'm posting for help. It's crazy. Here is a summary of the problem and then I'll post what I have.

Write a program that calculates the take-home pay for an employee. Two types of employees: Hourly and salary. User inputs employee type and the hourly wage if hourly, and salary if salary. You'll see my constants. Those are put into the equation, but I can't even get there to use them. I have to make this in two classes.

```using System;
using System.Collections.Generic;
using System.Text;

namespace TakeHomePayCh5
{
class CalculatePay
{
private double salaryPay;
private double hourlyPay;
private const double FEDERAL_TAX = .18;
private const double SOCIAL_TAX = .06;
private const double RETIREMENT = .10;
private string employeeType;

public CalculatePay()
{
}
public CalculatePay(double s1, double h1)
{
salaryPay = s1;
hourlyPay = h1;
}
public double SalaryPay
{
get
{
return salaryPay;
}
set
{
salaryPay = value;
}
}
public double HourlyPay
{
get
{
return hourlyPay;
}
set
{
hourlyPay = value;
}
}
public double ComputeSalaryPay()
{
return salaryPay - ((salaryPay * RETIREMENT) - (salaryPay * SOCIAL_TAX)
- (salaryPay * FEDERAL_TAX));
}
public double ComputeHourlyPay()
{
return hourlyPay - ((hourlyPay * RETIREMENT) - (hourlyPay * SOCIAL_TAX)
- (hourlyPay * FEDERAL_TAX));
}
public double DetermineTypeOfPay()
{
if (employeeType == '1')
hourlyPay = pay * hoursWorked;
if (hoursWorked > 40)
hourlyPay = pay *
}
}

}
```

Any advide? I need some guidance!
Any help is appreciated.

EDIT: Title edited to be more descriptive

PsychoCoder

Is This A Good Question/Topic? 0

Replies To: Payroll calculator

#2 PsychoCoder

Reputation: 1659
• Posts: 19,853
• Joined: 26-July 07

Re: Payroll calculator

Posted 11 November 2008 - 04:56 PM

I guess I'm not understanding what problems you're having

#3 Joe Six-Pack

Reputation: 0
• Posts: 7
• Joined: 11-November 08

Re: Payroll calculator

Posted 11 November 2008 - 07:12 PM

I don't know if what I'm doing is going to work.

I'm also having trouble with the App.

I don't know how to get the user to select between hourly and salary.

#4 Joe Six-Pack

Reputation: 0
• Posts: 7
• Joined: 11-November 08

Re: Payroll calculator

Posted 11 November 2008 - 07:23 PM

EDIT: I'll post the App class here in a minute.

This post has been edited by Joe Six-Pack: 11 November 2008 - 07:24 PM

#5 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

Re: Payroll calculator

Posted 11 November 2008 - 07:26 PM

The class you posted looks ok, you have properties and methods. Although you are missing a property for your employee type, so currently you have no way of setting/getting its value.

Show us the code that instantiates an object of CalculatePay, so we can see how you are using it.

EDIT: I just noticed you need to finish the DetermineTypeOfPay method. The code is incomplete.

#6 Joe Six-Pack

Reputation: 0
• Posts: 7
• Joined: 11-November 08

Re: Payroll calculator

Posted 11 November 2008 - 07:49 PM

If I do an if statement with employeeType how would I set it up?

I don't know how to set it up so when the User enters '1' the employee is hourly and '2' for salary.

This thing is killing me. I'm getting sloppy with my source code. I may need a break for 30 mins and then come back to it.

#7 fuzzylunkinz

Reputation: 8
• Posts: 185
• Joined: 11-November 08

Re: Payroll calculator

Posted 11 November 2008 - 08:01 PM

Hope this helps.

```Console.WriteLine("What do you want?! ");  // Ask a question.
Console.WriteLine("\t1. I want you!");  // Option 1
Console.WriteLine("\t2. I wanna leave, I'm scared!");  // Option 2
Console.WriteLine("\t3. Beat it, fool!");  // Option 3
Console.Write("Enter option: ");
string input = Console.ReadLine();  // Makes string input with the value user enters

```

Can be condensed to:
```// The question and options 1, 2, and 3 are now condensed into one write line
// \n is a new line and \t is a tab
Console.WriteLine("What do you want?! \n\t1. I want you!\n\t2. I wanna leave, I'm scared!\n\t3. Beat it, fool!");
Console.Write("Enter option: ");

```

And to make sure they enter the correct input:
```string input = null;
// Will loop around the same thing until they type in 1, 2, or 3
do{
Console.WriteLine("What do you want?! \n\t1. I want you!\n\t2. I wanna leave, I'm scared!\n\t3. Beat it, fool!");
Console.Write("Enter option: ");
}while(input != "1" && input != "2" && input != "3");

```

This post has been edited by fuzzylunkinz: 11 November 2008 - 08:10 PM

#8 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

Re: Payroll calculator

Posted 12 November 2008 - 06:54 AM

where does "hoursWorked" come from? It's not listed with your other variables at the top, and it's not passed into the method as a parameter.

it also looks like you didn't complete the calculation of working more than 40 hours.

also, you never actually set "employeeType".
```public double DetermineTypeOfPay()
{
if (employeeType == '1')
hourlyPay = pay * hoursWorked;
if (hoursWorked > 40)
hourlyPay = pay *
}

```

when you show code, SHOW ALL CODE. Don't show little by little. How do you expect us to debug your code when you don't post all the code?

also, post what the actual problem is. are you getting an error message? Just saying, "it doesn't work" doesn't help us out any.

This post has been edited by eclipsed4utoo: 12 November 2008 - 06:59 AM

#9 Joe Six-Pack

Reputation: 0
• Posts: 7
• Joined: 11-November 08

Re: Payroll calculator

Posted 13 November 2008 - 04:34 PM

Here is the app. I'm having trouble calling the methods in main. I'm trying different things, but I can't get it.

```using System;
using System.Collections.Generic;
using System.Text;

namespace TakeHomePayCh5
{
class TakeHomeApp
{
static void Main(string[] args)
{
string inValue;
string boolValue;

Console.WriteLine("Enter 1 For Hourly Employee: ");
boolValue = bool.Parse(inValue);

if (inValue = "1")
DetermineTypeOfPay();

CalculatePay one = new CalculatePay();

one.DetermineTypeOfPay();
}
//	public static double InputEmployeeType(string whichOne, out double employeeType)
//	{
//		string inValue;
//		string convertValue;

//		Console.Write("Enter 1 if the employee is paid hourly: ");
//		convertValue = bool.Parse(inValue);

//		if (inValue = "1")
//			Console.WriteLine("Employee is paid Hourly");
//		else
//			Console.WriteLine("Empolyee is on salary");

//}
//	public static void InputPay()
//	{
//		string inputPay;
//		Console.Write("Enter Employee's Pay: ");
```

```using System;
using System.Collections.Generic;
using System.Text;

namespace TakeHomePayCh5
{
class CalculatePay
{
private double salaryPay;
private double hourlyPay;
private const double FEDERAL_TAX = .18;
private const double SOCIAL_TAX = .06;
private const double RETIREMENT = .10;
int employeeType;
private double pay;
private double hoursWorked;
private double overtimePay;

public CalculatePay()
{
}
public CalculatePay(double s1, double h1)
{
salaryPay = s1;
hourlyPay = h1;
}
public double SalaryPay
{
get
{
return salaryPay;
}
set
{
salaryPay = value;
}
}
public double HourlyPay
{
get
{
return hourlyPay;
}
set
{
hourlyPay = value;
}
}
public double ComputeSalaryPay()
{
return salaryPay - ((salaryPay * RETIREMENT) - (salaryPay * SOCIAL_TAX)
- (salaryPay * FEDERAL_TAX));
}
public double ComputeHourlyPay()
{
return hourlyPay - ((hourlyPay * RETIREMENT) - (hourlyPay * SOCIAL_TAX)
- (hourlyPay * FEDERAL_TAX));
}
public double DetermineTypeOfPay()
{
if (employeeType == '1')
hourlyPay = pay * hoursWorked;
else
if (hoursWorked > 40)
overtimePay = (pay * 1.5) * (hoursWorked - 40);
else
ComputeSalaryPay();
return hourlyPay;

}
public override string ToString()
{
return "Enter Employee Type: " + DetermineTypeOfPay().ToString() +
ComputeHourlyPay().ToString("C");
}
}

}
```

I'm having trouble getting the user to enter in "1" for Hourly and "2" for Salary.

I'm having trouble calling the methods. As you can see, I've been trying many different things. Our instructor doesn't give us clues. I try to use the book, but with little success.

She hasn't prepared us for this stuff. Out of a class of 19, only two turned in this project on time. The two people that turned it in have prog backgrounds.

#10 eclipsed4utoo

Reputation: 1533
• Posts: 5,972
• Joined: 21-March 08

Re: Payroll calculator

Posted 14 November 2008 - 06:43 AM

1. you call "Console.ReadLine()" twice. This is most likely the reason you are not getting the number that the user is inputting as the employee type
2. Why are you trying to convert a string value into a boolean(using bool.Parse) then trying to assign it to a string value? Do you know what a boolean is?
3. Since C# is an object-oriented programming language, we will stick with using the "CalculatePay" object.

```static void Main(string[] args)
{
string inValue;
string boolValue;

Console.WriteLine("Enter 1 For Hourly Employee: ");

CalculatePay one = new CalculatePay();
one.EmployeeType = inValue;  // this will set the value of "m_EmployeeType" in the CalculatePay object
one.Pay = 15.00;
one.DetermineTypeOfPay();
}

```

4. Create a property for the "employeeType". Also, change the name of the private variable to something like "m_employeeType". This is a common way of declaring a private variable that also has a public property.(also do this for "pay")

```public int EmployeeType
{
get { return m_employeeType}
set {m_employeeType = value}
}

```

5. now you need to get the "pay". Is this something that the user is going to tell you, or is this something you are going to hard code? You can't calculate pay without first saying what the pay is.

6. the "DetermineTypeOfPay" method isn't exactly doing what the name says. If the employee type is 2, then you are calculating like it's overtime instead of a salaried employee. And we all know that salaried employees don't get overtime.

7. you also need to know how many hours an employee worked if they are an hourly employee. Did you not notice that you were multiplying the "pay", which is nothing, by the number of hours worked, which is also nothing. What did you expect to come out of that?

My suggestion is that you open your book back up and start over. I could continue to go on about all of the problems with this code.

This post has been edited by eclipsed4utoo: 14 November 2008 - 06:47 AM