Advertisement
tcroc2010

Present Value class error

Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1.  
  2. package business;
  3.  
  4. import java.text.NumberFormat;
  5.  
  6. /**
  7.  *
  8.  * @author
  9.  */
  10. public class PV extends Financial {
  11.     public static final String FUTAMTDESC = "Future Amount";
  12.     public static final String RESULTDESC = "Present Value";
  13.    
  14.     private boolean built;
  15.     private double[] bbal, iearn, ebal;
  16.    
  17.        public PV(){
  18.         super();
  19.         this.built = false;
  20.     }
  21.        
  22.     public PV (double a, double r, int t) {
  23.         super(a,r,t);
  24.         this.built = false;
  25.         if (super.isValid()) {
  26.             buildPV();
  27.         }
  28.     }
  29.    
  30.     private void biuldPV () {
  31.           try {
  32.             this.bbal = new double[super.getTerm()];
  33.             this.iearn = new double[super.getTerm()];
  34.             this.ebal = new double[super.getTerm()];
  35.             double morate = 0;
  36.             double denom = 0;
  37.  
  38.             this.bbal[0] = 0;
  39.             for (int i=0; i < super.getTerm(); i++) {
  40.                if (i > 0) {
  41.                    this.bbal[i] = this.ebal[i-1];
  42.                }
  43.                morate = super.getRate() / 12.0;
  44.                denom = super.getAmt() /Math.pow((1+morate), super.getTerm());
  45.                this.iearn[i] = (this.bbal[i] + super.getAmt()) * (super.getRate() / 12.0);
  46.                this.ebal[i] = this.bbal[i] + super.getAmt() + this.iearn[i];
  47.             }
  48.             this.built = true;
  49.         } catch (Exception e) {
  50.             super.setErrorMsg("PV build error.");
  51.             this.built = false;
  52.         }
  53.     }
  54.    
  55.        @Override
  56.     public double getBegBal(int mo) {
  57.         if (!this.built) {
  58.             buildPV();
  59.             if (!this.built) {
  60.                 return -1;
  61.             }
  62.         }
  63.         if (mo < 1 || mo > super.getTerm()) {
  64.             return -1;
  65.         }
  66.         return this.bbal[mo-1];
  67.     }
  68.  
  69.     @Override
  70.     public double getInterest(int mo) {
  71.         if (!this.built) {
  72.             buildPV();
  73.             if (!this.built) {
  74.                 return -1;
  75.             }
  76.         }
  77.         if (mo < 1 || mo > super.getTerm()) {
  78.             return -1;
  79.         }
  80.         return this.iearn[mo-1];
  81.     }
  82.    
  83.     @Override
  84.     public double getPrincipal (int mo) {
  85.         if (!this.built) {
  86.             buildPV();
  87.             if (!this.built) {
  88.                 return -1;
  89.             }
  90.         }
  91.         return super.getAmt();
  92.     }
  93.    
  94.     @Override
  95.     public double getEndBal(int mo){
  96.         if (!this.built) {
  97.             buildPV();
  98.             if (!this.built) {
  99.                 return -1;
  100.             }
  101.         }
  102.         if (mo < 1 || mo > super.getTerm()) {
  103.             return -1;
  104.         }
  105.         return this.ebal[mo-1];
  106.     }
  107.  
  108.     @Override
  109.     public double getResult() {
  110.         if (!this.built) {
  111.             buildPV();
  112.             if (!this.built) {
  113.                 return -1;
  114.             }
  115.         }
  116.         return this.ebal[super.getTerm()-1];
  117.     }
  118.    
  119.     @Override
  120.     public String getTitle () {
  121.         NumberFormat curr = NumberFormat.getCurrencyInstance();
  122.         NumberFormat pct = NumberFormat.getPercentInstance();
  123.         pct.setMaximumFractionDigits(3);
  124.        
  125.         return "Annuity Schedule " + curr.format(super.getAmt()) + " per month @ " + pct.format(super.getRate()) + " for " + super.getTerm() + " months.";
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement