購物車的升級系統

購物車的升級系統

要求以下:git

FUNC_MSG = {
'0': '註銷',cancel
'1': '登陸',login
'2': '註冊',refister
'3': '查看餘額',Check the balance
'4': '轉帳',transfewr accounts
'5': '還款',repayment
'6': '取款',withdrawal
'7': '查看流水',Check the current account
'8': '購物',shopping
'9': '購物車',shopping cart
'q': '退出'quit
}
account information 帳戶信息app

# -*- encoding:utf-8 -*-

import os

product_list = [
    ['Iphone7', 5800],
    ['Coffee', 30],
    ['python Book', 99],
    ['Bike', 199],
    ['viv0 X9', 2499]
]
db_file = r'account_information.txt'
account_information = list()  # 帳戶信息
information = list()
uname_pwd = list()  # 用戶名,與密碼
shopping_cart = dict()  # 購物車
yao_balance = list()  # 別人的
message_info=list() #本身的流水


# 00刪除用戶信息
def del_cancel():
    if len(uname_pwd) == 0:
        login()
        del_cancel()
    elif len(uname_pwd) == 3:
        longinhou()
    else:
        print('已經被註銷,請登錄後刪除信息')


# 若是刪除信息,檢測沒登錄執行
def longinhou():
    uname = uname_pwd[0]
    pwd = uname_pwd[1]
    print('開始刪除')
    str_file = db_file
    dst_file = '%s.swap' % db_file
    with open(str_file, 'r', encoding='utf8') as rf, \
            open(dst_file, 'w', encoding='utf8') as wf:
        for line in rf:
            line = line.strip('\n').split(',',3)
            if line[0] == uname and pwd == line[1]:
                continue
            else:
                line = ','.join(line) + '\n'
                wf.write(line)
    os.remove(str_file)
    os.rename(dst_file, str_file)
    print('刪除成功')


# 0註銷
def cancel():
    if len(uname_pwd) == 0:
        login()
        cancel()
    elif len(uname_pwd) == 2:
        print('已經註銷')
    else:
        print('開始註銷')
        uname_pwd.pop(0)
        print('註銷成功')


# 1登錄
def login():
    count = 0
    tag = True
    while tag:
        count += 1
        uname = input('請輸入用戶名>:').strip()
        pwd = input('請輸入密碼>:').strip()

        with open(db_file, 'r', encoding='utf8') as f:
            for line in f:
                line = line.strip('\n').split(',',3)
                if line[0] == uname and line[1] == pwd:
                    print('登錄成功')
                    uname_pwd.extend(line[:3])
                    print(uname_pwd)
                    tag = False
                    break
                else:
                    continue
        if count == 3:
            tag = False


# 2註冊
def refister():
    if len(uname_pwd) == 3:
        print('請退出先註銷在註冊')
    else:
        uname = input('請輸入用戶名>:').strip()
        with open(db_file, 'r', encoding='utf8') as fr:
            for line in fr:
                line = line.strip('\n').split(',',3)
                if line[0] == uname:
                    print('已經被註冊,請直接登錄')
                    return 0

        while True:
            pwd1 = input('請輸入密碼>:').strip()
            pwd2 = input('請確認密碼>:').strip()
            if pwd2 != pwd1:
                print('密碼不一致,請重新輸入')
                continue
            else:
                print('正在註冊,請等待')
                break
        balance = input('請充錢>:').strip()
        messgae = list() #type:list
        with open(db_file, 'a', encoding='utf8') as f:
            f.write(('%s,%s,%s,%s\n') % (uname, pwd1, balance,messgae))

        print('註冊成功')


# 3查看餘額
def Check_the_balance():
    if len(uname_pwd) == 0:
        login()
        print(uname_pwd[2])
    elif len(uname_pwd) == 3:
        print(uname_pwd[2])
    else:
        print('已經被註銷')


