1 In Chapter 3 on pages 96 and 97 there is a Bank Account pr
1. In Chapter 3, on pages 96 and 97 there is a Bank Account program. The original tester is on page 103.
a. Modify the BankAccount program as follows:
i. Add a name to each BankAccount
ii. Add an account number to your BankAccount. This should be similar to the process described on page 391. Remember to increase the lastAssignedNumber.
iii. Add a method to get the account number.
iv. Add a method to get the name associated in the account.
v. Add a method to add or change the name on the account. This should be a set method.
vi. Create a method that adds an OverDraftFee which is a static public variable, page 392-393. The overdraft fee should be $20.
vii. Change your withdraw method to check for an overdraft and if overdraft call the method in iv.
b. Modify the BankAccount tester as follows:
i. Create an ArrayList to hold the BankAccounts.
ii. Create three bankAccounts by asking the user to input the name and balance for each holder.
iii. Add the three BankAccounts to the ArrayList.
iv. Using the first account, deposit $100.
v. From that same account, create a deposit that would cause the account to overdraft.
vi. Print the name, account, and balance for all three accounts.
Here is what I have so far for the main class.
/**
* A bank account has a balance that can be changed by deposits and withdrawls
*
* @author (Taylor Baird)
* @version (October 9th 2016)
*/
public class BankAccount
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
Scanner input = new Scanner(System.in);
final static double OVERDRAFT_LIMIT = 20.00;
/**
* Constructs a bank account with zero balance and zero bank account number.
*/
public BankAccount()
{
balance = 0;
acctNum = 0;
}
/**
* Constructs a bank account with a given balance, name, and account number.
*
*/
public BankAccount(double initialBalance, String owner, long number)
{
balance = initialBalance;
name = owner;
acctNum = number;
acctNum++;
}
/**
* Constructs a bank account with information given by user.
*/
public BankAccount(double balance, String name)
{
this.balance = balance;
this.name = name;
acctNum = ???
}
/** Deposits money into the bank account.
* @param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
* Withdraws money from the bank account
* @param amount the amount to withdraw
*/
public double withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
}
else
{
System.out.println(\"Insufficient funds\");
}
return balance;
}
/**
* Gets the current balance of the bank account.
* @return the current balance
*/
public double getBalance()
{
return balance;
}
/**
* Gets the current account number.
* @return the account number
*/
public long getAccountNumber()
{
return acctNum;
}
/**
* Sets/Changes the name on the account
* @param owner the name of the account holder
*/
public void setName(String owner)
{
name = owner;
}
/**
* Gets the name of the account holder.
* @return the owners name
*/
public String getName()
{
return name;
}
}
Here is what I have so far for the tester.
public class BankAccountTester
{
/**
* Creates three BankAccount object and teste the methods of BankAccount
*/
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
BankAccount sallysChecking = new BankAccount (5000.00);
BankAccount marysChecking = new BankAccount (200.00);
harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
marysChecking.deposit(4000);
System.out.println(\"Harrys result before interest \" + harrysChecking.getBalance());
System.out.println(\"Sallys result before interest \" + sallysChecking.getBalance());
System.out.println(\"Marys result before interest \" + marysChecking.getBalance());
}
}
This is the original tester from the old bank account. I have not made any changes because I am unsure where to start.
Solution
Hi,
PFA the program below. I have added comments where ever neccessary.
Keep the following things in mind:
1. All the constructors would automatically assign an account number using a static count of accounts. There\'s no point in assigning account numbers manually/through application.
2. Overdraft fee concept that I understood is, that when a user tries to withdraw more amount than actually present in his account, then bank charges an overdraft fees to cover the transactional costs.
BankAccount.java:
import java.util.Scanner;
/**
* @author dell
*
*/
/**
* @author dell
*
*/
public class BankAccount
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
Scanner input = new Scanner(System.in);
final static double OVERDRAFT_LIMIT = 20.00;
/**
* Constructs a bank account with zero balance and no account name.
*/
public BankAccount()
{
numAccounts++;
balance = 0;
acctNum = numAccounts;
}
/**
* Constructs a bank account with a given balance, name.
*
*/
public BankAccount(double initialBalance, String owner)
{
numAccounts++;
balance = initialBalance;
name = owner;
acctNum = numAccounts;
}
/** Deposits money into the bank account.
* @param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
System.out.println(amount +\" deposited in Account\" + getAccountNumber() + \". New Balance is \" + balance);
}
/**
* Withdraws money from the bank account
* @param amount the amount to withdraw
*/
public double withdraw(double amount)
{
if (balance >= amount)
{
balance -= amount;
System.out.println(amount +\" withdrawn from Account\" + getAccountNumber() + \". New Balance is \" + balance);
}
else
{
System.out.println(\"Insufficient funds in Account\"+ getAccountNumber());
deductOverdraftFee();
//If balance to be withdrawn exceeds the current balance, an overdraft fee would be charged.
}
return balance;
}
/**
* Deducts the overdraft charges from bank account
*/
public void deductOverdraftFee(){
balance-=OVERDRAFT_LIMIT;
System.out.println(\"Overdraft Charged to Account \"+ getAccountNumber());
}
/**
* Gets the current balance of the bank account.
* @return the current balance
*/
public double getBalance()
{
return balance;
}
/**
* Gets the current account number.
* @return the account number
*/
public long getAccountNumber()
{
return acctNum;
}
/**
* Sets/Changes the name on the account
* @param owner the name of the account holder
*/
public void setName(String owner)
{
name = owner;
}
/**
* Gets the name of the account holder.
* @return the owners name
*/
public String getName()
{
return name;
}
}
BankAccountTester.java:
import java.util.ArrayList;
import java.util.Scanner;
public class BankAccountTester
{
/**
* Creates three BankAccount object and teste the methods of BankAccount
*/
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String name;
double balance;
BankAccount ba;
ArrayList<BankAccount> bankAccounts=new ArrayList<BankAccount>();
//ArrayList to store bank accounts
for(int i=0;i<3;i++){
System.out.println(\"Please enter the account holder\'s name: \");
name=sc.nextLine();
System.out.println(\"Please enter the initial account balance: \");
balance=Double.parseDouble(sc.nextLine());
ba = new BankAccount(balance, name);
bankAccounts.add(ba);
//adding a bank account to array list
}
BankAccount b1=bankAccounts.get(0);
b1.deposit(100.00);
b1.withdraw(400);
for(int i=0;i<3;i++){
System.out.print(\"Name: \" + bankAccounts.get(i).getName());
System.out.print(\"\\t Balance: \" + bankAccounts.get(i).getBalance());
System.out.println(\"\\t Account Number: \" + bankAccounts.get(i).getAccountNumber());
}
}
}
Sample Run:
Please enter the account holder\'s name:
A
Please enter the initial account balance:
200
Please enter the account holder\'s name:
B
Please enter the initial account balance:
100
Please enter the account holder\'s name:
C
Please enter the initial account balance:
600
100.0 deposited in Account1. New Balance is 300.0
Insufficient funds in Account1
Overdraft Charged to Account 1
Name: A Balance: 280.0 Account Number: 1
Name: B Balance: 100.0 Account Number: 2
Name: C Balance: 600.0 Account Number: 3





