-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced multiple IFs in the AccountAppraisalDelegate by implementing…
… State pattern to represent the AccountSide of the Account implementation
- Loading branch information
Showing
7 changed files
with
162 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/ghacupha/keeper/book/base/state/AccountCreditState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.ghacupha.keeper.book.base.state; | ||
|
||
import io.github.ghacupha.keeper.book.api.Account; | ||
import io.github.ghacupha.keeper.book.balance.AccountBalance; | ||
import io.github.ghacupha.keeper.book.balance.AccountSide; | ||
import io.github.ghacupha.keeper.book.unit.money.Cash; | ||
|
||
import static io.github.ghacupha.keeper.book.balance.AccountSide.DEBIT; | ||
|
||
public class AccountCreditState implements AccountState { | ||
|
||
private Account account; | ||
public AccountCreditState(Account account) { | ||
this.account = account; | ||
} | ||
|
||
/** | ||
* Get AccountBalance given the sum of debits and sum of credits | ||
* | ||
* @param debits | ||
* @param credits | ||
* @return | ||
*/ | ||
@Override | ||
public AccountBalance getAccountBalance(final Cash debits,final Cash credits) { | ||
|
||
if(credits.isMoreThan(debits)){ | ||
return new AccountBalance(credits.minus(debits).abs(),account.getAccountSide()); | ||
} | ||
|
||
// The journal side of the account changes to DEBIT | ||
account.setAccountSide(DEBIT); | ||
|
||
return new AccountBalance(credits.minus(debits).abs(),DEBIT); | ||
} | ||
|
||
/** | ||
* @return {@code AccountSide} of the Account | ||
*/ | ||
@Override | ||
public AccountSide getAccountSide() { | ||
|
||
return account.getAccountSide(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/io/github/ghacupha/keeper/book/base/state/AccountDebitState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.ghacupha.keeper.book.base.state; | ||
|
||
import io.github.ghacupha.keeper.book.api.Account; | ||
import io.github.ghacupha.keeper.book.balance.AccountBalance; | ||
import io.github.ghacupha.keeper.book.balance.AccountSide; | ||
import io.github.ghacupha.keeper.book.base.SimpleAccount; | ||
import io.github.ghacupha.keeper.book.base.state.AccountState; | ||
import io.github.ghacupha.keeper.book.unit.money.Cash; | ||
|
||
import static io.github.ghacupha.keeper.book.balance.AccountSide.CREDIT; | ||
|
||
public class AccountDebitState implements AccountState { | ||
|
||
private Account account; | ||
|
||
public AccountDebitState(Account account) { | ||
this.account = account; | ||
} | ||
|
||
@Override | ||
public AccountBalance getAccountBalance(Cash debits, Cash credits) { | ||
|
||
if(debits.isMoreThan(credits)){ | ||
return new AccountBalance(debits.minus(credits).abs(),account.getAccountSide()); | ||
} | ||
|
||
account.setAccountSide(CREDIT); | ||
|
||
return new AccountBalance(credits.minus(debits).abs(),CREDIT); | ||
} | ||
|
||
@Override | ||
public AccountSide getAccountSide() { | ||
return account.getAccountSide(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return "Debit state"; | ||
} | ||
|
||
public Account getAccount() { | ||
return account; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/io/github/ghacupha/keeper/book/base/state/AccountState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.ghacupha.keeper.book.base.state; | ||
|
||
import io.github.ghacupha.keeper.book.balance.AccountBalance; | ||
import io.github.ghacupha.keeper.book.balance.AccountSide; | ||
import io.github.ghacupha.keeper.book.unit.money.Cash; | ||
|
||
/** | ||
* Experimental representation of the states of the account | ||
*/ | ||
public interface AccountState { | ||
|
||
/** | ||
* Get AccountBalance given the sum of debits and sum of credits | ||
* @param debits | ||
* @param credits | ||
* @return | ||
*/ | ||
AccountBalance getAccountBalance(Cash debits,Cash credits); | ||
|
||
/** | ||
* | ||
* @return {@code AccountSide} of the Account | ||
*/ | ||
AccountSide getAccountSide(); | ||
} |