namespace Zau_Lab_Employee
{
[Serializable]
public class employee
{
//private fields
private string _fName;
private string _lName;
private string _emeId;
private DateTime _dateHire;
//constructor
public employee (string fName, string lName, string emeId, DateTime dateHire)
{
this._fName = fName;
this._lName = lName;
this._emeId = emeId;
this._dateHire = dateHire;
}
//properties
public string Fname
{ get { return _fName; } }
public string Lname
{ get { return _lName; } }
public string EmployeeID
{ get { return _emeId; } }
public DateTime DateHire
{ get { return _dateHire; } }
//method to get biweeklysalary
public decimal GetBiWeeklySalary(decimal salaryPerMonth)
{
decimal biwklyPaid = salaryPerMonth / 2;
return biwklyPaid;
}
public int GetAccruedVacation()
{
TimeSpan dt = DateTime.Now - DateHire;
int years = dt.Days / 365;
if (years >= 3 && years < 4)
return 7;
else if (years >= 4 && years < 5)
return 8;
else if (years >= 5 && years < 6)
return 9;
else if (years >= 6 && years < 7)
return 10;
else if (years >= 7 && years < 8)
return 11;
else if (years >= 8 && years < 9)
return 12;
else if (years >= 9 && years < 10)
return 13;
else
return 14;
}
public int GetSeniority()
{
TimeSpan dt = DateTime.Now - DateHire;
int years = dt.Days / 365;
return years;
}
}
}
//===========================================================
namespace Zau_Lab_Employee
{
[Serializable]
public class SalariedEmployee : employee
{
internal protected decimal _yearlySalary;
public SalariedEmployee(string fName, string lName, string emeId, DateTime dateHire, decimal yearlySalary)
: base(fName, lName, emeId, dateHire)
{
_yearlySalary = yearlySalary;
}
public decimal YearlySalary
{ get { return _yearlySalary; } }
new public int GetAccruedVacation()
{
TimeSpan dt = DateTime.Now - DateHire;
int years = dt.Days / 365;
if (years >= 3 && years < 4)
return 7;
else if (years >= 4 && years < 5)
return 8;
else if (years >= 5 && years < 6)
return 9;
else if (years >= 6 && years < 7)
return 10;
else if (years >= 7 && years < 8)
return 11;
else if (years >= 8 && years < 9)
return 12;
else if (years >= 9 && years < 10)
return 13;
else if (years >= 10)
return 14;
else
return 0;
}
public decimal GetByMonthlySalary()
{
decimal monthlySalary = YearlySalary / 26;
return monthlySalary;
}
}
}
//===========================================================
namespace Zau_Lab_Employee
{
[Serializable]
public class Manager: SalariedEmployee
{
public Manager(string fName, string lName, string emeId, DateTime dateHire, decimal yearlySalary)
: base(fName, lName, emeId, dateHire, yearlySalary)
{
}
new public double GetAccruedVacation()
{
TimeSpan dt = DateTime.Now - DateHire;
int years = dt.Days / 365;
if (years >= 1 && years < 6)
return 14;
else if (years >= 6 && years < 7)
return 15;
else if (years >= 7 && years < 8)
return 16;
else if (years >= 8 && years < 9)
return 17;
else if (years >= 9 && years < 10)
return 18;
else if (years >= 10 && years < 11)
return 19;
else if (years >= 11)
return 20;
else
return 0;
}
new public decimal GetByMonthlySalary()
{
decimal monthlySalary = YearlySalary / 26;
return monthlySalary;
}
}
}
//===========================================================
namespace Zau_Lab_Employee
{
[Serializable]
public class HourlyEmployee : employee
{
internal protected decimal _hourlywage;
internal protected decimal _noOfhoursWorked;
public HourlyEmployee(string fName, string lName, string emeId, DateTime dateHire, decimal hourlywage, decimal noOfhoursWorked)
: base(fName, lName, emeId, dateHire)
{
_hourlywage = hourlywage;
_noOfhoursWorked = noOfhoursWorked;
}
public decimal HourlyWage
{ get { return _hourlywage; } }
public decimal NoOfHoursWorked
{ get { return _noOfhoursWorked; } }
public decimal GetByMonthlySalaryTem()
{
decimal monthlySalary = HourlyWage * NoOfHoursWorked;
return monthlySalary;
}
new public double GetAccruedVacation()
{
TimeSpan dt = DateTime.Now - DateHire;
int years = dt.Days / 365;
if (years >= 5)
{
double accrueVacation = 0;
for (int i = 0; i < 19; i++)
{
accrueVacation += 0.5;
}
return accrueVacation;
}
else
return 0;
}
}
}
//===========================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; //added
using System.Runtime.Serialization;//added
using System.Runtime.Serialization.Formatters.Binary;//added
namespace Zau_Lab_Employee
{
public partial class Form1 : Form
{
List<employee> employeeList = new List<employee>();
string fileLocation = "employeeList.dat";
public Form1()
{
InitializeComponent();
}
private void btnCreateSalariedEmployee_Click(object sender, EventArgs e)
{
decimal yearlySalary;
if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty &&
txtEmplyeeID.Text != String.Empty && txtYearlySalary.Text != String.Empty)
{
string fname = txtFirstName.Text;
string lname = txtLastName.Text;
string employeeID = txtEmplyeeID.Text;
DateTime hiredate = dtDateOfHired.Value;
decimal.TryParse(txtYearlySalary.Text, out yearlySalary);
SalariedEmployee employee = new SalariedEmployee(fname, lname, employeeID, hiredate, yearlySalary);
employeeList.Add(employee);
MessageBox.Show("A New Salaried Employee has been added to your list!");
ResetFormToDefault();
}
else
{
MessageBox.Show("You must input all necessary items for Salaried Employee.");
}
}
private void btnCreateHourlyEmployee_Click(object sender, EventArgs e)
{
decimal noOfworkhrs;
decimal hourlywage;
if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty &&
txtEmplyeeID.Text != String.Empty && txtNoOfHrPerMonth.Text != String.Empty
&& txtPayRatePerHr.Text != String.Empty )
{
string fname = txtFirstName.Text;
string lname = txtLastName.Text;
string employeeID = txtEmplyeeID.Text;
DateTime hiredate = dtDateOfHired.Value;
decimal.TryParse(txtNoOfHrPerMonth.Text, out noOfworkhrs);
decimal.TryParse(txtPayRatePerHr.Text, out hourlywage);
HourlyEmployee employee = new HourlyEmployee(fname, lname, employeeID, hiredate, noOfworkhrs, hourlywage);
employeeList.Add(employee);
MessageBox.Show("A New Hourly Employee has been added to your list!");
Display();
ResetFormToDefault();
}
else
{
MessageBox.Show("You must input all necessary items for Hourly Employee.");
}
}
private void btnCreateManager_Click(object sender, EventArgs e)
{
decimal yearlySalary;
if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty &&
txtEmplyeeID.Text != String.Empty && txtYearlySalary.Text != String.Empty)
{
string fname = txtFirstName.Text;
string lname = txtLastName.Text;
string employeeID = txtEmplyeeID.Text;
DateTime hiredate = dtDateOfHired.Value;
decimal.TryParse(txtYearlySalary.Text, out yearlySalary);
Manager employee = new Manager(fname, lname, employeeID, hiredate, yearlySalary);
employeeList.Add(employee);
MessageBox.Show("A New Manager has been added to your list!");
ResetFormToDefault();
}
else
{
MessageBox.Show("You must input all necessary items for Manager Employee.");
}
}
private void btnDisplayAllEmployee_Click(object sender, EventArgs e)
{
Display();
}
private void Display()
{
listView1.Items.Clear();//clear the contents
ListViewItem lstvitem;
foreach (employee e in employeeList)
{
if (e is Manager)
{
Manager manager = (Manager)e;
lstvitem = new ListViewItem(manager.GetType().Name.ToString());
lstvitem.SubItems.Add(manager.Fname);
lstvitem.SubItems.Add(manager.Lname);
lstvitem.SubItems.Add(manager.EmployeeID);
lstvitem.SubItems.Add(manager.DateHire.ToShortDateString());
lstvitem.SubItems.Add(manager.GetSeniority().ToString());
lstvitem.SubItems.Add(manager.GetByMonthlySalary().ToString("f2"));
lstvitem.SubItems.Add(manager.GetAccruedVacation().ToString());
listView1.Items.Add(lstvitem);
}
else if (e is HourlyEmployee)
{
HourlyEmployee pt = (HourlyEmployee)e;
lstvitem = new ListViewItem(pt.GetType().Name.ToString());
lstvitem.SubItems.Add(pt.Fname);
lstvitem.SubItems.Add(pt.Lname);
lstvitem.SubItems.Add(pt.EmployeeID);
lstvitem.SubItems.Add(pt.DateHire.ToShortDateString());
lstvitem.SubItems.Add(pt.GetSeniority().ToString());
lstvitem.SubItems.Add(pt.GetByMonthlySalaryTem().ToString("f2"));
lstvitem.SubItems.Add(pt.GetAccruedVacation().ToString());
listView1.Items.Add(lstvitem);
}
else if (e is SalariedEmployee)
{
SalariedEmployee se = (SalariedEmployee)e;
lstvitem = new ListViewItem(se.GetType().Name.ToString());
lstvitem.SubItems.Add(se.Fname);
lstvitem.SubItems.Add(se.Lname);
lstvitem.SubItems.Add(se.EmployeeID);
lstvitem.SubItems.Add(se.DateHire.ToShortDateString());
lstvitem.SubItems.Add(se.GetSeniority().ToString());
lstvitem.SubItems.Add(se.GetByMonthlySalary().ToString("f2"));
lstvitem.SubItems.Add(se.GetAccruedVacation().ToString());
listView1.Items.Add(lstvitem);
}
}
}
private void ResetFormToDefault()
{
txtFirstName.Clear();
txtLastName.Clear();
txtEmplyeeID.Clear();
dtDateOfHired.Value = DateTime.Now.Subtract(TimeSpan.FromDays(0));
txtNoOfHrPerMonth.Clear();
txtPayRatePerHr.Clear();
txtYearlySalary.Clear();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Would you like to save any changes before exiting the application?", "Save Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
IFormatter formatter = null;
Stream stream = null;
try
{
formatter = new BinaryFormatter();
stream = new FileStream(fileLocation, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
formatter.Serialize(stream, employeeList);
}
catch (SerializationException se)
{
MessageBox.Show(se.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
MessageBox.Show("All data saved successfully", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//deserialize peopleList
BinaryFormatter formatter = new BinaryFormatter();
Stream s = null;
try
{
s = new FileStream(fileLocation,
FileMode.Open,
FileAccess.ReadWrite, FileShare.None);
//3.Deserialize (reconstruct the list of rect
while (s.Position != s.Length)
{
employeeList = (List<employee>)formatter.Deserialize(s);
}
MessageBox.Show("Employee list has been deserialized");
}
catch (IOException ioe)
{
MessageBox.Show(ioe.Message);
}
catch (SerializationException se)
{
MessageBox.Show(se.Message);
}
finally
{
if (s != null) s.Close();
}
}
}
}

No comments:
Post a Comment