首页 > 代码库 > ATM+购物商城(第一版)

ATM+购物商城(第一版)

#!/usr/bin/env python
# -*- coding:utf-8 -*-  
# by wk

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# by wk


import os
import time

‘‘‘
     1.额度 15000或自定义
     2.实现购物商城,买东西加入 购物车,调用信用卡接口结账
     3.可以提现,手续费5%
     4.支持多账户登录
     5.支持账户间转账
     6.记录每月日常消费流水
     7.提供还款接口
     8.TM记录操作日志
     9.提供管理接口,包括添加账户、用户额度,冻结账户等。。。
     10.用户认证用装饰器
‘‘‘


# 用户信息文件转换为列表
def users_txt_to_list():
    users_lists = []
    with open(users.txt, r, encoding=utf-8) as goods_r:
        for line in goods_r:
            user_list = line.split()
            user_dict = {name: user_list[0], password: user_list[1],money: user_list[2],
                         grant: user_list[3],status:user_list[4]}
            users_lists.append(user_dict)
    return users_lists


# 用户登录装饰器
def user_login(func):
    user_lists = users_txt_to_list()
    def wragger(*args, **kwargs):
        with open(user_session.txt,r,encoding=utf-8) as session_user_r:
            read_result = session_user_r.read()
        if read_result == ‘‘:
            username = input(登录名: )
            passwords = input(密码: )
            for user in user_lists:
                if username == user[name] and passwords == user[password] and                                 user[status] == open:
                    with open (user_session.txt,w,encoding=utf-8) as user_session_w:
                        user_session_w.write(user[name])
                    # print(‘写入成功‘)
                    func(*args, **kwargs)
                elif user[status] == lock:
                    user_login_flag = lock
                    break
                else:
                    user_login_flag = other
                    break
            if user_login_flag == lock:
                print(您的账户被锁定)
            elif user_login_flag == other:
                print(您登录的信息有误,请重新登录)
        else:
            func(*args, **kwargs)
    return wragger


# 用户管理装饰器
def admin_user_login(func):
    user_lists = users_txt_to_list()
    def wragger(*args, **kwargs):
        with open(user_session.txt,r,encoding=utf-8) as session_user_r:
            read_result = session_user_r.read()
        # if read_result == ‘‘:
        #     username = input(‘登录名: ‘)
        #     passwords = input(‘密码: ‘)
        #     print(‘ffff‘,read_result)
            for user in user_lists:
                if user[name] == read_result and user[grant] == admin:
                    # print(‘写入成功‘)
                    func(*args, **kwargs)
                else:
                    print(您不是管理员)
                    # print(user[‘name‘],user[‘status‘])
                    break
        # else:
        #     func(*args, **kwargs)
    return wragger


# 购物中心模块
@user_login
def goods_center():
    with open(user_session.txt, r, encoding=utf-8) as session_user_r:
        session_user = session_user_r.read()
        users = users_txt_to_list()
        for user in users:
            if session_user == user[name]:
                user_dict = user
    flag_list = True
    my_cart_id = -1
    mycat_list = []
    # goodslists = goods_txt_to_list()
    while flag_list:
        goodsid = goods_list(mygoodslist)
        # goodslist = goods_txt_to_list()
        car_list = cartlists(goodsid, my_cart_id, mycat_list)
        continue_flag = input(是否继续购买(y or n): )
        if continue_flag == Y or continue_flag == y:
            flag_list = True
        elif continue_flag == N or continue_flag == n:
            flag_list = False
    print(cartlists, car_list)
    # print(‘aaaaaaaaa‘, mygoodslist)
    my_buy(car_list, user_dict)

    # while True:
    #     my_choose = input(‘是否继续购物,确认请按(Y or y),退出请按(B or b): ‘)
    #     if my_choose == ‘Y‘ or my_choose == ‘y‘:
    #         flag = True
    #         break
    #     elif my_choose == ‘B‘ or my_choose == ‘b‘:
    #         flag = False
    #         break
    #     else:
    #         print(‘输入的信息不正确请重新输入‘)
    #         continue


# 商品文件转换为列表
def goods_txt_to_list():
    good_lists = []
    with open(goods.txt, r, encoding=utf-8) as goods_r:
        for line in goods_r:
            good_list = line.split()
            good_dict = {name: good_list[0], price: good_list[1], nums: good_list[2]}
            good_lists.append(good_dict)
    return good_lists


