diff --git a/task-1/com/pizzacompany/Main.java b/task-1/com/pizzacompany/Main.java index 5c380fa..f80aa77 100644 --- a/task-1/com/pizzacompany/Main.java +++ b/task-1/com/pizzacompany/Main.java @@ -1,7 +1,20 @@ package com.pizzacompany; +import com.pizzacompany.Pizza.*; +import java.util.List; +// Main entry point to pizza orders and printing a receipt. public class Main { public static void main(String[] args) { System.out.println("Hello Pizza!"); + Order order = new Order(); + + // Creating an order with 4 pizzas and outputs the final bill. + order.addPizza(new Pizza(Pizza.Size.MEDIUM, List.of(Topping.CHEESE, Pizza.Topping.MUSHROOMS))); + order.addPizza(new Pizza(Pizza.Size.LARGE, List.of(Topping.PEPPERONI, Topping.OLIVES, Topping.CHEESE))); + order.addPizza(new Pizza(Pizza.Size.SMALL, List.of(Topping.PINEAPPLE))); + order.addPizza(new Pizza(Pizza.Size.LARGE, List.of(Topping.PINEAPPLE))); + + order.printReceipt(); + } } \ No newline at end of file diff --git a/task-1/com/pizzacompany/Pizza.java b/task-1/com/pizzacompany/Pizza.java new file mode 100644 index 0000000..c7de262 --- /dev/null +++ b/task-1/com/pizzacompany/Pizza.java @@ -0,0 +1,92 @@ +package com.pizzacompany; + +import java.util.ArrayList; +import java.util.List; + +// Pizza order item with a specific size and a list of toppings. +public class Pizza { + // Sizes of pizza, each have a base price. + public enum Size { + SMALL(6.0), + MEDIUM(9.0), + LARGE(12.0); + private final double price; + + Size(double basePrice) { + this.price = basePrice; + } + + // Getter + public double getBasePrice() { + return price; + } + } + + // Available pizza toppings, each have an extra cost. + public enum Topping { + CHEESE(1.0), + MUSHROOMS(1.5), + PEPPERONI(2.0), + OLIVES(1.0), + PINEAPPLE(1.5); + private final double price; + Topping(double toppingPrice) { + this.price = toppingPrice; + } + + // Getter + public double getPrice() { + return price; + } + } + + private final Size size; + private final List toppings; + + public Pizza(Size size, List toppings) { + this.size = size; + this.toppings = new ArrayList<>(toppings); + } + + // Calculate the complete price of this single pizza. + public double getTotalPrice() { + double basePrice = size.getBasePrice(); + double toppingsTotal = toppings.stream() + .mapToDouble(topping -> topping.getPrice()) + .sum(); + return basePrice + toppingsTotal; + } + + @Override + // Converting pizza details into a readable string. + public String toString() { + return size + " pizza with " + toppings + " - €" + String.format("%.2f", getTotalPrice()); + } +} + +// Manages a collection of pizzas and calculates the final receipt totals. +class Order { + private final List pizzas = new ArrayList<>(); + + //Adds a completed pizza object into the current order list. + public void addPizza(Pizza pizza) { + pizzas.add(pizza); + } + + // Getter + // Calculates the entire cost of the current order. + public double getTotalPrice() { + double orderTotal = pizzas.stream() + .mapToDouble(pizza -> pizza.getTotalPrice()) + .sum(); + return orderTotal; + } + + public void printReceipt() { + System.out.println("=== Receipt ==="); + for (Pizza pizza : pizzas) { + System.out.println(pizza); + } + System.out.printf("---------------\nTotal: €%.2f\n", getTotalPrice()); + } +} \ No newline at end of file diff --git a/task-2/src/main/java/org/my/cache/Cache.java b/task-2/src/main/java/org/my/cache/Cache.java index 3804372..693f62c 100644 --- a/task-2/src/main/java/org/my/cache/Cache.java +++ b/task-2/src/main/java/org/my/cache/Cache.java @@ -1,24 +1,13 @@ package org.my.cache; +import java.util.HashMap; class Cache { - public void put(String key, T value) { - throw new UnsupportedOperationException("Not implemented yet"); - } - - public T get(String key) { - throw new UnsupportedOperationException("Not implemented yet"); - } - - public T remove(String key) { - throw new UnsupportedOperationException("Not implemented yet"); - } - - public int size() { - throw new UnsupportedOperationException("Not implemented yet"); - } - - public void clear() { - throw new UnsupportedOperationException("Not implemented yet"); - } - + private HashMap cashe = new HashMap<>(); + + // Methods + public void put(String key, T value) {cashe.put(key, value);} + public T get(String key) {return cashe.get(key);} + public T remove(String key) {return cashe.remove(key);} + public int size() {return cashe.size();} + public void clear() {cashe.clear();} } diff --git a/task-2/src/main/java/org/my/cache/Main.java b/task-2/src/main/java/org/my/cache/Main.java index 38c023b..b17ddd7 100644 --- a/task-2/src/main/java/org/my/cache/Main.java +++ b/task-2/src/main/java/org/my/cache/Main.java @@ -2,6 +2,33 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello Cache!"); + System.out.println("✦✦ Hello Cache ✦✦"); + System.out.println("this is a generic cache\n"); + + // String Cache + Cache stringCache = new Cache<>(); + System.out.println("-✦- String Cache -✦-"); + stringCache.put("name", "HackYourFuture"); + stringCache.put("city", "Amsterdam"); + + System.out.println("◈ "+"Cache size: " + stringCache.size()); // Should print --> 2 + System.out.println("◈ "+"Get 'name': " + stringCache.get("name")); // Should print --> HackYourFuture + + stringCache.remove("city"); + System.out.println("◈ "+"Get 'city' after removal: " + stringCache.get("city")); // Should print --> null + + // Double numbers (decemal) Cache + Cache doubleCache = new Cache<>(); + + System.out.println("\n-✦- Double Cache -✦-"); + doubleCache.put("pi", 3.14159); + doubleCache.put("version", 1.0); + + System.out.println("◈ "+"Cache size: " + doubleCache.size()); // Should print --> 2 + System.out.println("◈ "+"Get 'pi': " + doubleCache.get("pi")); // Should print --> 3.14159 + + doubleCache.clear(); + System.out.println("◈ "+"Size after clear: " + doubleCache.size()); } + } \ No newline at end of file diff --git a/task-2/src/test/java/org/my/cache/CacheTests.java b/task-2/src/test/java/org/my/cache/CacheTests.java index 3afbee4..c8fd54f 100644 --- a/task-2/src/test/java/org/my/cache/CacheTests.java +++ b/task-2/src/test/java/org/my/cache/CacheTests.java @@ -6,7 +6,50 @@ class CacheTests { @Test - void simpleTest() { - assertEquals(2, 1 + 1); + void testPutAndGet() { + Cache stringCache = new Cache<>(); + // Test putting an item into the cache + stringCache.put("0056", "Sun & Moon"); + // verify that getting the item returns the correct value + assertEquals("Sun & Moon", stringCache.get("0056")); } + + @Test + void testGetNonExistentKeyReturnsNull() { + Cache numberCache = new Cache<>(); + // Verify looking up a key that doesn't exist safely returns null + assertNull(numberCache.get("missingKey")); + } + + @Test + void testSizeAndClear() { + Cache doubleCache = new Cache<>(); + // Size should be 0 initially + assertEquals(0, doubleCache.size()); + doubleCache.put("PS1", 1.1); + doubleCache.put("PS2", 2.2); + + assertEquals(2, doubleCache.size()); // Size should now be 2 + + // clean the cache + doubleCache.clear(); + + assertEquals(0, doubleCache.size());// Size should be back to 0 + } + + @Test + void testRemove() { + Cache stringCache = new Cache<>(); + stringCache.put("GBA", "Mariokart"); + + // Verify that remove() deletes the item & returns its old value + String removedValue = stringCache.remove("GBA"); + assertEquals("Mariokart", removedValue); + + // Verify the item is truly gone from the cache + assertNull(stringCache.get("GBA")); + assertEquals(0, stringCache.size()); + } + } +