private void AddEmployee_OnClick(object sender, RoutedEventArgs e) { Type selectedEmployee; switch (PositionsComboBox.Text) { case "Analyst": selectedEmployee = typeof(Analyst); break; case "Director": selectedEmployee = typeof(Director); break; case "Intern": selectedEmployee = typeof(Intern); break; case "Manager": selectedEmployee = typeof(Manager); break; case "QaEngineer": selectedEmployee = typeof(QaEngineer); break; default: throw new Exception("Тип не распознан."); } if (treeView.SelectedItem is Department department && department.Employees != null) { EmployeeInputDataWindow employeeWindow = new EmployeeInputDataWindow(); // Если добавляем директора, то блокируем поле ввода зп. if (selectedEmployee == typeof(Director)) { employeeWindow.salaryCoefficientTextBox.Text = "Не трожь ЗП директора!"; employeeWindow.salaryCoefficientTextBox.IsEnabled = false; } if (employeeWindow.ShowDialog() == true) { // Если добавляем директора, то проверяем, что директора там нет. if (selectedEmployee == typeof(Director) && department.Employees[0] is Director) { MessageBox.Show("В департаменте может быть только один директор!"); return; } // Проверяем что зп введен, за исключениям для директора. while (selectedEmployee != typeof(Director) && !employeeWindow.IsDouble(employeeWindow.salaryCoefficientTextBox.Text)) { MessageBox.Show("Введите коэффициент для расчета зарплаты!"); return; } // Добавляем сотрудника используя механихм позднего связывания. Employee emp = employeeWindow.SalaryCoefficient == 0 ? (Employee)Activator.CreateInstance(selectedEmployee, employeeWindow.Name, employeeWindow.Surname) : (Employee)Activator.CreateInstance(selectedEmployee, employeeWindow.Name, employeeWindow.Surname, employeeWindow.SalaryCoefficient); department.Employees.Add( emp); Company.EmployeesCount++; } } }
/// <summary> /// Добавляет департамент. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddDepartment_OnClick(object sender, RoutedEventArgs e) { if (treeView.SelectedItem is Department department) { // Если в ветке департаментов нет, то создать такую коллекцию. if (department.Departments == null) { DepartmentDialogWindow departmentDialogWindow = new DepartmentDialogWindow(); if (departmentDialogWindow.ShowDialog() == true) { EmployeeInputDataWindow employeeWindow = new EmployeeInputDataWindow(); employeeWindow.salaryCoefficientTextBox.Text = "Не трожь ЗП директора!"; employeeWindow.salaryCoefficientTextBox.IsEnabled = false; if (employeeWindow.ShowDialog() == true) { department.Departments = new ObservableCollection <Department> { new Department(departmentDialogWindow.nameTextBox.Text, new ObservableCollection <Employee> { new Director(employeeWindow.Name, employeeWindow.Surname) }) }; Company.EmployeesCount++; } } treeView.Items.Refresh(); } // Если в ветку департаменты уже есть, то просто добавить. else { DepartmentDialogWindow departmentDialogWindow = new DepartmentDialogWindow(); if (departmentDialogWindow.ShowDialog() == true) { EmployeeInputDataWindow employeeWindow = new EmployeeInputDataWindow(); employeeWindow.salaryCoefficientTextBox.Text = "Не трожь ЗП директора!"; employeeWindow.salaryCoefficientTextBox.IsEnabled = false; if (employeeWindow.ShowDialog() == true) { department.Departments.Add(new Department(departmentDialogWindow.nameTextBox.Text, new ObservableCollection <Employee> { new Director(employeeWindow.Name, employeeWindow.Surname) })); Company.EmployeesCount++; } } } } else { DepartmentDialogWindow departmentDialogWindow = new DepartmentDialogWindow(); if (departmentDialogWindow.ShowDialog() == true) { EmployeeInputDataWindow employeeWindow = new EmployeeInputDataWindow(); employeeWindow.salaryCoefficientTextBox.Text = "Не трожь ЗП директора!"; employeeWindow.salaryCoefficientTextBox.IsEnabled = false; if (employeeWindow.ShowDialog() == true) { Company.Departments.Add(new Department(departmentDialogWindow.nameTextBox.Text, new ObservableCollection <Employee> { new Director(employeeWindow.Name, employeeWindow.Surname) })); Company.EmployeesCount++; } } } }