Click here to Skip to main content
15,792,397 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Good day,

I wanted to created a simple payroll console application that will display the employee details, number of hours worked, tax and the deducted amount. I am using a 2d array to create the table. I am however having a problem. I cannot seem to find a way to display the deducted amount (IT IS NOT SAVED IN THE ARRAY, IT GETS ITS VALUE FROM THE PREVIOUS TWO COLUMNS). So my question is, can we do column arithmetic like in Excel.

Thanks

Shaan
Posted
Updated 11-Apr-11 6:03am
v2

I don't think you should use a 2-d array here. Define a struct or class with all the properties you need like employee name, hours worked, tax etc. and then have an array (or other collection) of this object. Calculating the deducted amount would be a function of the class/struct and you'd be able to do the calculations required directly using the other class members.

Here's an example of what I mean:

C#
namespace Example
{
    class Program
    {
        class Employee
        {
            private const int HourlyRate = 55;
            private const double TaxPercentage = 0.35;
            public int Id { get; set; }
            public string FullName { get; set; }
            public int HoursWorked { get; set; }
            public int DeductedAmount
            {
                get
                {
                    return (int)((HoursWorked * HourlyRate) * (1 - TaxPercentage));
                }
            }
        }
        static void Main()
        {
            Employee emp1 = new Employee()
            {
                Id = 1,
                FullName = "Joe Smith",
                HoursWorked = 10
            };
            Employee emp2 = new Employee()
            {
                Id = 2,
                FullName = "Sally Brown",
                HoursWorked = 11
            };
            Employee[] employees = new Employee[] { emp1, emp2 };
            foreach (var employee in employees)
            {
                Console.WriteLine("Employee: {0}", employee.FullName);
                Console.WriteLine("Hours worked: {0}", employee.HoursWorked);
                Console.WriteLine("Deducted amount: {0}", employee.DeductedAmount);
            }
        }
    }
  
}
 
Share this answer
 
v2
Comments
#realJSOP 11-Apr-11 11:00am    
It's probably homework, and he was instructed to use an array. Nobody builds console apps for general use anymore in the real world (and haven't since the late 1980's).
Nish Nishant 11-Apr-11 11:08am    
Yeah, I guess so. Personally I don't really mind helping people with their homework. It's not very different to helping someone with his paid-work :-)
Nish Nishant 11-Apr-11 11:09am    
BTW did you notice how CP removes white spacing from pre-blocks? Kinda annoying.
fazleh ahmed 11-Apr-11 11:57am    
Thank You both. I will try to use both codes and have a look which works best. Thanks again.
fazleh ahmed 11-Apr-11 12:49pm    
Hi guys,

I just cant seem to get it to work. Could it be possible you look at my code and see whats the problem?

namespace FlyEazyStarter
{
class Program
{
static void Main(string[] args)
{
Payroll myPayroll = new Payroll();

myPayroll.addEmployee("007", "Crun", "Henry", 'A', 'M', 50);
myPayroll.print();
}//end method main()

}// end class

}

namespace FlyEazyStarter
{
class Payroll
{
public List<employee> employeeList;

static public Payroll()
{
employeeList = new List<employee>();
}// end constructor Payroll()

static public void addEmployee(string id, string surname, string forename, char grade, char deptCode, int hoursWorked)
{

Employee anEmployee = new Employee(id, surname, forename, grade, deptCode, hoursWorked);

employeeList.Add(anEmployee);

}// end method addEmployee()
static public void print()
{
foreach (Employee e in employeeList)
{
e.print();
}//end for each e: employeeList

}//end method print()

}//end class Payroll

}

namespace FlyEazyStarter
{
class PayRate
{
char grade;
int normalHrs;
double basicPayPerHr;
double overtimeRate;

public PayRate(char grade, int normalHrs, double basicPayPerHr, double overtimeRate)
{
this.grade = grade;
this.normalHrs = normalHrs;
this.basicPayPerHr = basicPayPerHr;
this.overtimeRate = overtimeRate;
} //end constructor PayRate(char, int, double, double)

} //end class PayRate

}

namespace FlyEazyStarter
{
class Employee
{
public string id;
public string surname;
public string forename;
public PayRate payRate;
public Department department;
public int hoursWorked;

public Employee(string id, string surname, string forename, char grade, char deptCode, int hoursWorked)
{
this.id = id;
this.surname = surname;
this.forename = forename;
//
// more code needed here
//
}//end constructor Employee(s,s,s,s,i)

public void print()
{
Console.WriteLine("{0} {1}\t{2}", id, surname, forename);
}//end method print()

}//end class Employee
}

namespace FlyEazyStarter
{
class Department
{

char deptCode;
string deptName;

public Department(char deptCode, string deptName)
{
this.deptCode = deptCode;
this.deptName = deptName;
}// end constructor Department (char, String)
}//end class Department
}

Thanks in advance.
I'd do it something like this:

C#
for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine("Deducted amount = {0}", array[i][1] - array[i][0]);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900