Python編寫車票訂購系統 Python實現快遞收費系統
本文實例為大傢分享瞭Python編寫車票訂購系統,Python實現快遞收費系統的具體代碼,供大傢參考,具體內容如下
要求:
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()
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- SQLite5-使用Python來讀寫數據庫
- python 操作sqlite數據庫的方法
- Python 操作SQLite數據庫詳情
- 利用Python連接Oracle數據庫的基本操作指南
- python處理SQLite數據庫的方法