The way I understand things is that " salariedEmployeeObject.GrossPay = GrossPay(salary); " calls the GrossPay property for the "salariedEmployeeObject" and assigns it the value from the GrossPay(salary) class method.
So I was thinking that the problem was in the body of my ToString() which is like this:
return "Gross Pay: " + GrossPay.ToString("C") + "\n" +
"Net Pay: " + NetPay.ToString("C");
If that is the case I think I see what is happening. The ToString() method is calling the GrossPay and NetPay properties before they are assigned a value from the class methods. The problem is twofold. The GrossPay class methods are different depending on if the employee is hourly or salary, therefore, I am unable to replace the GrossPay.ToString("C") statement with a class method statement. I could do this with the NetPay.ToString("C") though. This is the second problem I run into. It's in a different class so when I replace the NetPay.ToString("C") with NetPay(grossPay).ToString("C") I get an error message telling me that "the NetPay does not exist in the current context." I am assuming that this is because the ToString() and the class method NetPay(grossPay) are in two different classes.
I'm not sure if my assumptions are correct and if they are I can't figure out what to do about it. I would appreciate it of someone could point out where I am going wrong and why. If you can tell me the best way to fix the problem without breaking the rules and doing my homework for me that would be nice as well. I've been banging my head for a couple of hours now and am at a loss.
Here is the code for the first class
using System;
namespace Payroll
{
public class PayrollApp
{
public static void Main(string[] args)
{
String employeeType = "";
//String employeeName = "";
double salary = 0;
double hoursWorked = 0 ;
double hourlyWage = 0;
double overTimeWorked = 0;
double overTimeWage = 0;
double grossPay = 0;
//call class method to begin control statement
employeeType = SalaryOrHourly();
//if salary employee, perform this work
if (employeeType == "Salary")
{
//instantiate salaried employee
Employee salariedEmployeeObject = new Employee();
salariedEmployeeObject.EmployeeName = AskForEmployeeName();
salariedEmployeeObject.Salary = AskForSalary();
salariedEmployeeObject.GrossPay = GrossPay(salary);
salariedEmployeeObject.NetPay = NetPay(grossPay);
Console.WriteLine(salariedEmployeeObject.ToString());
}
//if hourly employee, perform this work
else if (employeeType == "Hourly")
{
Employee hourlyEmployeeObject = new Employee();
hourlyEmployeeObject.EmployeeName = AskForEmployeeName();
hourlyEmployeeObject.HourlyRate = AskForHourlyWage();
hourlyEmployeeObject.HoursWorked = AskForHoursWorked();
hourlyEmployeeObject.OverTimeWorked = OverTimeWorked(
hoursWorked);
hourlyEmployeeObject.OverTimeRate = OverTimeWage(hourlyWage);
hourlyEmployeeObject.GrossPay = GrossPay(hourlyWage,
hoursWorked, overTimeWorked, overTimeWage);
hourlyEmployeeObject.NetPay = NetPay(grossPay);
Console.WriteLine(hourlyEmployeeObject.ToString());
}
}
//class method to determin if employee is salary or hourly
public static String SalaryOrHourly()
{
Console.WriteLine("Salary or Hourly (case sensitive): ");
String employeeType = Console.ReadLine();
return employeeType;
}
//classs method to request employee name
public static String AskForEmployeeName()
{
String employeeName;
Console.WriteLine("Enter Employee Name: ");
employeeName = Console.ReadLine();
return employeeName;
}
//class method to request salary
public static double AskForSalary()
{
Console.WriteLine("Enter salary: ");
double salary = double.Parse(Console.ReadLine());
return salary;
}
//class method to request hourly wage
public static double AskForHourlyWage()
{
Console.WriteLine("Enter Hourly Wage: ");
double hourlyWage = double.Parse(Console.ReadLine());
return hourlyWage;
}
//class method to request hours worked
public static double AskForHoursWorked()
{
Console.WriteLine("Enter hours worked: ");
double hoursWorked = double.Parse(Console.ReadLine());
return hoursWorked;
}
//class method to calculate overtime worked
public static double OverTimeWorked(double hoursWorked)
{
double overTimeWorked;
if (hoursWorked <= 40)
{
overTimeWorked = 0;
return overTimeWorked;
}
else
{
overTimeWorked = hoursWorked - 40;
return overTimeWorked;
}
}
//class method to calculate overtime rate
public static double OverTimeWage(double hourlywage)
{
double overTimeWage = 1.5 * hourlywage;
return overTimeWage;
}
//one of the overloaded mutator methods for grossPay datafield
public static double GrossPay(double salary)
{
double grossPay = salary;
return grossPay;
}
//one of the overloaded mutator methods for grossPay datafield
public static double GrossPay(double hourlyRate, double hoursWorked,
double overTimeWorked, double overTimeRate)
{
double grossPay = (hourlyRate * hoursWorked) +
(overTimeRate * overTimeWorked);
return grossPay;
}
//mutator method for netPay datafield
public static double NetPay(double grossPay)
{
const double FEDERAL_TAX_RATE = 0.18,
SOCIAL_SECURITY_TAX_RATE = 0.06,
RETIREMENT_FUND = 0.10;
double federalTaxOwed = grossPay * FEDERAL_TAX_RATE,
socialSecurityTaxOwed =
grossPay * SOCIAL_SECURITY_TAX_RATE,
retirementContribution = grossPay * RETIREMENT_FUND;
double netPay =
grossPay - federalTaxOwed - socialSecurityTaxOwed -
retirementContribution;
return netPay;
}
}
}
and here is the code for my second class
using System;
namespace Payroll
{
public class Employee
{
//declare variables
private String employeeName;
private double salary;
private double hourlyRate,
hoursWorked,
overTimeWorked,
overTimeRate,
grossPay,
netPay;
//property of the employeeName datafield
public String EmployeeName
{
get
{
return employeeName;
}
set
{
employeeName = value;
}
}
//property of the salary datafield
public double Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
//property of hourlyRate datafield
public double HourlyRate
{
get
{
return hourlyRate;
}
set
{
hourlyRate = value;
}
}
//property of the hoursWorked datafield
public double HoursWorked
{
get
{
return hoursWorked;
}
set
{
hoursWorked = value;
}
}
//property of the overTimeWorked datafield
public double OverTimeWorked
{
get
{
return overTimeWorked;
}
set
{
overTimeWorked = value;
}
}
//property of the overTimeRate datafield
public double OverTimeRate
{
get
{
return overTimeRate;
}
set
{
overTimeRate = value;
}
}
//property of the overTimeRate datafield
public double GrossPay
{
get
{
return grossPay;
}
set
{
grossPay = value;
}
}
//property of the NetPay datafield
public double NetPay
{
get
{
return netPay;
}
set
{
netPay = value;
}
}
//create default constructor
public Employee()
{
}
//create constructor for salaried employees
public Employee(String empName, double sal)
{
employeeName = empName;
salary = sal;
}
//create constructor for hourly employees
public Employee(String empName, double hrlyRate, double hrsWorked)
{
employeeName = empName;
hourlyRate = hrlyRate;
hoursWorked = hrsWorked;
}
//edit ToString() method
public override string ToString()
{
return "Gross Pay: " + GrossPay.ToString("C") + "\n" +
"Net Pay: " + NetPay(grossPay).ToString("C"); //NetPay.ToString("C");
}
}
}

New Topic/Question
Reply



MultiQuote





|