package com.ufund.api.ufundapi.model; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonProperty; public class User { public enum UserType { HELPER, MANAGER } @JsonProperty("username") private final String username; @JsonProperty("passwordHash") private int passwordHash; @JsonProperty("basket") private final List basket; @JsonProperty("type") private final UserType type; /** * Create a new user * * @param username The name of the user * @param basket A basket to copy from */ public User(@JsonProperty("username") String username, @JsonProperty("passwordHash") int passwordHash, @JsonProperty("basket") List basket, @JsonProperty("type") UserType userType) { this.username = username; this.basket = basket; this.passwordHash = passwordHash; this.type = userType; } public static User create(String username, String password) { return new User( username, password.hashCode(), new ArrayList<>(), UserType.HELPER ); } public String getUsername() { return username; } public boolean verifyPassword(String password) { return password.hashCode() == passwordHash; } public void addToBasket(Need need) { basket.add(need.getId()); } public Integer[] getBasketNeeds() { return basket.toArray(Integer[]::new); } public void removeBasketNeed(Need need) { basket.remove(need.getId()); } public User withoutPasswordHash() { return new User(this.username, 0, this.basket, this.type); } public UserType getType() { return type; } public void copyPassword(User other) { this.passwordHash = other.passwordHash; } public String toString() { return this.username + "; basket: " + this.basket + "; type:" + this.type + "; hash: " + this.passwordHash; } }