private void btnAdd_Click(object sender, RoutedEventArgs e) { // Adds the info from the textboxes to either a full time or part time employee class. // This only works if the salary (or hoursworked and hourlyrate for a part time) are numbers // Otherwise, it displays an error message on the top of the screen. // If the FT radio is checked, create a fulltime employee, otherwise part time if ((Boolean)FTradio.IsChecked) { decimal salary = 0; // Attmpy to get salary, if it's not valid, display error msg if (decimal.TryParse(tbxSalary.Text, out salary)) { FullTimeEmployee emp = new FullTimeEmployee(tbxFirstName.Text, tbxSurname.Text, salary); employees.Add(emp); } else { tbkMsg.Text = "Salary is not valid"; } } else if ((Boolean)PTradio.IsChecked) { decimal hourlyrate = 0; double hoursworked = 0; // If the hourly rate and works worked are not valid, display error msg if (decimal.TryParse(tbxHourlyRate.Text, out hourlyrate) && double.TryParse(tbxHourlyRate.Text, out hoursworked)) { // Make a new part time employee with the constuctors PartTimeEmployee emp = new PartTimeEmployee(tbxFirstName.Text, tbxSurname.Text, hourlyrate, hoursworked); employees.Add(emp); } else { tbkMsg.Text = "Hourly rate and hours worked must be numbers"; } } else { tbkMsg.Text = "A part time or full time option must be selected"; } // Clears the info once done, then refresh the list ClearInfo(); RefreshList(); }
private void btnUpdate_Click(object sender, RoutedEventArgs e) { // Updates a selected employee object with info entered into the boxes. // Only updates if the info is valid // If you are changing an employees part/full time status, it make a new object and deletes the selected // Get the selected employee Employee SelectedEmployee = LbxEmployees.SelectedItem as Employee; // If the employee exists if (SelectedEmployee != null) { // If the employee is full time, try to get and set salary to the one in the textbox if (SelectedEmployee is FullTimeEmployee) { // If the full time radio button is checked and the employee is fulltime, just change the values if ((Boolean)FTradio.IsChecked) { FullTimeEmployee FTemp = SelectedEmployee as FullTimeEmployee; decimal salary = 0; if (decimal.TryParse(tbxSalary.Text, out salary)) { FTemp.FirstName = tbxFirstName.Text; FTemp.Surname = tbxSurname.Text; FTemp.Salary = salary; } else { tbkMsg.Text = "Salary is not valid"; } } // If the part time button is checked, then make a new employee obj to replace the old one else { decimal hourlyrate = 0; double hoursworked = 0; // If the hourly rate and works worked are not valid, display error msg if (decimal.TryParse(tbxHourlyRate.Text, out hourlyrate) && double.TryParse(tbxHoursWorked.Text, out hoursworked)) { // Make a new part time employee with the constuctors employees.Remove(SelectedEmployee); PartTimeEmployee emp = new PartTimeEmployee(tbxFirstName.Text, tbxSurname.Text, hourlyrate, hoursworked); employees.Add(emp); } else { tbkMsg.Text = "Hourly rate and hours worked must be numbers"; } } } else if (SelectedEmployee is PartTimeEmployee) { // If the part time radio button is checked and the employee is part time // then parse the hourlyrate and hoursworked, and change the values of the partime // employee obj if ((Boolean)PTradio.IsChecked) { // Casting the selected employee to part time PartTimeEmployee PTemp = SelectedEmployee as PartTimeEmployee; decimal hourlyrate = 0; double hoursworked = 0; // Try to get the hourlyrate and hours worked. If successful, set them to the employee if (decimal.TryParse(tbxHourlyRate.Text, out hourlyrate) && double.TryParse(tbxHoursWorked.Text, out hoursworked)) { PTemp.FirstName = tbxFirstName.Text; PTemp.Surname = tbxSurname.Text; PTemp.HourlyRate = hourlyrate; PTemp.HoursWorked = hoursworked; } else { tbkMsg.Text = "Hourly rate and hours worked not valid"; } } // If the ft radio button is checked, make a new fulltimeemployee class // and then append to the list, and remove the previous employee class else { decimal salary = 0; // Attmpy to get salary, if it's not valid, display error msg if (decimal.TryParse(tbxSalary.Text, out salary)) { employees.Remove(SelectedEmployee); FullTimeEmployee emp = new FullTimeEmployee(tbxFirstName.Text, tbxSurname.Text, salary); employees.Add(emp); } else { tbkMsg.Text = "Salary is not valid"; } } } else { tbkMsg.Text = "Select an employee to update"; } } else { tbkMsg.Text = "Select an employee to update"; } ClearInfo(); RefreshList(); }