|
From: Pelle B. <pe...@us...> - 2004-03-22 22:09:58
|
Update of /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/simple In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7293/src/java/org/neuclear/ledger/simple Modified Files: SimpleLedger.java Log Message: SimpleLedger now passes all unit tests Index: SimpleLedger.java =================================================================== RCS file: /cvsroot/neuclear/neuclear-ledger/src/java/org/neuclear/ledger/simple/SimpleLedger.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SimpleLedger.java 22 Mar 2004 20:08:24 -0000 1.1 --- SimpleLedger.java 22 Mar 2004 21:59:37 -0000 1.2 *************** *** 4,7 **** --- 4,10 ---- * $Id$ * $Log$ + * Revision 1.2 2004/03/22 21:59:37 pelle + * SimpleLedger now passes all unit tests + * * Revision 1.1 2004/03/22 20:08:24 pelle * Added simple ledger for unit testing and in memory use *************** *** 85,89 **** ledger = new LinkedHashMap(); held = new LinkedHashMap(); ! books = new HashMap(); } --- 88,93 ---- ledger = new LinkedHashMap(); held = new LinkedHashMap(); ! balances = new HashMap(); ! } *************** *** 102,108 **** --- 106,130 ---- final PostedTransaction posted = new PostedTransaction(trans, new Date()); ledger.put(id, posted); + updateBalances(posted); return posted; } + private void updateBalances(final PostedTransaction trans) { + synchronized (balances) { + Iterator iter = trans.getItems(); + while (iter.hasNext()) { + TransactionItem item = (TransactionItem) iter.next(); + addTransactionItem(item); + } + } + } + + private void addTransactionItem(TransactionItem item) { + if (balances.containsKey(item.getBook())) + balances.put(item.getBook(), new Double(((Double) balances.get(item.getBook())).doubleValue() + item.getAmount())); + else + balances.put(item.getBook(), new Double(item.getAmount())); + } + /** * Similar to a transaction but guarantees that there wont be any negative balances left after the transaction. *************** *** 166,175 **** * */ ! public PostedTransaction performCompleteHold(PostedHeldTransaction hold, double amount, String comment) throws InvalidTransactionException, LowlevelLedgerException, TransactionExpiredException { ! if (!held.containsKey(hold) || hold.getExpiryTime().before(new Date())) throw new TransactionExpiredException(this, hold); held.remove(hold.getId()); PostedTransaction posted = new PostedTransaction(hold, new Date(), amount, comment); ledger.put(posted.getId(), posted); return posted; } --- 188,200 ---- * */ ! public PostedTransaction performCompleteHold(PostedHeldTransaction hold, double amount, String comment) throws InvalidTransactionException, LowlevelLedgerException, TransactionExpiredException, UnknownTransactionException { ! if (!held.containsKey(hold.getId())) ! throw new UnknownTransactionException(this, hold.getId()); ! if (hold.getExpiryTime().before(new Date())) throw new TransactionExpiredException(this, hold); held.remove(hold.getId()); PostedTransaction posted = new PostedTransaction(hold, new Date(), amount, comment); ledger.put(posted.getId(), posted); + updateBalances(posted); return posted; } *************** *** 196,214 **** */ public double getBalance(final String book) { ! double balance = 0; ! // Very silly slow and lazy implementation ! final Iterator iter = ledger.keySet().iterator(); ! final boolean going = true; ! while (iter.hasNext() && going) { ! final PostedTransaction tran = (PostedTransaction) ledger.get((String) iter.next()); ! final Iterator items = tran.getItems(); ! while (items.hasNext()) { ! final TransactionItem item = (TransactionItem) items.next(); ! if (item.getBook().equals(book)) ! balance += item.getAmount(); ! } ! } ! System.out.println("Book: " + book + " has a balance of: " + balance); ! return balance; } --- 221,227 ---- */ public double getBalance(final String book) { ! if (balances.containsKey(book)) ! return ((Double) balances.get(book)).doubleValue(); ! return 0; } *************** *** 228,234 **** // Very silly slow and lazy implementation final Iterator iter = held.keySet().iterator(); ! final boolean going = true; ! while (iter.hasNext() && going) { ! final PostedHeldTransaction tran = (PostedHeldTransaction) ledger.get((String) iter.next()); if (now.after(tran.getExpiryTime())) { iter.remove(); --- 241,247 ---- // Very silly slow and lazy implementation final Iterator iter = held.keySet().iterator(); ! while (iter.hasNext()) { ! final Object o = held.get(iter.next()); ! final PostedHeldTransaction tran = (PostedHeldTransaction) o; if (now.after(tran.getExpiryTime())) { iter.remove(); *************** *** 237,241 **** while (items.hasNext()) { final TransactionItem item = (TransactionItem) items.next(); ! if (item.getBook().equals(book)) balance += item.getAmount(); } --- 250,254 ---- while (items.hasNext()) { final TransactionItem item = (TransactionItem) items.next(); ! if (item.getBook().equals(book) && item.getAmount() < 0) balance += item.getAmount(); } *************** *** 284,287 **** --- 297,301 ---- private final LinkedHashMap held; private final String id; + private final HashMap balances; |