day2购物商城

Posted (野生程序员)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day2购物商城相关的知识,希望对你有一定的参考价值。

  作业:购物商城

    商品展示,价格

    买,加入购物车

    付款,钱不够

代码如下:

import codecs
#登录接口,用户名密码都正确登录成功,否则失败

def login(your_name,your_password):
    with open("user_file","r") as l:
        for line in l:
            user_name,user_password,user_balance=line.strip().split(":")
            if your_name == user_name and your_password == user_password:
                flag = 1
                break
            else:
                print("用户名或密码错误")
                flag = 0
    return flag

#商品展示列表
def show_goods():
    print(10** + "商品列表" + 10*"*")
    print(30 * "-")
    print("| 编号 | 名称 | 价格 | 数量 | ")
    try:
        with codecs.open("goods_file","r","utf-8") as f1:
            for line in f1:
                (goods_id,goods_name,goods_rate,goods_num) = line.strip().split(":")
                print(30 * "-")
                print(| %s | %s | %s | %s | %(goods_id,goods_name,goods_rate,goods_num))
            print(30*"-")
        # print("购买商品请输入产品编号/返回请输入b/退出请输入q")
        # the_num = input("请选择:")
    except ValueError:
        pass

#展示购物车
def show_gouwuche(your_name):
    with codecs.open("gouwuche","r","utf-8") as f2:
        n = 0
        print(30 * "-")
        print("| 编号 | 名称 | 数量 | 总价 |")
        for line in f2:
            user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
            if user_name == your_name:
                n += 1
                print(30 * "-")
                print(" | %s | %s | %s | %s |" %(n,goods_name,goods_num,goods_rate))
        print(30*"-")

#查看当前用户已购产品
def show_yigou(your_name):
    with codecs.open("yigoushangpin","r","utf-8") as f3:
        n = 0
        print(30*"-")
        print("| 编号 | 名称 | 数量 | 总价 |")
        for line in f3:
            user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
            if user_name == your_name:
                n += 1
                print(30*"-")
                print(| %s | %s | %s | %s | %(n,goods_name,goods_num,goods_rate))
        print(30*"-")

#账户余数查看
def show_money(your_name):
    with open("user_file","r") as l:
        for line in l:
            user_name,user_password,user_blance = line.strip().split(":")
            if your_name == user_name:
                print("您当前账户余额为:%s" %user_blance)
                break


# 账户余额查看
def show_money(your_name):
    with open(user_file, r) as l:
        for line in l:
            user_name, user_password, user_balance = line.strip().split(:)
            if your_name == user_name:
                print(您当前账户余额为:%s % user_balance)
                break

