finished basic account add
[Bookkeeping.git] / src / com / interrupt / bookkeeping / account / Accounts.java
blobd6ceabb199e0f304cd2a72e5663848e7561f610d
2 package com.interrupt.bookkeeping.account;
5 import java.util.List;
6 import java.util.ArrayList;
7 import java.util.Iterator;
9 import org.apache.log4j.Logger;
11 import com.interrupt.bob.base.BobSystem;
13 import com.interrupt.bob.base.IBob;
14 import com.interrupt.bookkeeping.exception.AccountException;
15 import com.interrupt.bookkeeping.exception.CurrencyException;
17 /* - asset/expense ([DEBIT] outflow of cash -> paying for an exp or buying an asset)
18 * - liability/revenue ([CREDIT] inflow of cash -> sold something or borrowing $)
20 public class Accounts extends GAccounts {
23 private Logger logger = Logger.getLogger(Accounts.class);
25 //private Accounts instance = null;
27 /* figure out a way to make Singleton work
29 public Accounts() {
30 super();
32 public Accounts(String namespace, String tagName) {
33 super(namespace,tagName);
38 public IBob make() {
39 return new Accounts();
42 /*public IAccounts getInstance() {
44 if(instance == null) {
45 instance = new Accounts();
47 return instance;
52 /* remove all accounts from system - DANGEROUS
54 public void purge() {
56 // >> replace this code with 'Accounts.removeAllAccount()' when ready <<
57 this.removeAllAccount();
59 /*List accountList = this.allAccount();
60 Iterator iter = accountList.iterator();
62 IAccount nextAccount = null;
63 String nextid = null;
64 while(iter.hasNext()) {
66 nextAccount = (IAccount)iter.next();
67 nextid = nextAccount.getId();
68 this.removeAccountById(nextid);
74 public void addAccount(Account account) {
75 this.addAccount((IAccount)account);
77 public void addAccount(IAccount account) {
79 //ensure there are no duplicate IDs
80 List accounts = this.allAccount();
81 Iterator iter = accounts.iterator();
82 IAccount next = null;
83 while(iter.hasNext()) {
84 next = (IAccount)iter.next();
85 String nextid = next.getId();
87 if(nextid.equals(account.getId())) {
89 logger.debug("Accounts.addAccount: " + account.toXML());
90 throw new AccountException("duplicate id");
93 super.addAccount(account);
97 /* 1. account types are: asset,liability,expense,revenue,capital
98 * - depending on the type of account, counterWeight is (debit or credit)
100 public boolean balances() {
102 List accounts = this.allAccount();
103 Iterator iter = accounts.iterator();
104 IAccount nextAccount = null;
106 double d_dcount = 0;
107 double d_ccount = 0;
108 double c_dcount = 0;
109 double c_ccount = 0;
110 while(iter.hasNext()) {
112 nextAccount = (IAccount)iter.next();
113 if((nextAccount.getCounterWeight()).equals(Account.DEBIT)) {
115 List alldebit = ((Account)nextAccount).allDebitLookup();
116 Iterator diter = alldebit.iterator();
117 while(diter.hasNext()) {
119 IDebit nextDebit = (IDebit)diter.next();
120 if(!nextAccount.getCurrency().equals(nextDebit.getCurrency())) {
121 throw new CurrencyException("Debit currency["+nextDebit.getCurrency()+"] mismatch with Account currency["+nextAccount.getCurrency()+"]");
123 d_dcount += Double.parseDouble(nextDebit.getAmount());
126 List allcredit = ((Account)nextAccount).allCreditLookup();
127 Iterator citer = allcredit.iterator();
128 while(citer.hasNext()) {
130 ICredit nextCredit = (ICredit)citer.next();
131 if(!nextAccount.getCurrency().equals(nextCredit.getCurrency())) {
132 throw new CurrencyException("Credit currency["+nextCredit.getCurrency()+"] mismatch with Account currency["+nextAccount.getCurrency()+"]");
134 d_ccount += Double.parseDouble(nextCredit.getAmount());
137 else if((nextAccount.getCounterWeight()).equals(Account.CREDIT)) {
139 List allCredit = ((Account)nextAccount).allCreditLookup();
140 Iterator diter = allCredit.iterator();
141 while(diter.hasNext()) {
143 IDebit nextDebit = (IDebit)diter.next();
144 if(!nextAccount.getCurrency().equals(nextDebit.getCurrency())) {
145 throw new CurrencyException("Debit currency["+nextDebit.getCurrency()+"] mismatch with Account currency["+nextAccount.getCurrency()+"]");
147 c_dcount += Double.parseDouble(nextDebit.getAmount());
150 List allcredit = ((Account)nextAccount).allCreditLookup();
151 Iterator citer = allcredit.iterator();
152 while(citer.hasNext()) {
154 ICredit nextCredit = (ICredit)citer.next();
155 if(!nextAccount.getCurrency().equals(nextCredit.getCurrency())) {
156 throw new CurrencyException("Credit currency["+nextCredit.getCurrency()+"] mismatch with Account currency["+nextAccount.getCurrency()+"]");
158 c_ccount += Double.parseDouble(nextCredit.getAmount());
162 double totalLeft = d_dcount + c_ccount;
163 double totalRight = d_ccount + c_dcount;
165 if(totalLeft == totalRight) {
166 return true;
168 return false;