zoukankan      html  css  js  c++  java
  • 作业: 小型购物系统1---按顺序编写

    作业:
     用户入口:
     1. 商品信息存在文件里
     2. 已够商品,余额记录
     
     商家入口
     2. 可以添加商品,修改商品价格

     数据文件:商品库:product_store.db

    {'book': {'price': 45, 'stocks': 80}, 'watch': {'price': 210, 'stocks': 27}, 'phone': {'price': 1999, 'stocks': 29}, 'apple': {'price': 6, 'stocks': 21}, 'orange': {'price': 4, 'stocks': 23}, 'computer': {'price': 4800, 'stocks': 5}, 'cap': {'price': 56, 'stocks': 13}}

    数据文件:购物车:cart_list.db

    []

    编码:

    # Author:Brace Li
    import sys,time
    # list entrance for user
    print("*" * 60)
    print("*%s*" % " ".center(58))
    print("*%s*" % "Shopping System".center(58))
    print("*%s*" % "v1.00".center(58))
    print("*%s*" % "1.For Host   2.For Customer".center(58))
    print("*%s*" % " ".center(58))
    print("*" * 60)
    # for user selected
    while True:
        user_selected = input("Pls Input:").strip()
        if not (user_selected and user_selected.isdigit()):
            print("33[1;31;40mError: input error!33[0m ")
            continue
        if not int(user_selected) in (1, 2):
            print("33[1;31;40mError: input error!33[0m ")
            continue
        else:
            break
    # for shop host
    if int(user_selected) == 1:
        # get product date from database
        with open("product_store.db", "r") as fs:
            product_data = eval(fs.read())
        print("-" * 60)
        print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "), balance="Balance".center(16, " ")))
        print("-" * 60)
        for n in product_data:
            # print(n, product_data[n]["price"], product_data[n]["stocks"])
            print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
            print("-" * 60)
        # info for action of host
        flag = True
        while True:
            if flag:
                print("*" * 60)
                print("*%s*" % "1.Edit Price   2.Add Products  3. Exit System".center(58, " "))
                print("*" * 60)
            host_selected = input("Pls input:").strip()
            if not (host_selected and host_selected.isdigit()):
                print("33[1;31;40mError: input error!33[0m ")
                flag = False
                continue
            if not int(host_selected) in (1, 2, 3):
                print("33[1;31;40mError: input error!33[0m ")
                flag = False
                continue
            flag = True
            if int(host_selected) == 3:
                with open("product_store.db", "w") as fs:
                    fs.write(str(product_data))
                sys.exit("Bye, the system exiting......")
            elif int(host_selected) == 1:
                edit_pro_flag = {"status": False, "sel_pro": ""}
                while True:
                    product_selected = input("Input product:").strip()
                    if not product_selected:
                        print("33[1;31;40mError: input error!33[0m ")
                        continue
                    elif product_selected in product_data:
                        edit_pro_flag["status"] = True
                        edit_pro_flag["sel_pro"] = product_selected
                        break
                    else:
                        print("33[1;31;40mError: Product not exist...!33[0m ")
                        continue
                if edit_pro_flag["status"]:
                    while True:
                        new_pro_price = input("Input New Price:").strip()
                        if not (new_pro_price and new_pro_price.isdigit()):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        else:
                            product_data[edit_pro_flag["sel_pro"]]["price"] = int(new_pro_price)
                            break
                    print("-" * 60)
                    print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "),balance="Balance".center(16, " ")))
                    print("-" * 60)
                    for n in product_data:
                        print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
                        print("-" * 60)
                    edit_pro_flag = {"status": False, "sel_pro": ""}
            else:
                while True:
                    pro_name = input("Input Product Name:").strip()
                    if not pro_name:
                        print("33[1;31;40mError: input error!33[0m ")
                        continue
                    else:
                        break
                if pro_name in product_data:
                    while True:
                        add_count = input("the %s  exit, add stocks:" % pro_name).strip()
                        if not (add_count and add_count.isdigit()):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        else:
                            product_data[pro_name]["stocks"] += int(add_count)
                            break
                else:
                    while True:
                        add_price = input("Input %s's prince:"% pro_name).strip()
                        if not (add_price and add_price.isdigit()):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        else:
                            break
                    while True:
                        add_count = input("Input %s's stocks:" % pro_name)
                        if not (add_count and add_count.isdigit()):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        else:
                            break
                    product_data[pro_name] = {'price': int(add_price), 'stocks': int(add_count)}
                print("-" * 60)
                print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "),balance="Balance".center(16, " ")))
                print("-" * 60)
                for n in product_data:
                    print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
                    print("-" * 60)
    # for customer
    if int(user_selected) == 2:
        # load cart data
        with open("cart_list.db", "r") as fs:
            cart_list = eval(fs.read())
        # load list product
        with open("product_store.db", "r") as fs:
            product_data = eval(fs.read())
        while True:
            print("-" * 60)
            print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "), balance="Balance".center(16, " ")))
            print("-" * 60)
            for n in product_data:
                print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
                print("-" * 60)
            while True:
                select_product = input("Input your products:").strip()
                if not select_product:
                    print("33[1;31;40mError: input error!33[0m ")
                    continue
                if select_product in product_data:
                    cart_list.append(select_product)
                    print("33[1;33;40mMsg: Product been in cart successfully!33[0m ")
                    break
                else:
                    print("33[1;31;40mError: Product not exit!33[0m ")
                    continue
            while True:
                print("*" * 60)
                print("*%s*" % "1.Continue  2.Charge  3. Exit System".center(58, " "))
                print("*" * 60)
                customer_action = input("Input select: ").strip()
                if not (customer_action and customer_action.isdigit()):
                    print("33[1;31;40mError: input error!33[0m ")
                    continue
                if not int(customer_action) in (1, 2, 3):
                    print("33[1;31;40mError: input error!33[0m ")
                    continue
                if int(customer_action) == 1:
                    break
                elif int(customer_action) == 3:
                    with open("cart_list.db", "w") as fs:
                         fs.write(str(cart_list))
                    sys.exit("System exiting .....!")
                else:
                    # list product info in cart
                    cart_info = list(set(cart_list))
                    print("")
                    print("")
                    print("*" * 60)
                    print("|%s|" %"Products List In Cart".center(58, " "))
                    print("-" * 60)
                    print("|{id}|{name}|{price}|{quantity}|{total}|".format(id="ID".center(5, " "), name="Name".center(19, " "), price="Price".center(10, " "), quantity="Quantity".center(10, " "), total="Total".center(10, " ")))
                    print("-" * 60)
                    total_price = 0
                    for c_id, c_name in enumerate(cart_info):
                        print("|{id}|{name}|{price}|{quantity}|{total}|".format(id=str(c_id).center(5, " "), name=c_name.center(19, " "), price=str(product_data[c_name]["price"]).center(10, " "), quantity=str(cart_list.count(c_name)).center(10, " "),total=str(product_data[c_name]["price"] * cart_list.count(c_name)).center(10, " ")))
                        print("-" * 60)
                        total_price += product_data[c_name]["price"] * cart_list.count(c_name)
                    print("|{data_name}|{date_value}|{total_name}|{total_value}|".format(data_name="Date".center(10," "), date_value=str(time.strftime('%Y-%m-%d',time.localtime())).center(15, " "), total_name="Total".center(10, " "), total_value=str(total_price).center(20, " ")))
                    print("-" * 60)
                    print()
                    print("*" * 60)
                    print("*%s*" % "1.Back to Menu  2.Pay Account".center(58, " "))
                    print("*" * 60)
                    while True:
                        c_selected = input("Pls Input:").strip()
                        if not (c_selected and c_selected.isdigit()):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        if not int(c_selected) in (1, 2):
                            print("33[1;31;40mError: input error!33[0m ")
                            continue
                        else:
                            break
                if int(c_selected) == 2:
                    cart_list.clear()
                    with open("cart_list.db", "w") as fs:
                        fs.write(str(cart_list))
                        sys.exit("System exiting .....!")
                else:
                    break
  • 相关阅读:
    美国独立电影人制作的纪录片《南京梦魇——南京大屠杀》
    我的黑莓电子书阅读解决方案
    NativeExcel 破解笔记
    右键刷新弹出网页广告的解决办法
    可恶的硬件故障(已解决)
    XP系统无法出现关机界面的解决一例
    任务栏无任务显示的问题
    centos 删除指定文件之外的其他文件
    Canvas中的save方法和restore方法
    博弈论取石子问题
  • 原文地址:https://www.cnblogs.com/brace2011/p/9185421.html
Copyright © 2011-2022 走看看