Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The good news is, I am practcing:
- package structure;a
- /**
- * Represents a Bank Account , has a account that doesnโt change, a balance, that does change.
- *
- * @author Alex Stoneham
- * @version 1
- */
- public class BankAccount
- {
- /**
- * The Account Number
- */
- protected String account;
- /**
- * The Balance Associated with the account.
- */
- protected double balance;
- /**
- * Precondition account is a string identifying the bank account
- * balance is thr starting balance
- * post: constructs a bank account with the desired balance
- */
- public BankAccount(String acc, double bal)
- {
- account = acc;
- balance = bal;
- }
- /**
- * pre: other is a valid BankAccount
- * post: returns true if this account is the same as the other
- */
- public boolean equeals(Object other)
- {
- BankAccount that = (BankAccount)other;
- // two accounts are the same if account numbers are the same.
- return this.account.equals(that.account);
- }
- /**
- * post: Returns the bank account number of this account
- */
- public String getAccount()
- {
- return account;
- }
- /**
- * post retuns the balance of this bank account
- */
- public double getBalance()
- {
- return balance;
- }
- /**
- * post: deposit money in the bank account
- */
- public void deposit(double amount)
- {
- balance = balance + amount;
- }
- /**
- * pre: there are sufficent funds in the account
- * post: withdraw money from the bank account
- */
- public void withdraw(double amount)
- {
- balance = balance - amount;
- }
- public static void main(String[] args)
- {
- // Question: is it better to invest $100 over 10 years at 5%
- // or to ivest $100 over 20 years at 2.5% intrest
- BankAccount jd = new BankAccount("John Dough", 100.00);
- BankAccount js = new BankAccount("John Smith", 100.00);
- for (int years = 0; years < 10; years++)
- {
- jd.deposit(jd.getBalance() * 0.05);
- }
- for (int years = 0; years < 20; years++)
- {
- js.deposit(js.getBalance() * 0.025);
- }
- System.out.println("John D invests $100 over 10 years at 5%.");
- System.out.println("After 10 years " + jd.getAccount() +
- " has $" + jd.getBalance());
- System.out.println("John S invests $100 over 20 years at 2.5%");
- System.out.println("After 20 years " + js.getAccount() +
- " has $" + js.getBalance());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement