| 1 | package net.sourceforge.finmodel.rules; |
| 2 | |
| 3 | import java.math.BigDecimal; |
| 4 | import java.math.MathContext; |
| 5 | import java.math.RoundingMode; |
| 6 | import java.util.Calendar; |
| 7 | |
| 8 | import net.sourceforge.finmodel.account.Account; |
| 9 | |
| 10 | public class APRRule extends PeriodicRule implements Rule { |
| 11 | public static final long serialVersionUID = 1L; |
| 12 | |
| 13 | private BigDecimal apr; |
| 14 | private Account examine; |
| 15 | |
| 16 | public void setAPR(BigDecimal apr) { |
| 17 | this.apr = apr; |
| 18 | } |
| 19 | |
| 20 | public BigDecimal getAPR() { |
| 21 | return apr; |
| 22 | } |
| 23 | |
| 24 | public void setExamine(Account examine) { |
| 25 | this.examine = examine; |
| 26 | } |
| 27 | |
| 28 | public Account getExamine() { |
| 29 | return examine; |
| 30 | } |
| 31 | |
| 32 | public BigDecimal getAmount(Calendar calendar) { |
| 33 | double timesPerYear = 12; |
| 34 | switch (getFrequency()) { |
| 35 | case WEEKLY: |
| 36 | timesPerYear = 52; |
| 37 | break; |
| 38 | case MONTHLY: |
| 39 | timesPerYear = 12; |
| 40 | break; |
| 41 | case ANNUALLY: |
| 42 | timesPerYear = 1; |
| 43 | break; |
| 44 | case QUARTERLY: |
| 45 | timesPerYear = 4; |
| 46 | break; |
| 47 | case SEMI_ANNUALLY: |
| 48 | timesPerYear = 2; |
| 49 | break; |
| 50 | default: |
| 51 | timesPerYear = 12; |
| 52 | } |
| 53 | |
| 54 | double interestPerCycle = apr.doubleValue() / timesPerYear; |
| 55 | double amt = getExamine().getBalance(calendar).doubleValue() * interestPerCycle; |
| 56 | MathContext context = new MathContext(2, RoundingMode.HALF_DOWN); |
| 57 | |
| 58 | BigDecimal amount = new BigDecimal(amt, context); |
| 59 | return amount; |
| 60 | } |
| 61 | } |