Donric

个人作业三-ATM管理系统

这个作业属于哪个课程 AHPU-软件工程导论-计算机18级
这个作业要求在哪里 个人作业三-ATM管理系统
这个作业的目标 实现一个ATM系统
学号 3181002122

一、题目要求

编写一个ATM管理系统,语言不限,要求应包括以下主要功能:

(1)开户,销户

(2)查询账户余额

(3)存款

(4)取款

(5)转账(一个账户转到另一个账户)等...

二、代码提交

数据库建立

create table if not exists `account`(  # 创建一个account表,用于记录atm中的账户信息
`cardId` char(8) ,  # 银行卡号
`cardPassword` varchar(16) not null,  # 银行卡密码
`cardName` varchar(30) not null,  # 持卡人姓名
`idNumber` char(19) not null,  # 持卡人身份证号
`balance` int(20) not null,  # 银行卡金额
primary key(`cardId`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

insert into account(cardId,cardPassword,cardName,idNumber,balance)  # 插入一条测试数据
values(12345678,"12345678","易号东",019208,2390);

头文件以及数据库配置

import pymysql
import random
import string

# 数据库配置
host = '127.0.0.1'  # ip地址,端口号
port = 3306
database = 'atm'  # 数据库名称
username = 'root'  # 登录名
password = 'Mumu9568'  # 登录密码

菜单函数

def Menu():
    print("欢迎使用ATM自助银行系统!\n")
    print("1.开户\n2.销户\n3.查询账户余额\n4.存款\n5.取款\n6.转账\n7.修改密码")
    choose = input("请选择你想要使用的功能:")
    return choose

生成银行卡卡号函数

def cardId_generator(size=18, chars=string.digits):  # 随机产生一窜数字串
    return ''.join(random.choice(chars) for _ in range(size))

创建账户

def BuildAccount():
    print("请填写你的个人信息:")
    cardName = input("持卡人姓名:")
    idNumber = input("持卡人身份证号码:")
    cardPassword = input("银行卡密码:")
    cardId = cardId_generator()

    # 连接数据库
    conn = pymysql.connect(host=host, user=username, password=password, database=database,port=port)
    # 创建游标
    cur = conn.cursor()
    if not cur:
        raise (NameError,"连接数据库失败")
    # 执行sql语句
    cur.execute("insert into account(cardId,cardPassword,cardName,idNumber,balance) "
                "values ({},'{}','{}',{},{})".format(cardId,cardPassword,cardName,idNumber,0))
    conn.commit()
    # 关闭
    cur.close()
    conn.close()
    print("开户成功!您的银行卡号为:"+cardId)
    return

销毁账户函数

def DestoryAccount():
    cardId =  input("请输入银行卡号:")
    cardPassword = input("请输入银行卡密码:")

    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")

    num = cur.execute("select cardPassword from account where cardId = {}".format(cardId))
    if num == 0:
        print("无该银行卡账户!")
        cur.close()
        conn.close()
        return
    # 获取密码并验证
    ValiPassword = cur.fetchone()[0]
    if ValiPassword != cardPassword:
        print("您输入的银行卡密码有误!")
    else:
        print("密码正确!")
        cur.execute("delete from account where cardId = {}".format(cardId))
        conn.commit()
        print("销户成功!")

    cur.close()
    conn.close()
    return

查询账户金额

def SelectBalance():
    cardId = input("请输入银行卡号:")
    cardPassword = input("请输入银行卡密码:")

    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")

    num = cur.execute("select cardPassword from account where cardId = {}".format(cardId))
    if num == 0:
        print("无该银行卡账户!")
        cur.close()
        conn.close()
        return
    ValiPassword = cur.fetchone()[0]
    if ValiPassword != cardPassword:
        print("您输入的银行卡密码有误!")
    else:
        print("密码正确!")
        cur.execute("select balance from account where cardId = {}".format(cardId))
        balance = cur.fetchone()[0]
        print("您的账户余额为:"+str(balance))

    cur.close()
    conn.close()
    return

存款函数

def Deposit():
    cardId = input("请输入银行卡号:")
    balance = input("请输入存款金额:")

    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")

    num = cur.execute("update account set balance={} where cardId = {}".format(balance,cardId))
    if num == 0:
        print("无该银行卡账户!")
        cur.close()
        conn.close()
        return
    conn.commit()
    cur.close()
    conn.close()
    print("存款成功!")
    return

取款函数

def Withdraw():
    cardId = input("请输入银行卡号:")
    cardPassword = input("请输入银行卡密码:")
    money = input("请输入取款金额:")


    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")
    num =  cur.execute("select cardPassword from account where cardId = {}".format(cardId))
    if num == 0:
        print("无该银行卡账户!")
        cur.close()
        conn.close()
        return
    ValiPassword = cur.fetchone()[0]
    if ValiPassword != cardPassword:

        print("您输入的银行卡密码有误!\n")
    else:
        print("密码正确!\n")
        cur.execute("select balance from account where cardId = {}".format(cardId))
        balance = cur.fetchone()[0]
        if balance >= int(money):
            cur.execute("update account set balance = balance-{} where cardId = {}".format(money, cardId))
            conn.commit()
            print("取款成功,金额为:"+money)
        else:
            print("账户余额不足。\n")


    cur.close()
    conn.close()
    return

转账函数

def transfer():
    sendcardId = input("请输入转账人银行卡号:")
    sendcardPassword = input("请输入转账人银行卡密码:")
    recivecardId = input("请输入收账人银行卡号:")
    money = input("请输入转账金额:")

    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")

    num = cur.execute("select cardPassword from account where cardId = {}".format(sendcardId))
    if num == 0:
        print("无转账银行卡账户!")
        cur.close()
        conn.close()
        return
    ValiPassword = cur.fetchone()[0]
    if ValiPassword != sendcardPassword:

        print("您输入的银行卡密码有误!\n")
    else:
        print("密码正确!\n")
        cur.execute("select balance from account where cardId = {}".format(sendcardId))
        balance = cur.fetchone()[0]
        print(balance)
        if balance >= int(money):
            cur.execute("update account set balance = balance-{} where cardId = {}".format(money, sendcardId))
            cur.execute("update account set balance = balance+{} where cardId = {}".format(money, recivecardId))
            conn.commit()
            print("转账成功,金额为:"+money)
        else:
            print("账户余额不足。\n")

    cur.close()
    conn.close()
    return

修改密码函数

def ChangePassword():
    cardId = input("请输入银行卡号:")
    cardPassword = input("请输入银行卡密码:")

    conn = pymysql.connect(host=host, user=username, password=password, database=database, port=port)
    cur = conn.cursor()
    if not cur:
        raise (NameError, "连接数据库失败")

    num = cur.execute("select cardPassword from account where cardId = {}".format(cardId))
    if num == 0:
        print("无银行卡账户!")
        cur.close()
        conn.close()
        return
    ValiPassword = cur.fetchone()[0]
    if ValiPassword != cardPassword:
        print("您输入的银行卡密码有误!\n")
    else:
        print("密码正确!\n")
        newCardPassword = input("请输入新的银行卡密码:")
        balance = cur.execute("update account set cardPassword = {} where cardId = {}".format(newCardPassword, cardId))
        conn.commit()
        print("密码修改成功!\n")

    cur.close()
    conn.close()
    return

入口函数

if __name__ == '__main__':
    while True:
        choose = Menu();
        if choose == '1':
            BuildAccount()
        elif choose == '2':
            DestoryAccount()
        elif choose == '3':
            SelectBalance()
        elif choose == '4':
            Deposit()
        elif choose == '5':
            Withdraw()
        elif choose == '6':
            transfer()
        elif choose == '7':
            ChangePassword()

        flag = input("是否需要继续操作?(Y/N)")
        if flag == "N":
            break

主运行界面

主界面

主界面

开户界面

开户界面

销户界面

销户界面

查询账户余额界面

查询账户余额界面

存款界面

存款界面

取款界面

取款界面

转账界面

转账界面

修改密码界面

修改密码界面

三、个人小结

psp2.1 任务内容 计划完成需要的时间(min) 实际完成需要的时间(min)
Planning 计划 10 10
Estimate 估计这个任务需要多少时间,并规划大致工作步骤 5 5
Development 开发 390 420
Analysis 需求分析(包括学习新技术) 20 25
Design Spec 生成设计文档 30 25
Design Review 设计复审 10 10
Coding Standard 代码规范 20 15
Design 具体设计 30 25
Coding 具体编码 200 220
Code Review 代码复审 25 20
Test 测试(自我测试,修改代码,提交修改) 30 40
Reporting 报告 20 25
Test Report 测试报告 15 10
Size Measurement 计算工作量 10 10
Postmortem & Process Improvement Plan 事后总结,并提出过程改进计划 10 15

收获

本次博客我使用了python与mysql实现了atm系统,其中最重要的就是python如何和数据库建立通信,需要对数据库的相关配置做设定以及遵循好python代码中调用数据库的一整个流程(建立连接→创建游标→进行相关操作→关闭游标→关闭连接),就可以完美实现与数据库的交互。

posted on 2020-11-18 23:49  Donric  阅读(261)  评论(0编辑  收藏  举报

导航