// BankAccountFile.java (c) 1998-2002 Kari Laitinen // 12.9.2002 File created. // 13.9.2002 Last modification. // The BankAccount class that is defined in this file // behaves in the same way as the Bank_account class in // the C++ file class_bank_account.h. // The BankAccount class of this file contains some // special methods, though. import java.io.* ; class StringIO { public static void write_sized_string( DataOutput output_stream, String string_to_write, int desired_string_size ) throws IOException { int character_index = 0 ; while ( character_index < string_to_write.length() ) { output_stream.writeChar( string_to_write.charAt( character_index ) ) ; character_index ++ ; } while ( character_index < desired_string_size ) { output_stream.writeChar( 0 ) ; character_index ++ ; } } public static String read_sized_string( DataInput input_source, int string_size ) throws IOException { StringBuffer character_buffer = new StringBuffer( string_size ) ; int number_of_characters_read = 0 ; boolean more_characters_available = true ; while ( more_characters_available == true && number_of_characters_read < string_size ) { char input_character = input_source.readChar() ; number_of_characters_read ++ ; if ( input_character == 0 ) { more_characters_available = false ; } else { character_buffer.append( input_character ) ; } } int number_of_filling_zeroes_left = 2 * ( string_size - number_of_characters_read ) ; input_source.skipBytes( number_of_filling_zeroes_left ) ; return character_buffer.toString() ; } } // end of class StringIO class BankAccount { String account_owner ; long account_number ; double account_balance = 0 ; public static final int PERSON_NAME_SIZE = 50 ; public static final int ACCOUNT_RECORD_SIZE = 2 * PERSON_NAME_SIZE + 8 + 8 ; BankAccount() {} 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 + ")" + "\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." ) ; } } public void write_account_record( DataOutput output_stream ) throws IOException { StringIO.write_sized_string( output_stream, account_owner, PERSON_NAME_SIZE ) ; output_stream.writeLong( account_number ) ; output_stream.writeDouble( account_balance ) ; } public void read_account_record( DataInput input_source ) throws IOException { account_owner = StringIO.read_sized_string( input_source, PERSON_NAME_SIZE ) ; account_number = input_source.readLong() ; account_balance = input_source.readDouble() ; } } // end of class BankAccount definition public class BankAccountFile { public static void main( String[] command_line_arguments ) { BankAccount mouse_account = new BankAccount( "Mickey Mouse", 222333, 3000.00 ) ; BankAccount duck_account = new BankAccount( "Donald Duck", 444555, 4000.00 ) ; BankAccount cow_account = new BankAccount( "Bertie Bird", 666777, 5000.00 ) ; try { DataOutputStream account_file = new DataOutputStream( new FileOutputStream( "account_records.data" ) ) ; mouse_account.write_account_record( account_file ) ; duck_account.write_account_record( account_file ) ; cow_account.write_account_record( account_file ) ; account_file.close() ; // We'll read the account records from the file into // an array. RandomAccessFile account_file_to_read = new RandomAccessFile( "account_records.data", "r" ) ; int number_of_records_in_file = (int) ( account_file_to_read.length() / BankAccount.ACCOUNT_RECORD_SIZE ) ; BankAccount[] account_array = new BankAccount[ number_of_records_in_file ] ; for ( int account_index = 0 ; account_index < number_of_records_in_file ; account_index ++ ) { account_array[ account_index ] = new BankAccount() ; account_array[ account_index ].read_account_record( account_file_to_read ) ; } account_file_to_read.close() ; // Finally, let's print the account records from the array. for ( int account_index = 0 ; account_index < account_array.length ; account_index ++ ) { account_array[ account_index ].show_account_data() ; } } catch ( IOException caught_exception ) { caught_exception.printStackTrace() ; } } }