-
December 26th, 2010, 08:26 PM
#1
Some thing interesting :)
Code given below, suppose to give
hourly pay * number of hours + if hours greator then 37 then calculate rest with given rate (overtime)
and display final result
but all its doing is just multiplying hourly pay with overtime hourly pay (more like a logical error)
Code:
C# Syntax (Toggle Plain Text)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PayrollSystem
{
public class partTimeEmployees : Employee
{
private decimal wage; // wage per hour of work
private double hoursWorked; // hours worked during week
private decimal overTimeWage; // wage per hour for over time
// constructor
public partTimeEmployees(string firstNameValue, string LastNameValue,
decimal wageValue, double hoursWorkedValue, decimal overTimeWageValue)
: base(firstNameValue, LastNameValue)
{
Wage = wageValue;
HoursWorked = hoursWorkedValue;
OverTimeWage = overTimeWageValue;
}
// property Wage
public decimal Wage
{
get
{
return wage;
}
set
{
// ensure non-negative wage value
if (value > 0)
wage = value;
}
}
// property HoursWorked
public double HoursWorked
{
get
{
return hoursWorked;
}
set
{
// ensure non-negative hoursWorked value
if (value > 0)
hoursWorked = value;
}
}
// property over time Wage
public decimal OverTimeWage
{
get
{
return overTimeWage;
}
set
{
// ensure non-negative wage value
if (value > 0)
overTimeWage = value;
}
}
// override base-class method to calculate
// HourlyWorker earnings
public override decimal Earnings()
{
// compensate for overtime (paid "time-and-a-half")
if ( HoursWorked <= 37 )
{
return Wage * Convert.ToDecimal( HoursWorked );
}
else
{
// calculate base and overtime pay
decimal basePay = Wage * Convert.ToDecimal(37);
decimal overtimePay = OverTimeWage * Convert.ToDecimal( HoursWorked - 37);
return basePay + overtimePay;
}
}
// return string representation of HourlyWorker
public override string ToString()
{
return "Part Time Employee: " + base.ToString();
}
}
}
-
January 4th, 2011, 05:44 PM
#2
Re: Some thing interesting :)
firstresult = hourlypay*numofhours;
if(firstresult>37)
{
bleh
}
-
January 5th, 2011, 06:54 PM
#3
Re: Some thing interesting :)
Is it an order of operations problem? Try making the smaller calculations into a double and then adding it.
-
January 5th, 2011, 07:16 PM
#4
Re: Some thing interesting :)
You will obviously need to show us where HoursWorked is being calculated. Your example only shows the if statement, but we already know that the problem is that HoursWorked is never less than 38.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|