bruh1214

Untitled

Oct 17th, 2021 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. I am really sorry, but I am too tired to replace everything that's in Romanian to English
  2. Most of it sounds like in English anyways
  3.  
  4.  
  5. from datetime import datetime
  6. import datetime
  7.  
  8.  
  9. def creeaza_pachet(data_i, data_s, dest, pret):
  10. #function that creates a package
  11. return {
  12. "data_i": data_i,
  13. "data_s": data_s,
  14. "dest": dest,
  15. "pret": pret
  16. }
  17.  
  18.  
  19. def get_data_i(pachet):
  20. # functie that returns the beginning date of the package
  21. return pachet["data_i"]
  22.  
  23.  
  24. def get_data_s(pachet):
  25. # function that returns the end date of a package
  26. return pachet["data_s"]
  27.  
  28.  
  29. def get_destinatie(pachet):
  30. # function that returns a destination of the package
  31. return pachet["dest"]
  32.  
  33.  
  34. def get_pret(pachet):
  35. # function that returns a price of the package, float
  36. return pachet["pret"]
  37.  
  38.  
  39. def valideaza_pachet(pachet):
  40. # function that validates if a package is entered correctly or not
  41. err = ""
  42. if get_data_i(pachet) > get_data_s(pachet):
  43. err += "wrong data!\n"
  44. if get_destinatie(pachet) == " ":
  45. err += "wrong destination!\n"
  46. if get_pret(pachet) <= 0:
  47. err += "wrong price!\n"
  48. if len(err) > 0:
  49. raise Exception(err)
  50.  
  51.  
  52. def egale_pachet(p0, p1):
  53. return get_pret(p0) == get_pret(p1)
  54.  
  55.  
  56. def adauga_pachet_in_lista(l, pachet):
  57. # function that adds a package in a list
  58. for _pachet in l:
  59. if egale_pachet(_pachet, pachet):
  60. raise Exception("package with same price introduced!\n")
  61. l.append(pachet)
  62.  
  63.  
  64. def srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret):
  65. # function that creates a package and adds in a list
  66. pachet = creeaza_pachet(data_i, data_s, dest, pret)
  67. valideaza_pachet(pachet)
  68. adauga_pachet_in_lista(l, pachet)
  69.  
  70.  
  71. def test_srv_adauga_pachet_in_lista():
  72. l = []
  73. assert (len(l) == 0)
  74. pachet = creeaza_pachet(datetime.strptime('24/08/2021', "%d/%m/%Y"), datetime.strptime('26/08/2021', "%d/%m/%Y"),
  75. "Bruh", 9000.1)
  76. srv_adauga_pachet_in_lista(l, pachet)
  77. assert (len(l) == 0)
  78. pachet_g = creeaza_pachet(datetime.strptime('24/08/2021', "%d/%m/%Y"), datetime.strptime('22/08/2021', "%d/%m/%Y"),
  79. " ", 9000.1)
  80. try:
  81. srv_adauga_pachet_in_lista(l, pachet_g)
  82. except Exception as ex:
  83. raise Exception("date gresite!\ndestinatie gresita!\n")
  84.  
  85.  
  86. def test_adauga_pachet_in_lista():
  87. l = []
  88. assert (len(l) == 0)
  89. pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  90. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  91. "Bruh", 9000.1)
  92. adauga_pachet_in_lista(l, pachet)
  93. assert (len(l) == 1)
  94. assert (get_data_i(pachet) == get_data_i(l[0]))
  95. assert (get_data_s(pachet) == get_data_s(l[0]))
  96. assert (get_destinatie(pachet) == get_destinatie(l[0]))
  97. assert (abs(get_pret(pachet) - get_pret(l[0]) < 0.0001))
  98. alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  99. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Bruh", 9000.1)
  100. try:
  101. adauga_pachet_in_lista(l, alt_pachet)
  102. except Exception as ex:
  103. assert (str(ex) == "pachet cu acelasi pret introdus!\n")
  104.  
  105.  
  106. def test_valideaza_pachet():
  107. pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  108. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  109. "Bruh", 9000.1)
  110. valideaza_pachet(pachet)
  111. pachet_gresit = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  112. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), "Bruh", 9000.1)
  113. try:
  114. valideaza_pachet(pachet_gresit)
  115. except Exception as ex:
  116. assert (str(ex) == "date gresite!\n")
  117. alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  118. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", -904)
  119. try:
  120. valideaza_pachet(alt_pachet)
  121. except Exception as ex:
  122. assert (str(ex) == "date gresite!\ndestinatie gresita!\npret gresit!\n")
  123.  
  124.  
  125. def test_creeaza_pachet():
  126. data_i_string = '24/08/2021'
  127. data_i = datetime.datetime.strptime(data_i_string, "%d/%m/%Y")
  128. data_s_string = '26/08/2021'
  129. data_s = datetime.datetime.strptime(data_s_string, "%d/%m/%Y")
  130. dest = "Bruh"
  131. pret = 9000.1
  132. pachet = creeaza_pachet(data_i, data_s, dest, pret)
  133. assert (get_data_i(pachet) == data_i)
  134. assert (get_data_s(pachet) == data_s)
  135. assert (get_destinatie(pachet) == dest)
  136. assert (abs(get_pret(pachet) - pret) < 0.0001)
  137.  
  138.  
  139. def run_teste():
  140. test_creeaza_pachet()
  141. test_valideaza_pachet()
  142. test_adauga_pachet_in_lista()
  143.  
  144.  
  145. def to_str_pachet(pachet):
  146. return str(
  147. "Data de inceput: " + str(pachet["data_i"]) + "\n" + "Data de sfarsit: " + str(pachet["data_s"]) + "\n" + "Destinatia: " +
  148. pachet["dest"] + "\n" + "Pretul: " + str(pachet["pret"]))
  149.  
  150.  
  151. def ui_adauga_pachet(l):
  152. try:
  153. data_i = input("Introduceti data de inceput in formatul dd-mm-yyyy: ")
  154. data_i = datetime.datetime.strptime(data_i, "%d-%m-%Y").date()
  155. data_s = input("Introduceti data de sfarsit in formatul dd-mm-yyyy: ")
  156. data_s = datetime.datetime.strptime(data_s, "%d-%m-%Y").date()
  157. except ValueError:
  158. print("date numerice invalide!")
  159. return
  160. dest = input("Introduceti destinatia aici: ")
  161. try:
  162. pret = float(input("Introduceti pretul aici: "))
  163. except ValueError:
  164. print("pret invalid!")
  165. return
  166. srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret)
  167.  
  168.  
  169. def ui_print_pachet(l):
  170. for pachet in l:
  171. print(to_str_pachet(pachet))
  172. if len(l) == 0:
  173. print("Nu a fost introdus niciun pachet!")
  174.  
  175.  
  176.  
  177. def ui_modify_data(l):
  178. processing_functions = [ui_add_package, ui_print_package]
  179. progress = 0
  180. while progress < len(processing_functions):
  181. cmd = input("Reintroduce dates: ")
  182. if cmd == "//":
  183. progress -= 1
  184. print("Go back to previous input")
  185. else:
  186. print("Processing" + cmd)
  187. processing_functions[progress](cmd)
  188. progress += 1
  189.  
  190.  
  191. def run():
  192. l = []
  193. while True:
  194. cmd = input(">>>")
  195. if cmd == "exit":
  196. return
  197. if cmd == " ":
  198. continue
  199. if cmd == "add_pachet":
  200. try:
  201. ui_adauga_pachet(l)
  202. except Exception as ex:
  203. print(ex)
  204. elif cmd == "/":
  205. try:
  206. ui_modifica_date(l)
  207. except ValueError:
  208. print("date numerice invalide!")
  209. elif cmd == "print_pachete":
  210. ui_print_pachet(l)
  211. else:
  212. print("comanda invalida!")
  213.  
  214.  
  215. def main():
  216. run()
  217. run_teste()
  218.  
  219.  
  220. main()
  221.  
Add Comment
Please, Sign In to add comment