Home > front end >  Room Booking - Hotel Booking System
Room Booking - Hotel Booking System

Time:04-29

This is part of my Hotel Booking System. I want to overwrite a part in my text file for rooms.

The structure of the text file -rooms.txt:

    01-One Bed AC-£30-Y 
    09-One Bed AC-£30-Y
    03-One Bed Non AC-£20-Y
    012-One Bed Non AC-£20-Y
    06-Standard Bed AC-£50-Y
    016-Standard Bed AC-£50-Y
    010-Standard Bed Non AC-£35-Y
    014-Standard Bed Non AC-£35-Y
    05-Three Bed AC-£75-Y
    07-Three Bed AC-£75-Y
    011-Three Bed Non AC-£60-Y
    015-Three Bed Non AC-£60-Y
    08-Four Bed AC-£100-Y
    02-Four Bed AC-£100-Y
    013-Four Bed Non AC-£85-Y
    04-Four Bed Non AC-£85-Y

The code is here:

def Room_Booking():
        
    check_In = str(input(" Enter Check In Date: "))
    check_Out = str(input(" Enter Check Out Date: "))
    
    print(" \n")
    
    print(" 1 One Bed AC\n")
    print(" 2 One Bed Non-AC\n")
    print(" 3 Standard Bed AC\n")
    print(" 4 Standard Bed Non-AC\n")
    print(" 5 Three Bed AC\n")
    print(" 6 Three Bed Non-AC\n")
    print(" 7 Four Bed AC\n")
    print(" 8 Four Bed Non-AC\n")
    
    Room_Type = int(input(" Choose a room type from above: "))
    
    if Room_Type == 1:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] and x[3] == 'One Bed AC' and 'Y':
                print(x)

    if Room_Type == 2:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'One Bed Non AC':
                print(x)
    
    if Room_Type == 3:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Standard Bed AC':
                print(x)
    
    if Room_Type == 4:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Standard Bed Non AC':
                print(x)
    
    if Room_Type == 5:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Three Bed AC':
                print(x)
    
    if Room_Type == 6:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Three Bed Non AC':
                print(x)
    
    if Room_Type == 7:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Four Bed AC':
                print(x)
    
    if Room_Type == 8:
        for line in open("rooms.txt", "r").readlines():
            x = line.split("-")
            if x[1] == 'Four Bed Non AC':
                print(x)

After choosing one of the room types. I want the system to print out the available rooms for that particular room type and allow users to enter the available room numbers. After choosing the room number for that particular room type, the availability of that specific room will change from Y to N and only change back after the check-out date.

Example: I choose room type 1 and the system will print out this:

    01-One Bed AC-£30-Y 
    09-One Bed AC-£30-Y

I then choose room number 01 and the availability for this room should change from Y to N in the text file, so in the text file it should look like this:

    01-One Bed AC-£30-N 
    09-One Bed AC-£30-Y
    .....etc

CodePudding user response:

You can't edit a part of txt file in python or any languages.
You need to rewrite the entire file.
What you can do is:

lines=[]
with open('sam.txt','r') as f:
    lines=[i.strip() for i in f.readlines()]
for i in range(len(lines)):
    tem=list(lines[i])
    if(True):##write your condition
        tem[-1]='N'    
    lines[i]="".join(tem)
    lines[i] ='\n'
with open('sam.txt','w') as f:
    f.writelines(lines)

Hope this solves your problem. Commmet if you need more help or accept the solution

Here if you need to change for room number '01', Here's the code:

lines=[]
with open('sam.txt','r') as f:
    lines=[i.strip() for i in f.readlines()]
for i in range(len(lines)):
    tem=list(lines[i])
    LineSplit=lines[i].split('-')
    if(LineSplit[0]=='01'):##write your condition
        tem[-1]='N'    
    lines[i]="".join(tem)
    lines[i] ='\n'
with open('sam.txt','w') as f:
    f.writelines(lines)

The output file is:

01-One Bed AC-£30-N
09-One Bed AC-£30-Y
03-One Bed Non AC-£20-Y
012-One Bed Non AC-£20-Y
06-Standard Bed AC-£50-Y
016-Standard Bed AC-£50-Y
010-Standard Bed Non AC-£35-Y
014-Standard Bed Non AC-£35-Y
05-Three Bed AC-£75-Y
07-Three Bed AC-£75-Y
011-Three Bed Non AC-£60-Y
015-Three Bed Non AC-£60-Y
08-Four Bed AC-£100-Y
02-Four Bed AC-£100-Y
013-Four Bed Non AC-£85-Y
04-Four Bed Non AC-£85-Y

See the change in room no '01'

  • Related