Description:
In this homework, you will essentially write a program that runs a
simple bank. Three classes will be used in this homework. You have to
implement these three classes. The names of these classes are
BankAccount, Bank, and HW03Bank. These classes should contain the
following instance methods. Make sure that all of your instance
variables are declared as private.
BankAccount class:
A BankAccount object keeps information about one particular bank
account at a bank. BankAccount class should contain following instance
variables (BankAccount class may contain static variables too).
private int accountNum; // each bank account should have a unique
account number
private String customerName; // the owner of the account
private double balance; // the current balance of the account
private double[] transactions; // all the transactions (deposits and
withdraws) on this account
private int numOfTransactions; // the number of the transactions on
the account
You have to include the following constructor and instance methods in
your BankAccount class:
// Constructor: Initializes the new BankAccount object. customerName
and balance are initialized
// from the given values.You have to generate a unique account number
for the new
// BankAccount object.The opening balance will be the first
transaction (deposit) on this account.
public BankAccount(String cname, double openingBalance)
// Returns the account number of the BankAccount object the method is
called on.
public int getAccountNum()
// Returns a string containing the account number, the customer name
and the balance of
// the current bank account (the BankAccount object the method is
called on).
public String getAccountInfo()
// Returns a string containing the last N transactions of the current
bank account
public String getTransactionInfo(int lastNTransactions)
// Withdraws the given amount from the current account. It should also
register this withdraw
// as a transaction. If the balance is not enough, it should print a
message.
public void withdraw(double amount)
// Deposits the given amount to the current account. It should also
register this deposit
// as a transaction.
public void deposit(double amount)
Bank class:
A Bank object keeps information about the bank accounts at a bank.
Bank class should contain following instance variables (Bank class may
contain static variables too).
private BankAccount[] accounts; // all the bank accounts at this
bank
private int numOfAccounts; // the number of bank accounts at this
bank
You have to include the following constructor and instance methods in
your Bank class:
// Constructor: Initializes the new Bank object. This new Bank object
initially doesnt contain
// any account.
public Bank()
// Opens a new bank account and returns the account number of this new
account.
// Adds this account into the account list of the bank (the Bank
object the method is called on).
// It takes the customer name and the opening balance as parameters
public int openNewAccount(String customerName, double openingBalance)
// Withdraws the given amount from the account whose account number is
given.
// If the account is not available at the bank, it should print a
message.
public void withdrawFrom(int accountNum, double amount)
// Deposits the given amount to the account whose account number is
given.
// If the account is not available at the bank, it should print a
message.
public void depositTo(int accountNum, double amount)
// Prints the account number, the customer number and the balance of
the bank account whose
// account number is given.
// If the account is not available at the bank, it should print a
message.
public void printAccountInfo(int accountNum)
// Prints the account number, the customer number and the balance of
the bank account whose
// account number is given (together with last N transactions on that
account).
// If the account is not available at the bank, it should print a
message.
public void printAccountInfo(int accountNum, int lastNTransactions)
HW03Bank class:
You will create a console application that will "run" a bank. Your
HW03Bank class should contain only a main method. In this main method,
you will create a Bank object first, and you will perform certain
menu-driven operations using this Bank object. The main method of your
HW03Bank class should do the followings:
Create a new Bank object. You will use this Bank object for the
operations.
Prompt the user with the following menu choices:
1) open a new bank account
2) deposit to a bank account
3) withdraw from a bank account
4) print short account information
5) print the detailed account information including last transactions
6) quit
Execute the choice the user chooses and then continue till the user
quits.
What each menu item choice should do:
1. Ask the user for the customer name and the opening balance of a new
bank account, and create this new bank account from that information
at the bank. Print the account number of this new account.
2. Ask the user for the account number of an account, and a deposit
amount. Deposit the given amount to the bank account whose account
number is given.
3. Ask the user for the account number of an account, and a withdraw
amount. Withdraw the given amount from the bank account whose account
number is given.
4. Ask the user for the account number of an account, and print the
short information (account number, customer name, and balance) about
this account.
5. Ask the user for the account number of an account, and the number
of last N transactions. Print the detailed information (account
number, customer name, balance, and the last N transactions) about
this account.
6. Quit.
This is what i have so far
import java.io.*;
public class HW03Bank {
public static void main (String[] args) throws IOException {
// declare variables
...
// create the BufferedReader object for input
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
// create a bank
abank = new Bank();
// Give the menu to user until he/she quits
...
}
}
//
// BankAccount Class
//
class BankAccount {
// other static variables
...
// Fields
private int accountNum;
private String customerName;
private double balance;
private double[] transactions;
private int numOfTransactions;
// Constructors
public BankAccount(String cname, double openingBalance) {
...
}
// getAccountNum
public int getAccountNum() {
...
}
// getAccountInfo
public String getAccountInfo() {
...
}
// getTransactionInfo
public String getTransactionInfo(int lastNTransactions) {
...
}
// withdraw
public void withdraw(double amount) {
...
}
// deposit
public void deposit(double amount) {
...
}
} // End of BankAccount Class
class Bank {
//Fields
private BankAccount[] accounts;
private int numOfAccounts;
// other static constants
...
// Constructor
public Bank() {
...
}
public int openNewAccount(String cname, double openingBalance) {
...
}
public void withdrawFrom(int accountNum, double amount) {
...
}
public void depositTo(int accountNum, double amount) {
...
}
public void printAccountInfo(int accountNum) {
...
}
public void printAccountInfo(int accountNum, int numOfTransactions) {
...
}
} |
Request for Question Clarification by
rbnn-ga
on
18 Oct 2002 08:13 PDT
BankAccount has a private double[] transactions variable to keep track
of the last N transactions, but there is no limit on N that I see.
Thus, if we create the array as double[] of, say length M, after M+1
transactions we run out of space in the array.
You are better off in cases where you want to store a possibly
unbounded information using an ArrayList, something like:
import java.util.*;
private ArrayList transactionsSoFar=new ArrayList();
To add a double to the ArrayList, do:
transactionsSoFar.add(new Double(d));
To get back a double from an integer, try:
((Double)transactionsSoFar.get(i)).doubleValue() ;
Is it OK to use an ArrayList instead of a double[] for the list of
past transactions?
In general, do we have to use the private instance variables stated?
Usually a specification does not specify private instance variables; a
private instance variable is fairly invisible to everyone but the
implementor - it is a little like specifying local variable names in a
method - a bit unusual .
|