电影票预定系统-简易版

167 阅读3分钟

一、效果演示

1.根据影片列表选择电影

image.png

2.根据选择的序号预定电影座位

image.png

3.根据选择座位预定的方式查询电影预定状态并选择

image.png

4.根据选择的行列信息预定座位

image.png

二、代码演示

1.入口类-main.py

class Controller:
    def __init__(self, films):
        self.films = films
        self.welcome()

        # 用户选择想观看的电影
        self.choice = self.choose_film()

        # 根据用户选择,执行不同流程
        if self.choice != 'x':
            # 为指定场次预订座位
            self.choose_seat()

        self.bye()

    def choose_film(self):
        """
        根据所有电影信息进行选择
        """
        selector = FilmSelector(self.films)
        selector.display_options()
        return selector.get_choice()

    def choose_seat(self):
        """
        根据用户选择电影取出该电影所有信息
        """
        film = self.films[int(self.choice) - 1]
        name = film['name']
        seats_list = film['seats']
        symbol = film['symbol']

        print('正在为您预订电影《{}》的座位...'.format(name))
        time.sleep(0.7)
        print(symbol)
        time.sleep(0.7)

        print('支持的座位预订方式如下:')
        time.sleep(0.7)
        print('|====================================================================================|')
        print("1:- 指定行列号预定座位")
        print("2:- 给我预订一个最靠前的座位")
        print('|====================================================================================|\n')
        time.sleep(0.7)

        method = input('请选择座位预订方式:')
        valid_method = ['1', '2']
        while method not in valid_method:
            method = input('没有按照要求输入哦,请重新输入:')

        booking = SeatBooking()
        booking.check_bookings(seats_list)

        if method == '1':
            booking.book_seat(seats_list)
        else:
            booking.book_seat_at_front(seats_list)

    @staticmethod
    def welcome():
        print('|====================================================================================|')
        print('|                               欢迎来到西湖影院                                 |')
        print('|====================================================================================|\n')
        time.sleep(0.7)

    @staticmethod
    def bye():
        print('')
        time.sleep(0.7)
        print('|====================================================================================|')
        print('+                        已经退出系统,下次见!👋                                    +')
        print('|====================================================================================|')


if __name__ == '__main__':
    Controller(infos)

2. 电影信息文件-infos.py

infos = [
    {
        'name': '八角笼中',
        'symbol': '''
+==================== 八角笼中 =====================+
  ▄▄▄▄▄▪   ▄▄▄▄▄  ▄▄▄·   ▐ ▄ ▪      ▄▄· 
  •██   ██  •██   ▐█ ▀█  •█▌▐█  ██  ▐█ ▌▪
  ▐█.▪ ▐█·  ▐█. ▪▄█▀▀█  ▐█▐▐▌  ▐█· ██ ▄▄
  ▐█▌ ·▐█▌  ▐█▌· ▐█ ▪▐▌ ██▐█▌  ▐█▌ ▐███▌
  ▀▀▀  ▀▀▀  ▀▀▀   ▀  ▀  ▀▀ █  ▪▀▀▀ ·▀▀▀ 
+===================== In the octagonal cage =====================+
''',
        'seats': [['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '●', '○', '○', '●'],
                  ['○', '○', '●', '○', '●', '○', '○', '○'],
                  ['○', '○', '●', '○', '○', '○', '○', '●'],
                  ['○', '○', '●', '○', '○', '○', '●', '○'],
                  ['●', '○', '○', '○', '●', '●', '●', '●']]
    },
    {
        'name': '速度与激情12',
        'symbol': '''
+====================== 速度与激情12 =======================+
  ,---.    .--.  ,-..-. .-.           .--.  .-. .-. 
  | .-.\  / /\ \ |(||  \| | |\    /| / /\ \ |  \| | 
  | `-'/ / /__\ \(_)|   | | |(\  / |/ /__\ \|   | | 
  |   (  |  __  || || |\  | (_)\/  ||  __  || |\  | 
  | |\ \ | |  |)|| || | |)| | \  / || |  |)|| | |)| 
  |_| \)\|_|  (_)`-'/(  (_) | |\/| ||_|  (_)/(  (_) 
      (__)         (__)     '-'  '-'       (__)     
+===================== Fast & Furious 12 ====================+
''',
        'seats': [['○', '○', '○', '○', '●', '○', '○', '●'],
                  ['○', '○', '○', '●', '●', '○', '○', '○'],
                  ['○', '●', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '●', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '○']]
    },
    {
        'name': '僵尸世界大战2',
        'symbol': '''
+======================= 僵尸世界大战2 =======================+
  ▄█▄    ██   █▄▄▄▄ █▀▄▀█ ▄███▄      ▄   
  █▀ ▀▄  █ █  █  ▄▀ █ █ █ █▀   ▀      █  
  █   ▀  █▄▄█ █▀▀▌  █ ▄ █ ██▄▄    ██   █ 
  █▄  ▄▀ █  █ █  █  █   █ █▄   ▄▀ █ █  █ 
  ▀███▀     █   █      █  ▀███▀   █  █ █ 
            █   ▀      ▀           █   ██ 
          ▀                              
+====================== World War Z 2 =====================+
''',
        'seats': [['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '●', '●', '○', '○', '●', '●'],
                  ['○', '○', '○', '○', '○', '○', '●', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '●', '○', '○', '○', '●']]
    },
    {
        'name': '机器人总动员',
        'symbol': '''
+==================== 机器人总动员 ===================+
   (`\ .-') /`  ('-.                           ('-.   
    `.( OO ),' ( OO ).-.                     _(  OO)  
 ,--./  .--.   / . --. / ,--.      ,--.     (,------. 
 |      |  |   | \-.  \  |  |.-')  |  |.-')  |  .---' 
 |  |   |  |,.-'-'  |  | |  | OO ) |  | OO ) |  |     
 |  |.'.|  |_)\| |_.'  | |  |`-' | |  |`-' |(|  '--.  
 |         |   |  .-.  |(|  '---.'(|  '---.' |  .--'  
 |   ,'.   |   |  | |  | |      |  |      |  |  `---. 
 '--'   '--'   `--' `--' `------'  `------'  `------'  
+====================== WALL·E =====================+
''',
        'seats': [['●', '○', '○', '○', '○', '○', '○', '○'],
                  ['●', '○', '○', '○', '○', '○', '○', '●'],
                  ['○', '○', '●', '○', '●', '○', '●', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '●'],
                  ['○', '○', '○', '○', '●', '○', '○', '○'],
                  ['●', '●', '○', '○', '○', '●', '○', '○']]
    },
    {
        'name': '黑客帝国8',
        'symbol': '''
+===================== 黑客帝国8 =====================+
   ________            __  ___      __       _     
  /_  __/ /_  ___     /  |/  /___ _/ /______(_)  __
   / / / __ \/ _ \   / /|_/ / __ `/ __/ ___/ / |/_/
  / / / / / /  __/  / /  / / /_/ / /_/ /  / />  <  
 /_/ /_/ /_/\___/  /_/  /_/\__,_/\__/_/  /_/_/|_|  
+==================== The Matrix 8 ===================+
''',
        'seats': [['○', '●', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '●', '●', '○', '○', '●'],
                  ['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '○'],
                  ['○', '○', '○', '○', '○', '○', '○', '●'],
                  ['○', '○', '●', '○', '○', '○', '○', '○']]
    },
]

2. 电影选择类-film_selector.py

class FilmSelector:
    def __init__(self, infos):
        self.__films = infos

    def display_options(self):
        """
        展示所有电影名称
        """
        print("今日影院排片列表:")
        print('|====================================================================================|')

        for i in range(len(self.__films)):
            print('{}: - {}'.format(i + 1, self.__films[i]['name']))
            time.sleep(0.2)

        print('x: - 退出')
        print('|====================================================================================|')
        time.sleep(0.7)

    def get_choice(self):
        """
        获取用户选择
        """
        valid_choice = [str(i + 1) for i in range(len(self.__films))]
        valid_choice.append('x')

        choice = input('你的选择是?')
        while choice not in valid_choice:
            choice = input('没有按照要求输入哦,请重新输入')
        return choice

3. 座位预定类-seat_book.py

class SeatBooking:
    @classmethod
    def check_bookings(cls, seats):
        """
        显示所有座位的预定信息
        """
        print("正在为您查询该场次电影的预订状态...")
        time.sleep(0.7)
        print('从上到下为 1~6 排,从左至右为 1~8 座')
        print("======================")
        for row in seats:
            time.sleep(0.1)
            print('  '.join(row))
        print("======================")
        time.sleep(0.7)

    @classmethod
    def __get_row(cls):
        input_row = input("预订第几排的座位呢?请输入 1~6 之间的数字")
        valid_row = [str(i + 1) for i in range(6)]

        while input_row not in valid_row:
            input_row = input('没有按要求输入哦,请输入 1~6 之间的数字')

        row = int(input_row) - 1
        return row

    @classmethod
    def __get_col(cls):
        input_column = input('预订这一排的第几座呢?请输入 1~8 之间的数字')
        valid_column = [str(i + 1) for i in range(8)]

        while input_column not in valid_column:
            input_column = input('没有按要求输入哦,请输入 1~8 之间的数字')

        column = int(input_column) - 1
        return column

    @classmethod
    def book_seat(cls, seats):
        """
        预定座位方式一:预定执行的座位
        """
        while True:
            row = cls.__get_row()
            column = cls.__get_col()

            if seats[row][column] == '○':
                print("正在为您预订指定座位...")
                time.sleep(0.7)
                seats[row][column] = '●'
                print("预订成功!座位号:{}排{}座".format(row + 1, column + 1))
                break
            else:
                print("这个座位已经被预订了哦,试试别的吧")
                time.sleep(0.7)

    @classmethod
    def book_seat_at_front(cls, seats):
        """
        预定座位方式二:预定最靠前的座位
        """
        print("正在为您预订最靠前的座位...")
        time.sleep(0.7)
        for row in range(6):
            for column in range(8):
                if seats[row][column] == '○':
                    seats[row][column] = '●'  # 预订该座位
                    print("预订成功!座位号:{}排{}座".format(row + 1, column + 1))
                    return
        print("非常抱歉🥺,所有座位都被订满了,无法为您保留座位")