Assistance with classes in this python program?

2019-09-05 16:51发布

i have a program like this: To reserve hotel room according to user requirements.

 class Customer:
     def __init__(self,customer_id,customer_fname,customer_lname,customer_address,customer_contactno,customer_noofdays):
                       self.customer_id=customer_id
                       self.customer_fname=customer_fname
                       self.customer_lname=customer_lname
                       self.customer_address=customer_address
                       self.customer_contactno=customer_contactno
                       self.cost=0
                       self.noofdays=customer_noofdays
                       self.reservation_id=[]
                       self.customer_record={'c_name':self.customer_fname+""+self.customer_lname,
                           'c_id':self.customer_id,
                           'c_wallet':self.cost,
                           'c_days':self.noofdays,
                           'c_reservation_id':self.reservation_id}
     def Total(self):
               print"\n"
               print"ÏD\t\t\t:\t",self.customer_record['c_id']
               print"Name\t\t\t:\t",self.customer_record['c_name']
               print"Amount to be paid\t:\t",self.customer_record['c_wallet']
               print"\n"
 class Reservation:
         def __init__(self):
               self.hotel_price={'KING DELUXE BEDROOM':700,
                      'QUEEN DUPLEX BEDROOM':800,
                      'CONTERMINOUS FAMILY SUITE':1000,
                      'GRAND TWIN PREMIER SUITE':900,
                      'TWOFOLD PENTHOUSE':600,
                      'LUXURIOUS POSH CABANA':1300,
                      'HEDONISTIC SPACIOUS LANAI':650}


               self.hotel_room={'KING DELUXE BEDROOM':7,
                      'QUEEN DUPLEX BEDROOM':8,
                      'CONTERMINOUS FAMILY SUITE':10,
                      'GRAND TWIN PREMIER SUITE':9,
                      'TWOFOLD PENTHOUSE':6,
                      'LUXURIOUS POSH CABANA':13,
                      'HEDONISTIC SPACIOUS LANAI':12}
        def currentstatus(self,option):
                   if(option=='Hotelroom'):
                         print"            THE FOLLOWING ROOMS ARE AVAILABLE FOR YOUR STAY"
                         print"    "
                         print"        ROOMS                   ROOMS AVAILABLE    "

                         print" 1.HEDONISTIC SPACIOUS LANAI        12    "                                  
                         print" 2.QUEEN DUPLEX BEDROOM              8    "   
                         print" 3.CONTERMINOUS FAMILY SUITE        10    "
                         print" 4.LUXURIOUS POSH CABANA            13    "                  
                         print" 5.TWOFOLD PENTHOUSE                 6    "
                         print" 6.GRAND TWIN PREMIER SUITE          9    "
                         print" 7.KING DELUXE BEDROOM               7    "
                         print""

                         for key,value in self.hotel_room.items():

                                     print key,value
                   elif(option=="Hotelprice"):
                               print"            THE FOLLOWING ROOMS ARE AVAILABLE FOR YOUR STAY"
                               print"    "
                               print"        ROOMS                       PRICE(RS)    "

                               print" 1.HEDONISTIC SPACIOUS LANAI        1200    "                                  
                               print" 2.QUEEN DUPLEX BEDROOM              800    "   
                               print" 3.CONTERMINOUS FAMILY SUITE        1000    "
                               print" 4.LUXURIOUS POSH CABANA            1300    "                   
                               print" 5.TWOFOLD PENTHOUSE                 600    "
                               print" 6.GRAND TWIN PREMIER SUITE          900   "
                               print" 7.KING DELUXE BEDROOM               700    "
                               print""


