aboutsummaryrefslogtreecommitdiff
path: root/Week16 Webshop/src/com/camilstaps/webshop/Cart.java
blob: a5bed8681b302d07f4e0fb4e56c528815f83f145 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * 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<Article> {
  
    private PaymentMethod paymentMethod = new IDeal();
    
    public double getSubtotal() {
        double total = 0;
        for (Article a : this) {
            total += a.getPrice();
        }
        return total;
    }
    
    public double getShippingCosts() {
        List<Double> 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;
    }
    
}