#-*- coding:utf-8 -*-
from moudle import *
import socketserver
import json
import os
import time
import datetime
carddict={}
def main(request):
conn=request
conn.sendall(by('欢迎光临大龙FTP!'))
return conn def login(conn):
card=int(st(conn.recv(1024)))
global carddict
if not os.path.isfile('DB\\cardinfo\\%s'%card):
conn.sendall(by('\033[031m该用户不存在\033[0m'))
else:
conn.sendall(by('\033[032m存在!\033[0m'))
password=st(conn.recv(1024))
print(password)
print()
carddict=json.load(open('DB\\cardinfo\\%s'%card))
if password==carddict['password']:
conn.sendall(by('\033[032m登陆成功!\033[0m'))
return True
else:
conn.sendall(by('\033[031m密码错误,登陆失败!\033[0m'))
return False def register(conn):
user=st(conn.recv(1024))
# if os.path.isfile('DB\\cardinfo\\%s'%card):
# conn.sendall(by('\033[031m注册失败,该用户已存在\033[0m'))
# else:
conn.sendall(by('\033[032m注册成功!\033[0m'))
password=st(conn.recv(1024))
card=creat[-1]+1
creat[0]=creat[0]+1
json.dump(creat,open('DB\\creat_card.json','w'))
conn.sendall(by(str(card)))
print(card)
carddict={'user':user,'password':password,'card':card,'credit':15000,'balance':15000,
'saving':0,'status':0,'debt':[[],[],[]]}
cardinfo(carddict)
os.mkdir('log\\%s'%card)
def giro(conn):
conn.sendall(by('粘包'))
tocard=int(st(conn.recv(1024)))
tocarddict=json.load(open('DB\\cardinfo\\%s' % tocard, 'r'))
conn.sendall(by(tocarddict['user']))
num= int(st(conn.recv(1024)))
result=pay(num)
if int(result):
userlog(carddict, '转账 -%s 到: %s' %(num,tocard))
repay(tocarddict, num,conn)
userlog(tocarddict, '转账 +%s 源: %s' % (num, carddict['card']))
conn.sendall(by(result)) def atm(conn):
num = int(st(conn.recv(1024)))
print('atm')
result = pay(num,1.05)
conn.sendall(by(result))
print(result)
if int(result):
userlog(carddict, '提现 -%s' %(num))
query(conn) def repayment(conn):
num = float(st(conn.recv(1024)))
repay(carddict,num,conn)
userlog(carddict, '还款 +%s' % (num)) def yue(debt,num):
if len(debt[0])==0: # 没有欠款直接返回还款金额
print('len ',num,debt)
return num,debt
# result=num-debt[0][0]
else: # 如果有欠款
result = num - debt[2][0] # 还款后 计算第一个月欠款
if not result<0: # 如果还款金额大于第一个月的欠款
num -= debt[2][0]
del debt[0][0]
del debt[1][0]
del debt[2][0]
print('result>>0 ', num,debt)
return yue(debt,num)
else:
debt[2][0]=debt[2][0]-num
print('result<<0 ', num,debt)
return 0,debt def repay(carddict,num,conn):
debt=carddict['debt']
now_owe=carddict['credit']-carddict['balance']
# if len(debt[0]) !=0:
num,debt=yue(debt,num)
print('num',num,debt)
carddict['debt']=debt
if num >0:
if now_owe > 0:
left = num - now_owe
if left > 0:
print(111)
carddict['balance'] = carddict['credit']
carddict['saving'] += left
else:
print(222)
carddict['balance'] += num
else:
print(333)
carddict['saving'] += num
# else:
# debt[4] -=num
print(carddict)
cardinfo(carddict)
query(conn) def query(conn):
conn.sendall(by('%d|%d'%(carddict['saving'],carddict['balance']))) def pay(num,add=1):
if carddict['saving']>= num:
carddict['saving']-=num
cardinfo(carddict)
print('pay success')
return ''
else:
left=(num-carddict['saving'])*add
if left <= carddict['balance']:
carddict['saving']=0
carddict['balance']-=left
cardinfo(carddict)
print('pay success')
return ''
else:
print('pay fail')
return '' def cardinfo(carddict):
card=carddict['card']
json.dump(carddict,open('DB\\cardinfo\\%s'%card,'w'))
# def owed(carddict):
# debt=carddict['debt'] class Myserver(socketserver.BaseRequestHandler):
def handle(self):
conn=main(self.request)
choose={'':login,'':register,}
choose2 = {'': query, '': giro, '': atm, '': repayment,}
# logchoose={'2': 'zhuanzhang', '3': 'tixian', '4': 'huankuan'}
while True:
# opt = st(conn.recv(1024))
# login(self.request)
opt=st(conn.recv(1024))
if opt in choose.keys():
result = choose[opt](self.request)
if result:
while True:
opt2 = st(conn.recv(1024))
print(opt2)
if opt2 in choose2.keys():
choose2[opt2](self.request)
elif opt2=='':
break
else:pr('输入无效!','r')
elif opt=='':break elif opt in choose.keys():
result=choose[opt]() if __name__=='__main__':
Server=socketserver.ThreadingTCPServer(('127.0.0.1',8889),Myserver)
Server.serve_forever()

