package code; public class SavingsAccount { . * Define this class to that repres
ID: 3603870 • Letter: P
Question
package code;
public class SavingsAccount {
.
* Define this class to that represents a simple Savings Account.
* Pay attention to the names, parameters and return types of the methods
* described.
*
* The account object must have two private instance variables, one of type
* double representing the current balance, and the other of type boolean
* indicating whether the account has authorization to perform a withdrawal.
*
* The balance of the account must never go below zero. A withdrawal must
* not be done unless there is prior authorization. If authorization has
* been obtained a single withdrawal can be performed. Performing an
* authorized withdrawal rescinds the withdrawal, preventing any additional
* withdrawals from taking place until authorization for another withdrawal
* is obtained.
*
* The constructor of the class must initialize the balance to zero, and the
* withdrawal authorization to false.
*
* The 'deposit' method must verify that the amount to be deposited is
* non-negative before proceeding with the deposit. The 'deposit' method
* must have a void return type.
*
* The 'withdraw' method must verify that the amount to be withdrawn is
* non-negative, that the amount is no greater than the current balance, and
* that the withdrawal is authorized before proceeding with the withdrawal.
* The 'withdraw' method must have a void return type.
*
* The 'balance' method must return the current balance. It must have a
* double return type.
*
* The 'authorize' method must set the authorization to true. It must have a
* void return type.
*
* The 'authorized' method must return the authorization. It must have a
* boolean return type.
*
*/
}
Explanation / Answer
SavingsAccount.java
public class SavingsAccount {
private double currentBalance;
private boolean isAuthorized;
public SavingsAccount() {
currentBalance = 0;
isAuthorized = false;
}
public void deposit(double amount) {
if (amount > 0)
currentBalance += amount;
else
System.out.println("deposit: " + "cannot deposit negative amount.");
}
public void withdraw(double amount) {
if(isAuthorized) {
if (amount > 0)
if (currentBalance - amount >= 0)
currentBalance -= amount;
else
System.out.println("withdraw: " + "cannot withdraw more than your balance.");
else
System.out.println("withdraw: " + "cannot withdraw negative amount.");
} else {
System.out.println("witdra: you do not have authorization");
}
}
public double balance() {
return currentBalance;
}
public void authorize() {
isAuthorized = false;
}
public boolean authorized() {
return isAuthorized;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.