#加入购物车,如果购物车中已经存在该商品则将商品数量相加,商品价格相加;如果购物车中不存在该商品,则添加到购物车中,并将可购买的库存量
def add_gouwuche(n,your_name):
    with codecs.open("goods_file","r+","utf-8") as f1:
        for line1 in f1:
            goods_id,goods_name,goods_rate,goods_num = line1.strip().split(":")
            if n == goods_id:
                goods_quantity = input("请输入数量:")
                #如果输入的并非数字则不合法,输入的数量大于库存,均需要重新输入
                while True:
                    if goods_quantity.isdigit():
                        if int(goods_num) < int(goods_quantity):
                            goods_quantity = input("您输入的数量大于当前库克数量,请重新输入数量:")
                        else:
                            break
                    else:
                        goods_quantity = input("您输入的不合法,请重新输入数量:")
                with codecs.open("gouwuche","r+","utf-8") as f2:
                    for line2 in f2:
                        che_user_name,che_goods_name,che_goods_num,che_goods_rate = line2.strip().split(":")
                        #判断当前选择的商品是否在购物车存在,如果存在标志位che_flag = 1
                        if your_name == che_user_name and goods_name == che_goods_name:
                            che_flag = 1
                            che_goods_num = str(int(che_goods_num) + int(goods_quantity))
                            che_goods_rate = str(int(che_goods_rate) + int(goods_rate)*int(goods_quantity))
                            old_line = line2
                            new_line = che_user_name + ":" + che_goods_name + ":" + che_goods_num + ":" + che_goods_rate + "\n"
                            break
                    #当前选择的商品购物车中不存在,标志位che_flag = 0
                    else:
                        che_flag = 0
                        che_goods_rate = str(int(goods_rate) * int(goods_quantity))
                        che_goods_num = goods_quantity
                        che_goods_name = goods_name
                        che_user_name = your_name
                        new_line = ("%s:%s:%s:%s\n" %(che_user_name,che_goods_name,che_goods_num,che_goods_rate))
                        #购物车中存在,则将购物车中的数量更新,总价格更新
                        if che_flag == 1:
                            f2.seek(0)
                            old_gouwuche = f2.read()
                            new_gouwuche = old_gouwuche.replace(old_line,new_line)
                            f2.seek(0)
                            f2.truncate()
                            f2.write(new_gouwuche)
                        #购物车中不存在,则在购物车中添加
                        elif che_flag == 0:
                            f2.seek(0)
                            old_gouwuche =f2.read()
                            new_gouwuche = old_gouwuche + new_line
                            f2.seek(0)
                            f2.truncate()
                            f2.write(new_gouwuche)
                #加入购物车后,将商品列表中的数量更新
                new_goods_num = str(int(goods_num) - int(goods_quantity))
                old_goods_line = line1
                new_goods_line = ("%s:%s:%s:%s\n" %(goods_id,goods_name,goods_rate,new_goods_num))
                f1.seek(0)
                old_goods_file = f1.read()
                new_goods_file = old_goods_file.replace(old_goods_line,new_goods_line)
                f1.seek(0)
                f1.truncate()
                f1.write(new_goods_file)
                goods_flag = True
                break
        else:
            goods_flag = False
            print("抱歉您选择的商品不存在,请重新选择")
        return goods_flag

#删除购物车的商品
def del_gouwuche(del_gouwuche_num,your_name):
    with codecs.open("gouwuche","r+","utf-8") as f1:
        while True:
            n = 0
            for line in f1:
                user_name,goods_name,goods_num,goods_rate = line.strip().split(":")
                #判断删除的商品是否存在,若存在撒谎拿出标志位del_flag = 1
                if user_name == your_name:
                    n += 1
                    if int(del_gouwuche_num) == n:
                        del_goods_name = goods_name
                        del_goods_num = goods_num
                        del_goods_line = line1
                        del_flag = 1
                        break
                #商品存在,进行删除
                if del_flag == 1:
                    f1.seek(0)
                    gouwuche_file = f1.readlines()
                    gouwuche_file.remove(del_goods_line)
                    f1.seek(0)
                    f1.truncate()
                    for line in gouwuche_file:
                        f1.write(line)
                    #更新商品清单,将从购物车删除的加到商品清单中
                    with codecs.open("goods_file","r+","utf-8") as f2:
                        for line2 in f2:
                            old_goods_line = list(line2.strip().split(":"))
                            old_line = line2.strip()
                            if old_goods_line[1] == del_goods_name:
                                old_goods_line[3] = str(int(old_goods_line[3])) + int(del_goods_num)
                                new_goods_line = old_goods_line
                                del_flag = 1
                                break
                        if del_flag == 1:
                            f2.seek(0)
                            new_line = ":".join(new_goods_line)
                            old_goods_file = f2.read()
                            new_goods_file = old_goods_file.replace(old_line,new_line)
                            f2.seek(0)
                            f2.truncate()
                            f2.write(new_goods_file)
                break
            #要删除的商品不存在,标志位del_flag = 0
            else:
                del_flag = 0
                print("您要删除的商品不存在,请重新输入编号!")
                break
    return del_flag

#支付购物车商品
def pay_gouwuche(gouwuche_num,your_name):
    with codecs.open("gouwuche","r+","utf-8") as f1:
        while True:
            n = 0
            for line1 in f1:
                user_name,goods_name,goods_num,goods_rate = line1.strip().split(":")
                #判断删除的商品是否存在,若存在删除标志位del_flag = 1
                if user_name == your_name:
                    n += 1
                    if int(gouwuche_num) == n:
                        pay_goods_name = goods_name
                        pay_goods_num = goods_num
                        pay_goods_rate = goods_rate
                        pay_goods_line = line1
                        #查看当前用户余额是否能够支付商品
                        with open("user_file","r+") as user_file:
                            for line in user_file:
                                user_name,user_password,user_balance = line.strip().split(":")
                                if your_name == user_name:
                                    old_balance_line = line
                                    new_balance = int(user_balance) - int(goods_rate)
                                    break
                            if new_balance < 0:
                                print("您的当前预余额不足,请充值后再支付!\n")
                                pay_flag = False
                                break
                            #大于等于0时,则可以支付,并更新账户余额
                            else:
                                pay_flag = True
                                new_balance_line = user_name + ":" + user_password + ":" + str(new_balance) + "\n"
                                user_file.seek(0)
                                old_user_file = user_file.read()
                                new_user_file = old_user_file.replace(old_balance_line,new_balance.line)
                                user_file.seek(0)
                                user_file.truncate()
                                user_file.write(new_user_file)
                                break
                    else:
                        pay_flag = ""
            #商品存在,支付成功后进行删除
            if pay_flag == True:
                f1.seek(0)
                gouwuche_file = f1.readlines()
                gouwuche_file.remove(pay_goods_line)
                f1.seek(0)
                f1.truncate()
                for line in gouwuche_file:
                    f1.write(line)
                #更新已购买的商品清单,将已支付的商品加入到购买的清单中
                with codecs.open("yigoushangpin","r+","utf-8") as f2:
                    for line2 in f2:
                        old_yigou_line = list(line2.strip().split(":"))
                        old_line = line2.strip()
                        #当已购商品清单存在该商品购买记录则将数量相加,总价格相加
                        if old_yigou_line[1] == pay_goods_name and old_yigou_line[0] == your_name:
                            old_yigou_line[2] = str(int(old_yigou_line[2]) + int(pay_goods_num))
                            old_yigou_line[3] = str(int(old_yigou_line[3]) + int(pay_goods_rate))
                            new_yigou_line = old_yigou_line
                            f2.seed(0)
                            new_line = ":".join(new_yigou_line)
                            old_yigou_file = f2.read()
                            new_yigou_file = old_yigou_file.replace(old_line,new_line)
                            f2.seed(0)
                            f2.truncate()
                            f2.write(new_yigou_file)
                            break
                    #当已购商品清单灭有购买记录,则附加购买记录
                    else:
                        f2.seek(0)
                        old_yigou_file = f2.read()
                        new_yigou_file = old_yigou_file + pay_goods_line
                        f2.seek(0)
                        f2.truncate()
                        f2.write(new_yigou_file)
                print("\n")
                print("恭喜你,支付成功!")
                break
            elif pay_flag == False:
                flag = True
                break
            #要删除的商品不存在,标志位del_flag = 0
            else:
                pay_flag = False
                print("您要支付的商品不存在,请重新输入编号!")
                break
    return pay_flag

#账户充值
def add_money(blance_num,your_name):
    with open("user_file","r+") as f:
        for line in f:
            user_name,user_passwd,user_balance = line.strip().split(":")
            if user_name == your_name:
                old_user_line = line
                new_user_balance = str(int(user_balance) + int(balance_num))
                new_user_line = user_name + ":" + user_passwd + ":" + new_user_balance + "\n"
                break
        f.seek(0)
        old_user_file = f.read()
        new_user_file = old_user_file.replace(old_user_line,new_user_line)
        f.seek(0)
        old_user_file = f.read()
        new_user_file = old_user_file.replace(old_user_line,new_user_line)
        f.seek(0)
        f.truncate()
        f.write(new_user_file)

def main():
    your_name = input("请输入用户名:")
    your_password = input("请输入密码:")
    menu_num = ""
    #登录,用户名或密码错误重新输入
    while True:
        if login(your_name,your_password):
            print("欢迎%s登录购物商城" %your_name)
            print(30 * "*")
            break
        else:
            your_name = input("请重新输入用户名:")
            your_password = input("请重新输入密码:")
    while True:
        print(30 * "*")
        print("欢迎来到主菜单,您可进行如下操作!")
        print(30 * "*")
        print("| 1 商品列表 | 2 查询购物车 | 3  查看已购商品 | 4 账户余额 | q 退出商城 |")
        if menu_num == "":
            menu_num = input("请输入:")
        #进入商品列表
        elif menu_num == "1":
            while True:
                show_goods()
                print("| 请输入商品编号加入购物车 | 输入b返回主菜单 | 输入q退出商城 |")
                your_goods_num = input("请选择:")
                print("\n")
                #判断输入的是否为纯数字,如果是
                if your_goods_num.isdigit():
                    while add_gouwuche(your_goods_num,your_name):
                        #当商品加入购物车后直接返回给购物车
                        menu_num = "2"
                        break
                    break
                elif your_goods_num == b:
                    menu_num = ""
                    break
                elif your_goods_num == "q":
                    print("您已退出商城!")
                else:
                    print("您输入的不合法,请重新输入!\n")
        #查看购物车
        elif menu_num == "2":
            while True:
                print(30 * "*")
                print("您当前购物车商品如下所示:")
                show_gouwuche(your_name)
                print("| 1 返回主菜单 | 2 删除商品 | 3 支付商品 | q 退出商城 |")
                gouwuche_menu_num = input("请输入:")
                print("\n")
                #返回主菜单
                if gouwuche_menu_num == 1:
                    menu_num = ‘‘
                    break
                #删除商品
                elif gouwuche_menu_num == 2:
                    while True:
                        del_gouwuche_num = input("请输入要删除的商品编号,或者输入b返回:")
                        if del_gouwuche_num == "b":
                            break
                        elif not del_gouwuche_num.isdigit():
                            print("您输入的不合法,请重新输入!\n")
                            continue
                        elif del_gouwuche(del_gouwuche_num,your_name):
                            print("删除成功\n")
                            break
                #支付商品
                elif gouwuche_menu_num == "3":
                    while True:
                        pay_gouwuche_num = input("请输入要支付的商品编号,返回请输入b:")
                        if pay_gouwuche_num.isdigit():
                            if pay_gouwuche(pay_gouwuche_num,your_name):
                                break
                        elif pay_gouwuche_num == "b":
                            break
                        else:
                            print("您输入的不合法,请重新输入!")
                elif gouwuche_menu_num == "q":
                    print("您已退出商城!")
                    exit()
                else:
                    print("您输入的不合法,请重新输入\n")
        #查看已购买记录
        elif menu_num == "3":
            print(30 * "*")
            print("您已购买商品如下所示:")
            show_yigou(your_name)
            print("| 输入b返回主菜单 | 输入q退出程序 |")
            while True:
                yigou_input = input("请输入:")
                if yigou_input == "b":
                    menu_num = ""
                    print("\n")
                    break
                elif yigou_input == "q":
                    print("您已退出商城!\n")
                else:
                    print("你输入的不合法,请重新输入!\n")
        #查看账户信息
        elif menu_num == "4":
            while True:
                print(30 * "-")
                print("您的账户信息如下所示:")
                show_money(your_name)
                print("| 1 充值账户 | b 返回主菜单 | q 退出商城 |")
                money_num == input("请输入:")
                if money_num == "1":
                    print(30 * "-")
                    add_money_num = input("请输入充值金额,输入b返回:")
                    if add_money_num.isdigit():
                        add_money_num = input("请输入充值金额,输入b返回:")
                        if add_money_num.isdigit():
                            add_money(add_money,your_name)
                            print("恭喜你,充值成功!\n")
                            continue
                        elif add_money_num == "b":
                            continue
                        else:
                            print("您输入的不合法,请重新输入!\n")
                            continue
                        elif money_num == "b":
                            menu_num = ""
                            break
                        elif money_num == "q":
                            print("您已退出商城!")
                            exit()
                        else:
                            print("您输入的不合法,请重新输入\n")
            elif menu_num == "q":
                print("您已退出商城!")
                exit()
            else:
                print("您的输入不合法,请重新输入!\n")

if __name__ == __main__:
    main()

 

    上面代码是网上找的,准备自己好好写一遍,开始的时候没有什么思路,里面主要是对文件的操作,还有一些新的方法,都是常用的方法,但是平时自己不会用的。

 

以上是关于day2购物商城的主要内容,如果未能解决你的问题,请参考以下文章

day2 购物商城

day2编写购物商城

day2编写购物商城

Day2作业:购物商城

DAY2练习-购物车

python运维开发实践--Day2