// Using Try Parse() here also! private void btnUpdate_Click(object sender, RoutedEventArgs e) { // This is almost identical to AddButton() // There is enough important changes required that it made sense to remake this code Employee selectedEmployee = lbxEmployeeLists.SelectedItem as Employee; if (selectedEmployee != null) { // first and last name will be used by both string firstName = tbxFName.Text; string lastName = tbxLName.Text.ToUpper(); // FullTime has a salary if (rbtnFT.IsChecked == true) { string salarytxt = tbxSalary.Text; decimal salary; bool isNumber = decimal.TryParse(salarytxt, out salary); if (isNumber) { FullTimeEmployee employee = new FullTimeEmployee(firstName, lastName, salary); employees.Add(employee); employees.Remove(selectedEmployee); cboxFullTime.IsChecked = true; cboxPartTime.IsChecked = false; lbxEmployeeLists.ItemsSource = filterEmployees.OrderBy(c => c.LastName.ToUpper()); // Remove will get rid of the selected Employee // and add the Newly created Employee } } // PartTime has a HoursWorked and HourlyRate if (rbtnPT.IsChecked == true) { string hRateText = tbxHourlyRate.Text; decimal hourlyRate; bool isDecimal = decimal.TryParse(hRateText, out hourlyRate); string hWorkedText = tbxHrsWorked.Text; double hoursWorked; bool isDouble = double.TryParse(hWorkedText, out hoursWorked); if (isDecimal && isDouble) { PartTimeEmployee employee = new PartTimeEmployee(firstName, lastName, hourlyRate, hoursWorked); employees.Add(employee); employees.Remove(selectedEmployee); cboxFullTime.IsChecked = false; cboxPartTime.IsChecked = true; lbxEmployeeLists.ItemsSource = filterEmployees.OrderBy(c => c.LastName.ToUpper()); // Remove will get rid of the selected Employee // and add the Newly created Employee } } } }
private void Window_Loaded(object sender, RoutedEventArgs e) { // PartTimers take Name First-Last then hourlyRate and finally hours worked PartTimeEmployee pTE1 = new PartTimeEmployee("Johnny", "KALLISTER", 11, 96); PartTimeEmployee pTE2 = new PartTimeEmployee("Abraham", "JOHNSTON", 12, 112); // Fulltimers take Name First-Last and a Years Salary FullTimeEmployee fTE1 = new FullTimeEmployee("Mike", "MANN", 32000); FullTimeEmployee fTE2 = new FullTimeEmployee("Mikey", "DEAN", 24000); // For testing purpose i add 4 employees // 4 was plenty to test. Allowed me to check all area of code ex. Delete(), Update() employees.Add(pTE1); employees.Add(pTE2); employees.Add(fTE1); employees.Add(fTE2); lbxEmployeeLists.ItemsSource = employees.OrderBy(c => c.LastName); }
// wanted better code so added Tryparse() // handles anything but a number and even empty, for better code private void btnAdd_Click(object sender, RoutedEventArgs e) { // first and last name will be used by both string firstName = tbxFName.Text; string lastName = tbxLName.Text.ToUpper(); // if its Fulltime : FullTime has a salary if (rbtnFT.IsChecked == true) { string salarytxt = tbxSalary.Text; decimal salary; bool isNumber = decimal.TryParse(salarytxt, out salary); if (isNumber) { FullTimeEmployee employee = new FullTimeEmployee(firstName, lastName, salary); employees.Add(employee); cboxFullTime.IsChecked = true; cboxPartTime.IsChecked = false; lbxEmployeeLists.ItemsSource = filterEmployees.OrderBy(c => c.LastName.ToUpper()); } } // if its PartTime : PartTime has a HoursWorked and HourlyRate if (rbtnPT.IsChecked == true) { string hRateText = tbxHourlyRate.Text; decimal hourlyRate; bool isDecimal = decimal.TryParse(hRateText, out hourlyRate); string hWorkedText = tbxHrsWorked.Text; double hoursWorked; bool isDouble = double.TryParse(hWorkedText, out hoursWorked); if (isDecimal && isDouble) { PartTimeEmployee employee = new PartTimeEmployee(firstName, lastName, hourlyRate, hoursWorked); employees.Add(employee); cboxFullTime.IsChecked = false; cboxPartTime.IsChecked = true; lbxEmployeeLists.ItemsSource = filterEmployees.OrderBy(c => c.LastName.ToUpper()); } } }