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
13 changes: 13 additions & 0 deletions task-1/com/pizzacompany/Main.java
Original file line number Diff line number Diff line change
@@ -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();

}
}
92 changes: 92 additions & 0 deletions task-1/com/pizzacompany/Pizza.java
Original file line number Diff line number Diff line change
@@ -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<Topping> toppings;

public Pizza(Size size, List<Topping> 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<Pizza> 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());
}
}
29 changes: 9 additions & 20 deletions task-2/src/main/java/org/my/cache/Cache.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package org.my.cache;
import java.util.HashMap;

class Cache<T> {
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<String, T> 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();}
}
29 changes: 28 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,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<String> 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<Double> 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());
}

}
47 changes: 45 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,50 @@
class CacheTests {

@Test
void simpleTest() {
assertEquals(2, 1 + 1);
void testPutAndGet() {
Cache<String> 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<Integer> numberCache = new Cache<>();
// Verify looking up a key that doesn't exist safely returns null
assertNull(numberCache.get("missingKey"));
}

@Test
void testSizeAndClear() {
Cache<Double> 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<String> 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());
}

}