# 商品列表
def goods_list(goodslists):
    my_goods_list = goodslists
    print(请选择商品)
    for k, goods in enumerate(my_goods_list):
        print(产品编号:%s 名称: %s 价格: %s元, 剩余: %s % (k, goods[name], goods[price], goods[nums]))
    while True:
        my_goods_id = input(请输入要买商品的编号: )
        if my_goods_id.isdigit():
            return my_goods_id
        print(输入的不是数字请重新输入\n)


# 购物车模块
def cartlists(goodsid, cart_id, carlist):
    # goodslist = goods_txt_to_list()
    cart_id = cart_id + 1  # 为了给购物车里的商品编号,便于移除商品
    goods_id = int(goodsid)
    print(您选择了 %s % (mygoodslist[goods_id][name]))
    goods_num = input(请输入购买数量: )
    if goods_num.isdigit():
        print(您购买的数量是 %s 件 % (goods_num))
        goods_name = mygoodslist[goods_id][name]
        goods_price = int(mygoodslist[goods_id][price])
        oldnum = int(mygoodslist[goods_id][nums])
        newnum = oldnum - int(goods_num)
        mygoodslist[goods_id][nums] = newnum
        if newnum > 0:  # 判断产品数量够不够
            car_dict = {}  # 创建购物车字典
            car_dict[id] = cart_id  # 编号
            car_dict[name] = goods_name  # 商品名称
            car_dict[nums] = goods_num  # 购买数量
            car_dict[price] = goods_price * int(goods_num)  # 价格
            carlist.append(car_dict)
        else:
            print(没那么多了,看看还有没有别的想买的\n)
    else:
        print(输入的不是数字请重新输入\n)

    return carlist


# 购物车商品合并模块
def union_cart_list(cartlists):
    new_cart_list = []
    for car in cartlists:
        if car not in new_cart_list:
            car[nums]
            new_cart_list.append(car)


# 购买模块
def my_buy(cart_list, user_dict):
    goods_total_money = 0
    if not cart_list:
        pass
    else:
        while True:
            print(购物车信息:)
            for buy_car in cart_list:
                print(购买编号: %s  产品名称: %s  价格: %s元  数量: %s个 % (
                    buy_car[id], buy_car[name], buy_car[price], buy_car[nums]))
            my_confirm = input(订单确认请按(Y or y),移除请输入编号(数字),取消订单请按(N or n): )
            if my_confirm == Y or my_confirm == y:
                for cart in cart_list:
                    goods_total_money += cart[price]
                myoldmoney = int(user_dict[money])
                # goodsmoney = carlist[goods_id][‘price‘] * int(goods_num)
                my_new_money = myoldmoney - goods_total_money
                if my_new_money >= 0:  # 判断钱够不够
                    # user_dict[‘money‘] = my_new_money
                    # goodslist[goods_id][‘nums‘] = goods_num
                    change_money(user_dict, my_new_money)
                    change_goods()
                    print(提交成功!)
                    print(当前您的订单信息:)
                    for buy_car in cart_list:
                        print(产品名称: %s  价格: %s元  数量: %s个 % (
                            buy_car[name], buy_car[price], buy_car[nums]))
                        now_time = str(time.asctime(time.localtime(time.time())))
                        month_report(buy_car[name], buy_car[nums], buy_car[price], now_time)
                    # print(‘当前您的账户信息:\n‘, users)
                    # for buy_users in users:
                    print(我的账户信息:\n账户名: %s  余额: %s元 % (user_dict[name], my_new_money))
                    break
                else:
                    print(钱不够了请去充值,提交失败\n)
                    break
            elif my_confirm == N or my_confirm == n:
                print(您已取消\n)
                break
            else:
                no_this_id = 0
                if my_confirm.isdigit():
                    # print(type(len(cart_list)))
                    for my_cart in cart_list:
                        if my_cart[id] == int(my_confirm):
                            cart_list.remove(my_cart)
                            print(移除成功!)
                            no_this_id = 1
                            break
                elif no_this_id == 0:
                    print(没有这个编号,请重新输入\n)
                else:
                    print(输入有误,请重新输入\n)


# 商品变化模块从列表保存到文件
def change_goods():
    with open(goods_new.txt, w, encoding=utf-8) as goods_new_w:
        for goods_dict in mygoodslist:
            goods_new_w.write(%s %s %s\n % (goods_dict[name], goods_dict[price], goods_dict[nums]))
    os.remove(goods.txt)
    os.rename(goods_new.txt, goods.txt)


