客房预订-酒店预订系统

Room Booking - Hotel Booking System

这是我的酒店预订系统的一部分。我想覆盖房间文本文件中的一部分。

文本文件的结构-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

代码在这里:

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)

选择其中一种房型后。我希望系统打印出该特定房间类型的可用房间,并允许用户输入可用房间号。选择特定房型的房间号后,该特定房间的可用性将从 Y 变为 N,并且仅在退房日期后变回。

示例: 我选择房型1,系统会打印出来:

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

然后我选择房间号 01,这个房间的可用性应该在文本文件中从 Y 变为 N,因此在文本文件中它应该如下所示:

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

您不能编辑 python 或任何语言的 txt 文件的一部分。
您需要重写整个文件。
您可以做的是:

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)

希望这能解决您的问题。如果您需要更多帮助或接受解决方案,请提交

如果您需要更改房间号“01”,请输入代码:

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)

输出文件为:

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

查看“01”号房间的变化