网店订单管理系统
广州市第?中学高一(1)班B505小组
问题
网店接受了大量的订单,如何安排发货呢?实际上,网店在处理订单时,一般采取“先下单,先发货”的原则。因此,所有的订单将按照下单的时间顺序放进一个列表中,先放进去的先发货,所有订单排列在一起,像是一群人在排队。请编制程序实现以下功能:提供“添加订单”“发货”“查看订单列表”“退出”四个操作选项。当我们选择“1"后输入订单数据,程序将订单数据添加到订单数据表中;选择“2"后,程序将当前订单列表中最早进入的数据删除(表示已安排发货处理);选择“3”可以显示当前订单列表中所有的订单数据;选择"4"将结束运行。
抽象与建模
首先将操作类型读入,设操作的类型为type。
所有订单都有一个特点:先下单,先发货,也就是先进先出。这正好是一个队列的模型。type=1相当于往队尾加入一个元素,type=2相当于在队头删除一个元素,type=3相当于遍历队列依次访问每一个元素并输出,type=4则直接退出程序。
在这个问题中我们小组决定给每个订单设定4个属性:时间time,收货人receiver,物品item,备注other。我们使用了4个字符串列表来模拟队列:time_list,receiver_list,item_list,other_list。这四个列表的长度在每次操作前后始终相等,都等于当前订单的总数。time_list[0],receiver_list[0],item_list[0],other_list[0]分别对应队头订单的4个属性。往队尾添加一个订单,相当于在time_list的末尾加入time,receiver_list的末尾加入receiver,item_list的末尾加入item,other_list的末尾加入other。移除队头的订单,则相当于移除time_list[0],receiver_list[0],item_list[0]和other_list[0]。之后我们只需要使用循环访问这4个列表就能获取所有订单的信息。
为了避免非法操作和运行错误,当添加的订单不满足要求或订单列表为空时进行发货操作等情况出现时,应当拒绝执行并输出提示信息。
下附网店订单管理系统的算法流程图。
程序实现
1.数据结构设计
数据量可能较多,采用列表储存数据,模拟队列,每进行一次操作处理一次数据。
2.算法框架
采用模拟策略来设计算法,即根据现实事物的实际流程和要求逐步进行处理。
3.界面设计
由于命令行界面比较简陋,较难进行输入和观察,我们使用python自带的tkinter图形界面库编写了一套简单的GUI系统,使用起来更加方便简洁。
4.代码
#!/usr/bin/python3
import tkinter
import sys
import time
from tkinter import *
from tkinter.messagebox import *
time_list = []
receiver_list = []
item_list = []
other_list = []
top = tkinter.Tk()
top.title("网店订单管理系统")
top.geometry('+400+200')
receiver_label = Label(top,text="收货人").grid(row=0,column=0)
receiver_text = Text(top,width=20, height=1)
receiver_text.grid(row=0,column=1,rowspan=1,columnspan=2)
item_label = Label(top,text="物品").grid(row=1,column=0)
item_text = Text(top,width=20, height=1)
item_text.grid(row=1,column=1,rowspan=1,columnspan=2)
other_label = Label(top,text="备注").grid(row=2,column=0)
other_text = Text(top,width=20, height=1)
other_text.grid(row=2,column=1,rowspan=1,columnspan=2)
def getstring(time,receiver,item,other):
return "时间:"+time+"\n收件人:"+receiver+"\n物品:"+item+"\n备注:"+other
def info(content):
showinfo(title = '提示',message=content)
def add_new():
t=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
receiver=receiver_text.get(1.0,END).strip().replace("\n","")
item=item_text.get(1.0,END).strip().replace("\n","")
other=other_text.get(1.0,END).strip().replace("\n","")
if receiver == "" or item == "":
info("收件人和物品不能为空!")
else:
info("以下订单添加成功\n"+getstring(t,receiver,item,other))
time_list.append(t)
receiver_list.append(receiver)
item_list.append(item)
other_list.append(other)
def del_first():
if len(time_list)==0:
info("订单列表为空!")
else:
t=time_list[0]
receiver=receiver_list[0]
item=item_list[0]
other=other_list[0]
info("以下订单发货成功\n"+getstring(t,receiver,item,other))
del time_list[0]
del receiver_list[0]
del item_list[0]
del other_list[0]
def show_list():
if len(time_list)==0:
info("订单列表为空!")
return
res = ""
for i in range(len(time_list)):
res = res + getstring(time_list[i],receiver_list[i],item_list[i],other_list[i]) +"\n\n"
info("订单列表\n"+res)
push_button = Button(top,text="1.添加订单",width=10,command=add_new)
push_button.grid(row=3, column=0)
push_button = Button(top,text="2.发货首单",width=10,command=del_first)
push_button.grid(row=3, column=2)
push_button = Button(top,text="3.订单列表",width=10,command=show_list)
push_button.grid(row=4, column=0)
exit_button = Button(top,text="4.退出程序",width=10,command=sys.exit)
exit_button.grid(row=4, column=2)
top.mainloop()
程序运行截图