Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.53 KB | None | 0 0
  1. /**
  2.  * @version 1.0
  3.  * @author Joe Stein
  4.  */
  5. public class TidBit
  6. {
  7.    private final double PAYMENT_RATE = .05;
  8.    private final double DOWN_PAYMENT_RATE = .10;
  9.    private final double INITIAL_PRICE;
  10.    private final double DOWN_PAYMENT;
  11.    private final double MONTHLY_PAYMENT;
  12.    private double curr_balance;
  13.    private double curr_interest;
  14.    private double curr_principal;
  15.    private int curr_month;
  16.    private double curr_pmt;
  17.    // principal = not with interest
  18.  
  19.    /**
  20.     * Creates a tidbit given the initial payment. If the initial payment is less
  21.     * than 0, the tidbit will use the absolute value of the number.
  22.     * @param p the intial payment
  23.     */
  24.    public TidBit(double p)
  25.    {
  26.       if (p < 0)
  27.       {
  28.          p = Math.abs(p);
  29.       }
  30.       INITIAL_PRICE = p;
  31.       DOWN_PAYMENT = p * DOWN_PAYMENT_RATE;
  32.       MONTHLY_PAYMENT = (PAYMENT_RATE)*(INITIAL_PRICE - DOWN_PAYMENT);
  33.       curr_balance = (INITIAL_PRICE - DOWN_PAYMENT);
  34.    }
  35.  
  36.    /**
  37.     * Returns the current balance.
  38.     * @return the current balance
  39.     */
  40.    public double getBalance()
  41.    {
  42.       return curr_balance;
  43.    }
  44.  
  45.    /**
  46.     * Returns the principal payment due for this month.
  47.     * @return the principal payment due for this month
  48.     */
  49.    public double getPrincipal()
  50.    {
  51.       return curr_principal;
  52.    }
  53.  
  54.    /**
  55.     * Returns the interest due for this month.
  56.     * @return the interest due for this month
  57.     */
  58.    public double getInterest()
  59.    {
  60.       return curr_interest;
  61.    }
  62.  
  63.    /**
  64.     * Returns the total payment due for this month.
  65.     * @return the total payment due for this month
  66.     */
  67.    public double getPayment()
  68.    {
  69.       return curr_pmt;
  70.    }
  71.  
  72.    /**
  73.     * Returns the month that this tidbit payment is on.
  74.     * @return the month that this tidbit payment is on
  75.     */
  76.    public int getMonth()
  77.    {
  78.       return curr_month;
  79.    }
  80.  
  81.    /**
  82.     * Advances a month on the payment and calculates the interest owed,
  83.     * principal, payment, etc.
  84.     */
  85.    public void nextMonth()
  86.    {
  87.       curr_interest = curr_balance*PAYMENT_RATE/12;
  88.       curr_principal = MONTHLY_PAYMENT - curr_interest;
  89.  
  90.       curr_month++;
  91.       if (MONTHLY_PAYMENT >= curr_balance) // if balance left < monthly payment
  92.       {
  93.          curr_principal = curr_balance;          // pay the rest of the balance
  94.          curr_pmt = curr_principal + curr_interest;
  95.          curr_balance = 0;
  96.       } else
  97.       {
  98.          curr_balance -= curr_principal;
  99.          curr_pmt = MONTHLY_PAYMENT;
  100.       }
  101.    }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement