购物车程序实现

 题目来源:老男孩教育Python视频

#  -*- coding: utf-8 -*-
"""
@Author:yhf1995
@time:2020/1/30 8:24
"""
salary = input("Please input your money number:")
product_list =[
    ("Apple watch", 4500),
    ("milk", 60),
    ("bike", 1000),
    ("Starbucks", 40),
    ("book",80)
]
shopping_list = []
if salary.isdigit():
    salary = int(salary)
# 判断输入的金额是否为数字,如果是将其转为int类型,如果不是,输出 输入有误
    while True:
        for item in product_list:
            print(product_list.index(item),item)
            # 打印商品列表
        choice_number = input("Please enter the number of the product you want:>>>")
        if choice_number.isdigit():
            choice_number = int (choice_number)
            # 判断输入的商品编号是否为数字,如果是转为int型,如果不是,提示输入有误
            if choice_number < len(product_list) and choice_number >= 0:
                # 判断商品是否存在
                if salary >= product_list[choice_number][1]:
                    shopping_list.append(product_list[choice_number])
                    salary -= product_list[choice_number][1]
                    print("add shop %s in your shopping cart,and your salary is \033[31;1m%s\033[0m!"%(product_list[choice_number],salary))
                    # 判断余额是否足够,余额足够,将商品放入购物车,余额减去该商品价格,输出放入购物车的商品和余额
                else:
                    print("You have insufficient balance!")
                    # 余额不足
            else:
                print("product_number does not exist!")
                # 商品不存在
        elif choice_number == "Q":
            print("The shopping cart list is as follows:")
            for shop_item in shopping_list:
                print(shop_item)
            print("Your current balance is %s."%(salary))
            exit()
            # 输入Q 打印购物车商品和余额,并退出购物车程序
        else:
            print("The product_number is error!")
            # 商品编号有误
else:
    print("The amount entered is not a number!")
    # 金额有误

 这是学习老男孩教育Python课程的一个记录,我觉得该课程讲的挺好的。可参考金角大王Python之路02

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.v512.cart; import java.util.ArrayList; import java.util.Iterator; public class ShoppingCart { /** * 保存所有CartItem对象的容器对象。 */ private ArrayList<CartItem> cart; public ShoppingCart() { cart = new ArrayList<CartItem>(); } /** * 返回包括所有已经购物的商品信息的容器对象。 * @return 当前的items容器对象 */ public ArrayList<CartItem> getCart() { return cart; } /** * 添加一种商品到购物车中,如果这种商品在购物车中已经存在, * 那就修改已有的商品的数量, * 反之,构造一个新的CartItem对象添加到items对象中。 * @param item 新增的代表这种商品的对象 */ public void addCartItem(CartItem item) { CartItem oldItem = null; if (item != null) { for (int i = 0; i < cart.size(); i++) { oldItem = cart.get(i); if (oldItem.getId().equals(item.getId())) { oldItem.setQuantity(oldItem.getQuantity() + item.getQuantity()); return; } } cart.add(item); } } /** * 从购物车中,删除商品。 * @param id 所删除商品的商品编号 * @return 删除成功,返回true,反之返回false */ public boolean removeCartItem(String id) { CartItem item = null; for (int i = 0; i < cart.size(); i++) { item = cart.get(i); if (item.getId().equals(id)) { cart.remove(i); return true; } } return false; } /** * 计算所购所有商品的总价。 * @return 商品的总价 */ public double getTotal() { Iterator<CartItem> it = cart.iterator(); double sum = 0.0; CartItem item = null; while (it.hasNext()) { item = it.next(); sum = sum + item.getSum(); } return sum; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值