jTruBela

Account Class

Nov 17th, 2020 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. // ***************************************************************************************/
  2. // File Name: Homework                              Class Name:Account.java      /     
  3. // Author: Justin Trubela                                                                                    /
  4. // Purpose: Demonstrates the use of separate classes                                     /
  5. //                                                                                                                           /
  6. // A bank account class with methods to deposit to, withdraw from,           /
  7. // change the name on, and get a String representation of the account.       /                                                                                                       /
  8. //****************************************************************************************/
  9. import java.text.NumberFormat;
  10. import java.util.Random;
  11. public class Account {
  12.    
  13.     private double balance;
  14.     private String name;
  15.     private long acctNum;
  16.     private static int numAccounts;
  17.     Random randAcctNum = new Random();
  18.    
  19.     //-----------------------------------------------------------------
  20.     //Constructor -- initializes balance, owner,
  21.     //              and account number as specified
  22.     //-----------------------------------------------------------------
  23.     public Account(double initBal, String owner, long number){
  24.         balance = initBal;
  25.         name = owner;
  26.         acctNum = number;
  27.         }
  28.    
  29.     //----------------------------------------------------------------------------------
  30.     //Constructor -- initializes balance and owner as specified;
  31.     //                          randomly generates the account number
  32.     //----------------------------------------------------------------------------------
  33.     public Account (double initBal, String owner) {
  34.         balance = initBal;
  35.         name = owner;
  36.         acctNum = randAcctNum.nextInt((999999)-100000);
  37.         }
  38.    
  39.     //-----------------------------------------------------------------------------------
  40.     //Constructor -- initializes  owner as specified;  
  41.     //                      Sets the initial balance to 0
  42.     //                      and randomly generates the account number
  43.     //------------------------------------------------------------------------------------
  44.     public Account (String owner) {
  45.         balance = 0;
  46.         name = owner;  
  47.         acctNum = randAcctNum.nextInt((999999)-100000);
  48.         }
  49.    
  50.     //---------------------------------------------------------------------------
  51.     // Checks to see if balance is sufficient for withdrawal.
  52.     // If so, decrements balance by amount;
  53.     // if not, prints message.
  54.     //-------------------------------------------------
  55.     public void withdraw(double amount){
  56.         if (balance >= amount) {
  57.             balance -= amount;
  58.         }
  59.         else {
  60.             System.out.println("Insufficient funds");
  61.                 }
  62.         }
  63.  
  64.     //------------------------------------------------------------------------------
  65.     //  Checks to see if balance is sufficient for withdrawal
  66.     //  and includes a fee.
  67.     //  If so, decrements balance by amount
  68.     //-------------------------------------------------------------------------------
  69.     public void withdraw(double amount, double fee){
  70.         if (balance >= (amount-fee)) {
  71.             balance -= (amount+fee);
  72.             }
  73.         else {
  74.             System.out.println("Insufficient funds");
  75.                 }
  76.         }
  77.     //-------------------------------------------------
  78.     // Adds deposit amount to balance.
  79.     //-------------------------------------------------
  80.     public void deposit(double amount){
  81.         balance += amount;
  82.     }
  83.    
  84.     //-------------------------------------------------
  85.     // Returns balance.
  86.     //-------------------------------------------------
  87.     public double getBalance(){
  88.         return balance;
  89.     }
  90.    
  91.     //-------------------------------------------------
  92.     // Returns a string containing the
  93.     //  name, account number, and balance.
  94.     //-------------------------------------------------
  95.     public String toString(){
  96.         NumberFormat fmt = NumberFormat.getCurrencyInstance();
  97.         return "Name:" + name +
  98.                    "\nAccount Number: " + acctNum +
  99.                    "\nBalance: " + fmt.format(balance);
  100.     }
  101.    
  102.     public void close() {
  103.         name = "CLOSED";
  104.         balance = 0;
  105.     }
  106.    
  107.     //-------------------------------------------------------------------------
  108.     //Constructor -- returns the number of account(s)
  109.     //                  that has been created
  110.     //-------------------------------------------------------------------------
  111.     public static int getNumAccounts() {
  112.         return numAccounts;
  113.     }
  114.    
  115.     //------------------------------------------------------------------------------------
  116.     //Constructor -- returns the name of account(s) in question
  117.     //------------------------------------------------------------------------------------
  118.     public String getNameAccounts() {
  119.         return name;
  120.     }
  121.  
  122.     //---------------------------------------------------------------------------------------------
  123.     //Constructor -- combines accounts if 2 names are the same
  124.     //  but will not combine if the account numbers are same
  125.     // Also, closes out the 2 identical accounts and creates a new one
  126.     //---------------------------------------------------------------------------------------------
  127.     public static Account consolidate(Account acct1, Account acct2)
  128.     {
  129.         if (acct1.getNameAccounts().equals(acct2.getNameAccounts())) {
  130.             if  (acct1.acctNum == acct2.acctNum ) {
  131.                 System.out.println("ERROR: Cannot combine "
  132.                         + "accounts with same account number ");
  133.                 return null;
  134.                 }
  135.  
  136.             double balance = acct1.getBalance() + acct2.getBalance();
  137.             String name = acct1.getNameAccounts();
  138.             Account acct3 = new Account(balance,name);
  139.             System.out.println("\n\nAccount after consolidation:\n" + acct3);
  140.             acct1.close();
  141.             acct2.close();
  142.             return acct3;
  143.         }
  144.         else {
  145.             System.out.println("ERROR: Cannot combine accounts. "
  146.                     + "Names do not match ");
  147.             return null;
  148.         }
  149.     }
  150. }
Add Comment
Please, Sign In to add comment