【JAVA】使用集合容器Map等,模拟购物车功能(Goods类和Shopping类),涵盖对Map的各种方法及其Key、Value、映射关系的应用~

本文通过JAVA编程实现购物车功能,利用集合容器Map存储商品信息,包括 Goods 实体类的设计和 Shopping 测试类的编写。文章详细介绍了Map的使用,包括Key、Value和映射关系的操作。不过内容未完全展开,后续会继续更新。
摘要由CSDN通过智能技术生成


✍版权声明:@CSDN遊光

版权声明:
@Kcx@遊光@胤殁@Karl@Karlcixio

有幸您能来看我的博客,本博客仅供大家学术交流,如您有需要将本博客用作它用,请与我联系!
我的联系方式:

微信搜索公众号:Karlcixio
也可以直接扫描以下二维码
----------------------------------公众号二维码
个人 QQ:1005589159


?第零步:审题!!!

首先,我们先来看看题目要求,然后再罗列我们大体需要做些什么:
创建购物车实体类,模拟购物车功能需求

  1. 创建实体类Goods,并写入相关变量及方法;
  2. 创建测试类Shopping,并写入测试过程;

?第一步:实体类Goods

package 【PackageName】;

import java.util.Objects;

public class Goods {
   
    private String gid;
    private String gname;
    private float gprice;

    public Goods(){
   }
    public Goods(String gid, String gname, float gprice) {
   
        this.gid = gid;
        this.gname = gname;
        this.gprice = gprice;
    }

    public String getGid() {
   
        return gid;
    }

    public void setGid(String gid) {
   
        this.gid = gid;
    }

    public String getGname() {
   
        return gname;
    }

    public void setGname(String gname) {
   
        this.gname = gname;
    }

    public float getGprice() {
   
        return gprice;
    }

    public void setGprice(float gprice) {
   
        this.gprice = gprice;
    }

    @Override
    public boolean equals(Object o) {
   
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return Float.compare(goods.gprice, gprice) == 0 &&
                Objects.equals(gid, goods.gid) &&
                Objects.equals(gname, goods.gname);
    }

    @Override
    public int hashCode() {
   
        return Objects.hash(gid, gname, gprice);
    }

    @Override
    public String toString() {
   
        return "\n[" +
                "* 编号:" + gid +
                ";名称:" + gname +
                ";价格:" + gprice +
                "]\n\t\t\t\t\t\t\t\t\t -->数量";
    }

}

?第二步:测试类Shopping

package 【PackageName】;

import java.util.*;

public class Shopping {
   
    private Map<Goods, Integer> goodsMap;
    private static 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值