银行程序

#-*- coding:utf-8 -*-
import socket
from moudle import *
def main(address,port):
obj=socket.socket()
obj.connect((address,port))
print(st(obj.recv(1024)))
return obj def login():
user=input('请输入卡号:')
password=input('请输入密码:')
obj.sendall(by(str(user)))
have=st(obj.recv(1024))
if '不' in have:
print(have)
return False
else:
obj.sendall(by(password))
result=st(obj.recv(1024))
print(result)
if '成功' in result:return True
else:return False def register():
user = input('请输入姓名:')
password = input('请输入密码:')
obj.sendall(by(user))
result=st(obj.recv(1024))
print(result)
# if '成功' in result:
obj.sendall(by(password))
card=st(obj.recv(1024))
print('您的卡号为%s,请牢记!'%card)
def shopgiro(tocard):
obj.sendall(by(''))
result = login()
if result:
obj.sendall(by(''))
giro(tocard)
def giro(tocard=None):
if tocard==None:
tocard=input('输入对方卡号:')
# else:
# result=login()
# if result:
obj.recv(1024)#'粘包'
obj.sendall(by(str(tocard)))
toname = st(obj.recv(1024))
print('请核对信息: 卡号:%s 姓名:%s' % (tocard,toname))
num = input('转账金额:')
obj.sendall(by(num))
query()
result=int(st(obj.recv(1024)))
if result:pr('支付成功!%s'%num)
else:pr('支付失败!','r') def atm():
num = input('取款金额:')
obj.sendall(by(num))
result = int(st(obj.recv(1024)))
if result:
pr('提现成功!%s' % num)
else:
pr('提现失败!', 'r')
query() def repay():
num = input('还款金额:')
obj.sendall(by(num))
pr('还款成功!%s' % num)
query() def query():
result=st(obj.recv(1024)).split('|')
print('您的存款:%s 信用款:%s'%(result[0],result[1]))
obj = main('127.0.0.1', 8889)
if __name__ == '__main__':
choose = {'': login, '': register, '': exit}
choose2 = {'': query, '': giro, '': atm, '': repay,}
while True:
opt = input('请选择: 1、登陆 2、注册 3、退出\n>>>')
obj.sendall(by(opt))
if opt in choose.keys():
result = choose[opt]()
if result:
while True:
opt2 = input('请选择: 1、查询 2、转账 3、提现 4、还款 5、退出\n>>>')
obj.sendall(by(opt2))
if opt2 in choose2.keys():
result = choose2[opt2]()
if opt2=='':break
elif opt=='':break
else:pr('输入无效','r')

客户端程序

