fork download
  1. using System;
  2.  
  3. namespace ClassDemo
  4. {
  5. class Staff
  6. {
  7. private string nameOfStaff;
  8. private const int hourlyRate = 30;
  9. private int hWorked;
  10. public int HoursWorked
  11. {
  12. get
  13. {
  14. return hWorked;
  15. }
  16. set
  17. {
  18. if (value > 0)
  19. hWorked = value;
  20. else
  21. hWorked = 0;
  22. }
  23. }
  24.  
  25. public void PrintMessage()
  26. {
  27. Console.WriteLine("Calculating Pay...");
  28. }
  29.  
  30. public int CalculatePay()
  31. {
  32. PrintMessage();
  33.  
  34. int staffPay;
  35. staffPay = hWorked * hourlyRate;
  36.  
  37. if (hWorked > 0)
  38. return staffPay;
  39. else
  40. return 0;
  41. }
  42.  
  43. public int CalculatePay(int bonus, int allowance)
  44. {
  45. PrintMessage();
  46.  
  47. if (hWorked > 0)
  48. return hWorked * hourlyRate + bonus + allowance;
  49. else
  50. return 0;
  51. }
  52.  
  53. public override string ToString()
  54. {
  55. return "Name of Staff = " + nameOfStaff + ", hourlyRate = " + hourlyRate + ", hWorked = " + hWorked;
  56. }
  57.  
  58. public Staff(string name)
  59. {
  60. nameOfStaff = name;
  61. Console.WriteLine("\n" + nameOfStaff);
  62. Console.WriteLine("--------------------------");
  63. }
  64.  
  65. public Staff(string firstName, string lastName)
  66. {
  67. nameOfStaff = firstName + " " + lastName;
  68. Console.WriteLine("\n" + nameOfStaff);
  69. Console.WriteLine("--------------------------");
  70. }
  71.  
  72. }
  73.  
  74. class Program
  75. {
  76. static void Main(string[] args)
  77. {
  78. int pay;
  79.  
  80. Staff staff1 = new Staff("Peter");
  81. staff1.HoursWorked = 160;
  82. pay = staff1.CalculatePay(1000, 400);
  83. Console.WriteLine("Pay = {0}", pay);
  84.  
  85. Staff staff2 = new Staff("Jane", "Lee");
  86. staff2.HoursWorked = 160;
  87. pay = staff2.CalculatePay();
  88. Console.WriteLine("Pay = {0}", pay);
  89.  
  90. Staff staff3 = new Staff("Carol");
  91. staff3.HoursWorked = -10;
  92. pay = staff3.CalculatePay();
  93. Console.WriteLine("Pay = {0}", pay);
  94.  
  95.  
  96. }
  97. }
  98. }
  99.  
  100.  
Success #stdin #stdout 0.03s 14700KB
stdin
Standard input is empty
stdout
Peter
--------------------------
Calculating Pay...
Pay = 6200

Jane Lee
--------------------------
Calculating Pay...
Pay = 4800

Carol
--------------------------
Calculating Pay...
Pay = 0