# 4轉帳
def transfewr_accounts():
    """給某人進行轉帳"""
    if len(uname_pwd) == 0:
        login()
        transfewr_accounts()
    elif len(uname_pwd) == 3:
        payee_name = input('請輸入收款人的姓名:').strip()
        with open(db_file, 'r', encoding='utf8') as fr:
            for line in fr:
                line = line.strip('\n').split(',',3)
                if payee_name in line:
                    yao_balance.append(int(line[2]))
                    print(12)
                    money = int(input('請輸入轉帳金額>>>:').strip())
                    yao_balance[0] += money
                    balance = int(uname_pwd[2])
                    balance -= money
                    uname_pwd[2] = balance
                    message_info_list =eval(line[3]) #type:list
                    info_list = f'收到{uname_pwd[0]}轉帳{money}'
                    info_list2 = f'向{payee_name}轉帳{money}'
                    message_info.append(info_list2)
        informat(payee_name,info_list)
        yao_balance.clear()
        print('轉帳成功,請查看')
        return message_info_list


    else:
        print('已經註銷,請登錄')


# 5還款
def repayment():
    """還款"""
    if len(uname_pwd) == 0:
        login()
        repayment()
    elif len(uname_pwd) == 3:
        bank = 'bank'
        with open(db_file, 'r', encoding='utf8') as fr:
            for line in fr:
                line = line.strip('\n').split(',',3)
                if bank in line:
                    yao_balance.append(int(line[2]))
                    money = int(input('請輸入還款金額>>>:').strip())
                    yao_balance[0] += money
                    balance = int(uname_pwd[2])
                    balance -= money
                    uname_pwd[2] = balance
                    message_info_list =eval(line[3]) #type:list
                    info_list = f'收到{uname_pwd[0]}轉帳{money}'
                    info_list2 = f'向{bank}轉帳{money}'
                    message_info.append(info_list2)
        informat(bank,info_list)
        print('還款成功,一共取出', money)
        yao_balance.clear()


    else:
        print('已經註銷,請登錄')


# 6取款
def withdrawal():
    """從銀行取款"""
    if len(uname_pwd) == 0:
        login()
        withdrawal()
    elif len(uname_pwd) == 3:
        bank = 'bank'
        with open(db_file, 'r', encoding='utf8') as fr:
            for line in fr:
                line = line.strip('\n').split(',',3)
                if bank in line:
                    yao_balance.append(int(line[2]))
                    money = int(input('請輸入取款金額>>>:').strip())
                    yao_balance[0] -= money
                    balance = int(uname_pwd[2])
                    balance -= money
                    uname_pwd[2] = balance
                    message_info_list =eval(line[3]) #type:list
                    info_list = f'收到{uname_pwd[0]}轉帳{money}'
                    info_list2 = f'向{bank}轉帳{money}'
                    message_info.append(info_list2)
        informat(bank,info_list)
        print('取款成功,一共取出', money)
        yao_balance.clear()


    else:
        print('已經註銷,請登錄')


# 7查看流水
#想不出來,如何調用
def Check_the_current_account():
    if len(uname_pwd) == 0:
        login()
        Check_the_current_account()
    elif not uname_pwd[0].isdigit():
        with open(db_file,'r',encoding='utf8') as fr:
            for line in fr:
                line = line.strip('\n').split(',',3)
                if uname_pwd[0] in line:
                    print(line[-1])
    else:
        print('請登錄')



