Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.ArrayList; / * * * Class Safe - simulates the behavior of a real safe. * * This class interacts with the SafeState class

import java.util.ArrayList;
/**
* Class Safe - simulates the behavior of a real safe.
*
* This class interacts with the SafeState class to model the functionality of a safe,
* allowing users to perform actions such as opening and locking the safe.
*
* @author Simon Pyne
* @version 8.12.23
*/
public class Safe {
// Private fields
private SafeState state; // Controls the state of the safe
private int maxItems; // Maximum number of items that can be stored in the safe
// Public field for testing purposes
public ArrayList contents; // Keeps a record of items in the safe
/**
* Constructor for objects of class Safe.
* Initializes the SafeState and sets the maximum number of items to a default value.
*/
public Safe(){
this(10); // Call the other constructor with a default value for maxItems
}
/**
* Constructor for objects of class Safe with a specified maximum number of items.
*
* @param aMaxItems The maximum number of items the safe can hold.
*/
public Safe(int aMaxItems){
state = new SafeState();
maxItems = aMaxItems;
contents = new ArrayList<>();
}
/**
* Add an item to the safe contents if the safe is not full.
*
* @param item The item to be added to the safe.
*/
public void addToContents(String item){
if (contents.size()< maxItems){
contents.add(item);
}
// If the safe is full, do nothing
}
/**
* Remove an item from the safe contents if it exists.
*
* @param item The item to be removed from the safe.
*/
public void removeFromContents(String item){
if (contents.contains(item)){
contents.remove(item);
System.out.println("Removed item");
} else {
System.out.println("Item "+ item +" not in safe");
}
}
/**
* Display all items currently stored in the safe contents.
*/
public void display(){
for (String item : contents){
System.out.println(item);
}
}
/**
* Remove all items from the safe, one at a time.
* Prints "Removed item" after each item is removed.
*/
public void empty(){
while (!contents.isEmpty()){
String removedItem = contents.remove(0);
System.out.println("Removed item: "+ removedItem);
}
}
/**
* Attempt to open the safe using a provided code.
*
* @param code The code to attempt to open the safe.
* @return True if the safe is now open, false otherwise.
*/
public boolean open(String code){
return state.open(code);
}
/**
* Attempt to lock the safe using a provided code.
*
* @param code The code to attempt to lock the safe.
* @return True if the safe is now locked, false otherwise.
*/
public boolean lock(String code){
return state.lock(code);
}
}

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions