Requirements: Your Java class names must follow the names specified above. Note
ID: 3745040 • Letter: R
Question
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
here is your files: --------------->>>>>>>>>>>
Payable.java : ---------
public interface Payable {
public boolean makePayment(double amount, String name, String pin);
}
BankAccount.java : ----------------
public abstract class BankAccount {
protected String firstname;
protected String lastname;
protected int ssn;
// implement your code
public BankAccount(String fn,String ln,int ssn){
firstname = fn;
lastname = ln;
this.ssn = ssn;
}
public abstract boolean withdraw(double amount);
public abstract void deposit(double amount);
public String toString(){
return " Firstname: "+firstname+" Lastname: "+lastname;
}
}
SavingAccount.java : -------------
import java.util.Random;
public class SavingAccount extends BankAccount {
private double interest;
private int acctNumber;
private double balance;
// implement your code
public SavingAccount(String fn,String ln,int ssn){
super(fn,ln,ssn);
Random rn = new Random();
acctNumber = rn.nextInt()+100000;
balance = 0.0;
interest = 1.0;
}
public boolean withdraw(double amount){
if(amount < balance){
System.out.println("Withdraw Failed: balance too low... ");
return false;
}
balance = balance - amount;
System.out.println("Successfully withdrew $"+amount);
return true;
}
public void deposit(double amount){
balance = balance + amount;
}
public String toString(){
return super.toString()+" Account Number: "+acctNumber+" Balance: $"+balance+" Interset: "+interest;
}
}
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;
// implement your code
public CheckingAccount(String fn,String ln,int ssn,String p){
super(fn,ln,ssn);
pin = p;
balance = 0.0;
interest = 0.1;
Random rn = new Random();
acctNumber = rn.nextInt()+1000000;
}
public boolean withdraw(double amount){
if(amount < balance){
System.out.println("Withdraw Failed: balance too low... ");
return false;
}
balance = balance - amount;
System.out.println("Successfully withdrew $"+amount);
return true;
}
public void deposit(double amount){
balance = balance + amount;
}
public boolean makePayment(double amount, String name, String pin){
if(!pin.equals(this.pin)){
System.out.println("Payment Failed: wrong pin ");
return false;
}
if(amount < balance){
System.out.println("Payment Failed: balance too low... ");
return false;
}
balance = balance - amount;
System.out.println("paid "+name+" with $"+amount);
return true;
}
public String toString(){
return super.toString()+" Account Number: "+acctNumber+" Balance: $"+balance+" Interest: "+interest+" Pin: "+pin;
}
}
GiftCard.java :-----------------------
public class GiftCard implements Payable {
private String pin;
private double balance;
// implement your code
public GiftCard(double bal,String p){
balance = bal;
pin = p;
}
public boolean makePayment(double amount, String name, String pin){
if(!pin.equals(this.pin)){
System.out.println("Payment Failed: wrong pin ");
return false;
}
if(amount < balance){
System.out.println("Payment Failed: balance too low... ");
return false;
}
balance = balance - amount;
System.out.println("paid "+name+" with $"+amount);
return true;
}
public String toString(){
return " Gift Card Pin: "+pin+" Gift Card Balance: "+balance;
}
}
Test.java:-----------------
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]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.