Java Reference
In-Depth Information
so forth. The class contains all the necessary getter/setter methods, but we do not
show all methods, for lack of space.
The bank must maintain records of transactions. For this purpose, we define
class Transaction , also in Fig. 12.13. An instance of this class maintains the
account number, the kind of the transaction, and the amount of the transaction.
We must modify methods deposit and withdraw in class BankAccount to
create instances of class Transaction , but first, we discuss the structure of this
program. Class Transaction is in its own file and is public, so the user of this
bank-account program sees it and can reference it. The principle of information
hiding (see Sec. 3.1.1) would have us hide class Transaction since the user does
/** A bank account */
public class BankAccount {
/** a ccount number, amount in account, and last transaction carried out
("deposit" or "withdrawal") */
private int account;
private int balance;
private Transaction lastTransaction;
// Getter and setter methods omitted.
/** Deposit amount in this account */
public void deposit( int amount)
{ balance= balance + amount; }
/** Withdraw amount from this account */
public void withdraw( int amount)
{ balance= balance - amount; }
/** An instance is a bank-account transaction */
public class Transaction {
/** account number, type of transaction ( "deposit" , "withdrawal"), and amount */
int account;
String transaction;
int amount;
/** Constructor: instance for account a , kind t , and amount n */
public Transaction( int a, int n, String t)
{ account= a; transaction= t; amount= n; }
/** representation of this transaction */
public String toString()
{ return account + ": " + transaction + " " + amount; }
Figure 12.13:
Classes BankAccount and Transaction (in separate files)
Search WWH ::

Custom Search