Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. The good news is, I am practcing:
  2. package structure;a
  3.  
  4.  
  5. /**
  6.  * Represents a Bank Account , has a account that doesnโ€™t change, a balance, that does change.
  7.  *
  8.  * @author Alex Stoneham    
  9.  * @version 1
  10.  */
  11. public class BankAccount
  12. {
  13.  
  14.     /**
  15.      * The Account Number
  16.      */
  17.     protected String account;
  18.    
  19.     /**
  20.      * The Balance Associated with the account.
  21.      */
  22.     protected double balance;
  23.    
  24.  
  25.     /**
  26.      * Precondition account is a string  identifying the bank account
  27.      * balance is thr starting balance
  28.      * post: constructs a bank account with the desired balance
  29.      */
  30.     public BankAccount(String acc, double bal)
  31.     {
  32.  
  33.         account = acc;
  34.         balance = bal;
  35.     }
  36.    
  37.     /**
  38.      * pre: other is a valid BankAccount
  39.      * post: returns true if this account is the same as the other
  40.      */
  41.     public boolean equeals(Object other)
  42.     {
  43.         BankAccount that = (BankAccount)other;
  44.         // two accounts are the same if account numbers are the same.
  45.         return this.account.equals(that.account);
  46.     }
  47.  
  48.     /**
  49.      * post: Returns the bank account number of this account
  50.      */
  51.     public String getAccount()
  52.     {
  53.        return account;
  54.     }
  55.    
  56.     /**
  57.      * post retuns the balance of this bank account
  58.      */
  59.     public double getBalance()
  60.     {
  61.         return balance;
  62.     }
  63.    
  64.    
  65.     /**
  66.      * post: deposit money in the bank account
  67.      */
  68.     public void deposit(double amount)
  69.     {
  70.         balance = balance + amount;
  71.     }
  72.    
  73.     /**
  74.      * pre: there are sufficent funds in the account
  75.      * post: withdraw money from the bank account
  76.      */
  77.     public void withdraw(double amount)
  78.     {
  79.         balance = balance - amount;
  80.     }
  81.    
  82.    
  83.    
  84.     public static void main(String[] args)
  85.     {
  86.         // Question: is it better to invest $100 over 10 years at 5%
  87.         //              or to ivest $100 over 20 years at 2.5% intrest
  88.        
  89.         BankAccount jd = new BankAccount("John Dough", 100.00);
  90.         BankAccount js = new BankAccount("John Smith", 100.00);
  91.        
  92.         for (int years = 0; years < 10; years++)
  93.         {
  94.             jd.deposit(jd.getBalance() * 0.05);
  95.         }
  96.        
  97.         for (int years = 0; years < 20; years++)
  98.         {
  99.             js.deposit(js.getBalance() * 0.025);
  100.         }
  101.        
  102.        
  103.         System.out.println("John D invests $100 over 10 years at 5%.");
  104.         System.out.println("After 10 years " + jd.getAccount() +
  105.                             " has $" + jd.getBalance());
  106.                            
  107.         System.out.println("John S invests $100 over 20 years at 2.5%");                    
  108.         System.out.println("After 20 years " + js.getAccount() +
  109.                             " has $" + js.getBalance());
  110.  
  111.                        
  112.     }
  113.    
  114.    
  115.    
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement