0

I'm trying to create a program that will assign a hotel room number to the guests. After it is assigned, inside the dictionary, the value for the key should be updated into either 'Unavailable' or 'Reserved'. But, the thing is, after I close the program. Those room numbers that were updated becomes back into 'Available'.

Is there a way to change the value, so that it stays updated even after I close the program?

import math
f = open('room.txt',  'r+')
#Hotel Rooms
for i in range(1, 7):
    locals()['Level{}'.format(i)] = {} # Creating Dictionary

start = 101
for x in range(1, 7):
    for i in range(start, start + 10):
        eval("Level" + str(x))[i] = 'Available' # Adding Values into Dictionary
    start += 100

# heading function
def heading(number, header):
    for i in range(60):
        print('-', end = '')
    print()
    for i in range(number):
        print('*', end = ' ')
    print('\n' + header)
    for i in range(number):
        print('*', end = ' ')
    print()

# customer details function
def customer():
    print('''Room Types:
1 --> Single (Level 1)
2 --> Double (Level 2)
3 --> Triple (Level 3)
4 --> Quad   (Level 4)
5 --> Queen  (Level 5)
6 --> King   (Level 6)
''')
    room = input('Room Type: ')
    name = input('Name: ')
    hp = input('Mobile Number: ')
    guests = input('Number of Guests: ')
    arrival = input('Arrival Date(dd/mm/yyyy): ')
    departure = input('Departure Date(dd/mm/yyyy): ')
    confirm = input('Confirmation(Y/N): ')
    conf(confirm, room, option)
    
# confirmation function + room for guest
def conf(con, num, opt):
    if con == 'Y':
        try:
            for level, availability in eval('Level' + str(num)).items():
                if availability == 'Available':
                    print('Guest Room Number:', level)
                    if option == '1':
                        eval('Level' + str(num))[level] = 'Reserved'
                    elif option == '2':
                        eval('Level' + str(num))[level] = 'Unavailable'
                    print('\nRoom Recorded!')
                    break
            for i in range(1, 7):
                f.write('Level ' + str(i) + '\n')
                for key in eval('Level' + str(i)):
                    f.write(str(key) + ": " + eval('Level' + str(i))[key] + '\n')
                f.write('\n')
            f.close()
        except NameError:
            print('\nInvalid Room Type.')
    elif con == 'N':
        print('\nRecord Not Recorded.')      
    else:
        print('\nInvalid Confirmation.')
    
# sub-headings (options)        
def options():
    print('''
Choose one of the following options (0-3):
0 <-- Back to Main Menu.    
1 --> Booking Engine
2 --> Check In
3 --> Check Out''')
    
while True:
    cus = '* Customer Registration *'
    num = math.ceil(len(cus)/2)
    heading(num, cus)
    options()
    option = input('Enter your option: ')
    # Booking Engine
    if option == '1':
        boo = '*  Booking Engine *'
        num = math.ceil(len(boo)/2)
        heading(num, boo)
        customer()  
    #Check In
    elif option == '2':
        chein = '*  Check In *'
        num = math.ceil(len(chein)/2)
        heading(num, chein)
        customer()
                                       
    else:
        print("\nInvalid Option.")
        continue
  • You need to persist the changes somewhere. The program allocates memory which is used while the program is running and flushed away when it's closed, so you need to persist changes in some way. If this program is just for learning purposes, you could use a file. – Capie Nov 11 '20 at 13:21
  • look at the module `pickle` – rioV8 Nov 11 '20 at 13:26
  • @Capie any code examples for using file to get the program to work? – Yap Ming Xuan Nov 11 '20 at 13:32
  • Does this answer your question? [How could I save data after closing my program?](https://stackoverflow.com/questions/28661860/how-could-i-save-data-after-closing-my-program) – Aniket Tiratkar Nov 11 '20 at 13:35

1 Answers1

0

I haven't executed your code, but from what I see :

These are the first lines of your code :

    f = open('room.txt',  'r+')
    #Hotel Rooms
    for i in range(1, 7):
        locals()['Level{}'.format(i)] = {} # Creating Dictionary
    
    start = 101
    for x in range(1, 7):
        for i in range(start, start + 10):
            eval("Level" + str(x))[i] = 'Available' # Adding Values into Dictionary
        start += 100

Here, you create a dictionary of all rooms, marking ALL of them as Available. It means that whatever the current value of a room, it will be marked as Available again.

Then in the conf function, you re-write the whole file, so the previously Reserved or Unavailable rooms are tagged as Available again.

Here is an example to be clear:

  1. you start the program
  2. You book a room, (e.g room 101), it is marked as Reserved
  3. The rooms.txt file is saved with the correct value.
  4. You stop your program

Then, when starting the program again:

  1. A dict with ALL values set to Available is created. It means that even the room 101 is back to Available
  2. You book any other room (e.g 201)
  3. The dict is saved in your file. BUT remember that as you initialised your dict, all rooms where marked as Available again.

The problem, in fine is that you don't read your rooms.txt file when starting your program to actually load the state of each room in your dictionary.

I also recommend you to drop the custom data structure with which you save the file and go for a JSON file. It will simplify your code.

Origine
  • 49
  • 1
  • 9