aboutsummaryrefslogtreecommitdiff
path: root/Week9/src/com/camilstaps/shop/Cart.java
blob: 6c2b8361f6de0af310932c6966814e4519dabe72 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
 * See the LICENSE file for copying permission.
 */

package com.camilstaps.shop;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * A Cart holds the articles a User is planning to buy.
 * @author Camil Staps, s4498062
 */
public class Cart implements Serializable {
    
    private final Set<Article> articles = new HashSet<>();
    
    public Set<Article> getArticles() {
        return articles;
    }

    /**
     * Get the total price of all articles
     * @return 
     */
    public float getTotalAmount() {
        float result = 0;
        for (Article a : articles) {
            result += a.getPrice();
        }
        return result;
    }

    /**
     * Add a new article
     * @param article
     */
    public void add(Article article) {
        Database.getInstance().removeItem(article);
        articles.add(article);
    }

    /**
     * Remove an article (and put it back in the database)
     * @param article
     */
    public void remove(Article article) {
        articles.remove(article);
        try {
            Database.getInstance().addItem(article);
        } catch (DuplicateEntryException ex) {
        }
    }
    
    /**
     * Remove all articles in the manner of remove()
     * @see self#remove
     */
    public void reset() {
        for (Article a : articles) {
            remove(a);
        }
    }
    
    /**
     * Remove all articles, but don't put them back in the database
     */
    public void clear() {
        articles.clear();
    }

}