# ATM登录中心模块
@user_login
def atm_center():
    print(1.查看账户信息\n2.存钱\n3.提现\n4.转账\n5.消费单\n6.还款\n7.后台管理\n8.退出ATM)
    with open (user_session.txt,r,encoding=utf-8) as session_user_r:
        session_user = session_user_r.read()
        users = users_txt_to_list()
        for user in users:
            if session_user == user[name]:
                user_dict = user
    user_chose = input(请输入编号: )
    if user_chose == 1:
        user_center(user_dict)
    elif user_chose == 2:
        recharge(user_dict)
    elif user_chose == 3:
        withdraw_cash(user_dict)
    elif user_chose == 4:
        transfer_money(user_dict)
    elif user_chose == 5:
        look_month_report(user_dict)
    elif user_chose == 6:
        repayment_money(user_dict)
    elif user_chose == 7:
        admin_center()
    elif user_chose == 8:
        exit_session(user_dict)
    else:
        print(输入信息有误,请重新输入)
        atm_center()


# 账户金额变化模块从列表保存到文件
def change_money(user_dicts, new_money):
    users_list = users_txt_to_list()
    with open(users_new.txt, w, encoding=utf-8) as users_new_w:
        for user_dict in users_list:
            if user_dict[name] == user_dicts[name]:
                user_dict[money] = new_money
                users_new_w.write(%s %s %s\n % (user_dict[name], user_dict[password], user_dict[money]))
            else:
                users_new_w.write(%s %s %s\n % (user_dict[name], user_dict[password], user_dict[money]))
    os.remove(users.txt)
    os.rename(users_new.txt, users.txt)


# 查看账户信息模块
def user_center(user_dicts):
    login_user = user_dicts
    print(=======用户中心=======\n 用户名: %s  余额: %s % (login_user[name], login_user[money]))
    now_time = str(time.asctime(time.localtime(time.time())))
    atm_operation_log(user_dicts, see_user_center, now_time)
    user_chose = input(返回上一级请输入(R or r))
    if user_chose == R or user_chose == r:
        atm_center()


# 充值模块
def recharge(user_dicts):
    flag1 = True  # 循环标记
    # while flag1:
    #     flag2 = True  # 循环标记
    add_money = input(请输入充值金额(数字): )
    if add_money.isdigit():
        old_money = int(user_dicts[money])
        new_money = old_money + int(add_money)
        user_dicts[money] = new_money
        print(您现在的余额是 %s元 % user_dicts[money])
        change_money(user_dicts, new_money)
        print(充值成功!)
        now_time = str(time.asctime(time.localtime(time.time())))
        atm_operation_log(user_dicts, recharge, now_time)
        atm_center()
    else:
        print(输入的不是数字请重新输入\n)


# 提现模块
def withdraw_cash(user_dicts):
    print(提现手续费5%)
    withdraw_money = input(请输入提现金额(按R或者r返回上一级): )
    if withdraw_money.isdigit():
        new_money = int(user_dicts[money]) - int(withdraw_money) - int(int(user_dicts[money]) * 0.05)
        change_money(user_dicts, new_money)
        print(提现成功!)
        now_time = str(time.asctime(time.localtime(time.time())))
        atm_operation_log(user_dicts, withdraw_cash, now_time)
        atm_center()
    else:
        print(输入的不是数字请重新输入\n)


# 转账模块
def transfer_money(user_dicts):
    transfer_money_account = input(请输入转入账户: )
    transfer_get_money = input(请输入转入金额: )
    if transfer_get_money.isdigit():
        new_money = int(user_dicts[money]) - int(transfer_get_money)
        users_list = users_txt_to_list()
        with open(users_new.txt, w, encoding=utf-8) as users_new_w:
            for user_dict in users_list:
                if user_dict[name] == user_dicts[name]:
                    user_dict[money] = new_money
                    users_new_w.write(%s %s %s\n % (user_dict[name], user_dict[password], user_dict[money]))
                elif user_dict[name] == transfer_money_account:
                    user_dict[money] = int(user_dict[money]) + int(transfer_get_money)
                    users_new_w.write(%s %s %s\n % (user_dict[name], user_dict[password], user_dict[money]))
                else:
                    users_new_w.write(%s %s %s\n % (user_dict[name], user_dict[password], user_dict[money]))
        os.remove(users.txt)
        os.rename(users_new.txt, users.txt)
        print(转账成功!)
        now_time = str(time.asctime(time.localtime(time.time())))
        atm_operation_log(user_dicts, repayment_money, now_time)
        atm_center()
    else:
        print(输入的不是数字请重新输入\n)


# 每月消费模块
def month_report(goods,num,money,buy_time):
    with open(month_report.txt,a,encoding=utf-8) as month_report_w:
        month_report_w.write(商品名称: %s 商品数量: %s 价格: %s 购买时间: %s \n %(goods,num,money,buy_time))


# 查看每月消费信息
def look_month_report(user_dicts):
    look_month_report_flag = False
    with open(month_report.txt, r, encoding=utf-8) as look_month_report_r:
        for line in look_month_report_r:
            look_user_list = line.split()
            if look_user_list[0] == user_dicts[name]:
                look_month_report_flag = True
                print(line.strip(\n))
    if look_month_report_flag == False:
        print(没有找到内容)
    atm_center()


# 还款模块
def repayment_money(user_dicts):
    return_money = input(请输入还款金额: )
    if return_money.isdigit():
        if return_money.isdigit():
            new_money = int(user_dicts[money]) + int(return_money)
            change_money(user_dicts, new_money)
            print(还款成功!)
            now_time = str(time.asctime(time.localtime(time.time())))
            atm_operation_log(user_dicts,repayment_money, now_time)
            atm_center()
        else:
            print(输入的不是数字请重新输入\n)
    else:
        print(输入的不是数字请重新输入\n)


# ATM操作日志模块
def atm_operation_log(user_dicts,operation_info, time):
    log_str = user_dicts[name] +   + operation_info +   + time + \n
    with open(atm_log.txt, a, encoding=utf-8) as atm_operation_log_a:
        atm_operation_log_a.write(log_str)


# 退出ATM会话模块
def exit_session(user_dicts):
    with open(user_session.txt,w,encoding=utf-8) as exit_session_w:
        pass
    print(账户: %s 已退出 %(user_dicts[name]))
    main()


# ATM后台管理模块
@admin_user_login
def admin_center():
    print(1.查看账户信息\n2.添加账户\n3.修改账户额度\n4.冻结账户\n5.返回)
    with open (user_session.txt,r,encoding=utf-8) as session_user_r:
        session_user = session_user_r.read()
        users = users_txt_to_list()
        for user in users:
            if session_user == user[name]:
                user_dict = user
    user_chose = input(请输入编号: )
    if user_chose == 1:
        user_center(user_dict)
    elif user_chose == 2:
        admin_add_user()
    elif user_chose == 3:
        admin_user_quota()
    elif user_chose == 4:
        admin_user_lock()
    elif user_chose == 5:
        exit_session(user_dict)
    else:
        print(输入信息有误)


# 用户列表转换成文件
def change_user_to_file(user_list):
    with open(users_new.txt, w, encoding=utf8) as add_user_w:
        for user_dict in user_list:
            add_user_w.write(%s %s %s %s %s\n % (user_dict[name], user_dict[password],
                                             user_dict[money], user_dict[grant],
                                             user_dict[status]))
    os.remove(users.txt)
    os.rename(users_new.txt, users.txt)


# 添加账户模块
def admin_add_user():
    add_new_user = input(新用户名: )
    user_list = users_txt_to_list()
    while True:
        add_new_password = input(密码:)
        add_new_password2 = input(请再输入一遍密码: )
        if add_new_password == add_new_password2:
            user_list.append({name:add_new_user,password:add_new_password,money: 15000,
                              grant: ordinary,status:open})

            change_user_to_file(user_list)
            print(账户创建成功)
            break
        else:
            print(两次密码输入的不一致,请重新输入)


# 修改账户额度模块
def admin_user_quota():
    user_name = input(请输入账户名: )
    quota_money = input(请输入账户额度: )
    user_list = users_txt_to_list()
    for user in user_list:
        if user[name] == user_name:
            user[money] = quota_money
    change_user_to_file(user_list)
    print(额度修改成功)


# 冻结账户模块
def admin_user_lock():
    lock_name = input(请输入冻结账户: )
    user_list = users_txt_to_list()
    for user in user_list:
        if user[name] == lock_name:
            user[status] = lock
    change_user_to_file(user_list)
    print(账户冻结成功)


# 主程序模块
def main():
    flag = True  # 最外层循环标记
    while flag:
        # my_cart_id = -1
        # flag_list = True
        mainlist = ========我的网上商城=======\n1.ATM机\n2.购物商城
        print(mainlist)
        choose = input(请选择编号(1 or 2),退出请按(E or e): )
        if choose == E or choose == e:
            break
        else:
            if choose.isdigit() and choose == 2:
                goods_center()
            elif choose.isdigit() and choose == 1:
                atm_center()
            else:
                print(输入的选项错误,请重新输入\n)


if __name__ == __main__:
    mygoodslist = goods_txt_to_list()
    main()

 

ATM+购物商城(第一版)