// BankRichPersonAccount.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-02-16 File created. // 2006-02-16 Last modification. // A solution to Exercise 12-8. class BankAccount { protected String account_owner ; protected long account_number ; protected double account_balance ; public BankAccount( String given_account_owner, long given_account_number, double initial_balance ) { account_owner = given_account_owner ; account_number = given_account_number ; account_balance = initial_balance ; } public void show_account_data() { System.out.print( "\n\nB A N K A C C O U N T D A T A : " + "\n Account owner : " + account_owner + "\n Account number: " + account_number + "\n Current balance: " + account_balance ) ; } public void deposit_money( double amount_to_deposit ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; System.out.print( "\n Amount deposited: " + amount_to_deposit + "\n Old account balance: " + account_balance ) ; account_balance = account_balance + amount_to_deposit ; System.out.print( " New balance: " + account_balance ) ; } public void withdraw_money( double amount_to_withdraw ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; if ( account_balance < amount_to_withdraw ) { System.out.print("\n -- Transaction not completed: " + "Not enough money to withdraw " + amount_to_withdraw ) ; } else { System.out.print("\n Amount withdrawn: " + amount_to_withdraw + "\n Old account balance: " + account_balance ) ; account_balance = account_balance - amount_to_withdraw ; System.out.print(" New balance: " + account_balance ) ; } } public void transfer_money_to( BankAccount receiving_account, double amount_to_transfer ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; if ( account_balance >= amount_to_transfer ) { receiving_account.account_balance = receiving_account.account_balance + amount_to_transfer ; System.out.print("\n " + amount_to_transfer + " was transferred to " + receiving_account.account_owner + " (Account no. " + receiving_account.account_number + ")." + "\n Balance before transfer: " + account_balance ) ; account_balance = account_balance - amount_to_transfer ; System.out.print(" New balance: " + account_balance ) ; } else { System.out.print( "\n -- Not enough money for transfer." ) ; } } } class AccountWithCredit extends BankAccount { protected double credit_limit ; public AccountWithCredit( String given_account_owner, long given_account_number, double initial_balance, double given_credit_limit ) { super( given_account_owner, given_account_number, initial_balance ) ; credit_limit = given_credit_limit ; } public void withdraw_money( double amount_to_withdraw ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; if ( account_balance + credit_limit < amount_to_withdraw ) { System.out.print( "\n -- Transaction not completed: " + "Not enough credit to withdraw "+ amount_to_withdraw ) ; } else { System.out.print( "\n Amount withdrawn: " + amount_to_withdraw + "\n Old account balance: " + account_balance ) ; account_balance = account_balance - amount_to_withdraw ; System.out.print( " New balance: " + account_balance ) ; } } } class RestrictedAccount extends BankAccount { protected double maximum_amount_to_withdraw ; public RestrictedAccount( String given_account_owner, long given_account_number, double initial_balance, double given_withdrawal_limit ) { super( given_account_owner, given_account_number, initial_balance ) ; maximum_amount_to_withdraw = given_withdrawal_limit ; } public void withdraw_money( double amount_to_withdraw ) { if ( amount_to_withdraw > maximum_amount_to_withdraw ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; System.out.print( "\n -- Transaction not completed: Cannot withdraw " + amount_to_withdraw + "\n -- Withdrawal limit is " + maximum_amount_to_withdraw + "." ) ; } else { super.withdraw_money( amount_to_withdraw ) ; } } } class RichPersonAccount extends BankAccount { protected double minimum_amount_to_deposit ; public RichPersonAccount( String given_account_owner, long given_account_number, double initial_balance, double given_deposit_limit ) { super( given_account_owner, given_account_number, initial_balance ) ; minimum_amount_to_deposit = given_deposit_limit ; } public void deposit_money( double amount_to_deposit ) { if ( amount_to_deposit < minimum_amount_to_deposit ) { System.out.print( "\n\nTRANSACTION FOR ACCOUNT OF " + account_owner + " (Account number " + account_number + ")" ) ; System.out.print( "\n -- Transaction not completed: Cannot deposit " + amount_to_deposit + "\n -- Minimum deposit amount is " + minimum_amount_to_deposit + "." ) ; } else { super.deposit_money( amount_to_deposit ) ; } } } class BankRichPersonAccount { public static void main( String[] not_in_use ) { RichPersonAccount rockefeller_account = new RichPersonAccount( "John D.", 55555, 900000.00, 9000.00 ) ; rockefeller_account.deposit_money( 8000.00 ) ; RichPersonAccount getty_account = new RichPersonAccount( "Paul Getty", 66666, 900000.00, 7000.00 ) ; getty_account.deposit_money( 8000.00 ) ; } }