Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit f760076

Browse files
author
Taras
committed
Transformed projects into maven project
- added maven configuration - removed lombok lar library
1 parent 9f7f6dc commit f760076

38 files changed

+1195
-0
lines changed

binary-search-tree/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>java-8-parent</artifactId>
9+
<groupId>com.bobocode</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>binary-search-tree</artifactId>
14+
15+
16+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package bobocode;
2+
3+
import java.util.function.Consumer;
4+
5+
public interface BST<T extends Comparable> {
6+
boolean insert(T element);
7+
8+
int height();
9+
10+
void inOrderTraversal(Consumer<T> consumer);
11+
12+
void preOrderTraversal(Consumer<T> consumer);
13+
14+
void postOrderTraversal(Consumer<T> consumer);
15+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package bobocode;
2+
3+
import java.util.Objects;
4+
import java.util.function.Consumer;
5+
import java.util.function.Predicate;
6+
7+
public class BinarySearchTree<T extends Comparable> implements BST<T> {
8+
Node<T> root;
9+
10+
@Override
11+
public boolean insert(T element) {
12+
Predicate<Node<T>> predicate = Objects::nonNull;
13+
if (predicate.test(root)) {
14+
root = new Node<>(element);
15+
return true;
16+
} else {
17+
return insert(root, element);
18+
}
19+
}
20+
21+
@Override
22+
public int height() {
23+
return height(root) - 1;
24+
}
25+
26+
private int height(Node<T> node) {
27+
if (node != null) {
28+
return 1 + Math.max(height(node.getLeft()), height(node.getRight()));
29+
} else {
30+
return 0;
31+
}
32+
}
33+
34+
private boolean insert(Node<T> node, T element) {
35+
if (node.getElement().compareTo(element) > 0) {
36+
if (node.getLeft() != null) {
37+
return insert(node.getLeft(), element);
38+
} else {
39+
node.setLeft(new Node<>(element));
40+
return true;
41+
}
42+
} else if (node.getElement().compareTo(element) < 0) {
43+
if (node.getRight() != null) {
44+
return insert(node.getRight(), element);
45+
} else {
46+
node.setRight(new Node<>(element));
47+
return true;
48+
}
49+
} else {
50+
return false;
51+
}
52+
}
53+
54+
@Override
55+
public void inOrderTraversal(Consumer<T> consumer) {
56+
inOrderTraversal(consumer, root);
57+
}
58+
59+
private void inOrderTraversal(Consumer<T> consumer, Node<T> node) {
60+
if (node != null) {
61+
inOrderTraversal(consumer, node.getLeft());
62+
consumer.accept(node.getElement());
63+
inOrderTraversal(consumer, node.getRight());
64+
}
65+
}
66+
67+
@Override
68+
public void preOrderTraversal(Consumer<T> consumer) {
69+
preOrderTraversal(consumer, root);
70+
}
71+
72+
private void preOrderTraversal(Consumer<T> consumer, Node<T> node) {
73+
if (node != null) {
74+
consumer.accept(node.getElement());
75+
preOrderTraversal(consumer, node.getLeft());
76+
preOrderTraversal(consumer, node.getRight());
77+
}
78+
}
79+
80+
@Override
81+
public void postOrderTraversal(Consumer<T> consumer) {
82+
postOrderTraversal(consumer, root);
83+
}
84+
85+
private void postOrderTraversal(Consumer<T> consumer, Node<T> node) {
86+
if (node != null) {
87+
postOrderTraversal(consumer, node.getLeft());
88+
postOrderTraversal(consumer, node.getRight());
89+
consumer.accept(node.getElement());
90+
}
91+
}
92+
93+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package bobocode;
2+
3+
import java.util.function.Consumer;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
BinarySearchTree<Integer> intTree = new BinarySearchTree<>();
9+
intTree.insert(9);
10+
intTree.insert(2);
11+
intTree.insert(12);
12+
intTree.insert(1);
13+
intTree.insert(7);
14+
intTree.insert(14);
15+
16+
Consumer printer = element -> System.out.print(element + " ");
17+
18+
System.out.println("Height: " + intTree.height());
19+
20+
System.out.print("\nIn-order traversal: ");
21+
intTree.inOrderTraversal(printer);
22+
23+
System.out.print("\nPre-order traversal: ");
24+
intTree.preOrderTraversal(printer);
25+
26+
System.out.print("\nPost-order traversal: ");
27+
intTree.postOrderTraversal(printer);
28+
System.out.println();
29+
30+
}
31+
32+
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package bobocode;
2+
3+
public class Node<T> {
4+
private T element;
5+
Node<T> left;
6+
Node<T> right;
7+
8+
public Node(T element) {
9+
this.element = element;
10+
}
11+
12+
public T getElement() {
13+
return element;
14+
}
15+
16+
public void setElement(T element) {
17+
this.element = element;
18+
}
19+
20+
public Node<T> getLeft() {
21+
return left;
22+
}
23+
24+
public void setLeft(Node<T> left) {
25+
this.left = left;
26+
}
27+
28+
public Node<T> getRight() {
29+
return right;
30+
}
31+
32+
public void setRight(Node<T> right) {
33+
this.right = right;
34+
}
35+
}

function-factory-task/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>java-8-parent</artifactId>
9+
<groupId>com.bobocode</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>function-factory-task</artifactId>
14+
15+
16+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.bobocode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.function.Function;
6+
7+
/**
8+
* FunctionFactory is an API that allows you to store and retrieve functions by name. Functions are stored in a HashMap,
9+
* where the key is a function name, and the value is a Function<T,R> instance. Methods addFunction(), and getFunction()
10+
* are already implemented.
11+
* <p>
12+
* The task is to create different types of functions and manage them using FunctionFactory. The instruction is placed
13+
* to the main method.
14+
*/
15+
public class FunctionFactoryTask {
16+
17+
static class FunctionFactory<T, R> {
18+
private Map<String, Function<T, R>> functionMap = new HashMap<>();
19+
20+
public void addFunction(String name, Function<T, R> function) {
21+
functionMap.put(name, function);
22+
}
23+
24+
public Function<T, R> getFunction(String name) {
25+
if (functionMap.containsKey(name)) {
26+
return functionMap.get(name);
27+
} else {
28+
throw new InvalidFunctionNameException(name);
29+
}
30+
}
31+
}
32+
33+
static class InvalidFunctionNameException extends RuntimeException {
34+
public InvalidFunctionNameException(String functionName) {
35+
super("Function " + functionName + " doesn't exist.");
36+
}
37+
}
38+
39+
/**
40+
* Follow the instructions to finish the task
41+
*/
42+
public static void main(String[] args) {
43+
FunctionFactory<Integer, Integer> functionFactory = new FunctionFactory<>();
44+
// 1. add simple functions "square", "increment", "decrement", "negative"
45+
// 2. get each function by name, and apply to argument 5, print a result (should be 25, 6, 4,-5 accordingly)
46+
// 3. add simple function "abs" using method reference (use class Math)
47+
// 4. add try/catch block, catch InvalidFunctionNameException and print some error message to the console
48+
// 5. try to get function with invalid name
49+
50+
functionFactory.addFunction("square", n -> n * n);
51+
functionFactory.addFunction("abs", Math::abs);
52+
53+
functionFactory.getFunction("square").apply(5);
54+
try{
55+
functionFactory.getFunction("abs").apply(5);
56+
}catch (InvalidFunctionNameException e){
57+
System.out.println(e.getMessage());
58+
}
59+
60+
61+
62+
63+
}
64+
65+
}
66+
67+

lambda-basics/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>java-8-parent</artifactId>
9+
<groupId>com.bobocode</groupId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>lambda-basics</artifactId>
14+
15+
16+
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.model.Account;
4+
import com.bobocode.util.TestDataProvider;
5+
6+
import java.math.BigDecimal;
7+
import java.time.LocalDate;
8+
import java.time.Period;
9+
import java.util.List;
10+
11+
/**
12+
* Parameterization in old Java version.
13+
*
14+
* This is a typical example of Java code before version 8. The first class citizens in Java are objects.
15+
* Therefore we always pass as methods arguments only primitive values, and objects. In other words, we add methods
16+
* parameters to make methods more universal. But what if we need to parameterize the behavior? What if we need to
17+
* perform different operations on account list basing on different conditions? That's called behavior parameterization.
18+
*
19+
* Behavior parameterization can be achieved by passing some function as methods parameter, but Java does not allow us
20+
* to pass functions as method parameters. Does it?
21+
*
22+
* Let's try to solve this problem with old Java style. We can not pass a function, but we can create an interface
23+
* with one method, and then pass its anonymous implementation with required logic. That's much more flexible right?
24+
*
25+
* See LambdaExample_02.java
26+
*
27+
* */
28+
public class LambdaExample_01 {
29+
public static void main(String[] args) {
30+
List<Account> accounts = TestDataProvider.generateAccountList();
31+
giveBonusForLoyalClients(accounts, 4, BigDecimal.valueOf(50));
32+
}
33+
34+
private static void giveBonusForLoyalClients(List<Account> accounts, int yearsWithBank, BigDecimal bonus) {
35+
for (Account account : accounts) {
36+
// todo: HARD CODED condition should be passed as method argument
37+
if (Period.between(account.getCreationDate(), LocalDate.now()).getYears() >= yearsWithBank) {
38+
// todo: HARD CODED operation should be passed as method argument
39+
account.setBalance(account.getBalance().add(bonus));
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)