def __init__(self, person, date, time, description): ''' Constructor for the class Activity. :param person: Person - person who does the activity :param date: string - day, month and year of the activity in the format "yyyy.mm.dd" :param time: string - hour and minute of the activity in the format "hh:mm" :param description: string - description of the activity :exception NABException: if one of the parameters is not valid ''' # Checks if parameters are valid. if not isinstance(person, Person): raise NABException("The given person is not valid.") if not ActivityValidator.valid_date(date): raise NABException("The given date is not in a valid format.") if not ActivityValidator.valid_time(time): raise NABException("The given time is not in a valid format.") if not isinstance(description, str): raise NABException("The given description is not a string.") # Constructs object. self.__person = person self.__date = date self.__time = time self.__description = description
def redo(self): ''' Redoes the last undo. :return: repositories are modified accordingly :exception NABException: if no redo is possible ''' if self.__index+1 == len(self.__operations): raise NABException("# There's nothing to redo.\n") self.__index += 1 for lastOperation in self.__operations[self.__index]: if type(lastOperation) == AddOperation: self.__activityRepo.add(lastOperation.object()) elif type(lastOperation) == RemoveOperation: self.__activityRepo.remove(self.find_activity(lastOperation.object())) elif type(lastOperation) == UpdateOperation: pos = self.find_activity(lastOperation.old()) activity = self.__activityRepo.find(pos) activity.person = lastOperation.new().person activity.date = lastOperation.new().date activity.time = lastOperation.new().time activity.description = lastOperation.new().description else: raise NABException("# Not a redoable operation.\n")
def __init__(self, id, name, phoneNumber, address): ''' Constructor for the class Person. :param id: positive integer - ID of a person :param name: string - full name of a person :param phoneNumber: string - phone number of a person :param address: string - address of a person :exception NABException: if one of the parameters is not valid ''' # Checks if parameters are valid. if not PersonValidator.valid_id(id): raise NABException("The given ID is not a positive integer.") if not isinstance(name, str): raise NABException("The given name is not a string.") if not isinstance(phoneNumber, str): raise NABException("The given phone number is not a string.") if not isinstance(address, str): raise NABException("The given address is not a string.") # Constructs object. self.__id = id self.__name = name self.__phoneNumber = phoneNumber self.__address = address
def undo(self): ''' Undoes the last operation that modified the repositories. :return: repositories are modified accordingly :exception NABException: if no undo is possible ''' if self.__index == -1: raise NABException("# There's nothing to undo.\n") lastOperation = self.__operations[self.__index] if type(lastOperation) == AddOperation: self.__personRepo.remove_by_id(lastOperation.object().id) elif type(lastOperation) == RemoveOperation: self.__personRepo.add(lastOperation.object()) elif type(lastOperation) == UpdateOperation: person = self.__personRepo.find_by_id(lastOperation.new().id) person.name = lastOperation.old().name person.address = lastOperation.old().address person.phone = lastOperation.old().phone else: raise NABException("# Not an undoable operation.\n") self.__index -= 1
def search_activities_with_person_id(self): ''' Read an ID and prints the activities performed by the person with that ID. ''' system("clear") print( "# You have chosen to search a activity performed by a person indicated by their ID." ) if self.__activitiesCtrl.number_of_activities() == 0: raise NABException("The list of activities is empty.") id = Console.read_positive_integer("Type the ID: ") try: system("clear") foundActivities = self.__activitiesCtrl.find_activities_by_person_id( id) print("# The following activities has been found: ") print(foundActivities, end='') except NABException as err: print(err)
def search_people_with_phone(self): ''' Read a phone number and prints the people with that phone number. ''' system("clear") print( "# You have chosen to search people with an indicated phone number." ) if self.__peopleCtrl.number_of_people() == 0: raise NABException("The list of people is empty.") phone = input("Type the phone number: ") try: system("clear") foundPeople = self.__peopleCtrl.find_people_by_phone(phone) print("# The following people have been found: ") print(foundPeople, end='') except NABException as err: print(err)
def update_activity_submenu(self): ''' Read the position of an activity in order to update information about it. ''' system("clear") print("# You have chosen to update information about an activity.") if self.__activitiesCtrl.number_of_activities() == 0: raise NABException("The list of activities is empty.") print(self.__activitiesCtrl.activities_to_string_with_positions()) pos = Console.read_positive_integer( "Type a position from the ones above: ") try: system("clear") activity = self.__activitiesCtrl.find_activity_by_position(pos - 1) self.update_activity(activity) except NABException as err: print(err)
def update_person_submenu(self): ''' Reads the ID of a person in order to update information about it. ''' system("clear") print("# You have chosen to update information about a person.") if self.__peopleCtrl.number_of_people() == 0: raise NABException("The list of people is empty.") print(self.__peopleCtrl.people_to_string()) print( "ID must be the one of a person already existing in the list of people." ) id = Console.read_positive_integer("Type the ID: ") try: system("clear") person = self.__peopleCtrl.find_person_by_id(id) self.update_person(person) except NABException as err: print(err)
def remove_activity(self): ''' Reads the position of an activity in order to remove it from the NAB. ''' system("clear") print( "# You have chosen to remove an activity based on its position in the list." ) if self.__activitiesCtrl.number_of_activities() == 0: raise NABException("The list of activities is empty.") print(self.__activitiesCtrl.activities_to_string_with_positions()) pos = Console.read_positive_integer( "Type a position from the ones above: ") try: system("clear") self.__activitiesCtrl.remove_activity_by_position(pos - 1) self.__undoRedoCtrl.do_activities() print("Activity has been successfully removed.") except NABException as err: print(err)
def remove_person(self): ''' Reads the ID of a person in order to remove it from the NAB. ''' system("clear") print("# You have chosen to remove a person represented by their ID.") if self.__peopleCtrl.number_of_people() == 0: raise NABException("The list of people is empty.") print( "By removing a person you also remove all activities that they perform." ) print(self.__peopleCtrl.people_to_string()) print( "ID must be the one of a person already existing in the list of people." ) id = Console.read_positive_integer("Type the ID: ") try: system("clear") self.__peopleCtrl.remove_person_by_id(id) self.__activitiesCtrl.remove_activities_by_person_id(id) self.__undoRedoCtrl.do_both() print("Person has been successfully removed.") except NABException as err: print(err)
def search_activities_with_description(self): ''' Read a description and prints the activities with that description incorporated. ''' system("clear") print( "# You have chosen to search activities which contain a certain description." ) if self.__activitiesCtrl.number_of_activities() == 0: raise NABException("The list of activities is empty.") description = input("Type the description: ") try: system("clear") foundActivities = self.__activitiesCtrl.find_activities_by_description( description) print("# The following activities have been found: ") print(foundActivities, end='') except NABException as err: print(err)
def __init__(self, personRepo, activityRepo): ''' Constructor for the class StatsController. :param personRepo: PersonRepository - repository containing people :param activityRepo: ActivityRepository- repository containing activities :exception NABException: if one of the parameters is not valid ''' if not isinstance(personRepo, PersonRepository): raise NABException("The given repository of people is not valid.") if not isinstance(activityRepo, ActivityRepository): raise NABException("The given repository of activities is not valid.") self.__personRepo = personRepo self.__activityRepo = activityRepo
def add(self, person): ''' Adds a person to the current list of people, if his ID does not already exist. :param person: Person - person to be added :return: person is added to the current list of people :exception NABException: if one of the parameters is not valid or a person with that ID already exists ''' if type(person) is not Person: raise NABException("The given person is not valid.") if self.find(person.id) != -1: raise NABException("There's already a person with that ID.") self.__list.append(person) self.__list[:] = sort(self.__list, key=lambda x: x.id)
def search_activities_with_date(self): ''' Read a date and prints the activities taking place on that date. ''' system("clear") print( "# You have chosen to search activities which take place on a certain date." ) if self.__activitiesCtrl.number_of_activities() == 0: raise NABException("The list of activities is empty.") date = input("Type the date in format {0}: ".format( ActivityValidator.dateFormat())) try: system("clear") foundActivities = self.__activitiesCtrl.find_activities_by_date( date) print("# The following activities have been found: ") print(foundActivities, end='') except NABException as err: print(err)
def find_by_id(self, id): ''' Returns the person from the current list with the indicated ID. :param id: positive integer - ID of a person :return: Person - the person with the ID id :exception NABException: if one of the parameters is not valid or no person with the given ID was found ''' if not PersonValidator.valid_id(id): raise NABException("The given ID is not a positive integer.") pos = self.find(id) if pos != -1: return self.__list[pos] else: raise NABException("No person with the given ID was found.")
def remove_by_id(self, id): ''' Removes the person with the indicated ID from the list. :param id: positive integer - ID of a person :return: the person with the indicated ID is removed :exception NABException: if one of the parameters is not valid or no person with the given ID was found ''' if not PersonValidator.valid_id(id): raise NABException("The given ID is not a positive integer.") pos = self.find(id) if pos != -1: return self.remove(pos) else: raise NABException("No person with the given ID was found.")
def activities_in_interval_alphabetically(self, date1, date2): ''' Returns a list of activities performed in the indicated interval alphabetically by description. :param date1: string - start date in a valid format :param date1: string - end date in a valid format :return: list of Activity as specified :exception NABException: if one of the parameters is not valid ''' if date1 > date2: raise NABException("End date must be after the start date.") if not ActivityValidator.valid_date(date1) or not ActivityValidator.valid_date(date2): raise NABException("At least one of the dates is invalid.") funct = lambda x: date1 <= x.date and x.date <= date2 L = filter(funct, self.__activityRepo) return sort(L, key=lambda x: x.description)
def people_with_activities_in_interval(self, date1, date2): ''' Returns a list of people who perform activities in the indicated interval. :param date1: string - start date in a valid format :param date1: string - end date in a valid format :return: list of Person as specified :exception NABException: if one of the parameters is not valid ''' if date1 > date2: raise NABException("End date must be after the start date.") if not ActivityValidator.valid_date(date1) or not ActivityValidator.valid_date(date2): raise NABException("At least one of the dates is invalid.") funct = lambda x: len([a for a in self.__activityRepo if a.person == x and date1 <= a.date and a.date <= date2]) != 0 L = filter(funct, self.__personRepo) return sort(L, key=lambda x: x.id)
def add_activity(self, activity): ''' Adds an activity to the NAB, if the person performing the activity is in the list of people. :param activity: Activity - activity to be added to the NAB :return: activity is added to the NAB :exception NABException: if one of the parameters is not valid or person does not exist in the list of people ''' if type(activity) is not Activity: raise NABException("The given activity is not valid.") if activity.person not in self.__personRepo: raise NABException("The person performing the given activity does not belong to the list.") self.__activityRepo.add(activity) self.__operations[:] = self.__operations[:self.__index+1] self.__operations.append([AddOperation(deepcopy(activity))]) self.__index += 1
def redo(self): ''' Redoes the last undoes on the lastly undone controllers. ''' if self.__index + 1 == len(self.__ctrls): raise NABException("# There's nothing to redo.\n") self.__index += 1 for ctrl in self.__ctrls[self.__index]: ctrl.redo()
def undo(self): ''' Undoes the last operation on the lastly modified controllers. ''' if self.__index == -1: raise NABException("# There's nothing to undo.\n") for ctrl in self.__ctrls[self.__index]: ctrl.undo() self.__index -= 1
def description(self, newDescription): ''' Sets a new description of the current activity. :param newDescription: string - new description of the current activity :return: the description of the current activity is set to newDescription :exception NABException: if one of the parameters is not valid ''' if type(newDescription) is not str: raise NABException("The given description is not a string.") self.__description = newDescription
def find(self, pos): ''' Returns the activity with the specified position. :param pos: integer - position of the person in the current list, greater than or equal to 0 and strictly less than the length of the list :return: Activity - the activity with the indicated position :exception NABException: if one of the parameters is not valid ''' if (type(pos) is not int) or (pos < 0 or pos >= len(self.__list)): raise NABException("The given position is not valid.") return self.__list[pos]
def remove(self, pos): ''' Removes the the activities with the indicated position in the current list. :param pos: integer - position of the person in the current list, greater than or equal to 0 and strictly less than the length of the list :return: Activity - the activity at the position pos is popped out of the list :exception NABException: if one of the parameters is not valid ''' if (type(pos) is not int) or (pos < 0 or pos >= len(self.__list)): raise NABException("The given position is not valid.") return self.__list.pop(pos)
def person(self, newPerson): ''' Sets a new person to perform the current activity. :param newPerson: Person - new person performing the current activity :return: the person performing the current activity is set to newPerson :exception NABException: if one of the parameters is not valid ''' if type(newPerson) is not Person: raise NABException("The given person is not valid.") self.__person = newPerson
def phone(self, newPhoneNumber): ''' Sets a new value for the phone number of the current person. :param newPhoneNumber: string - new phone number :return: the phone number of the current person is set to newPhoneNumber :exception NABException: if one of the parameters is not valid ''' if type(newPhoneNumber) is not str: raise NABException("The given phone number is not a string.") self.__phoneNumber = newPhoneNumber
def date(self, newDate): ''' Sets a new date of the current activity. :param newDate: string - new date of the current activity of format "dd:mm:yyyy" :return: the date of the current activity is set to newDate :exception NABException: if one of the parameters is not valid ''' if not ActivityValidator.valid_date(newDate): raise NABException("The given date is not in a valid format.") self.__date = newDate
def name(self, newName): ''' Sets a new value for the name of the current person. :param newName: string - new name :return: the name of the current person is set to newName :exception NABException: if one of the parameters is not valid ''' if type(newName) is not str: raise NABException("The given name is not a string.") self.__name = newName
def address(self, newAddress): ''' Sets a new value for the address of the current person. :param newAddress: string - new address :return: the address of the current person is set to newAddress :exception NABException: if one of the parameters is not valid ''' if type(newAddress) is not str: raise NABException("The given address is not a string.") self.__address = newAddress
def time(self, newTime): ''' Sets a new time of the current activity. :param newTime: string - new time of the current activity of format "hh:mm" :return: the time of the current activity is set to newTime :exception NABException: if one of the parameters is not valid ''' if not ActivityValidator.valid_time(newTime): raise NABException("The given time is not in a valid format.") self.__time = newTime