home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in231vfd / mort.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.3 KB  |  125 lines

  1. //    Mort.java - Interactive Mortgage Calculator
  2. //
  3. //    Copyright (C) 1996 by Dale Gass
  4. //    Non-exclusive license granted to MKS, Inc.
  5. //
  6.  
  7. import java.lang.*;
  8. import java.util.*;
  9. import java.awt.*;
  10. import java.net.*;
  11. import java.applet.*;
  12.  
  13. // A labelled slider
  14.  
  15. class LabelSlider extends Panel {
  16.     Label label;
  17.     public Slider slider;
  18.  
  19.     public LabelSlider(String name, int w, int h, int m, int x, int a,
  20.         double s) {
  21.     setLayout(new PackerLayout());
  22.     label = new Label(name);
  23.     slider = new Slider(w, h, m, x, a, s);
  24.     add("label;side=right;anchor=e", slider);
  25.     add("label;side=right;anchor=se;padx=3", label);
  26.     resize(600, 40);
  27.     }
  28. }
  29.  
  30. // Mort - Interactive Morgage Calculator
  31.  
  32. public class Mort extends Panel {
  33.     LabelSlider Samt, Srat, Strm;    // Sliders for variables
  34.     Label result, biweek;        // Result indicators
  35.     static double amount = 100000.0;    // The variables (with defaults)
  36.     static double rate   = 10.0;
  37.     static double term   = 25.0;
  38.  
  39.     // Update the mortgage amount
  40.  
  41.     public void setAmount(int a) {
  42.         Samt.slider.SetValue(a / 1000);
  43.     recalc();
  44.     }
  45.  
  46.     // Perform the Canadian calculations, and update the results
  47.  
  48.     void recalc() {
  49.     amount = Samt.slider.GetValue() * 1000.0;
  50.     rate   = Srat.slider.GetValue() * .1;
  51.     term   = Strm.slider.GetValue() * 1.0;
  52.  
  53.     String s, t;
  54.     try {
  55.         double value = amount / ((1-1/Math.pow((rate/200+1), term*2)) /
  56.              (Math.pow(rate/200+1, 2.0/12)-1));
  57.         s = "$" + String.valueOf(value) + "/mo.";
  58.         double i = Math.pow(rate/200+1, 2*14/365.0);
  59.         value = Math.log(1/(1-(amount / (value/2) * (i-1)))) / 
  60.             Math.log(i) * 14 / 365.25;
  61.         t = "Bi-weekly payments pay off in " + value + " years.";
  62.     } catch (Exception e) {
  63.         s = "<Invalid>";
  64.         t = "";
  65.     }
  66.  
  67.     result.setText(s);
  68.         biweek.setText(t);
  69.     }
  70.  
  71.     // Constructor, with default price
  72.  
  73.     public Mort(int price) {
  74.  
  75.     PackerLayout pl;
  76.     setLayout(pl = new PackerLayout());
  77.     Label l = new Label("Mortgage Calculation (Canadian Method)");
  78.     l.setFont(new Font("TimesRoman", Font.BOLD|Font.ITALIC, 20));
  79.     add("title;side=top", l);
  80.  
  81.     result = new Label();
  82.     result.setAlignment(Label.CENTER);
  83.     result.setFont(new Font("TimesRoman", Font.BOLD, 25));
  84.     add("label;side=top", result);
  85.  
  86.     biweek = new Label();
  87.     biweek.setAlignment(Label.CENTER);
  88.     biweek.setFont(new Font("TimesRoman", Font.BOLD, 20));
  89.     add("short;side=top", biweek);
  90.  
  91.     Samt = new LabelSlider("Amount:", 500,35,30,400,price/1000,1000.0);
  92.     Srat = new LabelSlider("Rate %:", 500, 35, 50, 200, (int)(rate/.1),.1);
  93.     Strm = new LabelSlider("Years:",  500, 35, 1, 40, (int) term, 1.0);
  94.  
  95.     add("amt;side=top", Samt);
  96.     add("rat;side=top", Srat);
  97.     add("trm;side=top", Strm);
  98.  
  99.     recalc();
  100.     resize(600, 350);
  101.     }
  102.  
  103.     // Constructor, with no default price given (use current value or internal
  104.     // default.)
  105.  
  106.     public Mort() {
  107.         this((int) amount);
  108.     }
  109.  
  110.     // Watch for slider moves; update when we see any
  111.  
  112.     public boolean handleEvent(Event e) {
  113.         if (e.id == Slider.SLIDER_MOVE || e.id == Slider.SLIDER_STOP) {
  114.         if (e.id == Slider.SLIDER_STOP) {
  115.             new Sound(Sound.drip);
  116.         }
  117.         recalc();
  118.         result.resize(result.preferredSize());
  119.         biweek.resize(biweek.preferredSize());
  120.         return true;
  121.     } 
  122.     return false;
  123.     }
  124. }
  125.