Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

How is reliability determined?

Answered: 1 week ago

Question

Be familiar with the different perspectives of service quality.

Answered: 1 week ago

Question

Describe key customer feedback collection tools.

Answered: 1 week ago

Question

Know what customers expect from the firm when they complain.

Answered: 1 week ago