import os
import json
import time
import datetime
from moudle import userlog t = time.strftime("%Y-%m-%d %H:%I:%S", time.localtime(time.time()))
# month = t[:7]
# print(month)
card_list=os.listdir('DB\\cardinfo')
debt_sum=0
now = int(time.time())
for cardfile in card_list:
carddict=json.load(open('DB\\cardinfo\\%s'%cardfile,'r'))
for i in zip(carddict['debt'][0], carddict['debt'][1]):
print(i)
print(i[1][:4], i[1][5:])
s = datetime.datetime(int(i[1][:4]), int(i[1][5:]), 11)
ss = time.mktime(s.timetuple())
owe_time = int((now - ss) / (60 * 60 * 24)) + 1
owe_debt=i[0]*owe_time*0.0005
debt_sum+=owe_debt
print(carddict)
if carddict['saving']==0:
carddict['balance']-=debt_sum
else:
debt_owe=debt_sum-carddict['saving']
carddict['saving']=0
carddict['balance'] -= debt_sum
print(cardfile)
json.dump(carddict, open('DB\\cardinfo\\%s'%cardfile, 'w'))
userlog(carddict,'欠款金额:%d 利息:%d'%(sum(carddict['debt'][0]),debt_sum))

每天计算每个用户的利息

#-*- coding:utf-8 -*-
import os
import json
import time t = time.strftime("%Y-%m-%d %H:%I:%S", time.localtime(time.time()))
month = t[:7]
print(month)
card_list=os.listdir('DB\\cardinfo')
for i in card_list:
print(i)
carddict=json.load(open('DB\\cardinfo\\%s'%i,'r'))
# print(carddict)
# carddict={"card": 8003, "debt": [[5000,5000], ["2016-04","2016-05"], 5000], "status": 0, "credit": 15000, "user": "dalong", "saving": 950.0, "password": "123", "balance": 15000}
owe=carddict['credit']-carddict['balance']
if owe>0:
carddict['balance']=carddict['credit']
card = carddict['card']
carddict['debt'][0].append(owe)
carddict['debt'][1].append(month)
print(carddict)
json.dump(carddict,open('DB\\cardinfo\\%s'%i,'w'))

每个月计算是否有欠款

#-*- coding:utf-8 -*-
import time
t = time.strftime("%Y-%m-%d %H:%I:%S", time.localtime(time.time()))
month = t[:10]
# month='2016-06-23'
y,m,d=month.split('-')
y,m,d=int(y),int(m),int(d)
filename=''
def pr(word,color='g'):
if color =='r':
print('\033[031m%s\033[0m'%word)
else:
print('\033[032m%s\033[0m'%word)
def by(word):
a=bytes(word,encoding = 'utf-8')
return a
def st(word):
b=str(word,encoding = 'utf-8')
return b
import os,json BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) USER_DIR = os.path.join(BASE_DIR, 'db', 'user')
creat=json.load(open('DB\\creat_card.json','r'))
import logging
def userlog(carddict,word):
filename=log_file()
file2 = logging.FileHandler('log\\%s\\%s.log' %(carddict['card'],filename), 'a',encoding='utf-8')
fmt = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s')
file2.setFormatter(fmt)
logger2 = logging.Logger('s2', level=logging.DEBUG)
logger2.addHandler(file2)
logger2.info(word)
def log_file():
if d>22:
filename='%d-%d'%(y,m)
else:
if m-1 <1:
filename = '%d-%d' % (y-1, 12)
else:
filename = '%d-%d' % (y, m-1)
return filename

模板程序