class Hotel(Reservation):
            def __init__(self,Reservation,Customer,hotel_room_selection,hotel_check_in_date,hotel_check_out_date,hotel_noof_days):
             self.hotel_room_selection=hotel_room_selection
             self.hotel_check_in_date=hotel_check_in_date
             self.hotel_check_out_date=hotel_check_out_date
             self.noofdays=noofdays

           def Checkavailability(self):
                    if reserve.hotel_room.get(self.hotel_room_selection)>0:
                            reserve.hotel_room[self.hotel_room_selection]-=1
                                                                Person.customer_record['c_wallet']+=reserve.hotel_price[self.hotel_room_selection]
                            print"\n\n HOTEL ROOM HAS BEEN RESERVED FOR YOUR STAY \n\n"
                   else:
                           print"SORRY! ROOMS ARE NOT AVAILABLE"
          def calculateprice(self):
                    if (Person.customer_record['c_days']==1):
                             Person.customer_record['c_wallet']=0.1*Person.customer_record['c_wallet']+Person.customer_record['c_wallet']
                    elif (Person.customer_record['c_days']==2):
              Person.customer_record['c_wallet']=0.3*Person.customer_record['c_wallet']+Person.customer_record['c_wallet']
                   elif(Person.customer_record['c_days']==3):
          Person.customer_record['c_wallet']=0.5*Person.customer_record['c_wallet']+Person.customer_record['c_wallet']
                   elif(Person.customer_record['c_days']==4):
          Person.customer_record['c_wallet']=0.7*Person.customer_record['c_wallet']+Person.customer_record['c_wallet']
                   else:
          Person.customer_record['c_wallet']=0.9*Person.customer_record['c_wallet']+Person.customer_record['c_wallet']


  reservation=1
  reserve=Reservation()
  CustomerID=1
  Hotelroom=['KING DELUXE BEDROOM',
                      'QUEEN DUPLEX BEDROOM',
                      'CONTERMINOUS FAMILY SUITE',
                      'GRAND TWIN PREMIER SUITE',
                      'TWOFOLD PENTHOUSE',
                      'LUXURIOUS POSH CABANA',
                      'HEDONISTIC SPACIOUS LANAI']

  while(reservation==1):
    print"Royal Palace welcomes you"
    fname=raw_input("ENTER YOUR FIRST NAME: ")
    lname=raw_input("ENTER YOUR SECOND NAME: ")
    address=raw_input("ENTER THE ADRESS: ")
    contactno=input("ENTER THE PHONE NUMBER: ")
    noofdays=input("ENTER THE NUMBER OF DAYS OF STAY: ") 
    Person=Customer(CustomerID,fname,lname,address,contactno,noofdays)
    print"\n"

    HotelReservationChoice=input("DO YOU WANT HOTEL FACILITY?? IF YES,PRESS 1: ")
    if(HotelReservationChoice==1):
                  print"\n"
                  HotelChoice=1
                  reserve.currentstatus('Hotelroom')
                  print"\n"
                  for room in Hotelroom:

                       HotelChoice+=1
                  print""
                  HotelFare=input("DO YOU WANT TO CHECK RATE OF ROOMS?? IF YES,PRESS 1: ")
                  print""
                  if(HotelFare==1):
                          reserve.currentstatus('Hotelprice')
                  print""
                  HChoice=input("ENTER YOUR CHOICE OF ROOM: ")
                  CheckOutDate=raw_input("ËNTER THE DATE OF CHECKOUT FROM THE HOTEL: ")
                  CheckInDate=raw_input("ËNTER DATE OF CHECK IN TO THE HOTEL: ")
                  noofdays=input("ËNTER THE NUMBER OF DAYS OF STAY: ")
                  H1=Hotel(reserve,Person,Hotelroom[HChoice-1],CheckOutDate,CheckInDate,noofdays)
                  H1.Checkavailability()
                  H1.calculateprice()
                  reserve.currentstatus('Hotelroom')
                  Person.Total()
  reservation=input("DO YOU WANT ANOTHER RESERVATION?? IF YES,PRESS 1:")
  print""
  CustomerID+=1

The output comes like:

    Royal Palace welcomes you
    ENTER YOUR FIRST NAME: qqq
    ENTER YOUR SECOND NAME: qqq
    ENTER THE ADRESS: qqq
    ENTER THE PHONE NUMBER: 111
    ENTER THE NUMBER OF DAYS OF STAY: 1


   DO YOU WANT HOTEL FACILITY?? IF YES,PRESS 1: 1


        THE FOLLOWING ROOMS ARE AVAILABLE FOR YOUR STAY

    ROOMS                   ROOMS AVAILABLE    
1.HEDONISTIC SPACIOUS LANAI        12    
2.QUEEN DUPLEX BEDROOM              8    
3.CONTERMINOUS FAMILY SUITE        10    
4.LUXURIOUS POSH CABANA            13    
5.TWOFOLD PENTHOUSE                 6    
6.GRAND TWIN PREMIER SUITE          9    
7.KING DELUXE BEDROOM               7    

# The following is room availability. i want it to display like the print   format given above,but not here but at the end. but my program executes in such a way that its also getting printed here like this.

HEDONISTIC SPACIOUS LANAI 12
QUEEN DUPLEX BEDROOM 8
CONTERMINOUS FAMILY SUITE 10
LUXURIOUS POSH CABANA 13
TWOFOLD PENTHOUSE 6
GRAND TWIN PREMIER SUITE 9
KING DELUXE BEDROOM 7



DO YOU WANT TO CHECK RATE OF ROOMS?? IF YES,PRESS 1: 1

        THE FOLLOWING ROOMS ARE AVAILABLE FOR YOUR STAY

    ROOMS                       PRICE(RS)    
1.HEDONISTIC SPACIOUS LANAI        1200    
2.QUEEN DUPLEX BEDROOM              800    
3.CONTERMINOUS FAMILY SUITE        1000    
4.LUXURIOUS POSH CABANA            1300    
5.TWOFOLD PENTHOUSE                 600    
6.GRAND TWIN PREMIER SUITE          900   
7.KING DELUXE BEDROOM               700    


ENTER YOUR CHOICE OF ROOM: 1
ËNTER THE DATE OF CHECKOUT FROM THE HOTEL: 11
ËNTER DATE OF CHECK IN TO THE HOTEL: 11
ËNTER THE NUMBER OF DAYS OF STAY: 1


