Hi im stucked with my assignment. Its simple bank with bank accounts. I have to implement BankTester where i have to connect those two classes with do-while loop.
Using do-while loop ask a user to enter an accountNumber and a balance and passes them to the method addAccount to add a new BankAccount to the bank. After that asks a user to make one withdrawal and one deposit for the just created account.
This is what i did so far...
Bank.java
import java.util.ArrayList;
/**
This bank contains a collection of bank accounts.
*/
public class Bank
{
/**
Constructs a bank with no bank accounts.
*/
public Bank()
{
accounts = new ArrayList<BankAccount>();
}
public void addAccount(int aNumber, double aBalance, BankAccount a)
{
accounts.add(a);
}
/**
Finds a bank account with a given number.
@param aNumber the number to find
@return the account with the given number, or null if there
is no such account
*/
public BankAccount find(int aNumber)
{
for (BankAccount a : accounts)
{
if (a.getAccountNumber() == aNumber) // Found a match
return a;
}
return null; // No match in the entire array list
}
/**
Gets the bank account with the largest balance.
@return the account with the largest balance, or null if the
bank has no accounts
*/
public BankAccount getMaximum()
{
if (accounts.size() == 0) return null;
BankAccount largestYet = accounts.get(0);
for (int i = 1; i < accounts.size(); i++)
{
BankAccount a = accounts.get(i);
if (a.getBalance() > largestYet.getBalance())
largestYet = a;
}
return largestYet;
}
/**
Gets the bank account with the smallest balance.
@return the account with the largest balance, or null if the
bank has no accounts
*/
public BankAccount getMinimum()
{
if (accounts.size() == 0) return null;
BankAccount smallestYet = accounts.get(0);
for (int i = 1; i > accounts.size(); i++)
{
BankAccount b = accounts.get(i);
if (b.getBalance() < smallestYet.getBalance())
smallestYet = b;
}
return smallestYet;
}
private ArrayList<BankAccount> accounts;
}
BankAccount.java
import java.util.ArrayList;
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
/**
Constructs a bank account with a zero balance
@param anAccountNumber the account number for this account
*/
public BankAccount(int anAccountNumber)
{
accountNumber = anAccountNumber;
balance = 0;
transactions = new ArrayList<Double>();
}
public void addTransaction(Double a)
{
transactions.add(a);
}
/**
Constructs a bank account with a given balance
@param Number the account number for this account
@param initialBalance the initial balance
*/
public BankAccount(int anAccountNumber, double initBalance)
{
accountNumber = anAccountNumber;
balance = initBalance;
transactions.add(balance);
}
/**
Gets the account number of this bank account.
@return the account number
*/
public int getAccountNumber()
{
return accountNumber;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
transactions.add(amount);
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
transactions.add(-amount);
}
public double getAverageTransaction()
{
double total = 0;
for (Double a : transactions)
{
total = total + getBalance();
}
return total / transactions.size();
}
private int accountNumber;
private double balance;
private ArrayList<Double> transactions;
}
Heres tester
import javax.swing.JOptionPane;
public class BankTester {
public static void main(String[] args)
{
String accnumber = JOptionPane.showInputDialog("Please enter your account number:");
int accountNumber = Integer.parseInt(accnumber);
String abalance = JOptionPane.showInputDialog("Please enter your account balance:");
double balance = Integer.parseInt(abalance);
Bank firstBankOfJava = new Bank();
firstBankOfJava.addAccount(new BankAccount(accnumber, abalance));
}
}
Thanks for any help or tips!