You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.6 KiB
107 lines
2.6 KiB
package hs.fulda.de.ci.exam.project;
|
|
|
|
public class Account {
|
|
private AccountRepository accountRepository;
|
|
|
|
public Account(String id, String password, AccountStatus status) {
|
|
this.id = id;
|
|
this.password = password;
|
|
this.status = status;
|
|
}
|
|
|
|
public String getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(String id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public String getPassword() {
|
|
return password;
|
|
}
|
|
|
|
public void setPassword(String password) {
|
|
this.password = password;
|
|
}
|
|
|
|
public AccountStatus getStatus() {
|
|
return status;
|
|
}
|
|
|
|
public void setStatus(AccountStatus status) {
|
|
this.status = status;
|
|
}
|
|
|
|
public boolean isEnabled() {
|
|
if(status == AccountStatus.ACTIVE) return true;
|
|
return false;
|
|
}
|
|
|
|
public String getPasswordHash() {
|
|
return new PasswordEncoder().encode(password);
|
|
}
|
|
|
|
public enum AccountStatus {
|
|
ACTIVE,
|
|
CLOSED,
|
|
CANCELED,
|
|
BLACKLISTED,
|
|
BLOCKED
|
|
}
|
|
|
|
private String id;
|
|
private String password;
|
|
private AccountStatus status;
|
|
|
|
public boolean resetPassword(String new_password){
|
|
if(status == AccountStatus.ACTIVE){
|
|
this.password = new_password;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void validatePersonDetails(Person person){
|
|
person.validateName();
|
|
person.validateAddress();
|
|
person.validateEmailAddress();
|
|
person.validatePhoneNumber();
|
|
}
|
|
|
|
public void addAccountDetails(String id, String name, Address address, String email, String phone){
|
|
|
|
Person person = new Person(name, address, email, phone);
|
|
validatePersonDetails(person);
|
|
accountRepository.addPersonalDetails(person, id);
|
|
}
|
|
|
|
public void validateId() {
|
|
if(this.id.isBlank())
|
|
throw new RuntimeException(("Id Cannot be null or empty"));
|
|
}
|
|
|
|
public void validatePassword(){
|
|
if(this.password.isBlank())
|
|
throw new RuntimeException(("Id Cannot be null or empty"));
|
|
}
|
|
|
|
public void validateAccountStatus(){
|
|
if(this.status.equals(null))
|
|
throw new RuntimeException(("Id Cannot be null or empty"));
|
|
}
|
|
|
|
}
|
|
class PasswordEncoder{
|
|
String encode(String password){
|
|
int p = 31;
|
|
int m = (int) Math.pow(10, 9) + 9;
|
|
int hash_value = 0;
|
|
int p_pow = 1;
|
|
for (char c : password.toCharArray()) {
|
|
hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
|
|
p_pow = (p_pow * p) % m;
|
|
}
|
|
return Integer.toString(hash_value);
|
|
}
|
|
}
|
|
|