# 8購物
def shopping():
    if len(uname_pwd) == 0:
        login()
        shopping()
    elif not uname_pwd[0].isdigit():
        uname = uname_pwd[0]
        balance = int(uname_pwd[2])
        tag = True
        while tag:
            for i, product in enumerate(product_list):
                print(i, product)
            choice = input('請輸入商品編號,輸入q退出>>').strip()
            if choice.isdigit():
                choice = int(choice)
                if choice < 0 or choice >= len(product_list): continue

                pname = product_list[choice][0]
                pprice = product_list[choice][1]
                if balance > pprice:
                    if pname in shopping_cart:
                        shopping_cart[pname]['count'] += 1
                        print(shopping_cart)
                    else:
                        shopping_cart[pname] = {
                            'pprice': pprice,
                            'count': 1
                        }
                    balance -= pprice
                    uname_pwd[2] = balance
                    info_list2 = f'購物消費了{balance}'
                    message_info.append(info_list2)

                else:
                    print("買不起,窮逼! 產品價格是{price},你還差{lack_price}".format(
                        price=pprice, lack_price=(pprice - balance)))
            elif choice == 'q':
                break
            else:
                print('請輸入正確的編號')
        informat()

    else:
        print('已經被註銷')
    print(shopping_cart)


# 9購物車
def Shopping_cart():
    if len(uname_pwd) == 0:
        login()
        Shopping_cart()
    elif len(uname_pwd) == 3:
        balance = int(uname_pwd[2])
        print("""
        --------------------已購買商品列表-------------------------
        id        商品           數量          單價         總價
        """)

        total_cost = 0
        for i, key in enumerate(shopping_cart):
            print('%10s%15s%10s%15s%12s' %
                  (i, key, shopping_cart[key]['count'],
                   shopping_cart[key]['pprice'],
                   shopping_cart[key]['pprice'] *
                   shopping_cart[key]['count']))
            total_cost += shopping_cart[key][
                              'pprice'] * shopping_cart[key]['count']
        print("""
        您的花費總額爲: %s
        您的餘額爲: %s
        --------------------------end---------------------------------
        """ % (total_cost, balance))
        shopping_cart.clear()

    else:
        print('已經被註銷')


### 更改信息
def informat(to_people=None,message=None):
    if to_people :
        print(message)
        src_file = db_file
        dst_file = r'%s.swap' % db_file
        with open(src_file, 'r', encoding='utf-8') as read_f, \
                open(dst_file, 'w', encoding='utf-8') as write_f:
            for line in read_f:
                if line.startswith(to_people):
                    l = line.strip('\n').split(',',3)
                    l[-2] = str(yao_balance[0])
                    l[-1]=eval(l[-1])#type:list
                    l[-1].append(message)
                    l[-1] = str(l[-1])
                    line = ','.join(l) + '\n'
                write_f.write(line)
        os.remove(src_file)
        os.rename(dst_file, src_file)
    uname = uname_pwd[0]
    pad = uname_pwd[1]
    balance = uname_pwd[2]
    src_file = db_file
    dst_file = r'%s.swap' % db_file
    with open(src_file, 'r', encoding='utf-8') as read_f, \
            open(dst_file, 'w', encoding='utf-8') as write_f:
        for line in read_f:
            if line.startswith(uname):
                l = line.strip('\n').split(',',3)
                l[-2] = str(balance)
                l[-1] = eval(l[-1])  # type:list
                l[-1].append(message_info)
                l[-1] = str(l[-1])
                line = ','.join(l) + '\n'
            write_f.write(line)
    os.remove(src_file)
    os.rename(dst_file, src_file)
    message_info.clear()


# 執行程序
while True:
    choice = input("請選擇00: 刪除用戶信息,0:註銷,1: 登陸,2:註冊,3:查看餘額,"
                   "4:轉帳,5:還款,6:取款,7:查看流水,8:購物,9:購物車,q:退出>>>:")
    if choice == '00':
        del_cancel()
    elif choice == '0':
        cancel()
    elif choice == '1':
        login()
    elif choice == '2':
        refister()
    elif choice == '3':
        Check_the_balance()
    elif choice == '4':
        transfewr_accounts()
    elif choice == '5':
        repayment()
    elif choice == '6':
        withdrawal()
    elif choice == '7':
        Check_the_current_account()
    elif choice == '8':
        shopping()
    elif choice == '9':
        Shopping_cart()
    elif choice == 'q':
        break


相關文章
相關標籤/搜索