Skip to content

Commit

Permalink
added ExtendedMoney class
Browse files Browse the repository at this point in the history
  • Loading branch information
ghacupha committed Mar 18, 2018
1 parent 36875f7 commit b8318df
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 76 deletions.
11 changes: 4 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<version>1.0-SNAPSHOT</version>

<name>accounts</name>
<description>Demo project to illustrate typical double book keeping workflows</description>
<description>Demo project to illustrate typical double book keeping workflow</description>

<licenses>
<license>
Expand Down Expand Up @@ -401,7 +401,7 @@
<linksource>true</linksource>
<!-- overview>${basedir}/overview.html</overview -->
<source>${project.build.sourceCompilerLevel}</source>
<!-- stylesheetfile>${basedir}/src/site/css/money-jdoc.css</stylesheetfile -->
<!-- stylesheetfile>${basedir}/src/site/css/unit-jdoc.css</stylesheetfile -->
<verbose>true</verbose>
<aggregate>true</aggregate>
<doclet>org.umlgraph.doclet.UmlGraphDoc</doclet>
Expand Down Expand Up @@ -434,7 +434,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.151</version>
<version>2.20.1</version>
<configuration>
<aggregate>true</aggregate>
<xrefLocation>${project.reporting.outputDirectory}/../xref-test</xrefLocation>
Expand Down Expand Up @@ -638,6 +638,7 @@
<surefireArgLine>-Dapp.env=LOCAL -Dapp.domain=localhost -Dapp.port=9901</surefireArgLine>
<failsafeArgLine>-Dapp.env=LOCAL -Dapp.domain=localhost -Dapp.port=9901</failsafeArgLine>
<env.TRAVIS_JOB_ID></env.TRAVIS_JOB_ID>
<maven.compile.targetLevel>1.8</maven.compile.targetLevel>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.reporting.outputDirectory>"documentation"</project.reporting.outputDirectory>
Expand Down Expand Up @@ -835,20 +836,17 @@

<ciManagement>
<system>Travis CI</system>
<!--//Todo amend this ASAP-->
<url>https://travis-ci.org/ghacupha/book-keeper</url>
</ciManagement>

<scm>
<!--//Todo amend this ASAP-->
<connection>scm:git:https://github.com/ghacupha/book-keeper.git</connection>
<tag>master</tag>
<developerConnection>scm:git:https://github.com/ghacupha/book-keeper.git</developerConnection>
<url>https://github.com/ghacupha/book-keeper</url>
</scm>

<distributionManagement>
<!--//Todo revisit this distribution management stuff-->
<repository>
<id>bintray-release</id>
<name>oss-jfrog-artifactory-release</name>
Expand All @@ -867,7 +865,6 @@
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>1.1</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.joda/joda-money -->
<dependency>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

import io.github.ghacupha.book_keeper.balance.AccountBalance;
import io.github.ghacupha.book_keeper.time.TimePoint;
import io.github.ghacupha.keeper.book.balance.AccountBalance;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;

