-Java 创建购物车实体类,模拟购物车功能需求

本文介绍了一个简单的购物车系统的实现过程,包括添加、删除、修改商品的功能,并能够展示购物车内商品的详细信息,按照商品总价升序排列。

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

创建购物车实体类,模拟购物车功能需求:
(1)添加商品到购物车(输入商品的编号和数量)
(2)删除商品(删除购物车中的指定购物项)
(3)修改商品(修改商品的数量)
(4)显示所购买的商品信息(按购买商品的总价进行升序显示)

package com.homework.lhh;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Ex04 {

    public static void main(String[] args) {
        ShoppingCart shoppingcart = new ShoppingCart();
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        System.out.println("-------------购物车系统-------------");
        while (true) {
            System.out.println("1.添加商品");
            System.out.println("2.删除商品");
            System.out.println("3.修改商品");
            System.out.println("4.查看商品");
            System.out.println("5.退出系统");
            System.out.println("请选择您要进行的操作:");
            int num = sc.nextInt();
            switch (num) {
            case 1:
                shoppingcart.addMerchandise();
                break;
            case 2:
                shoppingcart.delMerchandise();
                break;
            case 3:
                shoppingcart.alterMerchandise();
                break;
            case 4:
                shoppingcart.showInfo();
                break;
            case 5:
                System.out.println("退出系统成功!");
                System.exit(0);
                break;
            }
        }
    }
}

// 购物车类
class ShoppingCart {
    private int id;// 编号
    private int count;// 数量
    private double price;// 价格

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public ShoppingCart(int id, int count, double price) {
        super();
        this.id = id;
        this.count = count;
        this.price = price;

    }

    public ShoppingCart() {

    }

    ArrayList<ShoppingCart> list = new ArrayList<ShoppingCart>();
    Scanner sc = new Scanner(System.in);

    // 添加商品
    public void addMerchandise() {
        System.out.println("请输入商品的编号:");
        setId(sc.nextInt());
        System.out.println("请输入添加的数量:");
        setCount(sc.nextInt());
        System.out.println("请输入商品的价格:");
        setPrice(sc.nextDouble());
        list.add(new ShoppingCart(getId(), getCount(), getPrice()));
        System.out.println("添加商品成功");
    }

    // 删除商品
    public void delMerchandise() {
        System.out.println("请输入商品编号:");
        setId(sc.nextInt());
        for (int i = 0; i < list.size(); i++) {
            if (getId() == list.get(i).getId()) {
                list.remove(i);
            }
        }
        System.out.println("删除编号为" + getId() + "的商品成功");
    }

    // 修改商品
    public void alterMerchandise() {
        System.out.println("请输入商品的编号:");
        setId(sc.nextInt());
        System.out.println("请输入商品的更改数量:");
        setCount(sc.nextInt());
        System.out.println("请输入商品的单价:");
        setPrice(sc.nextDouble());
        for (int i = 0; i < list.size(); i++) {
            if (getId() == list.get(i).getId()) {
                list.set(i, new ShoppingCart(getId(), getCount(), getPrice()));
            }
        }
        System.out.println("修改商品成功");
    }

    // 显示所有商品的信息
    public void showInfo() {
        if (list.size() == 0) {
            System.out.println("你的购物车是空的,快去剁手吧!");
        } else {
            list.sort(new Comparator<ShoppingCart>() {

                @Override
                public int compare(ShoppingCart o1, ShoppingCart o2) {
                    if ((o1.getPrice() * o1.getCount()) > (o2.getPrice() * o2.getCount())) {
                        return 1;
                    } else if ((o1.getPrice() * o1.getCount()) < (o2.getPrice() * o2.getCount())) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });
            for (ShoppingCart shoppingcart : list) {
                System.out.println(shoppingcart);
            }
        }

    }
    // 输出
    @Override
    public String toString() {
        return "购物车 [商品编号=" + id + ", 商品数量=" + count + ", 商品单价=" + price + ",总价=" + (price * count) + "]";
    }
}
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、付费专栏及课程。

余额充值