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
12 changes: 10 additions & 2 deletions task-1/com/pizzacompany/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
27 changes: 27 additions & 0 deletions task-1/com/pizzacompany/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pizzacompany;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Order {
List<Pizza> 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());
}

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

Pizza(Size size, List<Topping> 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());
}

}
17 changes: 17 additions & 0 deletions task-1/com/pizzacompany/Size.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
19 changes: 19 additions & 0 deletions task-1/com/pizzacompany/Topping.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
19 changes: 13 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,31 @@
package org.my.cache;

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

public class Cache<T> {
private final Map<String, T> 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();
}

}

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> 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<Double> 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());
}
}
80 changes: 78 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,83 @@
class CacheTests {

@Test
void simpleTest() {
assertEquals(2, 1 + 1);
void addPairKeyAndStringValueInCache() {
//Arrange
Cache<String> cache = new Cache<>();

//Act
cache.put("box1", "yellow");
String value = cache.get("box1");

//Assert
assertEquals("yellow", value);
}

@Test
void returnNullWhenKeyDoesNotExist(){
//Arrange
Cache<String> cache = new Cache<>();

//Act
String value = cache.get("year");

//Assert
assertNull(value);
}

@Test
void returnDoubleValueOfKeyCache() {
//Arrange
Cache<Double> cache = new Cache<>();

//Act
cache.put("height", 1.64);
double value = cache.get("height");

//Assert
assertEquals(1.64, value);
}

@Test
void returnRemovedIntegerValueOfCache() {
//Arrange
Cache<Integer> cache = new Cache<>();

//Act
cache.put("year", 23);
int removedValue = cache.remove("year");

//Assert
assertEquals(23, removedValue);
}

@Test
void returnSizeOfCache() {
//Arrange
Cache<String> 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<Integer> cache = new Cache<>();

//Act
cache.put("followers", 1992);
cache.put("posts", 202);
cache.put("comments", 24);
cache.clear();

//Assert
assertEquals(0, cache.size());
}
}