diff --git a/task-1/com/pizzacompany/Main.java b/task-1/com/pizzacompany/Main.java index 5c380fa..38b6f46 100644 --- a/task-1/com/pizzacompany/Main.java +++ b/task-1/com/pizzacompany/Main.java @@ -1,7 +1,15 @@ -package com.pizzacompany; +package pizzacompany; + +import java.util.List; public class Main { public static void main(String[] args) { - System.out.println("Hello Pizza!"); + Order order1 = new Order(); + + order1.addPizza(new Pizza(Size.SMALL, List.of(Topping.PEPPERONI, Topping.OLIVES))); + order1.addPizza(new Pizza(Size.MEDIUM, List.of(Topping.MUSHROOMS, Topping.CHEESE, Topping.PINEAPPLE))); + order1.addPizza(new Pizza(Size.LARGE, List.of(Topping.MUSHROOMS, Topping.CHEESE, Topping.PEPPERONI, Topping.OLIVES, Topping.PINEAPPLE))); + + order1.printReceipt(); } } \ No newline at end of file diff --git a/task-1/com/pizzacompany/Order.java b/task-1/com/pizzacompany/Order.java new file mode 100644 index 0000000..77359f0 --- /dev/null +++ b/task-1/com/pizzacompany/Order.java @@ -0,0 +1,27 @@ +package pizzacompany; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Order { + List pizzas = new ArrayList<>(); + + Order(){}; + + public void addPizza(Pizza pizza){ + pizzas.add(pizza); + } + + public double getTotalPrice(){ + return pizzas.stream().filter(Objects::nonNull).mapToDouble(Pizza::getTotalPrice).sum(); + } + + public void printReceipt(){ + System.out.println("=== Receipt ==="); + pizzas.stream().filter(Objects::nonNull).forEach(pizza -> System.out.println(pizza.toString())); + System.out.println("---------------"); + System.out.println("Total: " +getTotalPrice()); + } + +} diff --git a/task-1/com/pizzacompany/Pizza.java b/task-1/com/pizzacompany/Pizza.java new file mode 100644 index 0000000..54d4ead --- /dev/null +++ b/task-1/com/pizzacompany/Pizza.java @@ -0,0 +1,29 @@ +package pizzacompany; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; + +public class Pizza { + Size size; + List toppings = new ArrayList<>(); + + Pizza(Size size, List toppings){ + this.size = size; + this.toppings = toppings; + } + + public double getTotalPrice(){ + double toppingsPrice = toppings.stream().filter(Objects::nonNull).mapToDouble(Topping::getPrice).sum(); + + + return size.getBasePrice() + toppingsPrice; + } + + @Override + public String toString(){ + return size + " pizza with " + toppings + "- €" + String.format("%.2f", getTotalPrice()); + } + +} diff --git a/task-1/com/pizzacompany/Size.java b/task-1/com/pizzacompany/Size.java new file mode 100644 index 0000000..c1064ee --- /dev/null +++ b/task-1/com/pizzacompany/Size.java @@ -0,0 +1,17 @@ +package pizzacompany; + +public enum Size { + SMALL(6.00), + MEDIUM(9.00), + LARGE(12.00); + + private final double price; + + Size(double price){ + this.price = price; + } + + public double getBasePrice(){ + return price; + } +} diff --git a/task-1/com/pizzacompany/Topping.java b/task-1/com/pizzacompany/Topping.java new file mode 100644 index 0000000..fa59d24 --- /dev/null +++ b/task-1/com/pizzacompany/Topping.java @@ -0,0 +1,19 @@ +package pizzacompany; + +public enum Topping { + CHEESE(1.00), + MUSHROOMS(1.50), + PEPPERONI(2.00), + OLIVES(1.00), + PINEAPPLE(1.50); + + private final double price; + + Topping(double price){ + this.price = price; + } + + public double getPrice(){ + return price; + } +} 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..7578bc4 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,31 @@ package org.my.cache; -class Cache { +import java.util.HashMap; +import java.util.Map; + +public class Cache { + private final Map cacheInfo = new HashMap<>(); + public void put(String key, T value) { - throw new UnsupportedOperationException("Not implemented yet"); + cacheInfo.put(key, value); } public T get(String key) { - throw new UnsupportedOperationException("Not implemented yet"); + return cacheInfo.get(key); + } public T remove(String key) { - throw new UnsupportedOperationException("Not implemented yet"); + return cacheInfo.remove(key); } public int size() { - throw new UnsupportedOperationException("Not implemented yet"); + return cacheInfo.size(); } public void clear() { - throw new UnsupportedOperationException("Not implemented yet"); + cacheInfo.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..c344abc 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,25 @@ public class Main { public static void main(String[] args) { - System.out.println("Hello Cache!"); +Cache cacheUsers = new Cache<>(); +cacheUsers.put("user1", "Anna"); +cacheUsers.put("user2", "Alex"); +System.out.println("User 1 is " + cacheUsers.get("user1")); +System.out.println("User 2 is " + cacheUsers.get("user2")); +System.out.println("Cache user size is " + cacheUsers.size()); +System.out.println(cacheUsers.remove("user1") + " is removed"); +cacheUsers.clear(); +System.out.println("Cache user size is " + cacheUsers.size()); + + +Cache cacheWeight = new Cache<>(); +cacheWeight.put("Anna", 53.7); +cacheWeight.put("Alex", 68.5); +System.out.println("Alex weight is " + cacheWeight.get("Alex")); +System.out.println("Anna weight is " + cacheWeight.get("Anna")); +System.out.println("Cache weight size is " + cacheWeight.size()); +System.out.println("Anna's weight " + cacheWeight.remove("Anna") + " is removed"); +cacheWeight.clear(); +System.out.println("Cache weight size is " + cacheWeight.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..240b951 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,83 @@ class CacheTests { @Test - void simpleTest() { - assertEquals(2, 1 + 1); + void addPairKeyAndStringValueInCache() { + //Arrange + Cache cache = new Cache<>(); + + //Act + cache.put("box1", "yellow"); + String value = cache.get("box1"); + + //Assert + assertEquals("yellow", value); + } + + @Test + void returnNullWhenKeyDoesNotExist(){ + //Arrange + Cache cache = new Cache<>(); + + //Act + String value = cache.get("year"); + + //Assert + assertNull(value); + } + + @Test + void returnDoubleValueOfKeyCache() { + //Arrange + Cache cache = new Cache<>(); + + //Act + cache.put("height", 1.64); + double value = cache.get("height"); + + //Assert + assertEquals(1.64, value); + } + + @Test + void returnRemovedIntegerValueOfCache() { + //Arrange + Cache cache = new Cache<>(); + + //Act + cache.put("year", 23); + int removedValue = cache.remove("year"); + + //Assert + assertEquals(23, removedValue); + } + + @Test + void returnSizeOfCache() { + //Arrange + Cache cache = new Cache<>(); + + //Act + cache.put("user1", "Alex"); + cache.put("user2", "Daniel"); + int size = cache.size(); + + //Assert + assertEquals(2, size); + + } + + @Test + void clearAllCache() { + //Arrange + Cache cache = new Cache<>(); + + //Act + cache.put("followers", 1992); + cache.put("posts", 202); + cache.put("comments", 24); + cache.clear(); + + //Assert + assertEquals(0, cache.size()); } }