【问题标题】:How do I add objects inside an ArrayList using oop?如何使用 oop 在 ArrayList 中添加对象?
【发布时间】:2021-12-15 11:47:01
【问题描述】:

我有类“BaseProduct” - 抽象类,“Food” - 实现 BaseProduct 和一个“Cart”,里面放了许多食物。

那么我如何将食物放入购物车以及如何添加它们的保质期折扣(保质期前 5 天,它们会获得 10% 的折扣)?

我还应该创建一个“收银员”类,该类具有打印收据的方法。该方法接受“购物车”(产品集合)以及购买日期和时间。它应该打印所有购买的产品及其价格、数量、总金额和总折扣。 如果有人提供帮助将不胜感激。

类 BaseProduct 如下所示:

public abstract class BaseProduct {

    private String name;
    private String brand;
    private Double price;

    protected BaseProduct(String name, String brand, Double price) {
        this.name = name;
        this.brand = brand;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getPrice() {
        return price;
    }

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

类食物看起来像这样:

package Products.PerishableProducts;

import Products.BaseProduct;

import java.util.Date;

public class Food extends BaseProduct {

    private Date expirationDate;
    //sample date - 2020/04/20 -> yyyy-MM-dd
    //TODO:CHECK WHETHER OR NOT YOU HAVE EXPIRATION DATE DISCOUNTS WHEN ADDING ITEMS IN CART


    public Food(String name, String brand, Double price, Date expirationDate) {
        super(name, brand, price);
        this.expirationDate = expirationDate;

    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }
    }

“购物车”类(购物车)如下所示:

package Cart;

import Products.BaseProduct;
import java.util.List;

public class Cart {

 private static List<BaseProduct> products;


    //TODO:add products
    public List<BaseProduct> add(List<BaseProduct> products){
      
    }


    //TODO: remove products
}

【问题讨论】:

    标签: java class oop


    【解决方案1】:

    可以有多种方法来解决这个问题。如果我们考虑在计算产品价格时,折扣在哪里算数。所以我会重构Food类如下:

    import java.time.LocalDate;
    
    public class Food extends BaseProduct {
    
        // Use LocalDate instead of the outdated Date class. It is way easier to use and to avoid some common pitfalls
        private LocalDate expirationDate;
    
        public Food(String name, String brand, Double price, LocalDate expirationDate) {
            super(name, brand, price);
            this.expirationDate = expirationDate;
        }
    
        public LocalDate getExpirationDate() {
            return expirationDate;
        }
    
        public void setExpirationDate(LocalDate expirationDate) {
            this.expirationDate = expirationDate;
        }
    
        // Override the calculation of the price of a Food product and apply the discount
        @Override
        public Double getPrice() {
            return super.getPrice() * (1.0 - getDiscount());
        }
    
        // Calculate the discount percentage. If the expiration day is less than 5 days compared to current day, return a 10% discount, otherwise return 0
        private Double getDiscount() {
            return expirationDate.minusDays(5).isAfter(LocalDate.now()) ? 0.0 : 0.1;
        }
    
    }
    

    现在,如果是Cart,我们可能关心客户愿意支付的总价格。所以我们可以编写如下方法:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Cart {
        List<BaseProduct> products = new ArrayList<>();
    
        public void addProduct(BaseProduct product) {
            products.add(product);
        }
    
        // Returns the total price of the items currently in our cart. Discount is applied to the items which are eligible
        public Double getTotalPrice() {
            return products.stream().mapToDouble(BaseProduct::getPrice).sum();
        }
    }
    

    我们可以使用Cart,并通过以下方式:

        public static void main(String[] args) {
            BaseProduct cheese = new Food("cheese", "CheeseBrand", 10.0, LocalDate.of(2021, 10, 1));
            BaseProduct vine = new Food("vine", "Vine", 50.0, LocalDate.of(2025, 12, 1));
    
            Cart cart = new Cart();
            cart.addProduct(cheese);
            cart.addProduct(vine);
    
            System.out.println(cart.getTotalPrice());
        }
    

    假设当前日期是:2021 年 10 月 31 日,对于上面的示例,我们将得到以下总金额:59.0。 此外,我们可能想考虑如果有过期的产品会发生什么。目前,我们对其应用 10% 的折扣,我们可能不希望能够将商品放入购物车。

    【讨论】:

    • 感谢您抽出宝贵的时间帮助解决我的问题。我实际上还需要做一件事,但我实际上也无法理解如何去做。我需要创建一个收银员类有一种打印收据的方法。该方法接受购物车(产品集合)以及购买日期和时间。它应该打印所有购买的产品及其价格、数量、总金额和总折扣。如何执行此操作?收据应如下所示:
    • 哦,你帮了我很多
    • 很遗憾,我无法投票
    【解决方案2】:
    1. 为什么您的产品列表是静态的?

    2. 要初始化列表,您可以做两件事:

    一个。急切初始化:

    private List<BaseProduct> products = new ArrayList<>();
    public void add(BaseProduct product) {
        this.products.add(product);
    }
    

    b.延迟初始化:

    public void add(BaseProduct product) {
        if (this.products == null) {
            this.products = new ArrayList<>();
        }
        this.products.add(product);
    }
    

    如果这回答了您的问题或您有任何其他疑问,请告诉我。

    【讨论】:

      【解决方案3】:

      将产品添加到购物车 完成 Cart::add 方法的编写。 初始化产品列表会有所帮助。然后查看您选择的列表实现中的添加方法。

      申请折扣。 使用 getprice(Date purchaseDate) 方法重载 Food 中的 getPrice 方法。在适当的时候从 super.getPrice() 返回价格减去你的折扣。 您可能会发现拥有 getDiscount(Date purchaseDate) 方法很有用,因为 Cashier 类需要总折扣。

      【讨论】:

      • 我如何初始化产品列表?你能给我看一个示例代码吗?
      猜你喜欢
      • 1970-01-01
      • 2013-11-06
      • 2021-05-26
      • 2019-02-18
      • 1970-01-01
      • 2015-12-13
      相关资源
      最近更新 更多