""" COMP.CS.100 Ohjelmointi 1 / Programming 1 Name: Samuel Ahopelto Email: samuel.ahopelto@tuni.fi Student Id: 050134989 """ DOORCODES = {'TC114': ['TIE'], 'TC203': ['TIE'], 'TC210': ['TIE', 'TST'], 'TD201': ['TST'], 'TE111': [], 'TE113': [], 'TE115': [], 'TE117': [], 'TE102': ['TIE'], 'TD203': ['TST'], 'TA666': ['X'], 'TC103': ['TIE', 'OPET', 'SGN'], 'TC205': ['TIE', 'OPET', 'ELT'], 'TB109': ['OPET', 'TST'], 'TB111': ['OPET', 'TST'], 'TB103': ['OPET'], 'TB104': ['OPET'], 'TB205': ['G'], 'SM111': [], 'SM112': [], 'SM113': [], 'SM114': [], 'S1': ['OPET'], 'S2': ['OPET'], 'S3': ['OPET'], 'S4': ['OPET'], 'K1705': ['OPET'], 'SB100': ['G'], 'SB202': ['G'], 'SM220': ['ELT'], 'SM221': ['ELT'], 'SM222': ['ELT'], 'secret_corridor_from_building_T_to_building_F': ['X', 'Y', 'Z'], 'TA': ['G'], 'TB': ['G'], 'SA': ['G'], 'KA': ['G']} class Accesscard: """ This class models an access card which can be used to check whether a card should open a particular door or not. """ def __init__(self, id, name): """ Constructor, creates a new object that has no access rights. :param id: str, card holders personal id :param name: str, card holders name THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME OR THE PARAMETERS! """ self.id = id self.__name = name self.accesses = {} def get_id(self): return self.id def info(self): """ The method has no return value. It prints the information related to the access card in the format: id, name, access: a1,a2,...,aN for example: 777, Thelma Teacher, access: OPET, TE113, TIE Note that the space characters after the commas and semicolon need to be as specified in the task description or the test fails. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE PARAMETERS, OR THE PRINTOUT FORMAT! """ print(f"{self.id}, {self.__name}, access: ", end="") accesses = [] for x in self.accesses: if x == "other": for y in self.accesses[x]: accesses.append(y) else: accesses.append(x) accesses.sort() print(*accesses, sep=", ") def get_name(self): """ :return: Returns the name of the accesscard holder. """ return self.__name def add_access(self, new_access_code): """ The method adds a new accesscode into the accesscard according to the rules defined in the task description. :param new_access_code: str, the accesscode to be added in the card. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD! """ if new_access_code in self.accesses: return for x in self.accesses: if new_access_code in x: return doorcodes = [] if new_access_code in DOORCODES: if "other" in self.accesses: doorcodes = self.accesses["other"] doorcodes.append(new_access_code) self.accesses["other"] = doorcodes return for x in DOORCODES: if new_access_code in DOORCODES[x]: doorcodes.append(x) if doorcodes == []: return mydoorcodes = [] if "other" in self.accesses: for x in self.accesses["other"]: mydoorcodes.append(x) for x in doorcodes: if x in mydoorcodes: mydoorcodes.remove(x) self.accesses["other"] = mydoorcodes self.accesses[new_access_code] = doorcodes def check_access(self, door): """ Checks if the accesscard allows access to a certain door. :param door: str, the doorcode of the door that is being accessed. :return: True: The door opens for this accesscard. False: The door does not open for this accesscard. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD! """ if door not in DOORCODES: return False for x in self.accesses: if door in self.accesses[x]: return True return False def merge(self, card): """ Merges the accesscodes from another accesscard to this accesscard. :param card: Accesscard, the accesscard whose access rights are added to this card. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD! """ for x in card.accesses: if x not in self.accesses: self.accesses[x] = card.accesses[x] doors = card.accesses["other"] myDoors = self.accesses["other"] for code in doors: if code not in myDoors: self.add_access(code) def main(): access_cards = [] file = open("accessinfo.txt", "r") for row in file: print("hei") row = row.rstrip() blocks = row.split(";") print(blocks) card = Accesscard(blocks[0], blocks[1]) if blocks[2] != "": accesses = blocks[2].split(",") for access in accesses: card.add_access(access) access_cards.append(card) access_cards.sort(key=lambda card: card.id) while True: line = input("command> ") if line == "": break strings = line.split() command = strings[0] if command == "list" and len(strings) == 1: for card in access_cards: card.info() elif command == "test" and len(strings) == 2: list_id = int(strings[1]) access_cards[list_id].info() print(access_cards[list_id].accesses) elif command == "info" and len(strings) == 2: card_id = strings[1] pass # TODO: Excecute the command info here elif command == "access" and len(strings) == 3: card_id = strings[1] door_id = strings[2] pass # TODO: Excecute the command access here elif command == "add" and len(strings) == 3: card_id = strings[1] access_code = strings[2] pass # TODO: Excecute the command add here elif command == "merge" and len(strings) == 3: card_id_to = strings[1] card_id_from = strings[2] pass # TODO: Excecute the command merge here elif command == "quit": print("Bye!") return else: print("Error: unknown command.") if __name__ == "__main__": main()