CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2010
    Posts
    0

    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();
            }
        }
     
    }

  2. #2
    Join Date
    Nov 2010
    Posts
    34

    Re: Some thing interesting :)

    firstresult = hourlypay*numofhours;
    if(firstresult>37)
    {
    bleh
    }

  3. #3
    Join Date
    Jul 2010
    Posts
    82

    Re: Some thing interesting :)

    Is it an order of operations problem? Try making the smaller calculations into a double and then adding it.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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
  •  





Click Here to Expand Forum to Full Width

Featured