#-*- coding:utf-8 -*-
from moudle import *
from client import *
class mall:
def shop(self):
while True:
for k,v in goods.items():
print('商品:%s\t\t价格:%d¥'%(k,v))
opt2=input('选择商品 q退出 \n请选择:').strip()
if opt2 == 'q':
break
else:
if opt2 in goods.keys():
num=input('请输入数量:')
shop_list[opt2]=int(num)
pr('商品已添加到购物车')
else:
pr('输入无效!','r')
continue
def pay(self,):
num=0
for i in shop_list.keys():
# print(shop_list[i],goods[i])
num+=shop_list[i]*goods[i]
pr(num)
shopgiro(8888)
pr('Welcome to the shopping mall')
shop_list={}
goods={'iphone':5000,'bike':500,'apple':15,'qb':1,}
shop=mall()
choose={'':shop.shop,'':shop.pay,'':exit}
while True:
# obj = main('127.0.0.1', 8889)
opt=input('1、选择商品 2、结算 3、退出 \n请选择:').strip()
if opt in choose.keys():choose[opt]()
else:pr('输入无效!','r')
print(shop_list)

购物车程序

使用socket实现信用卡程序和迷你购物商城的更多相关文章

  1. Python开发程序:ATM+购物商城

    一.程序要求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还 ...

  2. python_项目_ATM和购物商城的程序

    1 需求 模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 ...

  3. ATM:模拟实现一个ATM + 购物商城程序

    额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还款接口 ATM记录操作日志 提供管理接 ...

  4. 模拟实现一个ATM+购物商城程序

    记得上次小编上传了一个购物车程序,这次呢稍微复杂一点,还是那句话,上传在这里不是为了炫耀什么,只是督促小编学习,如果大神有什么意见和建议,欢迎指导.(PS:本次主要参考学习为主,自己原创的很少) 要求 ...

  5. 模拟实现ATM+购物商城程序

    流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...

  6. day4作业模拟实现一个ATM + 购物商城程序

    作业需求: 模拟实现一个ATM + 购物商城程序 1.额度 15000或自定义: 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账: 3.可以提现,手续费5%: 4.每月22号出账单,每月10 ...

  7. 2.2 - ATM+购物商城程序

    要求:模拟实现一个ATM + 购物商城程序1.额度 15000或自定义2.实现购物商城,买东西加入 购物车,调用信用卡接口结账3.可以提现,手续费5%4.支持多账户登录5.支持账户间转账6.记录每月日 ...

  8. First Project -用函数写的ATM+购物商城程序

    作业需求:模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  9. ATM&购物商城程序

    模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入购物车,调用信用卡接口转账 可以体现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还款接 ...

随机推荐

  1. Emmet:HTML/CSS代码快速编写神器(转)

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  2. POJ动态规划题目列表

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  3. 多线程之 CountDownLatch

    CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. CountDownLatch如其所写,是一个倒计数的锁存器,当计数减至0时触发特定 ...

  4. iPhone各种尺寸

    iPhone 6 Plus      736x414 points      2208x1242 pixels     3x scale     1920x1080 physical pixels   ...

  5. JavaIO(03)字节流--OutputStream and InputStream

    IO概述:   IO流用来处理设备之间的数据传输 java对数据的操作是通过流的方式 java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流(编码表) 流按流向分为:输入流,输出流 ...

  6. Unity中2D和UGUI图集的理解与使用

    图集 什么是图集? 在使用3D技术开发2D游戏或制作UI时(即使用GPU绘制),都会使用到图集,而使用CPU渲染的2D游戏和UI则不存在图集这个概念(比如Flash的原生显示列表),那么什么是图集呢? ...

  7. Unity3D之Mecanim动画系统学习笔记(三):Animation View

    动画组件之间的关系 我们先看一张图: 这里我们可以看到,我们在GameObject之上绑定的Animator组件是控制模型进行动画播放的. 而其属性Controller则对应一个Animator Co ...

  8. NGUI学习笔记(五):缓动

    在Unity3D中可以使用自带的Animation制作任意形式的动画,不过我们这篇笔记主要是学习和使用NGUI提供的Tween动画.NGUI提供的Tween库功能较为简单,主要是用来实现NGUI自身需 ...

  9. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  10. arcmap+vs2010

    esri为vs2010提供了addin开发模版,有几个关键的地方注意下 1.C:\Program Files (x86)\MSBuild\Esri\下存在ESRI.ArcGIS.AddIns.Serv ...