Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am really sorry, but I am too tired to replace everything that's in Romanian to English
- Most of it sounds like in English anyways
- from datetime import datetime
- import datetime
- def creeaza_pachet(data_i, data_s, dest, pret):
- #function that creates a package
- return {
- "data_i": data_i,
- "data_s": data_s,
- "dest": dest,
- "pret": pret
- }
- def get_data_i(pachet):
- # functie that returns the beginning date of the package
- return pachet["data_i"]
- def get_data_s(pachet):
- # function that returns the end date of a package
- return pachet["data_s"]
- def get_destinatie(pachet):
- # function that returns a destination of the package
- return pachet["dest"]
- def get_pret(pachet):
- # function that returns a price of the package, float
- return pachet["pret"]
- def valideaza_pachet(pachet):
- # function that validates if a package is entered correctly or not
- err = ""
- if get_data_i(pachet) > get_data_s(pachet):
- err += "wrong data!\n"
- if get_destinatie(pachet) == " ":
- err += "wrong destination!\n"
- if get_pret(pachet) <= 0:
- err += "wrong price!\n"
- if len(err) > 0:
- raise Exception(err)
- def egale_pachet(p0, p1):
- return get_pret(p0) == get_pret(p1)
- def adauga_pachet_in_lista(l, pachet):
- # function that adds a package in a list
- for _pachet in l:
- if egale_pachet(_pachet, pachet):
- raise Exception("package with same price introduced!\n")
- l.append(pachet)
- def srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret):
- # function that creates a package and adds in a list
- pachet = creeaza_pachet(data_i, data_s, dest, pret)
- valideaza_pachet(pachet)
- adauga_pachet_in_lista(l, pachet)
- def test_srv_adauga_pachet_in_lista():
- l = []
- assert (len(l) == 0)
- pachet = creeaza_pachet(datetime.strptime('24/08/2021', "%d/%m/%Y"), datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Bruh", 9000.1)
- srv_adauga_pachet_in_lista(l, pachet)
- assert (len(l) == 0)
- pachet_g = creeaza_pachet(datetime.strptime('24/08/2021', "%d/%m/%Y"), datetime.strptime('22/08/2021', "%d/%m/%Y"),
- " ", 9000.1)
- try:
- srv_adauga_pachet_in_lista(l, pachet_g)
- except Exception as ex:
- raise Exception("date gresite!\ndestinatie gresita!\n")
- def test_adauga_pachet_in_lista():
- l = []
- assert (len(l) == 0)
- pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Bruh", 9000.1)
- adauga_pachet_in_lista(l, pachet)
- assert (len(l) == 1)
- assert (get_data_i(pachet) == get_data_i(l[0]))
- assert (get_data_s(pachet) == get_data_s(l[0]))
- assert (get_destinatie(pachet) == get_destinatie(l[0]))
- assert (abs(get_pret(pachet) - get_pret(l[0]) < 0.0001))
- alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Bruh", 9000.1)
- try:
- adauga_pachet_in_lista(l, alt_pachet)
- except Exception as ex:
- assert (str(ex) == "pachet cu acelasi pret introdus!\n")
- def test_valideaza_pachet():
- pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Bruh", 9000.1)
- valideaza_pachet(pachet)
- pachet_gresit = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), "Bruh", 9000.1)
- try:
- valideaza_pachet(pachet_gresit)
- except Exception as ex:
- assert (str(ex) == "date gresite!\n")
- alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", -904)
- try:
- valideaza_pachet(alt_pachet)
- except Exception as ex:
- assert (str(ex) == "date gresite!\ndestinatie gresita!\npret gresit!\n")
- def test_creeaza_pachet():
- data_i_string = '24/08/2021'
- data_i = datetime.datetime.strptime(data_i_string, "%d/%m/%Y")
- data_s_string = '26/08/2021'
- data_s = datetime.datetime.strptime(data_s_string, "%d/%m/%Y")
- dest = "Bruh"
- pret = 9000.1
- pachet = creeaza_pachet(data_i, data_s, dest, pret)
- assert (get_data_i(pachet) == data_i)
- assert (get_data_s(pachet) == data_s)
- assert (get_destinatie(pachet) == dest)
- assert (abs(get_pret(pachet) - pret) < 0.0001)
- def run_teste():
- test_creeaza_pachet()
- test_valideaza_pachet()
- test_adauga_pachet_in_lista()
- def to_str_pachet(pachet):
- return str(
- "Data de inceput: " + str(pachet["data_i"]) + "\n" + "Data de sfarsit: " + str(pachet["data_s"]) + "\n" + "Destinatia: " +
- pachet["dest"] + "\n" + "Pretul: " + str(pachet["pret"]))
- def ui_adauga_pachet(l):
- try:
- data_i = input("Introduceti data de inceput in formatul dd-mm-yyyy: ")
- data_i = datetime.datetime.strptime(data_i, "%d-%m-%Y").date()
- data_s = input("Introduceti data de sfarsit in formatul dd-mm-yyyy: ")
- data_s = datetime.datetime.strptime(data_s, "%d-%m-%Y").date()
- except ValueError:
- print("date numerice invalide!")
- return
- dest = input("Introduceti destinatia aici: ")
- try:
- pret = float(input("Introduceti pretul aici: "))
- except ValueError:
- print("pret invalid!")
- return
- srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret)
- def ui_print_pachet(l):
- for pachet in l:
- print(to_str_pachet(pachet))
- if len(l) == 0:
- print("Nu a fost introdus niciun pachet!")
- def ui_modify_data(l):
- processing_functions = [ui_add_package, ui_print_package]
- progress = 0
- while progress < len(processing_functions):
- cmd = input("Reintroduce dates: ")
- if cmd == "//":
- progress -= 1
- print("Go back to previous input")
- else:
- print("Processing" + cmd)
- processing_functions[progress](cmd)
- progress += 1
- def run():
- l = []
- while True:
- cmd = input(">>>")
- if cmd == "exit":
- return
- if cmd == " ":
- continue
- if cmd == "add_pachet":
- try:
- ui_adauga_pachet(l)
- except Exception as ex:
- print(ex)
- elif cmd == "/":
- try:
- ui_modifica_date(l)
- except ValueError:
- print("date numerice invalide!")
- elif cmd == "print_pachete":
- ui_print_pachet(l)
- else:
- print("comanda invalida!")
- def main():
- run()
- run_teste()
- main()
Add Comment
Please, Sign In to add comment