Python implements the credit card system (supports shopping, transfers, and money access)

Source: Internet
Author: User
This article mainly introduces Python's implementation of credit card systems (supporting shopping, transfers, and money access). It is very good and has reference value, for more information about credit card systems, please refer to the projects you have been working on recently. I would like to say hello to everyone, and I should give you a message today, this project is based on the python programming language. this credit card supports shopping, transfers, and Money Access. the following section will share your needs and implementation ideas for your reference only, if you have any bugs, you are welcome to raise them and learn and make progress together. thank you!

I. requirements

II. ideas

1. shopping buy

Available credit card balances for receiving credit cards,

Return consumption amount

2. credit card (ATM)

After receiving the last operation, the available credit card balance, total amount owed, remaining amount owed, and deposit

Among them: 1. each transaction type does not process money independently, nor records the sequential account separately. each transaction type calls the function for processing money (incoming transaction type and transaction amount)

2. the money processing function calls the configuration file to add or subtract money and interest rates for each transaction type.

Return the available credit card balance, total amount owed, remaining amount owed, and deposit after this operation

3. client

Bank administrator registration login

Common User registration and login

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

4. server side

Call the shopping class to create shopping objects (shopping interface)
Call the credit card (ATM) class, handle the repayment, transfer and other operations, record the interest on a monthly basis, and write the file

5. scheduled tasks

Periodically execute the program to calculate the interest.

III. code

3.1 configuration file

