Question: Design a Java interface named Lockable that includes the following methods: setKey, lock, unlock, and isLocked. The setKey, lock, and unlock methods take an integer

Design a Java interface named Lockable that includes the following methods: setKey, lock, unlock, and isLocked. The setKey, lock, and unlock methods take an integer parameter that represents the key.

  • The setKey method establishes the key. The method is protected when the object is locked.
  • The lock and unlock methods lock and unlock the object, but only if the key passed is correct.
  • The isLocked method returns a boolean that indicates whether the or not the object is locked.

A Lockable object represents an object whose mutator methods are protected: If the object is locked, they result in LockedException being thrown.

Assignment:

  • Complete the implementation of Lockable.
  • Revise the implementation of LockableAccount, making it Lockable. Assume the behavior of the LockableAccount methods, before revision, is correct.
  • Complete LockableAccountTest to test whether the methods are appropriately locked/unlocked when the Lockable methods are used.

import java.text.NumberFormat;

public class LockableAccount {

private final double RATE = 0.025; // interest rate of 2.5%

private long acctNumber;

private double balance;

private String name;

//-----------------------------------------------------------------

// Sets up the account by defining its owner, account number,

// and initial balance.

//-----------------------------------------------------------------

public LockableAccount(String owner, long account, double initial) {

name = owner;

acctNumber = account;

balance = initial;

}

//-----------------------------------------------------------------

// Deposits the specified amount into the account and returns

// the new balance. The balance is not modified if the deposit

// amount is invalid.

//-----------------------------------------------------------------

public double deposit(double amount) {

if (amount > 0) {

balance += amount;

}

return balance;

}

//-----------------------------------------------------------------

// Withdraws the specified amount and fee from this account and

// returns the new balance. The balance is not modified if the

// withdraw amount is invalid or the balance is insufficient.

//-----------------------------------------------------------------

public double withdraw(double amount, double fee) {

if (amount + fee > 0 && amount + fee < balance) {

balance -= amount - fee;

}

return balance;

}

//-----------------------------------------------------------------

// Adds interest to the account and returns the new balance.

//-----------------------------------------------------------------

public double addInterest() {

balance += (balance * RATE);

return balance;

}

//-----------------------------------------------------------------

// Returns the current balance of this account.

//-----------------------------------------------------------------

public double getBalance() {

return balance;

}

//-----------------------------------------------------------------

// Returns a one-line description of this account as a string.

//-----------------------------------------------------------------

public String toString() {

NumberFormat fmt = NumberFormat.getCurrencyInstance();

return (acctNumber + " " + name + " " + fmt.format(balance));

}

}

class LockableAccountTest {

public static void main(String[] args) {

System.out.println("Lockable Account Test");

}

}

public class LockedException extends RuntimeException {

public LockedException() {

super();

}

public LockedException(String errorMessage) {

super(errorMessage);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!