jeudi 21 décembre 2017

Java: Bank account transatcion limit -> How to represent that there is no limit for some accounts?

I have a quick question about representing single transaction limits for bank accounts. I have different types of bank accounts where each one has a certain single transaction limit. However, some of these account types have no transaction limit. I could come up with a pretty simple solution which just uses a constant to indicate that (see example below).

      private static final double NO_TRANSACTION_LIMIT = -1;  
      public enum BankAccountType {
                GIRO_MINOR(0, 20, "Minor Account"),
                GIRO_STUDENT(0, 200, "Student Account"),
                GIRO_NORMAL(-2000, 4000, "Normal Account"),
                SAVING(0, NO_TRANSACTION_LIMIT, "Saving Account");
                // ...
                private double transactionLimit;
                // ...
                public boolean hasTransactionLimit() {
                    return transactionLimit == NO_TRANSACTION_LIMIT;
                }
}

After some thinking, I thought there must be a better solution to that problem. Because if for some reason the method hasTransactionLimit() is not checked before a transaction limit, this might cause unwanted behavior.
So my question: what is a good strategy to implement behavior like that. Somehow I thought of the Null-Object pattern, but I am pretty unsure.

Thanks!

Aucun commentaire:

Enregistrer un commentaire