Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion task-1/com/pizzacompany/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.pizzacompany;

import java.util.Arrays;

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

order.addPizza(new Pizza(Size.MEDIUM, Arrays.asList(Topping.CHEESE, Topping.MUSHROOMS)));
order.addPizza(new Pizza(Size.LARGE, Arrays.asList(Topping.PEPPERONI, Topping.OLIVES, Topping.CHEESE)));
order.addPizza(new Pizza(Size.SMALL, Arrays.asList(Topping.PINEAPPLE)));

order.printReceipt();

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

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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

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

public double getTotalPrice() {
double total = 0.0;
for (Pizza pizza : pizzas) {
total += pizza.getTotalPrice();
}
return total;
}

public void printReceipt() {
System.out.println("=== Receipt ===");
for (Pizza pizza : pizzas) {
System.out.println(pizza);
}
System.out.println("---------------");
System.out.println("Total: €" + String.format(Locale.US, "%.2f", getTotalPrice()));
}
}
32 changes: 32 additions & 0 deletions task-1/com/pizzacompany/Pizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pizzacompany;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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

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

public double getTotalPrice() {
double total = size.getBasePrice();
for (Topping topping : toppings) {
total += topping.getPrice();
}
return total;
}

@Override
public String toString() {
return size + " pizza with " + toppings + " - €" + formatPrice(getTotalPrice());
}

private static String formatPrice(double value) {
return String.format(Locale.US, "%.2f", value);
}
}
15 changes: 15 additions & 0 deletions task-1/com/pizzacompany/Size.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pizzacompany;

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;
}
}
15 changes: 15 additions & 0 deletions task-1/com/pizzacompany/Topping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.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;
}
}
17 changes: 11 additions & 6 deletions task-2/src/main/java/org/my/cache/Cache.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package org.my.cache;

class Cache<T> {
import java.util.HashMap;
import java.util.Map;

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

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

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

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

public int size() {
throw new UnsupportedOperationException("Not implemented yet");
return store.size();
}

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

}
12 changes: 11 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,16 @@

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

Cache<Double> numberCache = new Cache<>();
numberCache.put("c1", 12345.0);
numberCache.put("c2", 54321.0);
System.out.println(numberCache.size());
numberCache.remove("c1");
System.out.println(numberCache.get("c1"));
}
}
38 changes: 36 additions & 2 deletions task-2/src/test/java/org/my/cache/CacheTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,41 @@
class CacheTests {

@Test
void simpleTest() {
assertEquals(2, 1 + 1);
void putAndGetRetunsValue(){
Cache<String> cache = new Cache<>();
cache.put("name", "HackYourFuture");
assertEquals("HackYourFuture", cache.get("name"));
}

@Test
void getMIssingReturnsNull(){
Cache<String> cache = new Cache<>();
assertNull(cache.get("missing"));
}

@Test
void removeReturnValueAndRemoves(){
Cache<String> cache = new Cache<>();
cache.put("name", "HackYourFuture");
assertEquals("HackYourFuture", cache.remove("name"));
assertNull(cache.get("name"));
}

@Test
void sizeCountsEntries(){
Cache<String> cache = new Cache<>();
cache.put("c1", "1");
cache.put("c2", "2");
assertEquals(2, cache.size());

}

@Test
void clearEmptiesCache(){
Cache<String> cache = new Cache<>();
cache.put("c1", "1.0");
cache.put("c2", "2.0");
cache.clear();
assertEquals(0, cache.size());
}
}