diff options
Diffstat (limited to 'Week9/src/com/camilstaps/shop/User.java')
-rw-r--r-- | Week9/src/com/camilstaps/shop/User.java | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/Week9/src/com/camilstaps/shop/User.java b/Week9/src/com/camilstaps/shop/User.java deleted file mode 100644 index 12cfd4d..0000000 --- a/Week9/src/com/camilstaps/shop/User.java +++ /dev/null @@ -1,143 +0,0 @@ -/** - * 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 void setBlocked(boolean set) { - isBlocked = set; - } - - public boolean isBlocked() { - return isBlocked; - } - - public Set<Article> getArticles() { - Set<Article> articles = Database.getInstance().getArticles(); - Set<Article> result = new HashSet<>(); - for (Article a : articles) { - if (a.getOwner().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; - } - - /** - * 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 |