HOTEL ROOM HAS BEEN RESERVED FOR YOUR STAY 


        THE FOLLOWING ROOMS ARE AVAILABLE FOR YOUR STAY

       ROOMS                   ROOMS AVAILABLE    
 1.HEDONISTIC SPACIOUS LANAI        12    
 2.QUEEN DUPLEX BEDROOM              8    
 3.CONTERMINOUS FAMILY SUITE        10    
 4.LUXURIOUS POSH CABANA            13    
 5.TWOFOLD PENTHOUSE                 6    
 6.GRAND TWIN PREMIER SUITE          9    
 7.KING DELUXE BEDROOM               7    

 # this is where i want the room availability to display after decreasing   the  number of rooms when one is reserved. but i want it to be in the above format and not haphazardly like this: 

 #1
 HEDONISTIC SPACIOUS LANAI 12
 QUEEN DUPLEX BEDROOM 8
 CONTERMINOUS FAMILY SUITE 10
 LUXURIOUS POSH CABANA 13
 TWOFOLD PENTHOUSE 6
 GRAND TWIN PREMIER SUITE 9
 KING DELUXE BEDROOM 6


 ÏD         :   1
 Name           :   qqqqqq
 Amount to be paid  :   770.0


 DO YOU WANT ANOTHER RESERVATION?? IF YES,PRESS 1:2

 >>> 

Can you help with the code so that it comes like

 #2.

 ROOMS                   ROOMS AVAILABLE    
 1.HEDONISTIC SPACIOUS LANAI        12    
 2.QUEEN DUPLEX BEDROOM              8    
 3.CONTERMINOUS FAMILY SUITE        10    
 4.LUXURIOUS POSH CABANA            13    
 5.TWOFOLD PENTHOUSE                 6    
 6.GRAND TWIN PREMIER SUITE          9    
 7.KING DELUXE BEDROOM               6(# the decrease can be shown and not  printing.)   

i tried many things, but i still it won't work.Could you take a look and tell me the problem??I spend too much time in this.(The indentation under if conditions are correct). Thankyou!

2条回答
时光不老,我们不散
2楼-- · 2019-09-05 17:14
class Hotel(object):

    def __init__(self):
        self.header = ['Id', 'Room Type', 'Price', 'Units']
        self.hotel_room = {'1': {'name': 'KING DELUXE BEDROOM', 'price': 700, 'units': 7},
                           '2': {'name': 'QUEEN DUPLEX BEDROOM', 'price': 800, 'units': 8},
                           '3': {'name': 'CONTERMINOUS FAMILY SUITE', 'price': 1000, 'units': 10},
                           '4': {'name': 'GRAND TWIN PREMIER SUITE', 'price': 900, 'units': 9},
                           '5': {'name': 'TWOFOLD PENTHOUSE', 'price': 600, 'units': 6},
                           '6': {'name': 'LUXURIOUS POSH CABANA', 'price': 1300, 'units': 13},
                           '7': {'name': 'HEDONISTIC SPACIOUS LANAI', 'price': 650, 'units': 12}}

    def get_current_status(self):
        row_format = '{:>5}  {:<30}{:>10}{:>10}'
        print row_format.format(*self.header)
        for key in self.hotel_room:
            room = self.hotel_room[key]
            name = room['name']
            price = room['price']
            units = room['units']
            print row_format.format(key, name, price, units)

    def make_reservation(self, room_type_id):
        if room_type_id in self.hotel_room:
            print "Making reservation of type : " + self.hotel_room[room_type_id]['name']
            self.hotel_room[room_type_id]['units'] -= 1
            print "Units left for : " + self.hotel_room[room_type_id]['name'] + " #" + \
                str(self.hotel_room[room_type_id]['units'])

hotel = Hotel()
hotel.get_current_status()
print "\nmaking a reservation for LUXURIOUS POSH CABANA\n"
hotel.make_reservation('6')
print "\ngetting current status\n"
hotel.get_current_status()

Output:

   Id  Room Type                          Price     Units
    1  KING DELUXE BEDROOM                  700         7
    3  CONTERMINOUS FAMILY SUITE           1000        10
    2  QUEEN DUPLEX BEDROOM                 800         8
    5  TWOFOLD PENTHOUSE                    600         6
    4  GRAND TWIN PREMIER SUITE             900         9
    7  HEDONISTIC SPACIOUS LANAI            650        12
    6  LUXURIOUS POSH CABANA               1300        13

making a reservation for LUXURIOUS POSH CABANA

Making reservation of type : LUXURIOUS POSH CABANA
Units left for : LUXURIOUS POSH CABANA #12

getting current status

   Id  Room Type                          Price     Units
    1  KING DELUXE BEDROOM                  700         7
    3  CONTERMINOUS FAMILY SUITE           1000        10
    2  QUEEN DUPLEX BEDROOM                 800         8
    5  TWOFOLD PENTHOUSE                    600         6
    4  GRAND TWIN PREMIER SUITE             900         9
    7  HEDONISTIC SPACIOUS LANAI            650        12
    6  LUXURIOUS POSH CABANA               1300        12
查看更多
3楼-- · 2019-09-05 17:20
print"ÏD\t\t\t:\t",self.customer_record['c_id']

This line contains a character that is not an ASCII character and you are using it as a string .I think that is causing the problem

查看更多
登录 后发表回答