Files
2025-07-21 16:03:34 +02:00

144 lines
4.4 KiB
Java

public class Fridge { // TODO: change
/**
* The current items in the fridge.
*/
private FridgeItem[] items = new FridgeItem[0];
/**
* The maximum items to place in the fridge.
*/
private int maxItems;
/**
* The maximum weight of the items in the fridge.
* The sum of the weights of the items in the
* fridge must not be higher than this.
*/
private double maxWeight;
/**
* Creates a fridge that can support the specified
* number of items with the specified total
* maximum weight.
*
* @param maxItems The maximum number of items that
* the fridge can hold.
* @param maxWeight The total maximum weight that
* the current items in the fridge may have.
*/
public Fridge(int maxItems, double maxWeight) {
this.maxItems = maxItems;
this.maxWeight = maxWeight;
}
/**
* Determines whether an item is in the fridge.
*
* @param item The item to check.
* @return True if it is in the fridge, false otherwise.
*/
public boolean containsItem(FridgeItem item) {
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
return true;
}
}
return false;
}
/**
* Adds a non-null item that is not in the fridge to
* the fridge unless there is no more space.
*
* @param item The item to add.
* @return True if the item has been added, false if
* the item is null or if there is no more space or
* if the item is already in the fridge.
*/
public boolean addItem(FridgeItem item) { // TODO: add more checks
// check invalid states
if (item == null) { // Cannot add null item, sorry
return false;
}
if (items.length == maxItems) { // Cannot add more items to the fridge
return false;
}
// all checks ok, add the item
FridgeItem[] newItems = new FridgeItem[items.length + 1];
for (int j = 0; j < items.length; j++) {
newItems[j] = items[j];
}
newItems[items.length] = item;
items = newItems;
return true;
}
/**
* Recursively searches an item with a specified name in
* the item array (from startIndex to items.length - 1)
* and if present, returns the lowest index of such an item.
* If no such item exists, the method returns -1.
*
* @param name The name of the item to search.
* @param startIndex The index to start from (0 <= startIndex < items.length).
* @return If found, the index of the first item in the items
* array with the specified name when starting to search from
* startIndex to items.length - 1. If no such item exists, the
* method returns -1.
*/
public int indexOf(String name, int startIndex) {
// TODO: implement
return -2;
}
// Other helper methods, not needed for this task.
/**
* Removes an item from the fridge and returns if something
* has actually been removed.
*
* @param item The item to remove.
* @return True if the item was removed, false if the item was
* not in the fridge.
*/
public boolean removeItem(FridgeItem item) {
// search for the item on the fridge
for (int i = 0; i < items.length; i++) {
if (items[i] == item) {
// if found, create a copy of the items without it
FridgeItem[] newItems = new FridgeItem[items.length - 1];
for (int j = 0; j < newItems.length; j++) {
if (j < i) {
newItems[j] = items[i];
} else {
newItems[j] = items[i + 1];
}
}
// and replace the old items with the new ones
items = newItems;
return true;
}
}
return false;
}
@Override
public String toString() {
String result = "";
for (int i = 0; i < items.length; i++) {
result += items[i].toString() + "\n";
}
if (this instanceof Weightful) {
result += "Weigth: " + ((Weightful) this).getWeight() + " kg";
}
return result;
}
}