某电影院的观众席共有A、B、C、D四个区,每个区有500个座位。请用python语言编写程序模拟售票(退票)过程。要求如下: (1)界面菜单中至少应包括“售票”、“退票”、“统计满座率”、“退出”4项; (2)录入电影信息(包括:编号、电影名、时间、票价和数量),并保存在movies.txt文件中; (3)查询电影信息; (4)修改电影信息; (5)退出程序。
时间: 2023-12-24 13:04:30 浏览: 159
好的,我可以为您提供一个简单的程序,但是由于界面设计需要使用GUI库,因此代码可能会比较长,请您耐心查看。以下是程序代码:
```python
import tkinter as tk
import tkinter.messagebox as messagebox
import os
#定义票务类
class Ticket:
def __init__(self, movie_id, movie_name, time, price, num):
self.movie_id = movie_id
self.movie_name = movie_name
self.time = time
self.price = price
self.num = num
#定义电影票务管理系统类
class MovieTicketSystem:
def __init__(self):
self.movies = []
self.load_movies()
#从文件中读取电影信息
def load_movies(self):
if not os.path.exists('movies.txt'):
return
with open('movies.txt', 'r') as f:
for line in f:
movie_info = line.strip().split(',')
movie = Ticket(movie_info[0], movie_info[1], movie_info[2], movie_info[3], int(movie_info[4]))
self.movies.append(movie)
#保存电影信息到文件
def save_movies(self):
with open('movies.txt', 'w') as f:
for movie in self.movies:
f.write('{},{},{},{},{}\n'.format(movie.movie_id, movie.movie_name, movie.time, movie.price, movie.num))
#添加电影
def add_movie(self, movie):
self.movies.append(movie)
self.save_movies()
#查询电影
def query_movie(self, movie_id):
for movie in self.movies:
if movie.movie_id == movie_id:
return movie
return None
#修改电影信息
def modify_movie(self, movie_id, movie_name, time, price, num):
for movie in self.movies:
if movie.movie_id == movie_id:
movie.movie_name = movie_name
movie.time = time
movie.price = price
movie.num = num
self.save_movies()
return True
return False
#删除电影
def delete_movie(self, movie_id):
for movie in self.movies:
if movie.movie_id == movie_id:
self.movies.remove(movie)
self.save_movies()
return True
return False
#售票
def sell_ticket(self, movie_id, num):
for movie in self.movies:
if movie.movie_id == movie_id:
if movie.num >= num:
movie.num -= num
self.save_movies()
return True
else:
return False
return False
#退票
def refund_ticket(self, movie_id, num):
for movie in self.movies:
if movie.movie_id == movie_id:
movie.num += num
self.save_movies()
return True
return False
#统计满座率
def get_sold_rate(self):
total_num = 0
sold_num = 0
for movie in self.movies:
total_num += 500
sold_num += (500 - movie.num)
rate = sold_num / total_num
return rate
#定义电影票务管理系统界面类
class MovieTicketSystemGUI:
def __init__(self):
self.system = MovieTicketSystem()
self.window = tk.Tk()
self.window.title('电影票务管理系统')
self.window.geometry('500x300')
self.init_widgets()
#初始化界面控件
def init_widgets(self):
menu_bar = tk.Menu(self.window)
self.window.config(menu=menu_bar)
#添加“电影”菜单
movie_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label='电影', menu=movie_menu)
movie_menu.add_command(label='添加电影', command=self.add_movie)
movie_menu.add_command(label='查询电影', command=self.query_movie)
movie_menu.add_command(label='修改电影', command=self.modify_movie)
movie_menu.add_command(label='删除电影', command=self.delete_movie)
#添加“票务”菜单
ticket_menu = tk.Menu(menu_bar, tearoff=False)
menu_bar.add_cascade(label='票务', menu=ticket_menu)
ticket_menu.add_command(label='售票', command=self.sell_ticket)
ticket_menu.add_command(label='退票', command=self.refund_ticket)
ticket_menu.add_command(label='统计满座率', command=self.get_sold_rate)
#添加“退出”菜单
menu_bar.add_command(label='退出', command=self.window.quit)
#添加状态栏
self.status_bar = tk.Label(self.window, text='欢迎使用电影票务管理系统!')
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
#添加电影
def add_movie(self):
add_window = tk.Toplevel(self.window)
add_window.title('添加电影')
add_window.geometry('300x200')
tk.Label(add_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(add_window)
movie_id_entry.grid(row=0, column=1)
tk.Label(add_window, text='电影名称:').grid(row=1, column=0)
movie_name_entry = tk.Entry(add_window)
movie_name_entry.grid(row=1, column=1)
tk.Label(add_window, text='放映时间:').grid(row=2, column=0)
time_entry = tk.Entry(add_window)
time_entry.grid(row=2, column=1)
tk.Label(add_window, text='票价:').grid(row=3, column=0)
price_entry = tk.Entry(add_window)
price_entry.grid(row=3, column=1)
tk.Label(add_window, text='票数:').grid(row=4, column=0)
num_entry = tk.Entry(add_window)
num_entry.grid(row=4, column=1)
def add():
movie_id = movie_id_entry.get()
movie_name = movie_name_entry.get()
time = time_entry.get()
price = price_entry.get()
num = num_entry.get()
movie = Ticket(movie_id, movie_name, time, price, int(num))
self.system.add_movie(movie)
self.status_bar.config(text='添加电影成功!')
add_window.destroy()
tk.Button(add_window, text='添加', command=add).grid(row=5, column=0, columnspan=2)
#查询电影
def query_movie(self):
query_window = tk.Toplevel(self.window)
query_window.title('查询电影')
query_window.geometry('300x100')
tk.Label(query_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(query_window)
movie_id_entry.grid(row=0, column=1)
def query():
movie_id = movie_id_entry.get()
movie = self.system.query_movie(movie_id)
if movie:
messagebox.showinfo('查询结果', '电影编号:{}\n电影名称:{}\n放映时间:{}\n票价:{}\n票数:{}'.format(movie.movie_id, movie.movie_name, movie.time, movie.price, movie.num))
else:
messagebox.showerror('查询结果', '未找到电影!')
query_window.destroy()
tk.Button(query_window, text='查询', command=query).grid(row=1, column=0, columnspan=2)
#修改电影信息
def modify_movie(self):
modify_window = tk.Toplevel(self.window)
modify_window.title('修改电影')
modify_window.geometry('300x200')
tk.Label(modify_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(modify_window)
movie_id_entry.grid(row=0, column=1)
tk.Label(modify_window, text='电影名称:').grid(row=1, column=0)
movie_name_entry = tk.Entry(modify_window)
movie_name_entry.grid(row=1, column=1)
tk.Label(modify_window, text='放映时间:').grid(row=2, column=0)
time_entry = tk.Entry(modify_window)
time_entry.grid(row=2, column=1)
tk.Label(modify_window, text='票价:').grid(row=3, column=0)
price_entry = tk.Entry(modify_window)
price_entry.grid(row=3, column=1)
tk.Label(modify_window, text='票数:').grid(row=4, column=0)
num_entry = tk.Entry(modify_window)
num_entry.grid(row=4, column=1)
def modify():
movie_id = movie_id_entry.get()
movie_name = movie_name_entry.get()
time = time_entry.get()
price = price_entry.get()
num = num_entry.get()
if self.system.modify_movie(movie_id, movie_name, time, price, int(num)):
self.status_bar.config(text='修改电影信息成功!')
else:
self.status_bar.config(text='修改电影信息失败:未找到电影编号{}!'.format(movie_id))
modify_window.destroy()
tk.Button(modify_window, text='修改', command=modify).grid(row=5, column=0, columnspan=2)
#删除电影
def delete_movie(self):
delete_window = tk.Toplevel(self.window)
delete_window.title('删除电影')
delete_window.geometry('300x100')
tk.Label(delete_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(delete_window)
movie_id_entry.grid(row=0, column=1)
def delete():
movie_id = movie_id_entry.get()
if self.system.delete_movie(movie_id):
self.status_bar.config(text='删除电影成功!')
else:
self.status_bar.config(text='删除电影失败:未找到电影编号{}!'.format(movie_id))
delete_window.destroy()
tk.Button(delete_window, text='删除', command=delete).grid(row=1, column=0, columnspan=2)
#售票
def sell_ticket(self):
sell_window = tk.Toplevel(self.window)
sell_window.title('售票')
sell_window.geometry('300x100')
tk.Label(sell_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(sell_window)
movie_id_entry.grid(row=0, column=1)
tk.Label(sell_window, text='售出数量:').grid(row=1, column=0)
num_entry = tk.Entry(sell_window)
num_entry.grid(row=1, column=1)
def sell():
movie_id = movie_id_entry.get()
num = int(num_entry.get())
if self.system.sell_ticket(movie_id, num):
self.status_bar.config(text='售票成功!')
else:
self.status_bar.config(text='售票失败:电影编号{}票数不足!'.format(movie_id))
sell_window.destroy()
tk.Button(sell_window, text='售票', command=sell).grid(row=2, column=0, columnspan=2)
#退票
def refund_ticket(self):
refund_window = tk.Toplevel(self.window)
refund_window.title('退票')
refund_window.geometry('300x100')
tk.Label(refund_window, text='电影编号:').grid(row=0, column=0)
movie_id_entry = tk.Entry(refund_window)
movie_id_entry.grid(row=0, column=1)
tk.Label(refund_window, text='退票数量:').grid(row=1, column=0)
num_entry = tk.Entry(refund_window)
num_entry.grid(row=1, column=1)
def refund():
movie_id = movie_id_entry.get()
num = int(num_entry.get())
if self.system.refund_ticket(movie_id, num):
self.status_bar.config(text='退票成功!')
else:
self.status_bar.config(text='退票失败:未找到电影编号{}!'.format(movie_id))
refund_window.destroy()
tk.Button(refund_window, text='退票', command=refund).grid(row=2, column=0, columnspan=2)
#统计满座率
def get_sold_rate(self):
rate = self.system.get_sold_rate()
messagebox.showinfo('统计结果', '满座率:{}%'.format(rate * 100))
#运行界面程序
def run(self):
self.window.mainloop()
if __name__ == '__main__':
system_gui = MovieTicketSystemGUI()
system_gui.run()
```
注:本程序将电影票务信息保存在movies.txt文件中,因此需要在程序运行前创建该文件。
阅读全文