Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. # TIE-02101 Johdatus ohjelmointiin
  2. # Helena Tuovila, helena.tuovila@tuni.fi, opiskelijanumero: 282031
  3. # Tehtävän 15.2 ratkaisu:
  4. # Ohjelma kulkukorttien kulkuoikeuksien tarkasteluun.
  5.  
  6. DOORCODES = {'TC114': ['TIE'], 'TC203': ['TIE'], 'TC210': ['TIE', 'TST'],
  7. 'TD201': ['TST'], 'TE111': [], 'TE113': [], 'TE115': [],
  8. 'TE117': [], 'TE102': ['TIE'], 'TD203': ['TST'], 'TA666': ['X'],
  9. 'TC103': ['TIE', 'OPET', 'SGN'], 'TC205': ['TIE', 'OPET', 'ELT'],
  10. 'TB109': ['OPET', 'TST'], 'TB111': ['OPET', 'TST'],
  11. 'TB103': ['OPET'], 'TB104': ['OPET'], 'TB205': ['G'],
  12. 'SM111': [], 'SM112': [], 'SM113': [], 'SM114': [],
  13. 'S1': ['OPET'], 'S2': ['OPET'], 'S3': ['OPET'], 'S4': ['OPET'],
  14. 'K1705': ['OPET'], 'SB100': ['G'], 'SB202': ['G'],
  15. 'SM220': ['ELT'], 'SM221': ['ELT'], 'SM222': ['ELT'],
  16. 'secret_corridor_from_building_T_to_building_F': ['X', 'Y', 'Z'],
  17. 'TA': ['G'], 'TB': ['G'], 'SA': ['G'], 'KA': ['G']}
  18.  
  19. class Accesscard:
  20.  
  21. def __init__(self, id, name):
  22. """
  23. Constructor, creates a new object that has no access rights.
  24. :param id: card holders personal id (str)
  25. :param name: card holders name (str)
  26.  
  27. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME OR THE
  28. PARAMETERS!
  29. """
  30.  
  31. self.__tunniste = id
  32. self.__nimi = name
  33. self.__ovikoodit = []
  34.  
  35.  
  36.  
  37. def info(self):
  38. """
  39. The method has no return value. It prints the information related to
  40. the access card in the format:
  41. id, name, access: a1,a2,...,aN
  42. for example:
  43. 777, Thelma Teacher, access: OPET, TE113, TIE
  44. Note that the space characters after the commas and semicolon need to
  45. be as specified in the task description or the test fails.
  46. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  47. PARAMETERS, OR THE PRINTOUT FORMAT!
  48. """
  49.  
  50. print(self.__tunniste, ", ", self.__nimi, ", access: ", sep="", end="")
  51. i = 0
  52. for ovikoodi in sorted(self.__ovikoodit):
  53. if 0 <= i < int(self.__ovikoodit.index(self.__ovikoodit[-1])):
  54. print(ovikoodi, ", ", sep="", end="")
  55. i += 1
  56. else:
  57. print(ovikoodi, sep="")
  58.  
  59.  
  60.  
  61.  
  62. def get_name(self):
  63. """
  64. :return: Returns the name of the accesscard holder.
  65. """
  66.  
  67. return self.__nimi
  68.  
  69.  
  70. def add_access(self, new_access_code):
  71. """
  72. The method adds a new accesscode into the accesscard according to the
  73. rules defined in the task description.
  74. :param new_access_code: The accesscode to be added in the card.
  75. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  76. PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD!
  77. """
  78.  
  79. alue_tarkastus = etsi_alue(new_access_code)
  80. # alue_tarkastus: Jos kyseessä alue eikä yksittäinen ovikoodi
  81. if new_access_code not in self.__ovikoodit:
  82. self.__ovikoodit.append(new_access_code)
  83. elif alue_tarkastus == False:
  84. self.__ovikoodit.append(new_access_code)
  85.  
  86. # Rakenne poistamaan turhat ovikoodit sen mukaan, onko sen kattava
  87. # alue jo listassa:
  88. if len(self.__ovikoodit) > 0:
  89. for ovikoodi in self.__ovikoodit:
  90. for koodi in DOORCODES:
  91. for alue in DOORCODES[koodi]:
  92. if alue in self.__ovikoodit:
  93. if koodi in self.__ovikoodit:
  94. self.__ovikoodit.remove(koodi)
  95.  
  96.  
  97.  
  98. def check_access(self, door):
  99. """
  100. Checks if the accesscard allows access to a certain door.
  101.  
  102. :param door: the doorcode of the door that is being accessed.
  103. :return: True: The door opens for this accesscard.
  104. False: The door does not open for this accesscard.
  105. THIS METHOD IS AUTOMATICALLY TESTED, DON'T CHANGE THE NAME, THE
  106. PARAMETERS, OR THE RETURN VALUE! DON'T PRINT ANYTHING IN THE METHOD!
  107. """
  108.  
  109. if door in self.__ovikoodit:
  110. return True
  111. else:
  112. for alue in DOORCODES[door]:
  113. if alue in self.__ovikoodit:
  114. return True
  115. else:
  116. return False
  117.  
  118.  
  119. def merge(self, card):
  120. """
  121. Merges the accesscodes from another accesscard to this accesscard.
  122.  
  123. :param card: The accesscard whose access rights are added to this card.
  124. :return:
  125. """
  126.  
  127. for ovikoodi in card.__ovikoodit:
  128. if ovikoodi not in self.__ovikoodit:
  129. self.add_access(ovikoodi)
  130.  
  131.  
  132.  
  133. def lue_tiedosto():
  134. """
  135. Funktio lukemaan syötetiedosto.
  136. :return:
  137. """
  138. try:
  139. tiedosto = open("accessinfo.txt", "r")
  140. kaytto_oikeudet = {}
  141. for rivi in tiedosto:
  142. rivi = rivi.strip("\n")
  143. rivi = rivi.strip("")
  144. tiedot = rivi.split(";")
  145. tunniste = tiedot[0]
  146. nimi = tiedot[1]
  147. kaikki_ovet = tiedot[2]
  148. henkilo = Accesscard(tunniste, nimi)
  149. ovet = kaikki_ovet.split(",")
  150. for ovikoodi in ovet:
  151. henkilo.add_access(ovikoodi)
  152. kaytto_oikeudet[tunniste] = henkilo
  153. return kaytto_oikeudet
  154. except OSError:
  155. print("Error: file cannot be read.")
  156. return
  157.  
  158.  
  159. def etsi_alue(etsitty):
  160. """
  161. Funktio tarkistamaan, onko lisättävän alueen joku ovikoodi jo listassa.
  162. :param etsitty:
  163. :return:
  164. """
  165. for ovikoodi in DOORCODES:
  166. for alue in DOORCODES[ovikoodi]:
  167. if etsitty == alue:
  168. return True
  169. else:
  170. return False
  171.  
  172.  
  173. def main():
  174. """
  175. Pääfunktio lukee syötetiedoston ja annetun käskyn kutsuen tarvittavat
  176. funktiot.
  177. :return:
  178. """
  179. tiedot = lue_tiedosto()
  180.  
  181. while True:
  182. line = input("command> ")
  183.  
  184. if line == "":
  185. break
  186.  
  187. strings = line.split()
  188. command = strings[0]
  189.  
  190. if command == "list" and len(strings) == 1:
  191. for henkilo in sorted(tiedot):
  192. tiedot[henkilo].info()
  193.  
  194. elif command == "info" and len(strings) == 2:
  195. card_id = strings[1]
  196. if card_id not in tiedot:
  197. print("Error: unknown id.")
  198. else:
  199. tiedot[card_id].info()
  200.  
  201. elif command == "access" and len(strings) == 3:
  202. card_id = strings[1]
  203. door_id = strings[2]
  204. if card_id not in tiedot:
  205. print("Error: unknown id.")
  206. else:
  207. if door_id not in DOORCODES:
  208. print("Error: unknown doorcode.")
  209. else:
  210. paasy = tiedot[card_id].check_access(door_id)
  211. if paasy == True:
  212. print("Card", card_id, "(", tiedot[card_id].get_name(),
  213. ") has access to door", door_id)
  214. else:
  215. print("Card", card_id, "(", tiedot[card_id].get_name(),
  216. ") has no access to door", door_id)
  217.  
  218.  
  219. elif command == "add" and len(strings) == 3:
  220. card_id = strings[1]
  221. access_code = strings[2]
  222. alue_tarkastus = etsi_alue(access_code)
  223. if card_id not in tiedot:
  224. print("Error: unknown id.")
  225. else:
  226. if access_code not in DOORCODES and alue_tarkastus is False:
  227. print("Error: unknown accesscode.")
  228. else:
  229. tiedot[card_id].add_access(access_code)
  230.  
  231. elif command == "merge" and len(strings) == 3:
  232. card_id_to = strings[1]
  233. card_id_from = strings[2]
  234. if card_id_to not in tiedot or card_id_from not in tiedot:
  235. print("Error: unknown id.")
  236. else:
  237. tiedot[card_id_to].merge(tiedot[card_id_from])
  238.  
  239. elif command == "quit":
  240. print("Bye!")
  241. return
  242. else:
  243. print("Error: unknown command.")
  244.  
  245.  
  246. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement