Starting from:
$30

$24

Homework #1 Solution




Considering a Java class/interface design hierarchy shown in the following diagram,




![hw1](imgs/hw1.jpg)




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 random 6-digit number as the user account number.

* The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).

* The withdraw method signature is:

`public boolean withdraw(double amount).`




It returns true if the amount is less or equal to the balance. Otherwise, the withdraw attempt fails and outputs an error message.

* The deposit method signature is:

`public void deposit(double amount).`




* 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 (in String type).

* Similarily, the initial balance is 0 and its interest rate is fixed at 0.1% (0.001).

* In addition, a 7-digit account number is randomly generated for a new client.

* The withdraw method signature is:

`public boolean withdraw(double amount)`.

* It deducts the corresponding amount of money on the account balance and returns true if the amount is less or equal to the balance. Otherwise, the withdraw attempt fail s and outputs an error message.

* The deposit method signature is:

`public void deposit(double amount)`




* The makePayment method signature is:

`public boolean makePayment(double amount, String name, String pin)`




* 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:

`public boolean makePayment(double amount, String name, String pin).`

* It deducts the corresponding balance amount and 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.

* In each concrete class, you are required to override toString() method, which is orginally provided in Object class.




Program Template




```java

public abstract class BankAccount {

protected String firstname;

protected String lastname;

protected int ssn;




// implement your code here

}




public interface Payable {

public boolean makePayment(double amount, String name, String pin);

}




public class SavingAccount extends BankAccount {

private double interest;

private int acctNumber;

private double balance;




// implement your code here

}




public class CheckingAccount extends BankAccount implements Payable {

private double interest;

private int acctNumber;

private double balance;

private String pin;




// implement your code here

}




public class GiftCard implements Payable {

private String pin;

private double balance;




// implement your code here

}

```




Testing




A test program is given below. Your programs should work with this test program. A sample result is allow provided.




```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]);

}

}

```




Sample Execution Results:




```text

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

Saving's Account Number: 395605

Balance: 500.0

Interest: 0.0

Firstname: Shane

Lastname: Bieber

Checking Account Number: 1797409

Balance: 400.0

Interest: 0.1

Pin: 1234

Firstname: Adam

Lastname: Cimber

Saving's Account Number: 684689

Balance: 700.0

Interest: 0.0

Firstname: Corey

Lastname: Kluber

Checking Account Number: 2877463

Balance: 0.0

Interest: 0.1

Pin: 4321

Gift Card Pin: gift

Gift Card Balance: 1000.0

```

More products