Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***************************************************************************************/
- // File Name: Homework Class Name:Account.java /
- // Author: Justin Trubela /
- // Purpose: Demonstrates the use of separate classes /
- // /
- // A bank account class with methods to deposit to, withdraw from, /
- // change the name on, and get a String representation of the account. / /
- //****************************************************************************************/
- import java.text.NumberFormat;
- import java.util.Random;
- public class Account {
- private double balance;
- private String name;
- private long acctNum;
- private static int numAccounts;
- Random randAcctNum = new Random();
- //-----------------------------------------------------------------
- //Constructor -- initializes balance, owner,
- // and account number as specified
- //-----------------------------------------------------------------
- public Account(double initBal, String owner, long number){
- balance = initBal;
- name = owner;
- acctNum = number;
- }
- //----------------------------------------------------------------------------------
- //Constructor -- initializes balance and owner as specified;
- // randomly generates the account number
- //----------------------------------------------------------------------------------
- public Account (double initBal, String owner) {
- balance = initBal;
- name = owner;
- acctNum = randAcctNum.nextInt((999999)-100000);
- }
- //-----------------------------------------------------------------------------------
- //Constructor -- initializes owner as specified;
- // Sets the initial balance to 0
- // and randomly generates the account number
- //------------------------------------------------------------------------------------
- public Account (String owner) {
- balance = 0;
- name = owner;
- acctNum = randAcctNum.nextInt((999999)-100000);
- }
- //---------------------------------------------------------------------------
- // Checks to see if balance is sufficient for withdrawal.
- // If so, decrements balance by amount;
- // if not, prints message.
- //-------------------------------------------------
- public void withdraw(double amount){
- if (balance >= amount) {
- balance -= amount;
- }
- else {
- System.out.println("Insufficient funds");
- }
- }
- //------------------------------------------------------------------------------
- // Checks to see if balance is sufficient for withdrawal
- // and includes a fee.
- // If so, decrements balance by amount
- //-------------------------------------------------------------------------------
- public void withdraw(double amount, double fee){
- if (balance >= (amount-fee)) {
- balance -= (amount+fee);
- }
- else {
- System.out.println("Insufficient funds");
- }
- }
- //-------------------------------------------------
- // Adds deposit amount to balance.
- //-------------------------------------------------
- public void deposit(double amount){
- balance += amount;
- }
- //-------------------------------------------------
- // Returns balance.
- //-------------------------------------------------
- public double getBalance(){
- return balance;
- }
- //-------------------------------------------------
- // Returns a string containing the
- // name, account number, and balance.
- //-------------------------------------------------
- public String toString(){
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- return "Name:" + name +
- "\nAccount Number: " + acctNum +
- "\nBalance: " + fmt.format(balance);
- }
- public void close() {
- name = "CLOSED";
- balance = 0;
- }
- //-------------------------------------------------------------------------
- //Constructor -- returns the number of account(s)
- // that has been created
- //-------------------------------------------------------------------------
- public static int getNumAccounts() {
- return numAccounts;
- }
- //------------------------------------------------------------------------------------
- //Constructor -- returns the name of account(s) in question
- //------------------------------------------------------------------------------------
- public String getNameAccounts() {
- return name;
- }
- //---------------------------------------------------------------------------------------------
- //Constructor -- combines accounts if 2 names are the same
- // but will not combine if the account numbers are same
- // Also, closes out the 2 identical accounts and creates a new one
- //---------------------------------------------------------------------------------------------
- public static Account consolidate(Account acct1, Account acct2)
- {
- if (acct1.getNameAccounts().equals(acct2.getNameAccounts())) {
- if (acct1.acctNum == acct2.acctNum ) {
- System.out.println("ERROR: Cannot combine "
- + "accounts with same account number ");
- return null;
- }
- double balance = acct1.getBalance() + acct2.getBalance();
- String name = acct1.getNameAccounts();
- Account acct3 = new Account(balance,name);
- System.out.println("\n\nAccount after consolidation:\n" + acct3);
- acct1.close();
- acct2.close();
- return acct3;
- }
- else {
- System.out.println("ERROR: Cannot combine accounts. "
- + "Names do not match ");
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment