design the following Java programs: BankAccount.java, Payable.java, SavingAccoun
ID: 3744382 • Letter: D
Question
design the following Java programs: BankAccount.java, Payable.java, SavingAccount.java, CheckingAccount.java and GiftCard.java.
Requirements:
Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below.
BankAccount: an abstract class.
SavingAccount: a concrete class that extends BankAccount.
The constructor takes client's firstname, lastname and social security number.
The constructor should generate a number 6-digit number as the user account number.
The initial balance is 0 and the interest rate is fixed at 1.0%.
The withdraw method signature is:
It returns true if the amount is less or equal to the balance. Otherwise, the withdraw attempt fails and outputs a error message.The deposit method signature is:
CheckingAccount: a concrete class that extends BankAccount, also implements Payable interface.
The constructor takes client's firstname, lastname, social security number and a 4-character pin.
Similary, the initial balance is 0 and its interest rate is fixed at 0.1%.
In addition, a 7-digit account number is randomly generated of a new client.
The withdraw method signature is:
It returns true if the amount is less or equal to the balance. Otherwise, the withdraw attempt fail s and outputs a error message.The deposit method signature is:
The makePayment method signature is:
It returns true if the amount is less or equal to the balance. And a message is printed for the payment.
It returns false if the pin check fails.
It returns false if the balance is not sufficient to make the payment.
GiftCard: a concrete class that implements Payable interface.
The constructor takes the amount and a 4-character pin number.
The makePayment method signature is:
It returns true if the balance is sufficient and a confirmation messages is printed.
Otherwise, an error message should show the balance is not sufficient to make the payment.
Make sure your programs compile and submit your programs by following the submission instruction.
Program Template:
Testing:
A test program is given below. Your programs should work with this test program. A sample result is allow provided.
Sample Execution Results:
Explanation / Answer
Solution:
Please find below the classes:
SavingAccount.java
import java.util.Random;
public class SavingAccount extends BankAccount {
private double interest;
private int acctNumber;
private double balance;
public SavingAccount(String firstName, String lastName, int ssn) {
Random randomNumberGenerator = new Random();
this.interest = 0.01;
this.acctNumber = 100000 + randomNumberGenerator.nextInt(900000);
this.balance = 0;
super.firstname = firstName;
super.lastname = lastName;
super.ssn = ssn;
}
public boolean withdraw(double amount) {
if(amount <= this.balance) {
this.balance -= amount;
System.out.println("Successfully withdrew $+"+amount);
return true;
} else {
System.out.println("Withdraw Failed: balance too low...");
return false;
}
}
public void deposit(double amount) {
this.balance += amount;
}
}
CheckingAccount.java
import java.util.Random;
public class CheckingAccount extends BankAccount implements Payable {
private double interest;
private int acctNumber;
private double balance;
private String pin;
public CheckingAccount(String firstName, String lastName, int ssn, String pin) {
Random randomNumberGenerator = new Random();
this.interest = 0.001;
this.acctNumber = 1000000 + randomNumberGenerator.nextInt(9000000);
this.balance = 0;
this.pin = pin;
super.firstname = firstName;
super.lastname = lastName;
super.ssn = ssn;
}
@Override
public boolean makePayment(double amount, String name, String pin) {
if(amount > this.balance) {
System.out.println("Payment Failed: balance too low");
return false;
}else if(pin.equals(this.pin)) {
System.out.println("Payment Failed: wrong pin");
return false;
}else {
System.out.println("Paid "+name+" with $" + amount);
this.balance -= amount;
return true;
}
}
public boolean withdraw(double amount) {
if(amount <= this.balance) {
this.balance -= amount;
System.out.println("Successfully withdrew $+"+amount);
return true;
} else {
System.out.println("Withdraw Failed: balance too low...");
return false;
}
}
public void deposit(double amount) {
this.balance += amount;
}
}
GiftCard.java
public class GiftCard implements Payable {
private String pin;
private double balance;
public GiftCard(double balance, String pin) {
this.pin = pin;
this.balance = balance;
}
@Override
public boolean makePayment(double amount, String name, String pin) {
if(amount > this.balance) {
System.out.println("Payment Failed: balance too low");
return false;
}else if(pin.equals(this.pin)) {
System.out.println("Payment Failed: wrong pin");
return false;
}else {
System.out.println("Paid "+name+" with $" + amount);
this.balance -= amount;
return true;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.