design the following Java programs: BankAccount.java, Payable.java, SavingAccoun
ID: 3745496 • 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
Please find below attached code and sample output get back to me if you need any further help
Code:
public class Test {
public static void main(String[] args) {
BankAccount[] accts = { new SavingAccount("Cody", "Allen", 3428765),
new CheckingAccount("Shane", "Bieber", 3284842, "1234"), new SavingAccount("Adam", "Cimber", 8097423) };
Payable[] items = { new CheckingAccount("Corey", "Kluber", 79872487, "4321"),
new GiftCard(1000, "gift")
};
accts[0].deposit(500);
accts[1].deposit(1200);
accts[2].deposit(1500);
((CheckingAccount)items[0]).deposit(2000);
for (int i = 0; i<accts.length; i++)
{
accts[i].withdraw(800);
}
items[0].makePayment(2000, "Alonso", "4321");
items[1].makePayment(2000, "Barnes", "gifts");
((CheckingAccount)accts[1]).makePayment(2000, "Diaz", "1234");
for (int i = 0; i<accts.length; i++)
System.out.println(accts[i]);
for (int i = 0; i<items.length; i++)
System.out.println(items[i]);
}
}
public abstract class BankAccount {
protected String firstname;
protected String lastname;
protected int ssn;
// implement your code
public BankAccount(String firstname, String lastname, int ssn) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.ssn = ssn;
}
abstract void deposit(double i);
abstract boolean withdraw(double i);
}
public interface Payable {
public boolean makePayment(double amount, String name, String pin);
}
import java.util.Random;
public class SavingAccount extends BankAccount {
private double interest;
private int acctNumber;
private double balance;
// implement your code
public SavingAccount(String firstname, String lastname, int ssn) {
super(firstname, lastname, ssn);
this.interest = 1;
this.balance = 0;
Random ran = new Random();
this.acctNumber = 100000 + ran.nextInt(900000);
this.ssn = ssn;
}
@Override
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;
}
}
@Override
public void deposit(double amount){
this.balance += amount;
}
@Override
public String toString() {
return "Firstname: " + firstname + " Lastname: " + lastname + " SavingAccount Account Number: " + acctNumber + " Balance: " + balance
+" Interest: " + interest;
}
}
import java.util.Random;
public class CheckingAccount extends BankAccount implements Payable {
private double interest;
private int acctNumber;
private double balance;
private String pin;
// implement your code
public CheckingAccount(String firstname, String lastname, int ssn, String pin) {
super(firstname, lastname, ssn);
this.interest = 0.1;
this.balance = 0;
Random ran = new Random();
this.acctNumber = 1000000 + ran.nextInt(9000000);
this.ssn = ssn;
this.pin = pin;
}
@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(!this.pin.equals(pin))
{
System.out.println("Payment Failed: wrong pin");
return false;
}
else
{
System.out.println("Paid "+name+" with $"+amount);
this.balance-=amount;
return true;
}
}
}
@Override
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;
}
}
@Override
public void deposit(double amount){
this.balance += amount;
}
@Override
public String toString() {
return "Firstname: " + firstname + " Lastname: " + lastname + " CheckingAccount Account Number: " + acctNumber + " Balance: " + balance
+" Interest: " + interest + " Pin: " + pin ;
}
}
public class GiftCard implements Payable {
private String pin;
private double balance;
// implement your code
public GiftCard(double balance, String pin) {
super();
this.pin = pin;
this.balance = balance;
}
@Override
public boolean makePayment(double amount, String name, String pin) {
if(!this.pin.equals(pin))
{
System.out.println("Payment Failed: wrong pin");
return false;
}
if( amount > this.balance)
{
System.out.println("Payment Failed: balance too low");
return false;
}
else
{
{
System.out.println("Paid "+name+" with $"+amount);
this.balance-=amount;
return true;
}
}
}
@Override
public String toString() {
return "Gift Card Pin: " + pin + " Gift Card Balance: " + balance;
}
}
Sample Output:
Withdraw Failed: balance too low...
Successfully withdrew $800.0
Successfully withdrew $800.0
Paid Alonso with $2000.0
Payment Failed: wrong pin
Payment Failed: balance too low
Firstname: Cody Lastname: Allen SavingAccount Account Number: 793461 Balance: 500.0 Interest: 1.0
Firstname: Shane Lastname: Bieber CheckingAccount Account Number: 8882969 Balance: 400.0 Interest: 0.1 Pin: 1234
Firstname: Adam Lastname: Cimber SavingAccount Account Number: 908236 Balance: 700.0 Interest: 1.0
Firstname: Corey Lastname: Kluber CheckingAccount Account Number: 9288476 Balance: 0.0 Interest: 0.1 Pin: 4321
Gift Card Pin: gift Gift Card Balance: 1000.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.