search
HomeBackend DevelopmentPython TutorialPython is a credit card system that implements functions such as online shopping malls, transfers, deposits and withdrawals, etc.

1. Requirements

2. Ideas

1. Shopping buy

Receive credit card credit card available balance,

Return the consumption amount

2. Credit card (ATM) category

Receive credit card available balance, total arrears, remaining arrears, and deposits since the last operation

Among them: 1. Each transaction type does not process money separately, nor does it record separate running accounts. Each transaction type calls the function that processes money (incoming transaction type, transaction amount)

  2. The function that handles money calls the configuration file for the addition, subtraction, and interest rate of each transaction type

Return to the credit card’s available balance, total arrears, remaining arrears, and deposits after this operation

3.Client

Bank administrator registration and login

Ordinary user registration and login

Sending requirements: registration, login, transaction type, transaction amount

4. Server side

Call the shopping class and create the shopping object (shopping interface)

Call the credit card (ATM) class to process repayments, transfers and other operations, record interest on a monthly basis, and write to files

5. Scheduled tasks

Execute the program regularly to calculate interest.

3. Code

3.1 Configuration File

import os
 
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) #配置文件的上层目录
DB_DIR=os.path.join(BASE_DIR,'db')  #数据文件夹
ADMIN=os.path.join(DB_DIR,'admin')
ALL_USERS=os.path.join(DB_DIR,'allusrs')
A=os.path.join(BASE_DIR,'db','s')
LOG=os.path.join(BASE_DIR,'log')
 
 
TRANSACTION={
 'repay':{'action':'plus','interest':0}, #还款
 'withdraw':{'action':'minus','interest':0.05},#取现
 'transfer':{'action':'minus','interest':0.05},#转账
 'consume':{'action':'minus','interest':0},#消费
 'saving':{'action':'plus','interest':0} #存款
}
 

3.2 Public Class

3.2.1 Shopping category

class buy:
 goods=[
   {"name": "电脑", "price": 1999},
   {"name": "鼠标", "price": 10},
   {"name": "游艇", "price": 20},
   {"name": "美女", "price": 998},
  ]
 
 def __init__(self,money,consumption,shopping_cart,):
  self.money=money
  self.consumption=consumption
  self.shopping_cart=shopping_cart
 
 def gouwu(self): #购物模块
  print('您的当前余额为:%d' %self.money)
  num=int(input('请输入商品序号:'))
  num-=1
  if self.goods[num]["name"] in self.shopping_cart.keys():    #goods[num]["name"]取商品名
   self.shopping_cart[self.goods[num]["name"]]['n']+=1     #商品数量+1
  else:
   self.shopping_cart[self.goods[num]["name"]]={"price":self.goods[num]["price"],'n':1,} # 创建购物车字典 {keys{"price":价格,数量:1}}
  self.money-=self.shopping_cart[self.goods[num]["name"]]["price"]*self.shopping_cart[self.goods[num]["name"]]['n']  #单价*数量
  self.consumption+=self.shopping_cart[self.goods[num]["name"]]["price"]*self.shopping_cart[self.goods[num]["name"]]['n']
 
 def yichu(self): #移除购物车模块
  c=int(input(' 请输入0/1选择是否移除购物车商品, 移除请输入1:'))
  if c==1:
   e=int(input(' 请输入要移除的商品序号:'))
   d=self.goods[e-1]
   if d in self.shopping_cart.keys():    #判断要移除的商品是否在购物车内
    self.shopping_cart.remove(d)   #移除商品
    self.money=self.money+self.goods[self.goods.index(d)]["price"]    #余额增加
    self.consumption=self.consumption-self.goods[self.goods.index(d)]["price"] #消费总额减少
   else:
    print('商品不存在')
 def chongzhi(self): #充值模块
  pay=int(input('请输入充值金额'))
  self.money=self.money+pay
  print('您的当前余额为:%d' % self.money) #显示当前余额
 
 def main(self):
  print('商品清单:')
  for m,n in enumerate(self.goods,1):
   print(m)
   for v in n.values():
    print(v)
   print('=============')
  #消费总额清零
  self.consumption=0
  buy=True #定义默认一直购物
  while buy:
   price=0 #定义初始价格
   b=1 #定义默认不退出购物或充值状态
   if self.money>=price:
 
  #消费模块;金钱大于货物价格时,才能开始购物
    while self.money>=price:
  #计价模块,有钱就可以一直购物
     self.gouwu()
  #移除购物车商品模块
     self.yichu()
     if self.money>=0:
      print('您的当前余额为:%d' %self.money)   #显示当前余额
      b=int(input(' 请输入0/1选择是否继续购物, 购物请输入1:'))
      if b==0:  #
       break #退出计价模块
    if b==0:   #如果不购物
     break   #不购物退出整个购物程序
  #充值模块
   else:
    while self.money<price:   #金钱不足,可多次充钱,直到能买得起货物
     a=int(input(' 您的余额不足,请输入0/1选择是否充值,充值请输入1:'))
     if a==1:
      self.chongzhi()
     else:
      break   #退出充值模块
     if a==0:
      break   #不充值退出程序
  #打印购物车商品名、商品价格、消费总额、余额
  print('您的消费清单为:')
  for m,n in self.shopping_cart.items():
   print(m,n['price'],n['n'])
     #打印消费清单
   print('=============')
  print('您的当前余额为:%d,您的消费总额为:%d' % (self.money,self.consumption) )   #打印消费总额
  return self.consumption

