Account.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package eu.tankernn.accounts;
  2. import java.math.BigInteger;
  3. import java.security.SecureRandom;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class Account {
  7. private String firstName, lastName, accountNumber;
  8. private List<AccountEvent> history;
  9. public Account(String firstName, String lastName) {
  10. // Generate a random, unique account id
  11. do {
  12. accountNumber = new BigInteger(20, new SecureRandom()).toString();
  13. } while (AccountManager.getAccountByNumber(accountNumber).isPresent());
  14. this.firstName = firstName;
  15. this.lastName = lastName;
  16. this.history = new ArrayList<AccountEvent>();
  17. }
  18. Account(String firstName, String lastName, String accountNumber, List<AccountEvent> history) {
  19. this.firstName = firstName;
  20. this.lastName = lastName;
  21. this.accountNumber = accountNumber;
  22. this.history = history;
  23. }
  24. public String getFirstName() {
  25. return firstName;
  26. }
  27. public String getLastName() {
  28. return lastName;
  29. }
  30. public List<AccountEvent> getHistory() {
  31. return history;
  32. }
  33. public String getAccountNumber() {
  34. return accountNumber;
  35. }
  36. public String toString() {
  37. return firstName + " " + lastName;
  38. }
  39. public double calculateBalance() {
  40. return history.stream().mapToDouble(a -> a.getBalanceChange()).sum();
  41. }
  42. }