Reputation: 121
Hi guys I am pretty new on C# and I would like to make this code works the right way and understand why is not running the right way.
So I have this.
class PayrollRunner
{
static void Main(string[] args)
{
// use Employee without tax
Employee john = new Employee(1, "John Doe", 20000, false);
john.printInformation();
// use Employee with tax
Employee jane = new Employee(2, "Jane Doe", 36000);
jane.printInformation();
// use WeeklyEmployee without tax
// WeeklyEmployee jack = new WeeklyEmployee(3, "Jack Deer", 18500, false);
//jack.printInformation();
// use WeeklyEmployee with tax
//WeeklyEmployee jen = new WeeklyEmployee(4, "Jen Deer", 18000);
// jen.printInformation();
Console.Read();
}
}
class Employee
{
private int employeeId;
private string fullName;
private float salary;
private bool taxDeducted;
public Employee(int employeeId, string fullName, float salary, bool taxDeducted)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}
public Employee(int employeeId, string fullName, float salary)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = true;
}
public float getNetSalary()
{
float netSalary;
float tax = 0.8;
if (taxDeducted)
{
netSalary = salary * tax;
}
else
{
netSalary = salary;
}
return netSalary;
}
float netSalary = Employee.getNetSalary();
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
netSalary + " per month");
}
}
So I am expecting the following results on screen
1 John Doe earns 20000 per month 2 Jane Doe earns 28800 per month
But I am getting
1 John Doe earns 20000 per month 2 Jane Doe earns 36000 per month
I don't know how to define the method or maybe there is something else wrong, anybody would like to share its knowledge.
Thanks.
Upvotes: 1
Views: 432
Reputation: 15091
I changed the fields to automatic properties. This helps if you need to bind your class in other apps. I capitalized the initial letter of methods and public properties to make VS happy. I made the GetNetSalary private. It won't be accessed outside of the class and I used it to set a new property, NetSalary. It is called in the constructor. I also got rid of Console.WriteLine in the class and provided an Override of .ToString. This disconnects the class from the User Interface and allows the class to migrate to other types of applications.
class PayrollRunner
{
static void Main(string[] args)
{
// use Employee without tax
Employee john = new Employee(1, "John Doe", 20000, false);
Console.WriteLine(john.ToString());
// use Employee with tax
Employee jane = new Employee(2, "Jane Doe", 36000);
Console.WriteLine(jane.ToString());
// use WeeklyEmployee without tax
// WeeklyEmployee jack = new WeeklyEmployee(3, "Jack Deer", 18500, false);
//jack.printInformation();
// use WeeklyEmployee with tax
//WeeklyEmployee jen = new WeeklyEmployee(4, "Jen Deer", 18000);
// jen.printInformation();
Console.Read();
}
}
class Employee
{
public int EmployeeId { get; set; }
public string FullName { get; set; }
public decimal Salary { get; set; }
public bool TaxDeducted { get; set; }
public decimal NetSalary { get; set; }
public Employee(int employeeId, string fullName, decimal salary, bool taxDeducted)
{
EmployeeId = employeeId;
FullName = fullName;
Salary = salary;
TaxDeducted = taxDeducted;
NetSalary = GetNetSalary(salary, taxDeducted);
}
public Employee(int employeeId, string fullName, decimal salary)
{
EmployeeId = employeeId;
FullName = fullName;
Salary = salary;
TaxDeducted = true;
NetSalary = GetNetSalary(salary, true);
}
private decimal GetNetSalary(decimal grossPay, Boolean taxable)
{
decimal netPay;
decimal tax = 0.8M;
if (TaxDeducted)
netPay = grossPay * tax;
else
netPay = grossPay;
return netPay;
}
public override string ToString()
{
return $"{EmployeeId} {FullName} earns {NetSalary} per month";
}
}
}
Upvotes: 0
Reputation: 56
You can reduce the class this way.
class Employee
{
private int employeeId;
private string fullName;
private float salary;
private bool taxDeducted;
public Employee(int employeeId, string fullName, float salary, bool taxDeducted=true)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}
public float getNetSalary()
{
float tax = 0.8F;
float salary = this.salary;
if (taxDeducted)
salary *= tax;
return salary;
}
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " + getNetSalary() + " per month");
}
}
then try it
Employee john = new Employee(1, "John Doe", 20000, false);
john.printInformation();
// use Employee with tax
Employee jane = new Employee(2, "Jane Doe", 36000);
jane.printInformation();
1 John Doe earns 20000 per month
2 Jane Doe earns 28800 per month
Upvotes: 0
Reputation: 216353
Your code doesn't compile.
Calling Employee.getNetSalary cannot be compiled because getNetSalary is an instance method and cannot be called as a static method. You need an instance of the Employee class to call it.
To fix your problem you need to move the call to method getNetSalary inside the method printInformation and use a reference to the current instance (this) when calling it
This is your revised printInformation
public void printInformation()
{
// Call on the class instance properties to execute the calc
float netSalary = this.getNetSalary();
Console.WriteLine($"{employeeId} {fullName} earns {netSalary:C} per month");
}
In this way getNetSalary works using the properties of the current instance of the employee.
A second tip, I suggest, is to remove the second constructor (the one that doesn't take a taxDeducted boolean) but write just one constructor setting a default for the taxDeducted property to true
public Employee(int employeeId, string fullName, float salary, bool taxDeducted = true)
{
this.employeeId = employeeId;
this.fullName = fullName;
this.salary = salary;
this.taxDeducted = taxDeducted;
}
Upvotes: 2
Reputation: 24619
Simple fix is remove this line
float netSalary = Employee.getNetSalary();
and use this
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
getNetSalary() + " per month");
}
or create a property instead of method getNetSalary()
public float NetSalary
{
get
{
float netSalary;
float tax = 0.8;
if (taxDeducted)
{
netSalary = salary * tax;
}
else
{
netSalary = salary;
}
return netSalary;
}
}
then
public void printInformation()
{
Console.WriteLine(employeeId + " " + fullName + " earns " +
NetSalary + " per month");
}
or calculate this salary once to improve performance
Upvotes: 0