3.2.2 Credit Card ATM

class Atm:
 credit=15000 #信用卡额度
 def __init__(self,balance,debt,remaining_debt,interest,saving,id):
  self.id=id    #信用卡id
  self.balance=balance  #信用卡可用金额
  self.debt=debt   #总欠款
  self.remaining_debt=remaining_debt #剩余欠款
  self.interest=interest  #手续费
  self.saving=saving  #存款
  self.now_time=time.strftime("%Y-%m-%d %H:%M:%S")
  self.now_data=time.strftime("%Y-%m")
  self.struct_time=time.gmtime(time.time())
  if self.struct_time.tm_mday>22:
   self.now_data=self.struct_time.tm_year+'-'+str(int(self.struct_time.tm_mon)+1)
 
 def account_info(self):#打印账户信息
  return '账户id%s 信用卡额度%s;信用卡可用金额%s;剩余欠款%s;'%(self.id,self.credit,self.balance,self.remaining_debt,)
 def ret_account_info(self):
  return [self.id,self.credit,self.balance,self.debt,self.remaining_debt,self.interest]
 def repay(self,amount):#还款
  self.handel_money('repay',amount)
 def withdraw(self,amount): #取现
  self.handel_money('withdraw',amount)
 def transfer(self,amount): #转账
  self.handel_money('transfer',amount)
 def consume(self,amount): #消费
  self.handel_money('consume',amount)
 def saves(self,amount):
  self.handel_money('saving',amount)
 def transaction(self,a,amount):
  dic={
   '1':self.repay,
   '2':self.withdraw,
   '3':self.transfer,
   '4':self.consume,
   '5':self.saves
  }
  print("debug: a:",type(a),"amount:",type(amount))
 
  print(a)
  print(dic[a])
  print(dic["5"])
  dic[a](amount)
  print("end debug")
 
 def handel_money(self,transaction,amount): #交易类型,
  amount=int(amount)
  interest=amount*settings.TRANSACTION[transaction]['interest'] #手续费计算
  if settings.TRANSACTION[transaction]['action']=='plus':
 
   if amount<=self.remaining_debt:
    self.remaining_debt-=amount
    self.balance+=amount
   else:
    self.balance+=self.remaining_debt
    self.remaining_debt=0
    self.saving+=amount-self.remaining_debt
  else:
 
   if self.saving<amount:
    self.saving=0
    a=amount-self.saving
    self.balance-=a+interest-self.saving
    # self.debt+=amount+interest
    self.remaining_debt+=a+interest
  a='time:%s id:%s transaction: %s amount:%s interest %s \n'%(self.now_time,self.id,transaction,amount,interest)
  print(a)
  mulu=os.path.join(settings.ALL_USERS,self.id)
  path_name_liushui=os.path.join(mulu,str(self.id)+'name_liushui',str(self.now_data))
 
  with open(path_name_liushui,'a')as f:   #记录流水信息
   f.write(a)
 
  s=[self.balance,self.debt,self.remaining_debt,self.interest,self.saving,]  #更新基本信息
  path_name_base=os.path.join(mulu,str(self.id)+'name_base')
  pickle.dump(s,open(path_name_base,'wb'))

 3.3 Server side:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys,os
import hashlib
import pickle
import time
import socketserver
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import modules
from lib.modules import *
 
class Myserver(socketserver.BaseRequestHandler):
 
 def md5(self,pwd):
  '''
  对密码进行加密
  :param pwd: 密码
  :return:
  '''
  hash=hashlib.md5(bytes('xx7',encoding='utf-8'))
  hash.update(bytes(pwd,encoding='utf-8'))
  return hash.hexdigest()
 
 
 def login(self,usrname,pwd,x):
  '''
  登陆
  :param usrname: 用户名
  :param pwd: 密码
  :return:是否登陆成功
  '''
  conn=self.request
  if x=='1':
   path_name_pwd=os.path.join(settings.ADMIN,usrname)
  else:
   mulu=os.path.join(settings.ALL_USERS,usrname)
   path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
  s=pickle.load(open(path_name_pwd,'rb'))
  if usrname in s:
    if s[usrname]==self.md5(pwd):  #和加密后的密码进行比较
    return True
    else:
    return False
  else:
   return False
 
 
 def regist(self,usrname,pwd,x):
  '''
  注册
  :param usrname: 用户名
  :param pwd: 密码
  :return:是否注册成功
  '''
 
  conn=self.request
  if x=='1':
   mulu=os.path.join(settings.ADMIN,usrname)
  else:
   mulu=os.path.join(settings.ALL_USERS,usrname)
 
 
  if os.path.exists(mulu):
    return False
  else:
   os.mkdir(mulu)
   s={}
   s[usrname]=self.md5(pwd)
   path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
   pickle.dump(s,open(path_name_pwd,'wb'))
   path_name_base=os.path.join(mulu,usrname+'name_base')
   pickle.dump([15000,{},0,0,0],open(path_name_base,'wb'))
   path_name_liushui=os.path.join(mulu,usrname+'name_liushui')
   os.mkdir(path_name_liushui)
   return True
 
 def user_identity_authentication(self,usrname,pwd,ret,x):
  '''
  判断注册和登陆,并展示用户的详细目录信息,支持cd和ls命令
  :return:
  '''
  conn=self.request
  if ret=='1':
   r=self.login(usrname,pwd,x)
   if r:
    conn.sendall(bytes('y',encoding='utf-8'))
   else:
    conn.sendall(bytes('n',encoding='utf-8'))
  elif ret=='2':
   # print(usrname,pwd)
   if x=='1':
    r=self.regist(usrname,pwd,x)
   else: #用户注册
    s=[0,1]
    pickle.dump(s,open(settings.A,'wb'))
    while True:
     ret=pickle.load(open(settings.A,'rb'))
     if ret[0]==0:
      time.sleep(30)
      continue
     elif ret[0]==1 or ret[0]==2:
      break #默认值已更改,银行管理员已操作
 
    if ret[0]==1: #如果管理员同意
     r=self.regist(usrname,pwd,x)
    else:
     r=0
    s=[0,0]
    pickle.dump(s,open(settings.A,'wb'))
   if r:
    conn.sendall(bytes('y',encoding='utf-8'))
   else:
    conn.sendall(bytes('n',encoding='utf-8'))
 def interactive(self,usrname): #进行交互
  conn=self.request
  while True:
   c=conn.recv(1024) #接收用户交互选项
   r=str(c,encoding='utf-8')
   mulu=os.path.join(settings.ALL_USERS,usrname)
   path_name_base=os.path.join(mulu,usrname+'name_base')
   s=pickle.load(open(path_name_base,'rb'))
 
   #打印账户信息
   obj=modules.Atm(s[0],s[1],s[2],s[3],s[4],usrname) #Atm对象
   a=obj.account_info() #接收账户信息
   conn.sendall(bytes(a,encoding='utf-8'))
 
   b=obj.ret_account_info()
 
 
 
   if r== '4':
    buy_obj=modules.buy(b[2],0,{})
    amount=buy_obj.main()
   elif r=='q':
    break
   else:
    s=conn.recv(1024)
    amount=str(s,encoding='utf-8')
 
   obj.transaction(r,amount)
 
 
  pass
 
 
 def handle(self):
  conn=self.request
  x=conn.recv(1024)
  x=str(x,encoding='utf-8')
  conn.sendall(bytes('收到用户类别',encoding='utf-8'))
  while True:
   if x=='1' or x=='2':
    b=conn.recv(1024)
    ret=str(b,encoding='utf-8')
    conn.sendall(bytes('b ok',encoding='utf-8'))
    c=conn.recv(1024)
    r=str(c,encoding='utf-8')
    usrname,pwd=r.split(',')
    print(usrname,pwd)
    self.user_identity_authentication(usrname,pwd,ret,x) #登陆或注册验证
    if x=='2':#普通用户身份验证成功后
     self.interactive(usrname)
 
     pass
 
    break
   elif x=='q':
    break
 
if __name__=='__main__':
 sever=socketserver.ThreadingTCPServer(('127.0.0.1',9999),Myserver)
 sever.serve_forever()

