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

import java.util.Objects;

/**
 * @Author: 廾匸
 * @Date: 2020/11/18 18:32
 * @Description: 商品类
 * @version: 1.01
 */
public class Goods{
    private int id;
    private String name;
    private int quantity;
    private double price;

    public Goods(int id, String name, int quantity, double price) {
        this.id = id;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    public Goods() {
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public double getPrice() {
        return price;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Goods goods = (Goods) o;
        return id == goods.id &&
                quantity == goods.quantity &&
                Double.compare(goods.price, price) == 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, quantity, price);
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", Quantity=" + quantity +
                ", price=" + price +
                '}';
    }

}



import java.util.Comparator;
import java.util.List;

/**
 * @Author: 廾匸
 * @Date: 2020/11/18 18:36
 * @Description: 方法类
 * @version: 1.01
 */
public class Methed {

    // 添加
    public static void save(List<Goods> list,Goods goods){
        if(list ==null||goods==null) {
            System.out.println("无商品");
        }
        if(list.size() != 0){
            for (Goods goods1 : list) {
                if(goods1.getName().equals(goods.getName())){
                    System.out.println("用户名重复,请重新添加数据");
                }
            }
        }
        list.add(goods);
    }
    //删除
    public static void delect(List<Goods> list,int id){
        if (list==null||list.size()==0||id<1) {
            System.out.println("没有此商品");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == id){
                list.remove(i);
                System.out.println("删除成功");
            }
        }
        System.out.println("删除之后剩余:");
        list.forEach(System.out::println);
    }
    //修改
    public static void update(List<Goods> list,int id,int quantity){
        if (list==null||list.size()==0||id<1) {
            System.out.println("没有此商品");
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == id){
                list.get(i).setQuantity(quantity);
                System.out.println("数量修改成功");
            }
        }
        System.out.println("修改之后:");
        list.forEach(System.out::println);
    }
    // 按购买商品的总价进行升序显示所购买的商品信息
    public static void show(List<Goods> list){
        if (list.size() != 0) {
            list.sort(new Comparator<Goods>() {

                @Override
                public int compare(Goods o1, Goods o2) {
                    if ((o1.getPrice() * o1.getQuantity()) > (o2.getPrice() * o2.getQuantity())) {
                        return 1;
                    } else if ((o1.getPrice() * o1.getQuantity()) < (o2.getPrice() * o2.getQuantity())) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });
            for (Goods goods : list) {
                System.out.println(goods);
            }
        } else {
            System.out.println("你的购物车是空的,快去剁手吧!");
        }
    }
}



import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 廾匸
 * @Date: 2020/11/18 18:29
 * @Description: 购物车类
 * @version: 1.01
 */
public class ShoppingCart {
    public static void main (String[] args){

        List<Goods> list = new ArrayList<>();
        Methed.save(list,new Goods(1,"衣服",2,156.2));
        Methed.save(list,new Goods(2,"食物",6,50.2));
        Methed.save(list,new Goods(3,"鞋子",2,89.6));
        Methed.save(list,new Goods(4,"帽子",1,46.3));
        Methed.save(list,new Goods(5,"电脑",1,6399));
        list.forEach(System.out::println);

        System.out.println("******************************");
        Methed.delect(list,2);
        System.out.println("******************************");
        Methed.update(list,3,1);
        System.out.println("******************************");
        Methed.show(list);

    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值