Skip to content
Open
29 changes: 27 additions & 2 deletions task-1/com/pizzacompany/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package com.pizzacompany;
package pizzacompany;
import pizzacompany.model.Pizza;
import pizzacompany.enums.Size;
import pizzacompany.enums.Topping;
import pizzacompany.model.Order;
import java.util.List;

public class Main {
public static void main(String[] args) {
System.out.println("Hello Pizza!");

Pizza pizza1 = new Pizza(
Size.MEDIUM,
List.of(Topping.CHEESE, Topping.MUSHROOMS)
);

Pizza pizza2 = new Pizza(
Size.SMALL,
List.of(Topping.PEPPERONI)
);
Pizza pizza3 = new Pizza(
Size.LARGE,
List.of(Topping.OLIVES, Topping.PINEAPPLE, Topping.MUSHROOMS));
Order order = new Order();

order.addPizza(pizza1);
order.addPizza(pizza2);
order.addPizza(pizza3);

order.printReceipt();
}

}
17 changes: 17 additions & 0 deletions task-1/com/pizzacompany/enums/Size.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pizzacompany.enums;

public enum Size {
SMALL(6.00),
MEDIUM(9.00),
LARGE(12.00);

private final double basePrice;

Size(double basePrice) {
this.basePrice = basePrice;
}

public double getBasePrice() {
return basePrice;
}
}
16 changes: 16 additions & 0 deletions task-1/com/pizzacompany/enums/Topping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pizzacompany.enums;

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;
}
}
25 changes: 25 additions & 0 deletions task-1/com/pizzacompany/model/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pizzacompany.model;
import java.util.ArrayList;
import java.util.List;

public class Order {
private final List<Pizza> pizzas = new ArrayList<>();

public void addPizza(Pizza pizza) {
pizzas.add(pizza);
}

public double getTotalPrice() {
return pizzas.stream().mapToDouble(Pizza::getTotalPrice).sum();

}

public void printReceipt() {

for (Pizza pizza : pizzas) {
System.out.println(pizza);
}

System.out.printf("Total: €%.2f%n", getTotalPrice());
}
}
33 changes: 33 additions & 0 deletions task-1/com/pizzacompany/model/Pizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pizzacompany.model;
import pizzacompany.enums.Size;
import pizzacompany.enums.Topping;
import java.util.List;

public class Pizza {
private final Size size;
private final List<Topping> toppings;

public Pizza(Size size, List<Topping> toppings) {
this.size = size;
this.toppings = toppings;
}


public double getTotalPrice() {
double total = size.getBasePrice();

for(Topping topping : toppings) {
total += topping.getPrice();
}
return total;
}
@Override
public String toString() {
return String.format(
"%s pizza with %s - €%.2f",
size,
toppings,
getTotalPrice()
);
}
}
18 changes: 13 additions & 5 deletions task-2/src/main/java/org/my/cache/Cache.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package org.my.cache;

import java.util.HashMap;
import java.util.Map;

class Cache<T> {
private final Map<String, T> cache = new HashMap<>();

public void put(String key, T value) {
throw new UnsupportedOperationException("Not implemented yet");
cache.put(key, value);
}

public T get(String key) {
throw new UnsupportedOperationException("Not implemented yet");
return cache.get(key);
}

public T remove(String key) {
throw new UnsupportedOperationException("Not implemented yet");

return cache.remove(key);
}

public int size() {
throw new UnsupportedOperationException("Not implemented yet");

return cache.size();
}

public void clear() {
throw new UnsupportedOperationException("Not implemented yet");

cache.clear();
}

}
21 changes: 20 additions & 1 deletion task-2/src/main/java/org/my/cache/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

public class Main {
public static void main(String[] args) {
System.out.println("Hello Cache!");
Cache<String> stringCache = new Cache<>();
stringCache.put("name", "HackYourFuture");
System.out.println(stringCache.get("name"));
System.out.println(stringCache.get("city"));

Cache<Integer> numberCache = new Cache<>();
numberCache.put("c1", 12345);
numberCache.put("c2", 54321);
System.out.println(numberCache.size());
System.out.println(numberCache.remove("c1"));
System.out.println(numberCache.get("c1"));

Cache<Double> doubleCache = new Cache<>();
doubleCache.put("d1", 123.45);
doubleCache.put("d2", 543.21);
doubleCache.put("d3", 123.45);
System.out.println(doubleCache.size());
doubleCache.clear();
System.out.println(doubleCache.size());

}
}
78 changes: 75 additions & 3 deletions task-2/src/test/java/org/my/cache/CacheTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,84 @@
package org.my.cache;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class CacheTests {


@Test
void givenKeyAndValue_returnsStoredValue() {
//Arrange
Cache<String> stringCache = new Cache<>();
Cache<Integer> integerCache = new Cache<>();
// Act
stringCache.put("name", "Bob");
String result = stringCache.get("name");
integerCache.put("age", 25);
int age = integerCache.get("age");

//Assert
assertEquals("Bob", result,"name should be Bob");
assertEquals(25, age, "age should be 25");

}

@Test
void givenExistingKey_returnsStoredValue() {
//Arrange
Cache<String> stringCache = new Cache<>();
stringCache.put("name", "Alice");
// Act
String result = stringCache.get("name");
//Assert
assertEquals("Alice", result,"name should be Alice");
}

@Test
void givenMissingKey_returnsNull() {
//Arrange
Cache<String> stringCache = new Cache<>();
// Act
String result = stringCache.get("name");
//Assert
assertNull(result,"name should be null");
}

@Test
void givenExistingKey_returnsRemovedValue() {
//Arrange
Cache<Double> dobleCache = new Cache<>();
dobleCache.put("amount", 123.45);
// Act
Double result = dobleCache.remove("amount");
//Assert
assertEquals(123.45, result,"amount should be 123.45");
assertNull(dobleCache.get("amount"),"amount should be null");
}

@Test
void simpleTest() {
assertEquals(2, 1 + 1);
void givenTwoItems_returnsTwo() {
//Arrange
Cache<String> stringCache = new Cache<>();
stringCache.put("name", "Alice");
stringCache.put("city", "Amsterdam");
// Act
int result = stringCache.size();
//Assert
assertEquals(2, result,"size should be 2");

}

@Test
void givenItems_returnsSizeZero(){
//Arrange
Cache<String> stringCache = new Cache<>();
// Act
stringCache.put("name", "HackYourFuture");
stringCache.put("city", "Amsterdam");

stringCache.clear();
//Assert
assertEquals(0, stringCache.size(),"size should be 0");

}
}