Jave help please. Here is my instruction and my program below. My program works,
ID: 3580270 • Letter: J
Question
Jave help please.
Here is my instruction and my program below. My program works, but I need to use JUnit testing how do I implement that?
Implment a subclass of BankAccount called BasicAccount whose withdraw method will not withdraw more money than is currently in the account.
------BankAccount----
public class BankAccount
{
private double balance;
//Constructs a bank account with zero balance.
public BankAccount()
{
balance = 0;
}
//Makes a deposit into this account.
//@param amount the amount of the deposit
public void deposit(double amount)
{
balance = balance + amount;
}
//Makes a withdrawal from this account, or charges a penalty if sufficient
//funds are not available.
//@param amount the amount of the withdrawal
public void withdraw(double amount)
{
balance = balance - amount;
}
//Carries out the end of month processing that is appropriate for this
//account.
public void monthEnd()
{
}
//Gets the current balance of this bank account.
//@return the current balance
public double getBalance()
{
return balance;
}
}
------BasicAccount ----
public class BasicAccount extends BankAccount
{
public BasicAccount()
{
}
//method withdraw holds with amount declared a double if else loop that
//allows user to withdraw amount until it is not greater than balance else
//statement displays for unavailable funds
@Override
public void withdraw(double amount)
{
double balance = getBalance();
if (amount > balance)
{
super.withdraw(amount);
}
else
{
System.out.println("There are not enough funds available to process this transaction, please withdraw a smaller amount.");
}
}
}
------CheckingAccount----
public class CheckingAccount extends BankAccount
{
private int withdrawals;
//Constructs a checking account with a zero balance.
public CheckingAccount()
{
withdrawals = 0;
}
public void withdraw(double amount)
{
final int FREE_WITHDRAWALS = 3;
final int WITHDRAWAL_FEE = 1;
super.withdraw(amount);
withdrawals++;
if (withdrawals > FREE_WITHDRAWALS)
{
super.withdraw(WITHDRAWAL_FEE);
}
}
public void monthEnd()
{
withdrawals = 0;
}
}
public class CheckingAccount extends BankAccount
{
private int withdrawals;
//Constructs a checking account with a zero balance.
public CheckingAccount()
{
withdrawals = 0;
}
public void withdraw(double amount)
{
final int FREE_WITHDRAWALS = 3;
final int WITHDRAWAL_FEE = 1;
super.withdraw(amount);
withdrawals++;
if (withdrawals > FREE_WITHDRAWALS)
{
super.withdraw(WITHDRAWAL_FEE);
}
}
public void monthEnd()
{
withdrawals = 0;
}
}
public class CheckingAccount extends BankAccount
{
private int withdrawals;
//Constructs a checking account with a zero balance.
public CheckingAccount()
{
withdrawals = 0;
}
public void withdraw(double amount)
{
final int FREE_WITHDRAWALS = 3;
final int WITHDRAWAL_FEE = 1;
super.withdraw(amount);
withdrawals++;
if (withdrawals > FREE_WITHDRAWALS)
{
super.withdraw(WITHDRAWAL_FEE);
}
}
public void monthEnd()
{
withdrawals = 0;
}
}
----MainAccount----
public class MainAccount
{
//@param args the command line arguments
public static void main(String[] args)
{
// Create accounts
final int ACCOUNTS_SIZE = 10;
BankAccount[] accounts = new BankAccount[ACCOUNTS_SIZE];
for (int i = 0; i < accounts.length / 2; i++)
{
accounts[i] = new CheckingAccount();
}
for (int i = accounts.length / 2; i < accounts.length; i++)
{
SavingsAccount account = new SavingsAccount();
account.setInterestRate(0.75);
accounts[i] = account;
}
// adds a basicAccount to keep a running total
for (int i = accounts.length / 2; i < accounts.length; i++)
{
BasicAccount account = new BasicAccount();
accounts[i] = account;
}
// Execute commands
Scanner in = new Scanner(System.in);
boolean done = false;
while (!done)
{
System.out.print("D)eposit W)ithdraw M)onth end Q)uit: ");
String input = in.next();
if (input.equals("D") || input.equals("W")) // Deposit or withdrawal
{
System.out.print("Enter account number and amount: ");
int num = in.nextInt();
double amount = in.nextDouble();
if (input.equals("D"))
{
accounts[num].deposit(amount);
}
else
{
accounts[num].withdraw(amount);
}
System.out.println("Balance: " + accounts[num].getBalance());
}
else if (input.equals("M")) // Month end processing
{
for (int n = 0; n < accounts.length; n++)
{
accounts[n].monthEnd();
System.out.println(n + " " + accounts[n].getBalance());
}
}
else if (input.equals("Q"))
{
done = true;
}
}
}
}
----SavingsAccount---
public class SavingsAccount extends BankAccount
{
private double interestRate;
private double minBalance;
//Constructs a savings account with a zero balance.
public SavingsAccount()
{
interestRate = 0;
minBalance = 0;
}
//Sets the interest rate for this account.
//@param rate the monthly interest rate in percent
public void setInterestRate(double rate)
{
interestRate = rate;
}
public void withdraw(double amount)
{
super.withdraw(amount);
double balance = getBalance();
if (balance < minBalance)
{
minBalance = balance;
}
}
public void monthEnd()
{
double interest = minBalance * interestRate / 100;
deposit(interest);
minBalance = getBalance();
}
}
Explanation / Answer
I have Compiled the program at my end and have checked the outputs too .Please check the same below :-
Case 1:
D)eposit W)ithdraw M)onth end Q)uit: D
Enter account number and amount: 1
5000
Balance: 5000.0
D)eposit W)ithdraw M)onth end Q)uit: W
Enter account number and amount: 1
3000
Balance: 2000.0
D)eposit W)ithdraw M)onth end Q)uit: M
0 0.0
1 2000.0
2 0.0
3 0.0
4 0.0
5 0.0
6 0.0
7 0.0
8 0.0
9 0.0
D)eposit W)ithdraw M)onth end Q)uit: Q
Case 2:-
D)eposit W)ithdraw M)onth end Q)uit: D
Enter account number and amount: 1
40000
Balance: 40000.0
D)eposit W)ithdraw M)onth end Q)uit: D
Enter account number and amount: 2
50000
Balance: 50000.0
D)eposit W)ithdraw M)onth end Q)uit: 1
D)eposit W)ithdraw M)onth end Q)uit: W
Enter account number and amount: 2
40000
Balance: 10000.0
D)eposit W)ithdraw M)onth end Q)uit: Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.