/* * Savings.java * * Created on February 9, 2005, 10:21 PM */ package bank; /** * * @author Mike */ public class Savings extends BankAccount implements Comparable { private double intYTD = 0; // interest rate for savings accounts public static double intRate = 0.03; /** Creates a new instance of Savings */ public Savings() { //super(); System.out.println("In default Savings Constructor"); intYTD = 0; } /** * Creates a new instance of Savings given a pin and balance */ public Savings(String pin, double bal) { super(pin,bal); System.out.println("In 2 param Savings Constructor"); intYTD = 0; } /** * Creates a new instance of Savings given an account number, pin and balance */ public Savings(String acct, String pin, double bal) { super(acct,pin,bal); intYTD = 0; } // dont know if we need a constructor that sets intYTD /** * pay interest into the calling Account based on the number of days * paying interest for (also, the class knows the interest rate) * Return: amount of interest paid */ public double payInterest (int numDays) { if (numDays <= 0) { System.out.println("ERROR - invalid number of days"); // no interest if no days or fewer return 0; } // daily interest rate is annual interest rate reduced by // proportion of days paying for double intAmt = balance * (intRate * (numDays / 365.0)); // update account balance and interest year to date balance += intAmt; intYTD += intAmt; return intAmt; } /** * MAR - I think the above is better * pay interest into the calling Account based on the number of days * paying interest for (also, the account knows the interest rate) * Return: whether interest is paid */ public boolean payInterestfirstVersion (int numDays) { if (numDays <= 0) { System.out.println("ERROR - invalid number of days"); return false; } double intAmt = balance * (intRate * (numDays / 365.0)); // update account balance and interest year to date balance += intAmt; intYTD += intAmt; return true; } /** * process month end - pay interest into the calling Account * default version - pays based on 30 day months * Return: new balance */ public double processMonthEnd() { double interest = payInterest(30); return getBal(); } /** * process month end - pay interest into the calling Account * default version - pays based on days in month - passed * Return: new balance */ public double processMonthEnd(int days) { double interest = payInterest(days); return getBal(); } /////////////////////////////////////////////////////////////////// // Inspectors /////////////////////////////////////////////////////////////////// /** * report the year to date interest */ public double getIntYTD() { return intYTD; } /** * we need a way of determining the interest rate * Static - since interest rate is static - once per class */ public static double getIntRate () { return intRate; } /** * Override Account's toString */ public String toString() { String res = super.toString(); res = res + " intYTD: " + intYTD; return res; } /////////////////////////////////////////////////////////////////// // Mutators /////////////////////////////////////////////////////////////////// /** * change the year to date interest */ public void setIntYTD(double amt) { intYTD = amt; } /** * reset the year to date interest back to zero */ public void resetIntYTD() { intYTD = 0; } /** * we need a way of setting the interest rate * Static - since interest rate is static - once per class */ public static void setIntRate (double rate) { intRate = rate; } /** * Compares two savings accounts - needed for sorting - required since implements Comparable interface * Don't show until we cover interfaces * returns negative if object comparing to is less, zero if object comparing to is equal, * positive if object comparing to is greater * MAR - I have made the call that accounts should be compared based on their acct number */ public int compareTo(Object obj) { // convert the passed object to an BankAccount\ BankAccount other = (BankAccount) obj; return super.compareTo(obj); /* // pull out the ids Id otherId = other.getAcctID(); Id firstId = getAcctID(); // make an object based on the otherId Object otherObj = (Object) otherId; // do the comparison using Id class's capabilities (compareTo) // and return the result return firstId.compareTo(otherObj); **/ } }