def do_add_employee(self, line): """ Syntax: add_employee [employee id] Starts the process to add a new employee to the current datasource parameters: employee id: the four digit id of the employee to update. may not exist in the datasource yet, must be 1 letter and three numbers, e.g. "A123" output: after the process is finished, all information about the new employee is displayed """ if not IV.validate_input_employee_id(line): self.do_help("add_employee") return False new_emp_id = line if dataHandler.employee_exists(new_emp_id, self._datasource): print("An employee with this id already exists.") return False inputs = { "gender": "gender", "sales": "sales performance (0-999)", "bmi": "bmi (normal, overweight, obesity, underweight)", "salary": "salary (0-999)", "birthday": "birthday (dd-mm-yyyy)", "age": "age (0-99)" } prompt = "Please enter the new employee's {}: " for k in inputs.keys(): val = input(prompt.format(inputs[k])) while not IV.validate_input(val, k): print("There was an error. Please check your input.") val = input(prompt.format(inputs[k])) inputs[k] = IC.convert_input(val, k) inputs["empid"] = new_emp_id new_emp = employee.create_employee(inputs) dataHandler.save_employee(new_emp, self._datasource)
def do_update_employee(self, line): """ Syntax: update_employee [employee id] Starts the update process for the employee with the selected id parameters: employee id: the four digit id of the employee to update output: after the process is finished, all old and new information about the employee is displayed """ if not IV.validate_input_employee_id(line): print("invalid employee id.") self.do_help("update_employee") return False if not dataHandler.employee_exists(line, self._datasource): print("employee does not exist.") return False to_change = dataHandler.get_employee(line, self._datasource) print("The employee to change:") print(to_change) print("If you want to change an attribute, please enter the new value") print("To leave an attribute unchanged, leave prompt empty") attributes = ["gender", "sales", "bmi", "salary", "birthday", "age"] prompt = "Attribute: {} Current: {} Enter new value:\n" for a in attributes: uin = "XXXXX" skip = False while not IV.validate_input(uin, a): if len(uin) == 0: skip = True break uin = input(prompt.format(a, getattr(to_change, a))) if not skip: uin = IC.convert_input(uin, a) setattr(to_change, a, uin) print("Updated employee:") print(to_change) uin = "X" while not uin.lower() in ["y", "n"]: uin = input("Do you want to accept these changes (y/n)") if uin == "y": dataHandler.update_employee(to_change, self._datasource)
def do_delete_employees(self, line): """ Syntax: delete_employees [employee id] [employee id] [...] Deletes the employees identified by the specified employee ids from the current datasource. parameters: employee id (may be more than one): the four digit ids of the employees to delete output: list of employees that were deleted """ split = line.split(" ") if len(split) < 1: self.do_help("delete_employees") return False checked = [i for i in split if IV.validate_input(i, "empid")] [print("Invalid employee id: {}".format(a)) for a in split \ if not a in checked] dataHandler.delete_employees(checked, self._datasource)