209 lines
9.3 KiB
Java
209 lines
9.3 KiB
Java
public class Main {
|
|
|
|
/**
|
|
* Die folgende main-Methode dient Ihnen zur Hilfe, um Ihre Lösungen zu testen.
|
|
* Beachten Sie bitte, dass in dieser Methode nicht alle Tests enthalten sind,
|
|
* sondern nur einige, die Ihnen helfen sollen, Ihre Lösungen zu überprüfen.
|
|
*/
|
|
public static void main(String[] args) {
|
|
|
|
System.out.println();
|
|
System.out.println("=========================================");
|
|
System.out.println("Aufgabe 1 - Fridge");
|
|
System.out.println("=========================================");
|
|
System.out.println();
|
|
|
|
// create items
|
|
FridgeItem gouda = new Cheese(500.5, "Gouda", "yellow");
|
|
FridgeItem cheddar = new Cheese(1123.75, "Cheddar", "orange");
|
|
FridgeItem cherryYoghurt = new Yoghurt(250, "Cherry", "red");
|
|
FridgeItem vanillaYoghurt = new Yoghurt(500, "Vanilla", "light yellow");
|
|
|
|
// create fridge
|
|
Fridge fridge = new Fridge(4, 2000.0);
|
|
fridge.addItem(cheddar);
|
|
fridge.addItem(gouda);
|
|
|
|
// Aufgabe 1 - addItem
|
|
System.out.println("Testing Fridge.addItem()");
|
|
boolean addCheddarAgain = fridge.addItem(cheddar);
|
|
if (addCheddarAgain) {
|
|
System.out.println("Failure: The cheddar should not be added twice.");
|
|
} else {
|
|
System.out.println("Success: The cheddar was not added again.");
|
|
}
|
|
System.out.println();
|
|
|
|
boolean addCherryYoghurt = fridge.addItem(cherryYoghurt);
|
|
if (addCherryYoghurt) {
|
|
System.out.println("Success: The cherry yoghurt fits in the fridge.");
|
|
} else {
|
|
System.out.println("Failure: The cherry yoghurt should fit in the fridge.");
|
|
}
|
|
if (fridge.containsItem(cherryYoghurt)) {
|
|
System.out.println("Success: The fridge contains the cherry yoghurt.");
|
|
} else {
|
|
System.out.println("Failure: The fridge does not contain the cherry yoghurt.");
|
|
}
|
|
System.out.println();
|
|
|
|
boolean addVanillaYoghurt = fridge.addItem(vanillaYoghurt);
|
|
if (addVanillaYoghurt) {
|
|
System.out.println("Failure: The vanilla yoghurt should be too heavy.");
|
|
} else {
|
|
System.out.println("Success: The vanilla yoghurt is too heavy.");
|
|
}
|
|
|
|
// Aufgabe 1 - indexOf
|
|
System.out.println("Testing: Fridge.indexOf()");
|
|
System.out.println("Fridge.indexOf(cheddar.getName(), 0) Expected: 0, Your code: "
|
|
+ fridge.indexOf(cheddar.getName(), 0));
|
|
System.out.println("Fridge.indexOf(cheddar.getName(), 1) Expected: -1, Your code: "
|
|
+ fridge.indexOf(cheddar.getName(), 1));
|
|
System.out.println("Fridge.indexOf(cheddar.getName(), 2) Expected: -1, Your code: "
|
|
+ fridge.indexOf(cheddar.getName(), 2));
|
|
System.out.println("Fridge.indexOf(cherryYoghurt.getName(), 0) Expected: 2, Your code: "
|
|
+ fridge.indexOf(cherryYoghurt.getName(), 0));
|
|
fridge.addItem(new Yoghurt(50.5, "Cherry", "red")); // add same name to check ordering
|
|
System.out.println("Fridge.indexOf(cherryYoghurt.getName(), 0) Expected: 2, Your code: "
|
|
+ fridge.indexOf(cherryYoghurt.getName(), 0));
|
|
|
|
System.out.println();
|
|
System.out.println("=========================================");
|
|
System.out.println("Aufgabe 2 - Deque");
|
|
System.out.println("=========================================");
|
|
System.out.println();
|
|
|
|
// create a deque
|
|
Deque deque = new Deque();
|
|
|
|
// add first item to the deque
|
|
deque.addLast("Item 1");
|
|
// deque should now contain: ["Item 1"]
|
|
System.out.println(deque);
|
|
System.out.println("-> Should contain: [Item 1]");
|
|
// deque size should be 1
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 1");
|
|
// deque should not be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be false");
|
|
|
|
// peek first should return "Item 1"
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return 'Item 1'");
|
|
// peek last should return "Item 1"
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return 'Item 1'");
|
|
|
|
System.out.println("");
|
|
|
|
// add second item to the deque
|
|
deque.addLast("Item 2");
|
|
// deque should now contain: ["Item 1", "Item 2"]
|
|
System.out.println(deque);
|
|
System.out.println("-> Should contain: [Item 1, Item 2]");
|
|
// deque size should be 2
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 2");
|
|
// deque should not be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be false");
|
|
// peek first should return "Item 1"
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return 'Item 1'");
|
|
// peek last should return "Item 2"
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return 'Item 2'");
|
|
|
|
System.out.println("");
|
|
|
|
// add third item to the deque
|
|
deque.addLast("Item 3");
|
|
// deque should now contain: ["Item 1", "Item 2", "Item 3"]
|
|
System.out.println(deque);
|
|
System.out.println("-> Should contain: [Item 1, Item 2, Item 3]");
|
|
// deque size should be 3
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 3");
|
|
// deque should not be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be false");
|
|
// peek first should return "Item 1"
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return 'Item 1'");
|
|
// peek last should return "Item 3"
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return 'Item 3'");
|
|
|
|
System.out.println("");
|
|
|
|
// poll last should return "Item 3" and remove it from the deque
|
|
System.out.println("Poll last: " + deque.pollLast());
|
|
System.out.println("-> Should return 'Item 3'");
|
|
// deque should now contain: ["Item 1", "Item 2"]
|
|
System.out.println(deque);
|
|
System.out.println("-> Should contain: [Item 1, Item 2]");
|
|
// deque size should be 2
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 2");
|
|
// deque should not be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be false");
|
|
// peek first should return "Item 1"
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return 'Item 1'");
|
|
// peek last should return "Item 2"
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return 'Item 2'");
|
|
|
|
System.out.println("");
|
|
|
|
// poll last should return "Item 2" and remove it from the deque
|
|
System.out.println("Poll last: " + deque.pollLast());
|
|
System.out.println("-> Should return 'Item 2'");
|
|
// deque should now contain: ["Item 1"]
|
|
System.out.println(deque);
|
|
// deque size should be 1
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 1");
|
|
// deque should not be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be false");
|
|
// peek first should return "Item 1"
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return 'Item 1'");
|
|
// peek last should return "Item 1"
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return 'Item 1'");
|
|
|
|
System.out.println("");
|
|
|
|
// poll last should return "Item 1" and remove it from the deque
|
|
System.out.println("Poll last: " + deque.pollLast());
|
|
System.out.println("-> Should return 'Item 1'");
|
|
// deque should now be empty
|
|
System.out.println(deque);
|
|
System.out.println("-> Should be empty");
|
|
// deque size should be 0
|
|
System.out.println("Deque size: " + deque.size());
|
|
System.out.println("-> Should be size 0");
|
|
// deque should be empty
|
|
System.out.println("Is deque empty? " + deque.isEmpty());
|
|
System.out.println("-> Should be true");
|
|
// peek first should return null
|
|
System.out.println("Peek first: " + deque.peekFirst());
|
|
System.out.println("-> Peek first should return null");
|
|
// peek last should return null
|
|
System.out.println("Peek last: " + deque.peekLast());
|
|
System.out.println("-> Peek last should return null");
|
|
|
|
System.out.println("");
|
|
// poll last should return null since the deque is empty
|
|
System.out.println("Poll last: " + deque.pollLast());
|
|
System.out.println("-> Should return null since the deque is empty");
|
|
|
|
}
|
|
|
|
}
|