Import osBASE_DIR = OS. path. dirname (OS. path. dirname (_ file _) # DB_DIR = OS. path. join (BASE_DIR, 'DB') # data folder 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 = {'repa': {'action': 'Plus ', 'interest': 0}, # repayment 'withraw ': {'action': 'minus', 'interest ': 0.05}, # obtain 'transferer': {'action': 'minus', 'interest': 0.05 }, # Transfer 'consume': {'action': 'minus', 'interest ': 0}, # consume 'saving': {'action': 'Plus ', 'interest ': 0} # deposit}

3.2 Public class

3.2.1 shopping

Class buy: goods = [{"name": "computer", "price": 1999 },{ "name": "mouse", "price": 10 }, {"name": "yacht", "price": 20 },{ "name": "Beauty", "price": 998},] def _ init _ (self, money, consumption, shopping_cart,): self. money = money self. consumption = consumption self. shopping_cart = shopping_cart def gouwu (self): # print the shopping module ('Your current balance is: % d' % self. money) num = int (input ('Enter the product serial number: ') num-= 1 if self. goods [num] ["name"] in self. shopping_cart.keys (): # goods [num] ["name"] obtains the commodity name self. shopping_cart [self. goods [num] ["name"] ['n'] + = 1 # product quantity + 1 else: self. shopping_cart [self. goods [num] ["name"] = {"price": self. goods [num] ["price"], 'n': 1, }# create a shopping cart dictionary {keys {"price": price, quantity: 1} self. money-= self. shopping_cart [self. goods [num] ["name"] ["price"] * self. shopping_cart [self. goods [num] ["name"] ['n'] # unit price * quantity self. consumption + = self. shopping_cart [self. goods [num] ["name"] ["price"] * self. shopping_cart [self. goods [num] ["name"] ['n'] def yichu (self ): # Remove the cart module c = int (input ('Enter 0/1 to select whether to remove the cart item, input 1: ') if c = 1: e = int (input ('Enter the serial number of the item to be removed: ') d = self. goods [E-1] if d in self. shopping_cart.keys (): # determines whether the item to be removed is self in the shopping cart. shopping_cart.remove (d) # Remove item self. money = self. money + self. goods [self. goods. index (d)] ["price"] # increase in balance self. consumption = self. consumption-self.goods [self. goods. index (d)] ["price"] # reduce the total consumption else: print ('item does not exist') def chongzhi (self ): # recharge module pay = int (input ('Enter the recharge amount ') self. money = self. money + pay print ('Your current balance is: % d' % self. money) # display the current balance def main (self): print ('item list: ') for m, n in enumerate (self. goods, 1): print (m) for v in n. values (): print (v) print ('==============') # clear the total consumption self. consumption = 0 buy = True # define the default continuous shopping while buy: price = 0 # define the initial price B = 1 # define the default non-exit shopping or recharge status if self. money> = price: # consumption module; when money is greater than the price of goods, you can start shopping while self. money> = price: # The billing module can always shop for self if you have money. gouwu () # Remove the shopping cart module self. yichu () if self. money> = 0: print ('Your current balance is: % d' % self. money) # display the current balance B = int (input ('Enter 0/1 to choose whether to continue shopping, please enter 1: ') if B = 0: # break # exit the billing module if B = 0: # if you do not shop break # if you do not shop, exit the entire shopping program # recharge module else: while self. money
 
  

3.2.2 credit card ATM

Class Atm: credit = 15000 # credit card credit def _ init _ (self, balance, debt, remaining_debt, interest, saving, id): self. id = id # credit card id self. balance = balance # credit card amount available self. debt = debt # Total debt self. remaining_debt = remaining_debt # self. interest = interest # Commission self. saving = saving # deposit 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): # Print account information return 'account id % s credit card quota % s; credit card available amount % s; remaining amount % 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): # repay self. handel_money ('repa', amount) def withdraw (self, amount): # obtain self. handel_money ('withraw ', amount) def transfer (self, amount): # transfer self. handel_money ('transferer', amount) def consume (self, amount): # consume self. handel_money ('sume', 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): # transaction type, amount = int (amount) interest = amount * settings. TRANSACTION [transaction] ['interest '] # if settings. TRANSACTION [transaction] ['action'] = 'Gal': 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

3.3 Server side:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import sys, osimport hashlibimport pickleimport timeimport socketserversys. path. append (OS. path. dirname (OS. path. dirname (_ file _) from config import settingsfrom lib import modulesfrom lib. modules import * class Myserver (socketserver. baseRequestHandler): def md5 (self, pwd): ''' encrypt the password: param pwd: Password: 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): ''' login: param usrname: Username: param pwd: Password: return: whether the login is successful ''' 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): # compare with the encrypted password. return True else: return False def regist (self, usrname, pwd, x): ''' registration: param usrname: Username: param pwd: Password: return: whether the registration is successful ''' 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], 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): ''' checks registration and login, and displays detailed user directory information, supports cd and ls commands: 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: # User registration 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 # The default value has been changed, the bank administrator has operated if ret [0] = 1: # if the administrator agrees to 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): # perform interactive conn = self. request while True: c = conn. recv (1024) # receive user interaction option 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') # Print account information obj = modules. atm (s [0], s [1], s [2], s [3], s [4], usrname) # Atm object a = obj. account_info () # receiving account information 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 ('receive user class', 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) # login or registration verification if x = '2': # self. interactive (usrname) pass break elif x = 'Q': breakif _ name __= = '_ main _': sever = socketserver. threadingTCPServer ('2017. 0.0.1 ', 9999), Myserver) sever. serve_forever ()

3.4 client

#! /Usr/bin/env python #-*-coding: UTF-8-*-''' this program serves as the portal for users or bank administrators. c = 1 represents the bank administrator, c = 2 stands for normal user ''' import pickleimport sysimport timeimport osimport socketsys. path. append (OS. path. dirname (OS. path. dirname (_ file _) from config import settingsfrom lib import * from lib. modules import * def login (usrname, pwd): ''' login: param usrname: user name: param pwd: Password: return: login successful ''' 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 0def regist (usrname, pwd, x): ''' registration: param usrname: user name: param pwd: Password: return: whether the registration is successful ''' 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 0def user_identity_authentication (usrname, pwd, x): ''' SELECT login or registration to display the user's detailed directory information. the cd and ls commands are supported: return :' ''A = input ('Select 1. login 2. register ') obj. sendall (bytes (a, encoding = 'utf-8') obj. recv (1024) if a = '1': ret = login (usrname, pwd) if ret: print ('login successfully') return 1 else: print ('username or password error') return 0 elif a = '2': ret = regist (usrname, pwd, x) if ret: print ('registered successfully ') return 1 else: print ('user name already exists or the bank administrator rejects ') return 0def main (x): usrname = input ('Enter the user name ') pwd = input ('Enter the password') if user_identity_authentication (usrname, pwd, x): # if the authentication succeeds If x = '1': # process user registration information 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 ('User requests registration, input 1 agree, 2 reject ') 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 ('incorrect input') break else: # After A common user logs on to interactive () # interactive def interactive (): while True: a = input ('Select 1. repayment 2. withdrawal 3. transfer 4. consumption 5. save money q exited ') obj. sendall (bytes (a, encoding = 'utf-8') r = obj. recv (1024) # received account information ret = str (r, encoding = 'utf-8') print (ret) if! = '4' and! = 'Q': B = input ('Enter the amount ') obj. sendall (bytes (B, encoding = 'utf-8') elif a = 'Q': breakobj = socket. socket () # create the client socket Object obj. connect ('2017. 0.0.1 ', 9999) while True: x = input (' Select 1. bank administrator 2. user q, exit ') obj. sendall (bytes (x, encoding = 'utf-8') obj. recv (1024) # confirm to receive the user category if x = '1' or x = '2': main (x) break elif x = 'Q': break else: print ('input is incorrect. please input it again ') obj. close ()

3.5 scheduled task

#! /Usr/bin/env python #-*-coding: UTF-8-*-import OS, sysimport json, pickleimport timesys. path. append (OS. path. dirname (OS. path. dirname (_ file _) from config import settingsdef 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 () # The cyclic bill list, which is used to calculate the monthly arrears. And write it to the current month's bill for item in basic_info ['dest']: 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') # if the current value is equal to 10 (before 9) # The current balance is negative, the value is added to the bill list to start interest calculation, and the available quota is restored this month. 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) # Restore the available quota 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 section describes how to use Python to implement a credit card system (which supports shopping, transfers, and money access, if you have any questions, please leave a message and the editor will reply to you in time. I would like to thank you for your support for the script home website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.