Skip to content

Commit

Permalink
Amended bugs in the DirectedJournal and DirectedTrnsaction, enhanced …
Browse files Browse the repository at this point in the history
…exception messages and added test coverage
  • Loading branch information
ghacupha committed Mar 25, 2018
1 parent 238ca4d commit 67f3f3d
Show file tree
Hide file tree
Showing 29 changed files with 721 additions and 256 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Changelog for ghacupha book-keeper.
## Unreleased
### No issue

**commited changes to Isolate DirectedJournal for unit testing**


[238ca4d6c09966c](https://github.com/ghacupha/book-keeper/commit/238ca4d6c09966c) Edwin Njeru *2018-03-23 17:44:23*

**created tests for the Directed Journal and the DirectedTransaction**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.github.ghacupha.keeper.book.base.AccountVisitor;
import io.github.ghacupha.keeper.book.base.VisitableAccount;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;
import io.github.ghacupha.keeper.book.util.MismatchedCurrencyException;
import io.github.ghacupha.keeper.book.util.UntimelyBookingDateException;

import java.util.Currency;

Expand All @@ -37,7 +39,7 @@ public interface Account extends VisitableAccount {
*
* @param entry {@link Entry} to be added to the account
*/
void addEntry(Entry entry);
void addEntry(Entry entry) throws UntimelyBookingDateException, MismatchedCurrencyException;

/**
* Gives the account balance as at the {@link TimePoint} given
Expand All @@ -56,6 +58,8 @@ public interface Account extends VisitableAccount {

Currency getCurrency();

TimePoint getOpeningDate();

JournalSide getJournalSide();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.github.ghacupha.keeper.book.util.UnableToPostException;

import java.util.Currency;
import java.util.List;

public interface JournalizedTransaction {

Expand All @@ -46,5 +47,11 @@ public interface JournalizedTransaction {
* @throws UnableToPostException {@link UnableToPostException} thrown when the transaction is not balanced
* That is if the items posted on the debit are more than those posted on the credit or vice versa.
*/
void post() throws UnableToPostException;
void post() throws UnableToPostException,MismatchedCurrencyException;

/**
*
* @return Unmodifiable list of {@link Entry} items from this
*/
List<Entry> getEntries();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public interface Transaction {
* @throws UnableToPostException {@link UnableToPostException} thrown when the transaction is not balanced
* That is if the items posted on the debit are more than those posted on the credit or vice versa.
*/
void post() throws UnableToPostException;
void post() throws UnableToPostException, MismatchedCurrencyException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.github.ghacupha.keeper.book.unit.money.Cash;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;
import io.github.ghacupha.keeper.book.util.ImmutableEntryException;
import io.github.ghacupha.keeper.book.util.MismatchedCurrencyException;
import io.github.ghacupha.keeper.book.util.UntimelyBookingDateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -110,7 +112,15 @@ private boolean isOpen() {
@Override
public void post() {

forAccount.addEntry(this);
try {
forAccount.addEntry(this);
} catch (UntimelyBookingDateException e) {
log.error("Could not post the entry : {} into account : {} because the entry's booking date :{} is sooner than the account's opening " +
"date of : {}",this,forAccount,bookingDate,forAccount.getOpeningDate(),e.getStackTrace());
} catch (MismatchedCurrencyException e) {
log.error("Could not post the entry : {} into the account : {} because the entry's currency : {} does not match " +
"the account's currency : {}",this,forAccount,amount.getCurrency(),forAccount.getCurrency(), e.getStackTrace());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void post() throws UnableToPostException {
}
}

protected boolean canPost() {
private boolean canPost() {

return balance().isZero();
}
Expand All @@ -125,4 +125,13 @@ private Cash balance() {

return result;
}

protected Currency getCurrency() {
return currency;
}

protected TimePoint getDate() {

return date;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,43 @@
import io.github.ghacupha.keeper.book.api.EntryAttributes;
import io.github.ghacupha.keeper.book.api.Transaction;
import io.github.ghacupha.keeper.book.unit.money.Cash;
import io.github.ghacupha.keeper.book.unit.money.HasDenomination;
import io.github.ghacupha.keeper.book.unit.time.IsChronological;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;
import io.github.ghacupha.keeper.book.util.ImmutableEntryException;
import io.github.ghacupha.keeper.book.util.MismatchedCurrencyException;
import io.github.ghacupha.keeper.book.util.UnableToPostException;

import java.util.Currency;

class AccountingTransactionDecorator extends AccountingTransaction implements Transaction {
class AccountingTransactionDecorator implements Transaction,HasDenomination,IsChronological {

AccountingTransactionDecorator(TimePoint date, Currency currency) {
super(date, currency);
private final AccountingTransaction accountingTransaction;

public AccountingTransactionDecorator(AccountingTransaction accountingTransaction) {
this.accountingTransaction = accountingTransaction;
}

@Override
public void add(Cash amount, Account account, EntryAttributes attributes) throws ImmutableEntryException, MismatchedCurrencyException {
// we are muting this behaviour
accountingTransaction.add(amount,account,attributes);
}

@Override
public void post() throws UnableToPostException, MismatchedCurrencyException {

accountingTransaction.post();
}

@Override
public Currency getCurrency() {

return accountingTransaction.getCurrency();
}

@Override
public TimePoint getDate() {

return accountingTransaction.getDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,57 @@
import io.github.ghacupha.keeper.book.api.EntryAttributes;
import io.github.ghacupha.keeper.book.unit.money.Cash;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;
import io.github.ghacupha.keeper.book.util.ImmutableEntryException;

/**
* This acts as a decorator to the {@link AccountingEntry} object inorder to give
* additional functionality to the sub classes
*
* @author edwin.njeru
*/
class AccoutingEntryDecorator extends AccountingEntry implements Entry {
class AccoutingEntryDecorator implements Entry {

private final AccountingEntry accountingEntry;

AccoutingEntryDecorator(Account account, EntryAttributes entryAttributes, Cash amount, TimePoint bookingDate) {
super(account, entryAttributes, amount, bookingDate);
accountingEntry = new AccountingEntry(account, entryAttributes, amount, bookingDate);
}

@Override
public Entry newEntry(Account journal, EntryAttributes entryAttributes, Cash amount, TimePoint bookingDate) {

return accountingEntry.newEntry(journal,entryAttributes,amount,bookingDate);
}

@Override
public EntryAttributes getEntryAttributes() {
return accountingEntry.getEntryAttributes();
}

@Override
public void setEntryAttributes(EntryAttributes entryAttributes) throws ImmutableEntryException {

this.accountingEntry.setEntryAttributes(entryAttributes);
}

@Override
public Cash getAmount() {
return accountingEntry.getAmount();
}

@Override
public TimePoint getBookingDate() {
return accountingEntry.getBookingDate();
}

@Override
public void post() {

this.accountingEntry.post();
}

@Override
public String toString() {
return accountingEntry.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ public class DirectedJournal extends JournalDecorator implements Account {
*/
public DirectedJournal(JournalSide journalSide, Currency currency, AccountAttributes accountAttributes) {
super(journalSide, currency, accountAttributes);

log.debug("DirectedJournal :{} created",this);
}

@Override
public AccountBalance balance(TimePoint asAt) {

log.debug("Account balance enquiry raised as at {}",asAt);

AccountBalance balance = balance(new DateRange(this.getAttributes().getOpeningDate(), asAt));
log.debug("Returning accounting balance as at : {} as : {}", asAt, balance);

Expand All @@ -72,27 +76,37 @@ public AccountBalance balance(TimePoint asAt) {
@Override
public AccountBalance balance() {

return balance(new Moment());
TimePoint enquiryDate = new Moment();

log.debug("Account balance enquiry raised as at : {}",enquiryDate);

AccountBalance retVal = balance(enquiryDate);

log.debug("Balance as at : {} returned as : {}",retVal,retVal);

return retVal;
}

private AccountBalance balance(DateRange dateRange) {

// What to do???
final Cash amount = HardCash.of(0.00, getCurrency().getCurrencyCode());
final Cash[] amount = {HardCash.of(0.00, getCurrency().getCurrencyCode())};

this.entries
this.getEntries()
.stream()
.filter(entry -> dateRange.includes(entry.getBookingDate()))
.forEach(entry ->
calculateBalanceAmount(entry, amount)
amount[0] = amount[0].plus(calculateBalanceAmount(entry, amount[0]))
);

return new AccountBalance(amount, this.journalSide);
log.debug("Account balance contains Cash of : {} on the {} sidee", amount[0],this.getJournalSide());

return new AccountBalance(amount[0], this.getJournalSide());
}

private void calculateBalanceAmount(Entry entry, Cash amount) {
private Cash calculateBalanceAmount(Entry entry, Cash amount) {

if (this.journalSide == DEBIT) {
if (this.getJournalSide() == DEBIT) {

if (entry.getJournalSide() == DEBIT) {
amount = amount.plus(entry.getAmount());
Expand All @@ -103,7 +117,7 @@ private void calculateBalanceAmount(Entry entry, Cash amount) {

if (amount.isLessThan(entry.getAmount())) {

switchJournalSide(journalSide);
switchJournalSide(this.getJournalSide());

amount = amount.minus(entry.getAmount()).abs();

Expand All @@ -117,7 +131,7 @@ private void calculateBalanceAmount(Entry entry, Cash amount) {
}
}

if (this.journalSide == CREDIT) {
if (this.getJournalSide() == CREDIT) {

if (entry.getJournalSide() == CREDIT) {

Expand All @@ -129,7 +143,7 @@ private void calculateBalanceAmount(Entry entry, Cash amount) {

if (amount.isLessThan(entry.getAmount())) {

switchJournalSide(journalSide);
switchJournalSide(this.getJournalSide());

amount = amount.minus(entry.getAmount()).abs();

Expand All @@ -142,6 +156,8 @@ private void calculateBalanceAmount(Entry entry, Cash amount) {
}
}
}

return amount;
}

private void switchJournalSide(JournalSide journalSide) {
Expand All @@ -160,4 +176,8 @@ private void switchJournalSide(JournalSide journalSide) {
}


@Override
public String toString() {
return "DirectedJournal {" + super.toString();
}
}
Loading

0 comments on commit 67f3f3d

Please sign in to comment.