MatteB_01

hotel

Feb 12th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. def main():
  2.     HOTEL = "hotels.txt"
  3.     PRENOTAZIONI = "booking.txt"
  4.    
  5. #--------------MEMORIZZO LA LISTA DEGLI HOTEL IN UN DIZIONARIO nome-[ID , NUMERO CAMERE, NUMERO POSTI RIMANENTI]-----------
  6.     try:
  7.         with open(HOTEL, "r") as file:
  8.             hotel = {}
  9.             for line in file:
  10.                 info = line.strip().split(":")
  11.                 #riordino i valori della lista e duplico il numero di camere
  12.                
  13.                 temp = info[0]
  14.                 info[0] = info[1]
  15.                 info[1] = temp
  16.                 info[2] = int(info[2])
  17.                 info[3] = info[2]
  18.                
  19.                 hotel[info[0]] = info[1:]
  20.     except IOError:
  21.         print("il file contenente l' elenco degli hotel è inesistente o il suo nome è errato")
  22.         exit()
  23. #--------------MEMORIZZO I BOOKING IN UNA TABELLA------------------------------
  24.  
  25.     try:
  26.         with open(PRENOTAZIONI, "r") as file:
  27.             prenotazioni = []
  28.             for line in file:
  29.                 info = line.strip().split(" ")
  30.                 info[2] = int(info[2])
  31.                 prenotazioni.append(info)
  32.     except IOError:
  33.         print("il file contenente l' elenco delle prenotazioni è inesistente o il suo nome è errato")
  34.         exit()
  35. #------------CICLO SUI BOOKING RIDUCENDO IL NUMERO DI CAMERE DALL' ELEMENTO CORRISPONDENTE DEL DIZIONARIO QUALORA POSSIBILE----
  36.     confermate , respinte = controlloBooking(prenotazioni, hotel)
  37.     print(f"prenotazioni confermate: {confermate} , prenotazioni respinte: {respinte}")
  38.    
  39. #--------------STAMPO A SCHERMO GLI HOTEL CON RELATIVE CAMERE E POSTI LIBERI E NE VALUTO QUELLO CON PIU POSTI LIBERI CICLANDO-------------
  40.     migliorNumero = 0
  41.     migliore = ""
  42.     for albergo in hotel.keys():
  43.         print(f"{hotel[albergo][0]}: {hotel[albergo][1]} camere ({hotel[albergo][2]} libere)")
  44.         if hotel[albergo][2]> migliorNumero:
  45.             migliorNumero = hotel[albergo][2]
  46.             migliore = hotel[albergo][0]
  47.     print(f"hotel con più camere libere: {migliore}")
  48. #------------------------FUNZIONI----------------------------------------------
  49.  
  50. def controlloBooking(prenotazioni, hotel):
  51.    
  52.     confermate = 0
  53.     respinte = 0
  54.    
  55.     for booking in prenotazioni:
  56.         posti = booking[2]
  57.         luogo = booking[1]
  58.         if hotel[luogo][2] >= posti:
  59.             confermate +=1
  60.             hotel[luogo][2] -= posti
  61.         else:
  62.             respinte += 1
  63.             print(f"Richiesta non confermata: {booking[0]}")
  64.     return confermate, respinte
  65. #-------------------------------------------------------------------------
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment