Advertisement
folfix

Untitled

Jun 26th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. @Data
  2. @Embeddable // spłaszczenie struktury na poziomie bazy danych, nie zostanie stworzona nowa tabelka dla tej klasy
  3. public class CreditCardNumber {
  4.  
  5.     private static final String regex = "[0-9]{16}";
  6.  
  7.     @Column(unique = true) // zapewnienie unikalności na poziomie bazy danych
  8.     private final String number;
  9.  
  10.     public CreditCardNumber(String number) {
  11.         if (!isValid(number)) { // walidacja numeru karty kredytowej w momencie tworzenia
  12.             throw new IllegalArgumentException(String.format("Invalid credit card NUMBER %s!", number));
  13.         }
  14.         this.number = number;
  15.     }
  16.  
  17.     private boolean isValid(String number) {
  18.         return number == null ? false : number.matches(regex);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement