Question: Project called AccountTest using BankAccount.java, NegativeStartingBalance.java, AccountTest.java I.At AccountTest class create 3 customers accounts ; their starting balances are 200, 0, -200 II.Create loop to
Project called AccountTest using BankAccount.java, NegativeStartingBalance.java, AccountTest.java
I.At AccountTest class create 3 customers accounts ; their starting balances are 200, 0, -200
II.Create loop to ask the customer to choose
1- Balance, 2- Deposit, 3- Withdraw, 0- Exit (the loop ends when the customer exit)
III. If the customer asked for
1.Balance:
print the balance
2.Deposit:
A.Ask how much to deposit, and get the amount
B.Check
i.If the amount is negative, tell the customer that the amount is negative
ii.If the amount is positive, add the amount to the balance
3. Withdraw:
A.Ask how much to withdraw , and get the amount
B.Check
i.If the amount is negative, tell the customer that the amount is negative
ii.If the amount is positive, check
a.If balance >= the withdraw amount, subtract the amount from the balance
b.If balance < the withdraw amount, tell the customer cant withdraw
IV.Print the 3 customers balances
NegativeStartingBalance.java
/**
NegativeStartingBalance exceptions are thrown by the
BankAccount class when a negative starting balance is
passed to the constructor.
*/
public class NegativeStartingBalance
extends Exception
{
/**
This constructor uses a generic
error message.
*/
public NegativeStartingBalance()
{
super("Error: Negative starting balance");
}
/**
This constructor specifies the bad starting
balance in the error message.
@param The bad starting balance.
*/
public NegativeStartingBalance(double amount)
{
super("Error: Negative starting balance: " +
amount);
}
}
BankAccount.java
/**
The BankAccount class simulates a bank account.
*/
public class BankAccount
{
private double balance; // Account balance
/**
This constructor sets the starting balance
at 0.0.
*/
public BankAccount()
{
balance = 0.0;
}
/**
This constructor sets the starting balance
to the value passed as an argument.
@param startBalance The starting balance.
@exception NegativeStartingBalance When
startBalance is negative.
*/
public BankAccount(double startBalance)
throws NegativeStartingBalance
{
if (startBalance < 0)
throw new NegativeStartingBalance(startBalance);
balance = startBalance;
}
/**
This constructor sets the starting balance
to the value in the String argument.
@param str The starting balance, as a String.
*/
public BankAccount(String str)
{
balance = Double.parseDouble(str);
}
/**
The deposit method makes a deposit into
the account.
@param amount The amount to add to the
balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
The deposit method makes a deposit into
the account.
@param str The amount to add to the
balance field, as a String.
*/
public void deposit(String str)
{
balance += Double.parseDouble(str);
}
/**
The withdraw method withdraws an amount
from the account.
@param amount The amount to subtract from
the balance field.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
The withdraw method withdraws an amount
from the account.
@param str The amount to subtract from
the balance field, as a String.
*/
public void withdraw(String str)
{
balance -= Double.parseDouble(str);
}
/**
The setBalance method sets the account balance.
@param b The value to store in the balance field.
*/
public void setBalance(double b)
{
balance = b;
}
/**
The setBalance method sets the account balance.
@param str The value, as a String, to store in
the balance field.
*/
public void setBalance(String str)
{
balance = Double.parseDouble(str);
}
/**
The getBalance method returns the
account balance.
@return The value in the balance field.
*/
public double getBalance()
{
return balance;
}
}
AccountTest.java
/**
This program demonstrates how the BankAccount
class constructor throws custom exceptions.
*/
public class AccountTest
{
public static void main(String [] args)
{
// Force a NegativeStartingBalance exception.
try
{
BankAccount account =
new BankAccount(-100.0);
}
catch(NegativeStartingBalance e)
{
System.out.println(e.getMessage());
}
}
}
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