/**
* Forms a collection of related {@link Entry}. The {@link javax.money.CurrencyUnit} of the
* Forms a collection of related {@link Entry}. The {@link org.joda.money.CurrencyUnit} of the
* {@link Entry} must the same with that of the {@link Account}
*
* @author edwin.njeru
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

import io.github.ghacupha.book_keeper.time.TimePoint;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;

/**
* Encapsulates information concerning the nature of an {@link AccountImpl} to which
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

import io.github.ghacupha.book_keeper.balance.AccountBalance;
import io.github.ghacupha.book_keeper.balance.AccountBalanceType;
import io.github.ghacupha.book_keeper.time.TimePoint;
import io.github.ghacupha.book_keeper.time.DateRange;
import org.javamoney.moneta.Money;
import io.github.ghacupha.keeper.book.balance.AccountBalance;
import io.github.ghacupha.keeper.book.balance.AccountBalanceType;
import io.github.ghacupha.keeper.book.unit.money.Emonetary;
import io.github.ghacupha.keeper.book.unit.money.Emoney;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,7 +48,7 @@ public void addEntry(Entry entry) {

entries.add(entry); // done

} catch (WrongEntryCurrencyException e) {
} catch (MismatchedCurrencyException e) {
log.error("Exception encountered when adding amount : {} to account : {}", entry.getAmount(), accountDetails.getAccountName());
e.printStackTrace();
} catch (UntimelyBookingDateException e) {
Expand All @@ -69,36 +68,36 @@ private void assertBookingDate(TimePoint bookingDate) throws UntimelyBookingDate
}
}

private void assertCurrency(Currency currency) throws WrongEntryCurrencyException {
private void assertCurrency(Currency currency) throws MismatchedCurrencyException {

if (!this.currency.equals(currency)) {

String message = String.format("The monetary amount added is inconsistent with this account :" +
"Expected currency : %s but found %s", this.currency.toString(), currency.toString());
throw new WrongEntryCurrencyException(message);
throw new MismatchedCurrencyException(message);
}
}

private AccountBalance balance(DateRange dateRange) {

Money result = Money.of(0, currency.getCurrencyCode());
final Emonetary result = new Emoney(0,currency);

entries.stream()
.filter(entry -> dateRange.includes(entry.getBookingDate()))
.map(filteredEntry -> {
Money amount = filteredEntry.getAmount();
Emoney amount = filteredEntry.getAmount();
log.debug("Accounting entry : {} added into the balance with amount : {}",filteredEntry,amount);
return amount;
}
)
.forEachOrdered(orderedAmount -> {
log.debug("Adding amount : {}",orderedAmount);
result.add(orderedAmount);
result = result.plus(orderedAmount);
});

log.debug("Returning balance of amount : {} on the {} side",result,accountBalanceType);
log.debug("Returning balance of amount : {} on the {} side", result[0],accountBalanceType);

return new AccountBalance(result, accountBalanceType);
return new AccountBalance(result[0], accountBalanceType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

import io.github.ghacupha.book_keeper.time.TimePoint;
import org.javamoney.moneta.Money;
import io.github.ghacupha.keeper.book.unit.money.Emoney;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -13,9 +13,9 @@ public class AccountingEntry implements Entry {
private boolean open;
private TimePoint bookingDate;
private Account forAccount;
private Money amount;
private Emoney amount;

AccountingEntry(Account forAccount, EntryDetails entryDetails, Money amount, TimePoint bookingDate) {
AccountingEntry(Account forAccount, EntryDetails entryDetails, Emoney amount, TimePoint bookingDate) {
this.bookingDate=bookingDate;
this.open=true;
this.amount=amount;
Expand All @@ -31,7 +31,7 @@ public class AccountingEntry implements Entry {
}

@Override
public Entry newEntry(AccountImpl account, EntryDetails entryDetails, Money amount, TimePoint bookingDate) {
public Entry newEntry(AccountImpl account, EntryDetails entryDetails, Emoney amount, TimePoint bookingDate) {

return new AccountingEntry(account,entryDetails,amount,bookingDate);

Expand All @@ -45,9 +45,9 @@ public EntryDetails getEntryDetails() {
}

@Override
public Money getAmount() {
public Emoney getAmount() {

log.debug("Returning money amount : {} from accountingEntry : {}",amount,this);
log.debug("Returning unit amount : {} from accountingEntry : {}",amount,this);
return amount;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

import io.github.ghacupha.book_keeper.time.TimePoint;
import org.javamoney.moneta.Money;
import io.github.ghacupha.keeper.book.unit.money.Emoney;
import io.github.ghacupha.keeper.book.unit.time.TimePoint;

/**
* Collection of this {@link Entry} objects forms the {@link AccountImpl}, which is one of the
Expand All @@ -13,13 +13,13 @@
*/
public interface Entry {

Entry newEntry(AccountImpl account, EntryDetails entryDetails, Money amount, TimePoint bookingDate);
Entry newEntry(AccountImpl account, EntryDetails entryDetails, Emoney amount, TimePoint bookingDate);

EntryDetails getEntryDetails();

void setEntryDetails(EntryDetails entryDetails) throws ImmutableEntryException;

Money getAmount();
Emoney getAmount();

TimePoint getBookingDate();
}
106 changes: 106 additions & 0 deletions src/main/java/io/github/ghacupha/keeper/book/EntryDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package io.github.ghacupha.keeper.book;

/**
* Container for additional details for {@link Entry}
*
* @author edwin.njeru
*/
public class EntryDetails {

private final String narration;

private String remarks;

private String invoiceNumber;

private String supplier;

public EntryDetails(String narration) {
this.narration = narration;
this.remarks="";
this.invoiceNumber="";
this.supplier="";
}

public EntryDetails(String narration, String remarks) {
this(narration);
this.remarks = remarks;
this.invoiceNumber="";
this.supplier="";
}

public EntryDetails(String narration, String remarks, String invoiceNumber) {
this(narration,remarks);
this.invoiceNumber = invoiceNumber;
this.supplier="";
}

public EntryDetails(String narration, String remarks, String invoiceNumber, String supplier) {
this(narration,remarks,invoiceNumber);
this.supplier = supplier;
}

public String getNarration() {
return narration;
}

public String getRemarks() {
return remarks;
}

public EntryDetails setRemarks(String remarks) {
this.remarks = remarks;
return this;
}

public String getInvoiceNumber() {
return invoiceNumber;
}

public EntryDetails setInvoiceNumber(String invoiceNumber) {
this.invoiceNumber = invoiceNumber;
return this;
}

public String getSupplier() {
return supplier;
}

public EntryDetails setSupplier(String supplier) {
this.supplier = supplier;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

EntryDetails that = (EntryDetails) o;

if (!narration.equals(that.narration)) return false;
if (!remarks.equals(that.remarks)) return false;
if (!invoiceNumber.equals(that.invoiceNumber)) return false;
return supplier.equals(that.supplier);
}

@Override
public int hashCode() {
int result = narration.hashCode();
result = 31 * result + remarks.hashCode();
result = 31 * result + invoiceNumber.hashCode();
result = 31 * result + supplier.hashCode();
return result;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("EntryDetails{");
sb.append("narration='").append(narration).append('\'');
sb.append(", remarks='").append(remarks).append('\'');
sb.append(", invoiceNumber='").append(invoiceNumber).append('\'');
sb.append(", supplier='").append(supplier).append('\'');
sb.append('}');
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

public class ImmutableEntryException extends Throwable {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.ghacupha.keeper.book;

/**
* Thrown when the amount entered into an account has a different currency with the account's currency
*/
public class MismatchedCurrencyException extends Throwable {
public MismatchedCurrencyException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.ghacupha.book_keeper;
package io.github.ghacupha.keeper.book;

public class UntimelyBookingDateException extends Throwable {
public UntimelyBookingDateException(String message) {
Expand Down
Loading

0 comments on commit b8318df

Please sign in to comment.