Last active
December 26, 2015 04:19
-
-
Save RMcGee/7092340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee | |
{ | |
//Private Member Data | |
private String _firstName; | |
private String _lastName; | |
private Int32 _sin; | |
private Int32 _age; | |
private Char _gender; | |
private Int32 _yearsService; | |
/* | |
* Overloaded constructors: When a constructor takes multiple arguments | |
* >in this case, the first one takes 5, and the | |
* second one takes 6. | |
*/ | |
public Employee(String firstName, String lastName, Int32 sin, Int32 age, | |
Char gender) | |
{ | |
FirstName = firstName; | |
LastName = lastName; | |
Sin = sin; | |
Age = age; | |
Gender = gender; | |
YearsService = 0; | |
} | |
public Employee(String firstName, String lastName, Int32 sin, Int32 age, | |
Char gender, Int32 yearsService) | |
{ | |
FirstName = firstName; | |
LastName = lastName; | |
Sin = sin; | |
Age = age; | |
Gender = gender; | |
YearsService = yearsService; | |
} | |
/* | |
* Public accessors > one per member variable | |
* Returns value of private member data, or sets it | |
*/ | |
public String FirstName | |
{ | |
get | |
{ | |
return _firstName; | |
} | |
set | |
{ | |
if (value == "") | |
throw new Exception("First name cannot be empty"); | |
else | |
_firstName = value; | |
} | |
} | |
public String LastName | |
{ | |
get | |
{ | |
return _lastName; | |
} | |
set | |
{ | |
if (value == "") | |
throw new Exception("Last name cannot be empty"); | |
else | |
_lastName = value; | |
} | |
} | |
public Int32 Sin | |
{ | |
get | |
{ | |
return _sin; | |
} | |
set | |
{ | |
if (IsValidSIN(value) == false) | |
throw new Exception("Invalid SIN"); | |
else | |
_sin = value; | |
} | |
} | |
private Boolean IsValidSIN(Int32 sin) | |
{ | |
Int32 first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, sum; | |
Boolean validSIN = false; | |
first = sin / 100000000; | |
second = sin % 100000000 / 10000000; | |
third = sin % 10000000 / 1000000; | |
fourth = sin % 1000000 / 100000; | |
fifth = sin % 100000 / 10000; | |
sixth = sin % 10000 / 1000; | |
seventh = sin % 1000 / 100; | |
eighth = sin % 100 / 10; | |
ninth = sin % 10; | |
sum = first + second * 2 % 10 + second * 2 / 10 + third + fourth * 2 % 10 + fourth * 2 / 10 + | |
fifth + sixth * 2 % 10 + sixth * 2 / 10 + seventh + eighth * 2 % 10 + eighth * 2 / 10; | |
if (sum % 10 == 0 && ninth == 0 || 10 - sum % 10 == ninth) | |
validSIN = true; | |
return validSIN; | |
} | |
public Int32 Age | |
{ | |
get | |
{ | |
return _age; | |
} | |
set | |
{ | |
if (value <= 0) | |
throw new Exception("Age must be greater than zero"); | |
else | |
_age = value; | |
} | |
} | |
public Char Gender | |
{ | |
get | |
{ | |
return _gender; | |
} | |
set | |
{ | |
if (value != 'M' && value != 'F') | |
throw new Exception("Gender must be 'M' or 'F'"); | |
else | |
_gender = value; | |
} | |
} | |
public Int32 YearsService | |
{ | |
get | |
{ | |
return _yearsService; | |
} | |
set | |
{ | |
if (value < 0) | |
throw new Exception("Years service must be a positive number"); | |
else | |
_yearsService = value; | |
} | |
} | |
public Boolean IsEligibleToRetire() | |
{ | |
Boolean EligibleToRetire = false; | |
if (_age + _yearsService >= 85) | |
EligibleToRetire = true; | |
return EligibleToRetire; | |
//or you could write... return Age + YearsService >= 85; | |
/* Employee e1 = new Employee("Reid", "McGee", 651864324, 22, 'M'); | |
Console.WriteLine("e1: " + e1.LastName + ", " + e1.FirstName + " " + e1.Sin + " " + | |
e1.Age + " " + e1.Gender + " " + e1.YearsService + " " + e1.IsEligibleToRetire()); | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment