Map集合实现的纯java购物车

本文介绍了一个简单的购物车系统实现,包括商品和商品项类定义,以及购物车类的方法实现,如添加、删除商品及计算总价等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Product.java

public class Product {
    private Integer productid;
    private String productname;
    private String category;
    private float price;
    private String picture;
    public Product(Integer productid, String productname, String category, float price, String picture) {
        this.productid = productid;
        this.productname = productname;
        this.category = category;
        this.price = price;
        this.picture = picture;
    }
    public Integer getProductid() {
        return productid;
    }
    public void setProductid(Integer productid) {
        this.productid = productid;
    }
    public String getProductname() {
        return productname;
    }
    public void setProductname(String productname) {
        this.productname = productname;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
}


ProductIem.java

public class ProductItem {
    private Product product;
    private Integer count;
    public ProductItem(Product product, Integer count) {
        this.product = product;
        this.count = count;
    }
    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    public float getTotalPrice() {
        return this.product.getPrice()*count;
    }
}


ShoppingCart.java

import java.util.HashMap;
import java.util.Scanner;


import javax.swing.plaf.synth.SynthSeparatorUI;


public class ShoppingCart {
    private HashMap<Integer, ProductItem> items = new HashMap<Integer, ProductItem>();
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        System.out.println("初始化产品");
        Product banana = new Product(10000, "香蕉", "水果", 2.50F, "banana.jpg");
        Product apple = new Product(10001, "苹果", "水果", 3.99F, "apple.jpg");
        Product orange = new Product(10002, "桔子", "水果", 1.80F, "orange.jpg");
        Product pen = new Product(10003, "钢笔", "文具", 12.00F, "pen.jpg");
        Product pencil = new Product(10004, "铅笔", "文具", 1.00F, "pencil.jpg");
        cart.addToCart(apple);
        cart.addToCart(apple);
        while(true){
        System.out.println("\t1  查看全部");
    System.out.println("\t2 添加东西");
    System.out.println("\t3 删除东西");
    System.out.println("\t4 修改数量");
    System.out.println("\t5 结账");
    System.out.println("\t6 退出");
    System.out.print("请选择[1-6]:");
    System.out.println("请输入你的操作");
        Scanner sc=new Scanner(System.in);
        String x=sc.nextLine();
        switch (x) {
case "1":

System.out.println(cart.showAll());
break;
            case "2":
            System.out.println("添加的水果,请输入");
            x=sc.nextLine();
            //System.out.println(x);
            if(x.equals(banana.getProductname())){
            cart.addToCart(banana);
            }
            if(x.equals(pencil.getProductname())){
            cart.addToCart(pencil);
            }
            if(x.equals(pen.getProductname())){
            cart.addToCart(pen);
            }
            if(x.equals(orange.getProductname())){
            cart.addToCart(orange);
            }
            if(x.equals(apple.getProductname())){
            cart.addToCart(apple);
            //System.out.println(apple.getProductname());
            }
            //System.out.println(cart.showAll());
           
//cart.addToCart(product);
break;
            case "3":
            System.out.println("请输入要删除的东西");
            x=sc.nextLine();
            if(x.equals(banana.getProductname())){
            cart.delFromCart(banana);
            }
            if(x.equals(pencil.getProductname())){
            cart.delFromCart(pencil);
            }
            if(x.equals(pen.getProductname())){
            cart.delFromCart(pen);
            }
            if(x.equals(orange.getProductname())){
            cart.delFromCart(orange);
            }
            if(x.equals(apple.getProductname())){
            cart.delFromCart(apple);
            }
//cart.delFromCart()
break;
            case "4":
            System.out.println("请输入要修改的东西名和数量");
            x=sc.nextLine();
            System.out.println("请输入要修改的东西名的数量");
            int s=sc.nextInt();
            if(x.equals(banana.getProductname())){
            cart.modifyCart(banana,s);
            }
            if(x.equals(pencil.getProductname())){
            cart.modifyCart(pencil,s);
            }
            if(x.equals(pen.getProductname())){
            cart.modifyCart(pen,s);
            }
            if(x.equals(orange.getProductname())){
            cart.modifyCart(orange,s);
            }
            if(x.equals(apple.getProductname())){
            cart.modifyCart(apple,s);
            //System.out.println(apple.getProductname());
            }
            //modifyCart(x,s);
break;
            case "5":
            System.out.println(cart.getTotalPrice());
break;
            case "6":
System.exit(0);;
break;
default:
break;
}
      /*  System.out.println("###############################################");
        System.out.println("添加苹果");
        cart.addToCart(apple);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("添加香蕉");
        cart.addToCart(banana);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("修改香蕉数量");
        cart.modifyCart(banana, 5);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("删除苹果");
        cart.delFromCart(apple);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");
        System.out.println("添加铅笔");
        cart.addToCart(pencil);
        System.out.println(cart.showAll());
        System.out.println("当前购物车总价:" + cart.getTotalPrice());
        System.out.println("***********************************************");*/
        }
    }
    public void addToCart(Product product) {
        this.items.put(product.getProductid(), new ProductItem(product, 1));
    }
    public void delFromCart(Product product) {
        this.items.remove(product.getProductid());
    }
    public  String showAll() {
        String detail = "";
        for (Integer key : this.items.keySet()) {
            detail += this.items.get(key).getProduct().getProductname() + ":" + this.items.get(key).getCount() + "件,单价 " + this.items.get(key).getProduct().getPrice() + ",小计" + this.items.get(key).getTotalPrice() + "\n";
        }
        return detail;
    }
    public boolean modifyCart(Product product, Integer count) {
        if (this.items.containsKey(product.getProductid())) {
            this.items.get(product.getProductid()).setCount(count);
            return true;
        }
        return false;
    }
    public float getTotalPrice() {
        float total = 0;
        for (Integer key : this.items.keySet()) {
            total += this.items.get(key).getTotalPrice();
        }
        return total;


    }
}


















import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /** * 购物车类 */ public class Cart { //创建一个map对象,用来保存商品,key为商品,value为商品的数量 private Map<Goods, Integer> map = new HashMap<Goods, Integer>(); /** * 添加商品到购物车方法 * @param id 商品编号 * @param quantity 商品数量 */ public void addGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ Integer oQuantity = map.get(goods);//获取商品在购物车中原本的数量 if(oQuantity!=null){ oQuantity += quantity; }else{ oQuantity = quantity; } map.put(goods, oQuantity);//添加商品到map中 System.out.println("添加商品"+goods.getName()+"成功!"); }else{ System.out.println("添加失败!商品编号不存在!"); } } /** * 按指定的编号删除商品 * @param id 商品编号 */ public void delGoods(int id){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.remove(goods);//从map删除商品 System.out.println("删除商品"+goods.getName()+"成功!"); }else{ System.out.println("删除失败!商品编号不存在!"); } } /** * 修改商品数量方法 * @param id 商品编号 * @param quantity 要修改的商品数量 */ public void updateGoods(int id, int quantity){ Goods goods = GoodsDB.goodsMap.get(id); if(goods!=null){ map.put(goods, quantity);//从map删除商品 }else{ System.out.println("修改失败!商品编号不存在!"); } } /** * 打印购物车商品列表 */ public void show(){ Set<Entry<Goods, Integer>> entrySet = map.entrySet(); System.out.println("编号\t单价\t数量\t名称\t总价"); for(Entry<Goods, Integer> entry:entrySet){ Goods goods = entry.getKey(); Integer quantity = entry.getValue(); System.out.println(goods.getId()+"\t"+goods.getPrice()+"\t"+quantity+"\t"+goods.getName()+"\t"+goods.getPrice()*quantity); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值