aboutsummaryrefslogtreecommitdiff
path: root/Week9/src/com/camilstaps/shop/User.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/User.java
parentAdded tarball w8 (diff)
Week9
Diffstat (limited to 'Week9/src/com/camilstaps/shop/User.java')
-rw-r--r--Week9/src/com/camilstaps/shop/User.java143
1 files changed, 143 insertions, 0 deletions
diff --git a/Week9/src/com/camilstaps/shop/User.java b/Week9/src/com/camilstaps/shop/User.java
new file mode 100644
index 0000000..5696a60
--- /dev/null
+++ b/Week9/src/com/camilstaps/shop/User.java
@@ -0,0 +1,143 @@
+/**
+ * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
+ * See the LICENSE file for copying permission.
+ */
+
+package com.camilstaps.shop;
+
+import java.util.HashSet;
+import java.util.Random;
+import java.util.Set;
+
+/**
+ * A User is a person with a U/S-number, email and password.
+ * He may be blocked / be an admin, and has a Cart.
+ * @author Camil Staps, s4498062
+ */
+public class User extends DatabaseItem {
+
+ private final String nr;
+ private final String email;
+ private String hash;
+ private final boolean isAdmin;
+ private final Cart cart = new Cart();
+ private boolean isBlocked = false;
+
+ public User (String nr, String email) {
+ this.nr = nr;
+ this.email = email;
+ this.isAdmin = false;
+ }
+
+ public User (String nr, String email, boolean isAdmin) {
+ this.nr = nr;
+ this.email = email;
+ this.isAdmin = isAdmin;
+ }
+
+ public String getNr() {
+ return nr;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public boolean isAdmin() {
+ return isAdmin;
+ }
+
+ public Cart getCart() {
+ return cart;
+ }
+
+ public Set<Article> getArticles() {
+ Set<Article> articles = Database.getInstance().getArticles();
+ Set<Article> result = new HashSet<>();
+ for (Article a : articles) {
+ if (a.getUser().getNr().equals(nr)) {
+ result.add(a);
+ }
+ }
+ return result;
+ }
+
+ public Set<Order> getOrders() {
+ Set<Order> orders = Database.getInstance().getOrders();
+ Set<Order> result = new HashSet<>();
+ for (Order o : orders) {
+ if (o.getUser().getNr().equals(nr)) {
+ result.add(o);
+ }
+ }
+ return result;
+ }
+
+ public void setBlocked(boolean set) {
+ isBlocked = set;
+ }
+
+ public boolean isBlocked() {
+ return isBlocked;
+ }
+
+ /**
+ * Set a random new password for the user
+ * @return the new password
+ */
+ public String setRandomPassword() {
+ String pw = generatePassword();
+ hash = hash(pw);
+ return pw;
+ }
+
+ /**
+ * Hash a password. Currently, this is just the identity function.
+ * @param password
+ * @return
+ */
+ public static String hash(String password) {
+ return password;
+ }
+
+ /**
+ * Generate a random password
+ * @return
+ */
+ private static String generatePassword() {
+ // Only characters that cannot easily be confused
+ final String drawFrom = "123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
+ final Random r = new Random();
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 12; i++) {
+ sb.append(drawFrom.charAt(r.nextInt(drawFrom.length())));
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Verify if a password matches the user's password
+ * @param password the password to check
+ * @return
+ */
+ public boolean verify(String password) {
+ return hash(password).equals(hash);
+ }
+
+ @Override
+ public String toString() {
+ return (isAdmin ? "++ " : "") + nr + " (" + email + ")" + (isBlocked ? " -!-" : "");
+ }
+
+ /**
+ * This User as a String, with a detailed and a quick view
+ * @param showSensitive whether to show sensitive data (email, isAdmin, isBlocked) or not
+ * @return
+ */
+ public String toString(boolean showSensitive) {
+ return showSensitive ? toString() : nr;
+ }
+
+} \ No newline at end of file