/* * Copyright (c) 2015 Camil Staps */ package com.camilstaps.webshop; import com.camilstaps.webshop.article.Article; import com.camilstaps.webshop.payment.IDeal; import com.camilstaps.webshop.payment.PaymentMethod; import java.util.ArrayList; import java.util.List; /** * * @author camilstaps */ public class Cart extends ArrayList
{ private PaymentMethod paymentMethod = new IDeal(); public double getSubtotal() { double total = 0; for (Article a : this) { total += a.getPrice(); } return total; } public double getShippingCosts() { List seenCosts = new ArrayList<>(); double total = 0; for (Article a : this) { if (!seenCosts.contains(a.getShippingCosts())) { seenCosts.add(a.getShippingCosts()); total += a.getShippingCosts(); } } return total; } public double getTotal() { return getSubtotal() + getShippingCosts(); } public void setPaymentMethod(PaymentMethod paymentMethod) { this.paymentMethod = paymentMethod; } public boolean pay() { System.out.println("Subtotal : " + getSubtotal()); System.out.println("Shipping : " + getShippingCosts()); System.out.println("TOTAL : " + getTotal()); if (paymentMethod.pay(getTotal())) { clear(); return true; } return false; } }