| 1 | package net.sourceforge.finmodel.example; |
| 2 | |
| 3 | import java.math.BigDecimal; |
| 4 | import java.text.DateFormat; |
| 5 | import java.text.NumberFormat; |
| 6 | import java.text.SimpleDateFormat; |
| 7 | import java.util.Calendar; |
| 8 | import java.util.Date; |
| 9 | import java.util.GregorianCalendar; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import net.sourceforge.finmodel.account.Account; |
| 13 | import net.sourceforge.finmodel.account.AccountImpl; |
| 14 | import net.sourceforge.finmodel.account.CashAccount; |
| 15 | import net.sourceforge.finmodel.account.InterestAccount; |
| 16 | import net.sourceforge.finmodel.asset.Stock; |
| 17 | import net.sourceforge.finmodel.plan.FinancialPlan; |
| 18 | import net.sourceforge.finmodel.plan.Transaction; |
| 19 | import net.sourceforge.finmodel.rules.CashFlowRule; |
| 20 | |
| 21 | import org.jfin.date.Frequency; |
| 22 | import org.jfin.date.Period; |
| 23 | |
| 24 | public class SimplePlan { |
| 25 | static final BigDecimal ZERO = BigDecimal.ZERO; |
| 26 | |
| 27 | CashAccount job1 = new CashAccount("Job #1"); |
| 28 | CashAccount openingBalance = new CashAccount("Opening Balance"); |
| 29 | InterestAccount collegeFund = new InterestAccount("College Fund", new BigDecimal("0.05")); |
| 30 | CashAccount medical = new CashAccount("Medical Expenses"); |
| 31 | CashAccount job2 = new CashAccount("Job #2"); |
| 32 | InterestAccount cash = new InterestAccount("Available Cash", new BigDecimal("0.002")); |
| 33 | |
| 34 | Account msft = new AccountImpl("MSFT", Stock.getStock("MSFT")); |
| 35 | |
| 36 | public FinancialPlan finPlan = new FinancialPlan(); |
| 37 | |
| 38 | public SimplePlan() throws Exception { |
| 39 | Period simplePeriod = new Period(); |
| 40 | DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); |
| 41 | Calendar startDate = new GregorianCalendar(); |
| 42 | startDate.setTime(df.parse("2010/01/01")); |
| 43 | Calendar endDate = new GregorianCalendar(); |
| 44 | endDate.setTime(df.parse("2040/01/01")); |
| 45 | |
| 46 | simplePeriod.setStartCalendar(startDate); |
| 47 | simplePeriod.setEndCalendar(endDate); |
| 48 | finPlan.setName("SimplePlan"); |
| 49 | |
| 50 | CashFlowRule job1Pay = new CashFlowRule(); |
| 51 | job1Pay.setSource(job1); |
| 52 | job1Pay.setDestination(cash); |
| 53 | job1Pay.setAmount(new BigDecimal("1000.00")); |
| 54 | job1Pay.setFrequency(Frequency.WEEKLY); |
| 55 | job1Pay.setPeriod( simplePeriod ); |
| 56 | |
| 57 | finPlan.addRule( job1Pay ); |
| 58 | |
| 59 | CashFlowRule job2Pay = new CashFlowRule(); |
| 60 | job2Pay.setSource(job2); |
| 61 | job2Pay.setDestination(cash); |
| 62 | job2Pay.setAmount(new BigDecimal("3200.00")); |
| 63 | job2Pay.setFrequency(Frequency.MONTHLY); |
| 64 | job2Pay.setPeriod( simplePeriod ); |
| 65 | |
| 66 | finPlan.addRule( job2Pay ); |
| 67 | |
| 68 | CashFlowRule college = new CashFlowRule(); |
| 69 | college.setSource(cash); |
| 70 | college.setDestination(collegeFund); |
| 71 | college.setAmount(new BigDecimal("210.00")); |
| 72 | college.setFrequency(Frequency.WEEKLY); |
| 73 | college.setPeriod( simplePeriod ); |
| 74 | |
| 75 | finPlan.addRule( college ); |
| 76 | |
| 77 | CashFlowRule medicalBills = new CashFlowRule(); |
| 78 | medicalBills.setSource(cash); |
| 79 | medicalBills.setDestination(medical); |
| 80 | medicalBills.setAmount(new BigDecimal("150.00")); |
| 81 | medicalBills.setFrequency(Frequency.WEEKLY); |
| 82 | medicalBills.setPeriod( simplePeriod ); |
| 83 | |
| 84 | finPlan.addRule( medicalBills ); |
| 85 | |
| 86 | CashFlowRule msftStock = new CashFlowRule(); |
| 87 | msftStock.setSource(cash); |
| 88 | msftStock.setDestination(msft); |
| 89 | msftStock.setAmount(new BigDecimal("50.00")); |
| 90 | msftStock.setFrequency(Frequency.MONTHLY); |
| 91 | msftStock.setPeriod(simplePeriod); |
| 92 | |
| 93 | finPlan.addRule( msftStock ); |
| 94 | } |
| 95 | |
| 96 | public static void main(String args[]) { |
| 97 | try { |
| 98 | DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); |
| 99 | NumberFormat cf = NumberFormat.getCurrencyInstance(); |
| 100 | SimplePlan plan = new SimplePlan(); |
| 101 | List<Transaction> transactions = plan.finPlan.getScheduledTransactions(); |
| 102 | |
| 103 | long lastTime = 0; |
| 104 | System.err.println("DATE\t\tCASH\t\tCF\t\tMSFT"); |
| 105 | for (Transaction trax: transactions) { |
| 106 | long currTime = trax.getDate().getTimeInMillis(); |
| 107 | if (currTime != lastTime) { |
| 108 | System.err.print(df.format(new Date(lastTime))); |
| 109 | System.err.print("\t\t"); |
| 110 | System.err.print(cf.format(plan.cash.getBalance(trax.getDate()))); |
| 111 | System.err.print("\t\t"); |
| 112 | System.err.print(cf.format(plan.collegeFund.getBalance(trax.getDate()))); |
| 113 | System.err.print("\t\t"); |
| 114 | System.err.println(cf.format(plan.msft.getBalance(trax.getDate()))); |
| 115 | } |
| 116 | // BigDecimal xferAmt = trax.getAmount(); |
| 117 | // trax.getSource().sell(xferAmt, trax.getDate()); |
| 118 | // trax.getDestination().buy(xferAmt, trax.getDate()); |
| 119 | |
| 120 | lastTime = trax.getDate().getTimeInMillis(); |
| 121 | } |
| 122 | } catch (Exception e) { |
| 123 | e.printStackTrace(); |
| 124 | } |
| 125 | } |
| 126 | } |