private void btnUpdate_Click(object sender, RoutedEventArgs e) { //check which employee was selected Employee selectedEmployee = lbxEmployees.SelectedItem as Employee; //check if an employee is selected if (selectedEmployee != null) { //sets the employees name to the input in the textboxes selectedEmployee.FirstName = tbxFirstName.Text; selectedEmployee.LastName = tbxLastName.Text; //check if employee is a FullTimeEmployee if (selectedEmployee is FullTimeEmployee) { //puts selected employee in new employee of correct derived class (FullTimeEmployee) so you can call the correct properties (Salary) FullTimeEmployee selectedFTEmployee = selectedEmployee as FullTimeEmployee; //sets the Salary to the input in the textbox selectedFTEmployee.Salary = Convert.ToDecimal(tbxSalary.Text); } //check if employee is a PartTimeEmployee else if (selectedEmployee is PartTimeEmployee) { //puts selected employee in new employee of correct derived class (PartTimeEmployee) so you can call the correct properties (HoursWorked, HourlyRate) PartTimeEmployee selectedPTEmployee = selectedEmployee as PartTimeEmployee; //sets the HoursWorked and HourlyRate to the input in the textboxes selectedPTEmployee.HoursWorked = Convert.ToDouble(tbxHoursWorked.Text); selectedPTEmployee.HourlyRate = Convert.ToDecimal(tbxHourlyRate.Text); } } }
private void btnAdd_Click(object sender, RoutedEventArgs e) { //read inputs from textboxes and put them into variables string firstName = tbxFirstName.Text; string lastName = tbxLastName.Text; //checks if FullTime radio button is checked if (rbFullTime.IsChecked == true) { //reads salary input and places it in a variable decimal salary = Convert.ToDecimal(tbxSalary.Text); //make a new employee and put it into employees list FullTimeEmployee employee = new FullTimeEmployee(firstName, lastName, salary); employees.Add(employee); } //checks if PartTime radio button is checked else if (rbPartTime.IsChecked == true) { //reads hoursWorked and hourlyRate inputs and places them in a variables double hoursWorked = Convert.ToDouble(tbxHoursWorked.Text); decimal hourlyRate = Convert.ToDecimal(tbxHourlyRate.Text); //make a new employee and put it into employees list PartTimeEmployee employee = new PartTimeEmployee(firstName, lastName, hourlyRate, hoursWorked); employees.Add(employee); } //update display manually lbxEmployees.ItemsSource = null; lbxEmployees.ItemsSource = employees; }
private void lbxEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e) { //clears each tbx each time a new employee is selected tbxSalary.Clear(); tbxHoursWorked.Clear(); tbxHourlyRate.Clear(); //check which employee is selected Employee selectedEmployee = lbxEmployees.SelectedItem as Employee; //check to see if there is an employee selected if (selectedEmployee != null) { //display employee's name tbxFirstName.Text = selectedEmployee.FirstName; tbxLastName.Text = selectedEmployee.LastName; //check if selected employee is FullTimeEmployee or PartTimeEmployee if (selectedEmployee is FullTimeEmployee) { //puts selected employee in new employee of correct derived class (FullTimeEmployee) so you can call the correct properties (Salary) FullTimeEmployee selectedFTEmployee = selectedEmployee as FullTimeEmployee; //checks fulltime radio button rbFullTime.IsChecked = true; //displays FullTimeEmployee's Salary tbxSalary.Text = selectedFTEmployee.Salary.ToString(); //displays employees monthly pay by calling the class method tblkCalcMonthlyPay.Text = ("€" + selectedFTEmployee.CalculateMonthlyPay().ToString("F")); } else if (selectedEmployee is PartTimeEmployee) { //puts selected employee in new employee of correct derived class (PartTimeEmployee) so you can call the correct properties (HoursWorked, HourlyRate) PartTimeEmployee selectedPTEmployee = selectedEmployee as PartTimeEmployee; //checks part time radio button rbPartTime.IsChecked = true; //display PartTimeEmployee's HoursWorked and HourlyRate tbxHoursWorked.Text = selectedPTEmployee.HoursWorked.ToString(); tbxHourlyRate.Text = selectedPTEmployee.HourlyRate.ToString(); //displays employees monthly pay by calling the class method tblkCalcMonthlyPay.Text = ("€" + selectedPTEmployee.CalculateMonthlyPay().ToString("F")); } } }
public MainWindow() { InitializeComponent(); //makes both checkboxes checked upon initialization cboxFullTime.IsChecked = true; cboxPartTime.IsChecked = true; //create objects FullTimeEmployee em1 = new FullTimeEmployee("Jess", "Walsh", 50000); FullTimeEmployee em2 = new FullTimeEmployee("Joe", "Murphy", 100000); PartTimeEmployee em3 = new PartTimeEmployee("John", "Smith", 15, 10); PartTimeEmployee em4 = new PartTimeEmployee("Jane", "Jones", 13, 15); //add objects to list employees.Add(em1); employees.Add(em2); employees.Add(em3); employees.Add(em4); //sort employees by LastName(uses IComparible) employees.Sort(); }