我有類“BaseProduct” - 抽象,“Food” - 實作 BaseProduct 和一個“購物車”,里面放了很多食物。
那么我如何將食物放入購物車以及如何添加它們的保質期折扣(保質期前 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
}
uj5u.com熱心網友回復:
將產品添加到購物車 完成 Cart::add 方法的撰寫。初始化產品串列會有所幫助。然后查看您選擇的串列實作上的 add 方法。
申請折扣。使用 getprice(Date purchaseDate) 方法多載 Food 中的 getPrice 方法。在適當的時候從 super.getPrice() 回傳價格減去您的折扣。您可能會發現 getDiscount(Date purchaseDate) 方法很有用,因為 Cashier 類需要總折扣。
uj5u.com熱心網友回復:
為什么您的產品串列是靜態的?
要初始化串列,您可以做兩件事:
一種。急切初始化:
private List<BaseProduct> products = new ArrayList<>();
public void add(BaseProduct product) {
this.products.add(product);
}
灣 延遲初始化:
public void add(BaseProduct product) {
if (this.products == null) {
this.products = new ArrayList<>();
}
this.products.add(product);
}
如果這回答了您的問題,或者您有任何其他疑問,請告訴我。
uj5u.com熱心網友回復:
可以有多種方法來解決這個問題。如果我們在計算產品價格時考慮在哪里打折很明顯。所以我會重構這個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;
}
}
現在,在 a 的情況下,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% 的折扣,我們可能不希望能夠將商品放入購物車。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342785.html
下一篇:單機多應用