3.4 Client

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
本程序作为用户或银行管理员的入口,其中c=1代表银行管理员,c=2代表普通用户
'''
import pickle
import sys
import time
import os
import socket
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import *
from lib.modules import *
 
def login(usrname,pwd):
 '''
 登陆
 :param usrname:用户名
 :param pwd:密码
 :return:是否登陆成功
 '''
 obj.sendall(bytes(usrname+','+pwd,encoding='utf-8'))
 ret=obj.recv(1024)
 r=str(ret,encoding='utf-8')
 if r=='y':
  return 1
 else:
  return 0
 
def regist(usrname,pwd,x):
 '''
 注册
 :param usrname:用户名
 :param pwd:密码
 :return:是否注册成功
 '''
 obj.sendall(bytes(usrname+','+pwd,encoding='utf-8'))
 
 ret=obj.recv(1024)
 r=str(ret,encoding='utf-8')
 if r=='y':
  return 1
 else:
  return 0
def user_identity_authentication(usrname,pwd,x):
 '''
 选择登陆或注册,展示用户的详细目录信息,支持cd和ls命令
 :return:
 '''
 a=input('请选择1.登陆 2.注册')
 obj.sendall(bytes(a,encoding='utf-8'))
 obj.recv(1024)
 if a=='1':
  ret=login(usrname,pwd)
  if ret:
   print('登陆成功')
   return 1
  else:
   print('用户名或密码错误')
   return 0
 elif a=='2':
  ret=regist(usrname,pwd,x)
  if ret:
   print('注册成功')
   return 1
  else:
   print('用户名已存在或银行管理员拒绝')
   return 0
def main(x):
 usrname=input('请输入用户名')
 pwd=input('请输入密码')
 if user_identity_authentication(usrname,pwd,x): #如果验证身份成功
  if x=='1': #处理用户注册信息
 
   while True:
    s=pickle.load(open(settings.A,'rb'))
    if s[1]==0:
     time.sleep(30)
     continue
    elif s[1]==1:
     while True:
      a=input('用户请求注册,输入1同意,2拒绝')
      if a=='1':
       s=[1,0]
       pickle.dump(s,open(settings.A,'wb'))
       break
      elif a=='2':
       s=[2,0]
       pickle.dump(s,open(settings.A,'wb'))
       break
      else:
       print('输入有误')
     break
  else: #普通用户登陆后
   interactive() #进行交互
 
 
def interactive():
 while True:
  a=input('请选择 1.还款 2.取现 3.转账 4.消费 5.存钱 q退出')
  obj.sendall(bytes(a,encoding='utf-8'))
  r=obj.recv(1024) #接收账户信息
  ret=str(r,encoding='utf-8')
  print(ret)
  if a !='4'and a !='q':
   b=input('请输入金额')
   obj.sendall(bytes(b,encoding='utf-8'))
  elif a=='q':
   break
 
 
 
obj=socket.socket() #创建客户端socket对象
obj.connect(('127.0.0.1',9999))
while True:
 x=input('请选择1.银行管理员 2.用户 q、退出')
 obj.sendall(bytes(x,encoding='utf-8'))
 obj.recv(1024) #确认收到用户类别
 if x=='1' or x=='2':
  main(x)
  break
 elif x=='q':
  break
 else:
  print('输入有误请重新输入')
 
obj.close()

 3.5 Timed tasks

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys
import json,pickle
import time
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
 
 
 
def main():
 card_list = os.listdir(settings.ALL_USERS)
 for card in card_list:
  basic_info = pickle.load(open(os.path.join(settings.ALL_USERS, card, card+'name_base')))
  struct_time = time.localtime()
 
  # 循环账单列表,为每月的欠款计息。并写入到当月账单中
  for item in basic_info['debt']:
   interest = item['total_debt'] * 0.0005
   if basic_info[4] >= interest:
    basic_info[4] -= interest
   else:
    temp = interest - basic_info[4]
    basic_info[4]=0
    basic_info[0] -= temp
    pickle.dump(
      basic_info,
      open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
    )
 
  # 如果当前等于10号(9号之前)
  # 当前余额为负值,则将值添加到账单列表中,开始计息,同时,本月可用额度恢复。
  date = time.strftime("%Y-%m-%d")
  if struct_time.tm_mday == 11 and basic_info[2]>0:
   dic = {'date': date,
     "total_debt": basic_info[2],
     "balance_debt": basic_info[2],
     }
   basic_info[1].append(dic)
   # 恢复可用额度
   basic_info[0] = 15000
  pickle.dump(
   basic_info,
   open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
    )
 
 
def run():
 main()
  

The above is about using python to develop an online shopping mall credit card system. Students in need can refer to it.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python in Action: Real-World ExamplesPython in Action: Real-World ExamplesApr 18, 2025 am 12:18 AM

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python's Main Uses: A Comprehensive OverviewPython's Main Uses: A Comprehensive OverviewApr 18, 2025 am 12:18 AM

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

The Main Purpose of Python: Flexibility and Ease of UseThe Main Purpose of Python: Flexibility and Ease of UseApr 17, 2025 am 12:14 AM

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python: The Power of Versatile ProgrammingPython: The Power of Versatile ProgrammingApr 17, 2025 am 12:09 AM

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Learning Python in 2 Hours a Day: A Practical GuideLearning Python in 2 Hours a Day: A Practical GuideApr 17, 2025 am 12:05 AM

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools