Python车票订购和快递收费系统
M大王派我来巡山 人气:0要求:
1.上网查询郑州到北京,西安,石家庄,济南,太原,武汉的距离及票价,用数据库保存车次信息
2.要求输入目的地,能够查询到里程和票价
3.用数据库存储每一次售票记录,包括售票流水号,起点站,终点站,里程,金额等数据,并统计所有存储记录的总售票金额及各站的旅客流量(进站流量+出站流量)
4.能够打印票据信息,包括订票人信息,票价,票据号,车次等信息
import sqlite3 import os import time def createDB(): """ 创建并初始化数据库 """ # 原始数据 info = [("G1564", "郑州", "北京", "309", "693"), ("G802", "郑州", "北京", "315", "693"), ("G564", "郑州", "北京", "326.5", "693"), ("G2025", "郑州", "西安", "239", "479.3"), ("D311", "郑州", "西安", "159", "479.3"), ("G857", "郑州", "西安", "229", "479.3"), ("G1286", "郑州", "石家庄", "189.5", "417.9"), ("G2070", "郑州", "石家庄", "196", "417.9"), ("G430", "郑州", "石家庄", "208", "417.9"), ("G2074", "郑州", "济南", "303", "446"), ("G258", "郑州", "济南", "312.5", "446"), ("G1844", "郑州", "济南", "298.5", "446"), ("D290", "郑州", "太原", "189", "432.7"), ("D3348", "郑州", "太原", "153", "432.7"), ("D2782", "郑州", "太原", "171", "432.7"), ("G1991", "郑州", "武汉", "244", "509.8"), ("G3203", "郑州", "武汉", "314", "509.8"), ("G8171", "郑州", "武汉", "248", "509.8")] con = sqlite3.connect("DB.db")#连接到数据库 cur = con.cursor() # 创建游标对象 # 创建字段,num是车次,station是始发站,destination是目的地,price票价,distance是距离 cur.execute("create table test(num primary key, station, destination, price, distance)") for i in info: cur.execute("insert into test values(?,?,?,?,?)", i) con.commit() cur.close() con.close() def CreateSaleDB(): """ 创建销售数据库 """ con = sqlite3.connect("sale.db") cur = con.cursor() # 字段依次为流水号,起点站,终点站,金额,里程 cur.execute("create table sheet(num,station,destination,price,distance)") con.commit() cur.close() con.close() # 根据目的地查询车票信息 def Query(dest): con = sqlite3.connect("DB.db") cur = con.cursor() item = cur.execute("select * from test where destination=?", (dest,)) print("--------------------------查询到的信息--------------------------") for i in item.fetchall(): print("车次:%s %s==>%s 票价:%s元 距离:%skm" % (i[0], i[1], i[2], i[3], i[4])) print("--------------------------------------------------------------") cur.close() con.close() def SaveSaleRecord(num, SerialNumber): con = sqlite3.connect("DB.db") cur = con.cursor() item = cur.execute("select * from test where num=?", (num,)) data = [] data.append(SerialNumber) for i in item.fetchall(): data.append(i[1]) data.append(i[2]) data.append(i[3]) data.append(i[4]) cur.close() con.close() data = tuple(data) con = sqlite3.connect("sale.db") cur = con.cursor() cur.execute("insert into sheet values(?,?,?,?,?)", data) con.commit() cur.close() con.close() return data def Statistics(): con = sqlite3.connect("sale.db") cur = con.cursor() item = cur.execute("select * from sheet") total_sale = 0 # 进出站客流量统计列表,进站/出站 zhengzhou = [0, 0] beijing = [0, 0] xian = [0, 0] shijiazhuang = [0, 0] jinan = [0, 0] taiyuan = [0, 0] wuhan = [0, 0] for i in item.fetchall(): temp = float(i[3]) total_sale = temp + total_sale # 流水号,起点站,终点站,金额,里程 # 进站统计 if i[1] == '郑州': zhengzhou[0] += 1 elif i[1] == '北京': beijing[0] += 1 elif i[1] == '西安': xian[0] += 1 elif i[1] == '济南': jinan[0] += 1 elif i[1] == '石家庄': shijiazhuang[0] += 1 elif i[1] == '武汉': wuhan[0] += 1 elif i[1] == '太原': taiyuan[0] += 1 # 出站统计 if i[2] == '郑州': zhengzhou[1] += 1 elif i[2] == '北京': beijing[1] += 1 elif i[2] == '西安': xian[1] += 1 elif i[2] == '济南': jinan[1] += 1 elif i[2] == '石家庄': shijiazhuang[1] += 1 elif i[2] == '武汉': wuhan[1] += 1 elif i[2] == '太原': taiyuan[1] += 1 print("--------进出站统计--------") print(" 进站 出站") print("郑州 %d %d" % (zhengzhou[0], zhengzhou[1])) print("北京 %d %d" % (beijing[0], beijing[1])) print("石家庄 %d %d" % (shijiazhuang[0], shijiazhuang[1])) print("西安 %d %d" % (xian[0], xian[1])) print("太原 %d %d" % (taiyuan[0], taiyuan[1])) print("济南 %d %d" % (jinan[0], jinan[1])) print("武汉 %d %d" % (wuhan[0], wuhan[1])) print("------------------------") print("总销售额:%.2f元" % total_sale) cur.close() con.close() def PrintData(data, num): """ 打印票据信息 """ print("--------票据信息--------") print("车次:%s" %num) print("票据号:%s" %data[0]) print("票价:%s元" %data[3]) print("----------------------") if __name__ == '__main__': # 创建数据库DB.db if os.path.exists("DB.db"): pass else: # 创建数据库并初始化 createDB() # 创建销售数据库,存储销售记录 if os.path.exists("sale.db"): pass else: CreateSaleDB() flag = 1 while(flag): print("********欢迎使用车票订购系统*******") print('请选择您的进一步操作') print('1.查询车次信息') print('2.售票记录和票据信息') print('3.各站旅客流量统计') print('4.退出系统') a = int(input("请输入选项:")) if a == 1: dest = input("请输入目的地:") Query(dest) # num, SerialNumber = Query(dest) # num是购买的车次,SerialNumber是流水号 elif a == 2: num = input("请输入要购买的车次:") SerialNumber = time.strftime("%Y%m%d%H%M%S", time.localtime()) # 作为流水号 data = SaveSaleRecord(num, SerialNumber) # 保存售票记录,返回购票信息以供打印票据用 PrintData(data, num) # 打印票据信息 elif a == 3: Statistics() # 统计 elif a == 4: break else: print('输入错误,请重新输入!!!')
程序运行结果如图所示:
还有一个是快递收费系统,要求啥的找不到了,只剩代码
import sqlite3 #定义区域快递费 q=[10,10,15,15,15] x=[3,4,5,6.5,10] #打开数据库 def opendb(): #创建全局变量方便其他函数调用 global conn global cur conn=sqlite3.connect('customer.db') cur=conn.cursor() cur.execute('''create table if not exists customers(num,name,distance)''') #添加数据 p0 = [('0', "上海", "同城"), ('1', "江苏", "临近两省"), ('1', "浙江", "临近两省"), ('2', "北京", "1500公里(含)以内"), ('2', "天津", "1500公里(含)以内"), ('2', "河北", "1500公里(含)以内")] p1 = [('2', "河南", "1500公里(含)以内"), ('2', "安徽", "1500公里(含)以内"), ('2', "陕西", "1500公里(含)以内"), ('2', "湖北", "1500公里(含)以内"), ('2', "江西", "1500公里(含)以内"), ('2', "湖南", "1500公里(含)以内")] p2 = [('2', "福建", "1500公里(含)以内"), ('2', "广东", "1500公里(含)以内"), ('2', "山西", "1500公里(含)以内"), ('3', "吉林", "1500-2500公里"), ('3', "甘肃", "1500-2500公里"), ('3', "四川", "1500-2500公里")] p3 = [('3', "重庆", "1500-2500公里"), ('3', "青海", "1500-2500公里"), ('3', "广西", "1500-2500公里"), ('3', "云南", "1500-2500公里"), ('3', "海南", "1500-2500公里"), ('3', "内蒙古", "1500-2500公里")] p4 = [('3', "黑龙江", "1500-2500公里"), ('3', "贵州", "1500-2500公里"), ('3', "辽宁", "1500-2500公里"), ('4', "新疆", "2500公里以上"), ('4', "西藏", "2500公里以上")] cur.executemany('''insert into customers values(?,?,?)''',p0) cur.executemany('''insert into customers values(?,?,?)''',p1) cur.executemany('''insert into customers values(?,?,?)''',p2) cur.executemany('''insert into customers values(?,?,?)''',p3) cur.executemany('''insert into customers values(?,?,?)''',p4) #输出分隔线 def interval(): print('**************************************') #计算快递费 def Calculation(): num=int(input('请输入区域编码(0-4):')) if num<=4: weight = float(input('请输入快递重量(公斤):')) #不足1公斤按1公斤计算 if weight!=int(weight): weight=int(weight)+1 s=q[num]+(weight-1)*x[num] print('所需快递费为:%.2f'%s) interval() else: print('区域编号错误!请重新输入!') Calculation() #修改区域快递费 def modify(): #输出目前区域快递费方便修改 print("目前区域快递费为:") Inquire2() num=int(input('请输入要修改的区域编码(0-4):')) if num<=4: q[num]=float(input('请输入修改后的起重费:')) x[num]=float(input('请输入修改后的续重费:')) print('修改成功!') #修改完成后输出修改后的区域快递费 print('修改后的地区快递费为:') Inquire2() interval() else: print('区域编号错误!请重新输入!') modify() #查询地区编码 def Inquire1(): global cur cur.execute('''select * from customers order by num asc''') print('地区编码\t\t地区名称\t\t地区距离') for i in cur: print('%-5s\t\t%-5s\t\t%-5s'%(i[0],i[1],i[2])) interval() #查询地区快递费 def Inquire2(): print('地区编码\t\t起重费(元)\t\t续费(元/公斤)') for i in range(5): print(i,"\t\t\t%.2f\t\t\t%.2f"%(q[i],x[i])) #删除数据 def delete(): a=input('请输入要删除的地区名称:') global cur cur.execute('''select * from customers where name="%s"'''%a) print('地区编码\t\t地区名称\t\t地区距离') for i in cur: print('%-5s\t\t%-5s\t\t%-5s'%(i[0],i[1],i[2])) print('1.确认删除') print('2.取消') b=int(input()) if b==1: cur.execute('''delete from customers where name="%s"'''%a) print('删除成功!') else: print('取消删除!') #添加数据 def add(): global cur a = input('请输入要添加的地区编号:') b = input('请输入要添加的地区名称:') c = input('请输入要添加的地区距离:') s=[(a,b,c)] cur.executemany('''insert into customers values(?,?,?)''',s) print('添加成功!') opendb() print('**********欢迎使用快递费计算系统**********') while 1>0: print('请选择您的进一步操作') print('1:添加数据') print('2:计算快递费') print('3:查询地区编码') print('4:查询区域快递费') print('5:修改区域快递费') print('6:删除数据') print('7:退出系统') a=int(input()) if a==1: add() elif a==2: Calculation() elif a==3: Inquire1() elif a==4: Inquire2() interval() elif a==5: modify() elif a==6: delete() elif a==7: break else: print('输入错误!请重新输入!') interval() conn.commit() cur.close() conn.close()
加载全部内容