One double data field named balance with default values 0.0 to denote the balanc
ID: 675265 • Letter: O
Question
One double data field named balance with default values 0.0 to denote the balance of the account. . A no-arg constructor that creates a default bank account. A constructor that creates a bank account with the specified balance throw an IllegalArgumentException when constructing an account with a negative balance The accessor method for the data field A method named deposit (double amount) that deposits money into the bank account. throw an IllegalArgumentException when deposit a negative amount into an account A method named withdraw (double amount) that withdraws the money from the bank account. throw an IllegalArgumentException when withdrawing a negative amount from an account Vdefine the exception class InsufficientFundsException throw an InsufficientFundsException when the balance is less than the withdrawal amount. A method transfer (double amount, BankAccount other) that transfer money from the bank account to another account. A method named toString () that returns a string description for the account. Design two classes ChackingAccount and SavingAccount which are derived from the class BankAccount CheckingAccount will charge transaction fees. Each month there are 5 free transactions. S3 will be deducted from the checking account for every extra transaction. (Hint: You need to add a method to deduct the fees from the checking account if there are more than 5 tractions in a month. You need to modify the deposit and withdrawal method as well, and you need to add an instance field to remember the number of transactions. You may need to define two constants, one for number of allowed free transactions and the other for fee per extra transaction.) .Explanation / Answer
import java.text.*; // to use Decimal Format
public class AccountDriver
{
public static void main(String[] args)
{
double put_in = 500;
double take_out = 1000;
DecimalFormat myFormat;
String money;
String money_in;
String money_out;
boolean completed;
myFormat = new DecimalFormat("#.00");
CheckingAccount myCheckingAccount =
new CheckingAccount ("Benjamin Franklin", 1000);
System.out.println ("Account Number "
+ myCheckingAccount.getAccountNumber() + "-10"+
" belonging to " + myCheckingAccount.getOwner());
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("Initial balance = $" + money);
myCheckingAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(myCheckingAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = myCheckingAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(myCheckingAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
SavingsAccount yourAccount =
new SavingsAccount ("William Shakespeare", 400);
System.out.println ("Account Number "
+ yourAccount.getAccountNumber() +"-0"+
" belonging to " + yourAccount.getOwner());
money = myFormat.format(yourAccount.getBalance());
System.out.println ("Initial balance = $" + money);
yourAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
completed = yourAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(yourAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
yourAccount.postInterest();
money = myFormat.format(yourAccount.getBalance());
System.out.println ("After monthly interest has been posted,"
+ "balance = $" + money);
System.out.println();
SavingsAccount secondAccount =
new SavingsAccount (yourAccount,5);
System.out.println ("Account Number "
+ secondAccount.getAccountNumber()+"-1"+
" belonging to " + secondAccount.getOwner());
money = myFormat.format(secondAccount.getBalance());
System.out.println ("Initial balance = $" + money);
secondAccount.deposit (put_in);
money_in = myFormat.format(put_in);
money = myFormat.format(secondAccount.getBalance());
System.out.println ("After deposit of $" + money_in
+ ", balance = $" + money);
secondAccount.withdraw(take_out);
money_out = myFormat.format(take_out);
money = myFormat.format(secondAccount.getBalance());
if (completed)
{
System.out.println ("After withdrawal of $" + money_out
+ ", balance = $" + money);
}
else
{
System.out.println ("Insuffient funds to withdraw $"
+ money_out + ", balance = $" + money);
}
System.out.println();
//to test to make sure new accounts are numbered correctly
CheckingAccount yourCheckingAccount =
new CheckingAccount ("Issac Newton", 5000);
System.out.println ("Account Number "
+ yourCheckingAccount.getAccountNumber()
+ " belonging to "
+ yourCheckingAccount.getOwner());
}
}
public abstract class BankAccount
{
protected static int numberOfAccounts = 100001;
private double balance;
private String owner;
private String accountNumber;
public BankAccount()
{
balance = 0;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
public BankAccount(String name, double amount)
{
owner = name;
balance = amount;
accountNumber = numberOfAccounts + "";
numberOfAccounts++;
}
//copy constructor
public BankAccount(BankAccount oldAccount, double amount)
{
owner = oldAccount.owner;
balance = amount;
accountNumber = oldAccount.accountNumber;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public boolean withdraw(double amount)
{
boolean completed = true;
if (amount <= balance)
{
balance = balance - amount;
}
else
{
completed = false;
}
return completed;
}
//accessor method to balance
public double getBalance()
{
return balance;
}
public String getOwner()
{
return owner;
}
public String getAccountNumber()
{
return accountNumber;
}
//mutator method to change the balance
public void setBalance(double newBalance)
{
balance = newBalance;
}
//mutator method to change the account number
public void setAccountNumber(String newAccountNumber)
{
accountNumber = newAccountNumber;
}
}
public class SavingsAccount extends BankAccount
{
public SavingsAccount(String string, double rate)
{
interestRate = rate;
}
public SavingsAccount(SavingsAccount yourAccount, int rate) {
}
public void addInterest()
{
double interest = getBalance() * interestRate / 100;
deposit(interest);
}
private double interestRate;
public void deposit(double amount) {}
public boolean withdraw(double amount) {
return false;}
public void deductFees() {}
private int transactionCount;
public void postInterest() {
double balance = getBalance();
balance += (balance * interestRate);
deposit(balance);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.