aboutsummaryrefslogtreecommitdiff
path: root/Week9/src/com/camilstaps/shop/Cart.java
diff options
context:
space:
mode:
authorCamil Staps2015-04-18 00:26:48 +0200
committerCamil Staps2015-04-18 00:26:48 +0200
commit6dd7405206b2babfe491b59250f4d1d7f78e654b (patch)
treea5e53ec980a4cbdce038dd0beb3c8fe71fa0e80c /Week9/src/com/camilstaps/shop/Cart.java
parentAdded tarball w8 (diff)
Week9
Diffstat (limited to 'Week9/src/com/camilstaps/shop/Cart.java')
-rw-r--r--Week9/src/com/camilstaps/shop/Cart.java74
1 files changed, 74 insertions, 0 deletions
diff --git a/Week9/src/com/camilstaps/shop/Cart.java b/Week9/src/com/camilstaps/shop/Cart.java
new file mode 100644
index 0000000..6c2b836
--- /dev/null
+++ b/Week9/src/com/camilstaps/shop/Cart.java
@@ -0,0 +1,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();
+ }
+
+} \ No newline at end of file