Skip to content

Instantly share code, notes, and snippets.

@RMcGee
Created October 28, 2013 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RMcGee/7205644 to your computer and use it in GitHub Desktop.
Save RMcGee/7205644 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exceptions
{
class Program
{
static void Main(string[] args)
{
const Int32 MAXEMPLOYEES = 100;
Employee[] employees = new Employee[MAXEMPLOYEES];
Int32 employeeCount = 0, sin, age, yearsService;
char response = 'Y', gender;
String firstName, lastName;
do
{
Console.WriteLine("Enter employee first name");
firstName = Console.ReadLine();
Console.WriteLine("Enter employee last name");
lastName = Console.ReadLine();
Console.WriteLine("Enter employee SIN");
while (Int32.TryParse(Console.ReadLine(), out sin) == false)
Console.WriteLine("Input error! Enter employee SIN");
Console.WriteLine("Enter employee age");
while (Int32.TryParse(Console.ReadLine(), out age) == false)
Console.WriteLine("Input error! Enter employee age");
Console.WriteLine("Enter employee gender M/F");
while (Char.TryParse(Console.ReadLine(), out gender) == false)
Console.WriteLine("Input error! Enter employee gender M/F");
gender = Char.ToUpper(gender);
Console.WriteLine("Enter employee years service");
while (Int32.TryParse(Console.ReadLine(), out yearsService) == false)
Console.WriteLine("Input error! Enter employee years service");
try
{
employees[employeeCount] = new Employee(firstName, lastName, sin, age, gender, yearsService);
employeeCount++;
Console.WriteLine("Employee added successfully");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Employee not added");
}
Console.WriteLine("Enter another employee? Y/N");
while (Char.TryParse(Console.ReadLine(), out response) == false)
Console.WriteLine("Input error! Enter another employee? Y/N");
response = Char.ToUpper(response);
}
while (response == 'Y');
for (Int32 count = 0; count < employeeCount; count++)
Console.WriteLine(employees[count].FirstName + " " + employees[count].LastName + " " +
employees[count].Sin + " " + employees[count].Age + " " + employees[count].Gender +
" " + employees[count].YearsService);
Console.WriteLine("Goodbye");
Console.ReadLine();
}
}
}
-------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exceptions
{
class Employee
{
/*
* Private encapsulated member data
* _camelCasing
*/
private String _firstName;
private String _lastName;
private Int32 _sin;
private Int32 _age;
private Char _gender;
private Int32 _yearsService;
/*
* Public overloaded constructors
* The first takes 5 arguments
* The second takes 6 arguments
*/
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 for each private member variable
* Each accessor has a set and a get
*/
public String FirstName
{
get
{
return _firstName;
}
set
{
if (value != "")
_firstName = value;
else
throw new Exception("First name must not be empty");
}
}
public String LastName
{
get
{
return _lastName;
}
set
{
if (value != "")
_lastName = value;
else
throw new Exception("Last name must not be empty");
}
}
public Int32 Sin
{
get
{
return _sin;
}
set
{
if (IsValidSIN(value) == true)
_sin = value;
else
throw new Exception("Invalid SIN");
}
}
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)
_age = value;
else
throw new Exception("Age must be greater than zero");
}
}
public Char Gender
{
get
{
return _gender;
}
set
{
if (value == 'M' || value == 'F')
_gender = value;
else
throw new Exception("Gender must be 'M' or 'F'");
}
}
public Int32 YearsService
{
get
{
return _yearsService;
}
set
{
if (value >= 0)
_yearsService = value;
else
throw new Exception("Years service must be greater than or equal to zero");
}
}
public Boolean IsEligibleToRetire()
{
Boolean EligibleToRetire = false;
if (Age + YearsService >= 85)
EligibleToRetire = true;
return EligibleToRetire;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment