Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TIE-02101 Johdatus ohjelmointiin
- # Helena Tuovila, helena.tuovila@tuni.fi, opiskelijanumero: 282031
- # Tehtävän 15.2 ratkaisu:
- # Ohjelma kulkukorttien kulkuoikeuksien tarkasteluun.
- 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:
- def __init__(self, id, name):
- """
- Constructor, creates a new object that has no access rights.
- :param id: card holders personal id (str)
- :param name: card holders name (str)
- THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME OR THE
- PARAMETERS!
- """
- self.__tunniste = id
- self.__nimi = name
- self.__ovikoodit = []
- 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(self.__tunniste, ", ", self.__nimi, ", access: ", sep="", end="")
- i = 0
- for ovikoodi in sorted(self.__ovikoodit):
- if 0 <= i < int(self.__ovikoodit.index(self.__ovikoodit[-1])):
- print(ovikoodi, ", ", sep="", end="")
- i += 1
- else:
- print(ovikoodi, sep="")
- def get_name(self):
- """
- :return: Returns the name of the accesscard holder.
- """
- return self.__nimi
- 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: 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!
- """
- alue_tarkastus = etsi_alue(new_access_code)
- # alue_tarkastus: Jos kyseessä alue eikä yksittäinen ovikoodi
- if new_access_code not in self.__ovikoodit:
- self.__ovikoodit.append(new_access_code)
- elif alue_tarkastus == False:
- self.__ovikoodit.append(new_access_code)
- # Rakenne poistamaan turhat ovikoodit sen mukaan, onko sen kattava
- # alue jo listassa:
- if len(self.__ovikoodit) > 0:
- for ovikoodi in self.__ovikoodit:
- for koodi in DOORCODES:
- for alue in DOORCODES[koodi]:
- if alue in self.__ovikoodit:
- if koodi in self.__ovikoodit:
- self.__ovikoodit.remove(koodi)
- def check_access(self, door):
- """
- Checks if the accesscard allows access to a certain door.
- :param door: 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 in self.__ovikoodit:
- return True
- else:
- for alue in DOORCODES[door]:
- if alue in self.__ovikoodit:
- return True
- else:
- return False
- def merge(self, card):
- """
- Merges the accesscodes from another accesscard to this accesscard.
- :param card: The accesscard whose access rights are added to this card.
- :return:
- """
- for ovikoodi in card.__ovikoodit:
- if ovikoodi not in self.__ovikoodit:
- self.add_access(ovikoodi)
- def lue_tiedosto():
- """
- Funktio lukemaan syötetiedosto.
- :return:
- """
- try:
- tiedosto = open("accessinfo.txt", "r")
- kaytto_oikeudet = {}
- for rivi in tiedosto:
- rivi = rivi.strip("\n")
- rivi = rivi.strip("")
- tiedot = rivi.split(";")
- tunniste = tiedot[0]
- nimi = tiedot[1]
- kaikki_ovet = tiedot[2]
- henkilo = Accesscard(tunniste, nimi)
- ovet = kaikki_ovet.split(",")
- for ovikoodi in ovet:
- henkilo.add_access(ovikoodi)
- kaytto_oikeudet[tunniste] = henkilo
- return kaytto_oikeudet
- except OSError:
- print("Error: file cannot be read.")
- return
- def etsi_alue(etsitty):
- """
- Funktio tarkistamaan, onko lisättävän alueen joku ovikoodi jo listassa.
- :param etsitty:
- :return:
- """
- for ovikoodi in DOORCODES:
- for alue in DOORCODES[ovikoodi]:
- if etsitty == alue:
- return True
- else:
- return False
- def main():
- """
- Pääfunktio lukee syötetiedoston ja annetun käskyn kutsuen tarvittavat
- funktiot.
- :return:
- """
- tiedot = lue_tiedosto()
- while True:
- line = input("command> ")
- if line == "":
- break
- strings = line.split()
- command = strings[0]
- if command == "list" and len(strings) == 1:
- for henkilo in sorted(tiedot):
- tiedot[henkilo].info()
- elif command == "info" and len(strings) == 2:
- card_id = strings[1]
- if card_id not in tiedot:
- print("Error: unknown id.")
- else:
- tiedot[card_id].info()
- elif command == "access" and len(strings) == 3:
- card_id = strings[1]
- door_id = strings[2]
- if card_id not in tiedot:
- print("Error: unknown id.")
- else:
- if door_id not in DOORCODES:
- print("Error: unknown doorcode.")
- else:
- paasy = tiedot[card_id].check_access(door_id)
- if paasy == True:
- print("Card", card_id, "(", tiedot[card_id].get_name(),
- ") has access to door", door_id)
- else:
- print("Card", card_id, "(", tiedot[card_id].get_name(),
- ") has no access to door", door_id)
- elif command == "add" and len(strings) == 3:
- card_id = strings[1]
- access_code = strings[2]
- alue_tarkastus = etsi_alue(access_code)
- if card_id not in tiedot:
- print("Error: unknown id.")
- else:
- if access_code not in DOORCODES and alue_tarkastus is False:
- print("Error: unknown accesscode.")
- else:
- tiedot[card_id].add_access(access_code)
- elif command == "merge" and len(strings) == 3:
- card_id_to = strings[1]
- card_id_from = strings[2]
- if card_id_to not in tiedot or card_id_from not in tiedot:
- print("Error: unknown id.")
- else:
- tiedot[card_id_to].merge(tiedot[card_id_from])
- elif command == "quit":
- print("Bye!")
- return
- else:
- print("Error: unknown command.")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement