diff --git a/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/LinkedStack.java b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/LinkedStack.java new file mode 100644 index 00000000..3f55d096 --- /dev/null +++ b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/LinkedStack.java @@ -0,0 +1,67 @@ +package com.bobocode.stack; + +import com.bobocode.stack.exception.EmptyStackException; +import com.bobocode.util.ExerciseNotCompletedException; + +/** + * {@link LinkedStack} represents a last-in-first-out (LIFO) stack of objects that is based on singly linked generic nodes. + * A node is implemented as inner static class {@link Node}. + * + * @param generic type parameter + */ +public class LinkedStack implements Stack { + + /** + * This method creates a stack of provided elements + * + * @param elements elements to add + * @param generic type + * @return a new stack of elements that were passed as method parameters + */ + public static LinkedStack of(T... elements) { + throw new ExerciseNotCompletedException(); // todo: implement this method + } + + /** + * The method pushes an element onto the top of this stack. This has exactly the same effect as: + * addElement(item) + * + * @param element elements to add + */ + @Override + public void push(T element) { + throw new ExerciseNotCompletedException(); // todo: implement this method + } + + /** + * This method removes the object at the top of this stack + * and returns that object as the value of this function. + * + * @return The object at the top of this stack + * @throws EmptyStackException - if this stack is empty + */ + @Override + public T pop() { + throw new ExerciseNotCompletedException(); // todo: implement this method + } + + /** + * Returns the number of elements in the stack + * + * @return number of elements + */ + @Override + public int size() { + throw new ExerciseNotCompletedException(); // todo: implement this method + } + + /** + * Checks if a stack is empty + * + * @return {@code true} if a stack is empty, {@code false} otherwise + */ + @Override + public boolean isEmpty() { + throw new ExerciseNotCompletedException(); // todo: implement this method; + } +} diff --git a/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/README.md b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/README.md new file mode 100644 index 00000000..3c9e944b --- /dev/null +++ b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/README.md @@ -0,0 +1,21 @@ +# Stack exercise :muscle: +Improve your data structures skills + +### Task +**Stack** is a last in, first out (LIFO) collection of elements. Your job is to +implement the *todo* section of the class `LinkedStack`. Please note, that your implementation should be based on **singly +liked nodes.** It means that you should create your own class `Node` that will hold stack elements. + +To verify your implementation, run `StackTest.java` + +### Pre-conditions :heavy_exclamation_mark: +You're supposed to be familiar with Stack data structure, and be able to write Java code + +### How to start :question: +* Just clone the repository and start implementing `Stack` interface in `LinkedStack` class. +* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source) +* Don't worry if you got stuck, checkout the [exercise/completed](https://github.com/bobocode-projects/java-fundamentals-course/tree/exercise/completed) branch and see the final implementation + +### Related materials :information_source: + * [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) + diff --git a/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/Stack.java b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/Stack.java new file mode 100644 index 00000000..0e7b804b --- /dev/null +++ b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/Stack.java @@ -0,0 +1,12 @@ +package com.bobocode.stack; + +public interface Stack { + + void push(T element); + + T pop(); + + int size(); + + boolean isEmpty(); +} diff --git a/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/exception/EmptyStackException.java b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/exception/EmptyStackException.java new file mode 100644 index 00000000..b9d8faec --- /dev/null +++ b/2-0-data-structures-and-algorithms/src/main/java/com/bobocode/stack/exception/EmptyStackException.java @@ -0,0 +1,5 @@ +package com.bobocode.stack.exception; + +public class EmptyStackException extends RuntimeException{ + +} diff --git a/2-0-data-structures-and-algorithms/src/test/java/com/bobocode/stack/StackTest.java b/2-0-data-structures-and-algorithms/src/test/java/com/bobocode/stack/StackTest.java new file mode 100644 index 00000000..b4e4d674 --- /dev/null +++ b/2-0-data-structures-and-algorithms/src/test/java/com/bobocode/stack/StackTest.java @@ -0,0 +1,88 @@ +package com.bobocode.stack; + +import com.bobocode.stack.exception.EmptyStackException; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class StackTest { + + private Stack intStack = new LinkedStack<>(); + + @Test + @Order(1) + void pushAndPopElementOntoEmptyStack() { + intStack.push(234); + + assertThat(intStack.pop()).isEqualTo(243); + } + + @Test + @Order(2) + void popElementFromEmptyStack() { + assertThrows(EmptyStackException.class, () -> intStack.pop()); + } + + @Test + @Order(3) + void pushElements() { + intStack = LinkedStack.of(23, 35, 72); + + intStack.push(55); + + assertThat(intStack.pop()).isEqualTo(55); + } + + @Test + @Order(4) + void popElements() { + intStack = LinkedStack.of(87, 53, 66); + + intStack.pop(); + intStack.push(234); + Integer lastElement = intStack.pop(); + + assertThat(lastElement).isEqualTo(234); + } + + @Test + @Order(5) + void size() { + intStack = LinkedStack.of(87, 53, 66); + + int actualSize = intStack.size(); + + assertThat(actualSize).isEqualTo(3); + } + + @Test + @Order(6) + void sizeOnEmptyStack() { + int actualSize = intStack.size(); + + assertThat(actualSize).isEqualTo(0); + } + + @Test + @Order(7) + void isEmpty() { + intStack = LinkedStack.of(87, 53, 66); + + boolean stackEmpty = intStack.isEmpty(); + + assertThat(stackEmpty).isEqualTo(false); + } + + @Test + @Order(8) + void isEmptyOnEmptyStack() { + boolean stackEmpty = intStack.isEmpty(); + + assertThat(stackEmpty).isEqualTo(true); + } +} diff --git a/6-0-test-driven-development/README.md b/6-0-test-driven-development/README.md new file mode 100644 index 00000000..b5397e07 --- /dev/null +++ b/6-0-test-driven-development/README.md @@ -0,0 +1,25 @@ +# TDD exercises +The list of exercises dedicated to training your Test-driven development (TDD) skills + +### No pain, No gain :heavy_exclamation_mark: + +> Skill is only developed by hours and hours and hours of beating on your craft + +Working on real problems, you're focused on finding a solution. Learning new things, you're trying to understand how it works. +It is important to have a different type of activities, which purpose is improving your skill + +***An exercise** is a predefined task that you continuously implement to improve a certain skill* :muscle: + +## +### The three laws of TDD +1. You should not write production code until you have written a failing unit test +2. You should not write more of a unit test than is sufficient to fail +3. You should not write more production code than is sufficient to pass the failing test +## + + +* [Binary Search Tree]() relink +* [Stack]() relink +* [Linked List]() relink + + diff --git a/6-0-test-driven-development/pom.xml b/6-0-test-driven-development/pom.xml new file mode 100644 index 00000000..7340e75b --- /dev/null +++ b/6-0-test-driven-development/pom.xml @@ -0,0 +1,19 @@ + + + + java-fundamentals-course + com.bobocode + 1.0-SNAPSHOT + + 4.0.0 + + 6-0-test-driven-development + + + 11 + 11 + + + \ No newline at end of file diff --git a/6-0-test-driven-development/src/main/java/com/bobocode/stack/README.md b/6-0-test-driven-development/src/main/java/com/bobocode/stack/README.md new file mode 100644 index 00000000..e9e448bc --- /dev/null +++ b/6-0-test-driven-development/src/main/java/com/bobocode/stack/README.md @@ -0,0 +1,21 @@ +# Stack exercise :muscle: +Improve your TDD skill implementing Stack + +### Task +**Stack** is last in, first out (LIFO) collection of elements. Your job is to implement the interface `Stack` + using TDD discipline + +### Pre-conditions :heavy_exclamation_mark: +You're supposed to know [The Three laws of TDD](https://github.com/bobocode-projects/java-fundamentals-course#the-three-laws-of-tdd) relink, + be familiar with Stack data structure, and be able to write Java code + +### How to start :question: +* Just clone the repository and start implementing `Stack` interface following *three laws of TDD* +* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source) +* Don't worry if you got stuck, checkout the [exercise/completed](https://github.com/bobocode-projects/java-fundamentals-course/tree/exercise/completed) branch and see the final implementation + +### Related materials :information_source: + * [Як виробити звичку писати тести? (Bobocode channel )](https://youtu.be/L_CiX9C51BI) + * [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) + * [The Three Laws of TDD](https://www.youtube.com/watch?v=qkblc5WRn-U&t=3476s) + diff --git a/6-0-test-driven-development/src/main/java/com/bobocode/stack/Stack.java b/6-0-test-driven-development/src/main/java/com/bobocode/stack/Stack.java new file mode 100644 index 00000000..773e705b --- /dev/null +++ b/6-0-test-driven-development/src/main/java/com/bobocode/stack/Stack.java @@ -0,0 +1,35 @@ +package com.bobocode.stack; + +/** + * + * Stack is a data structure that follows "last in, first out" rule (LIFO). + */ +public interface Stack { + /** + * Adds an element to the begining of the stack. + * + * @param element the element to add + */ + void push(T element); + + /** + * Retrieves and removes stack head. + * + * @return an element that was retrieved from the head or null if stack is empty + */ + T pop(); + + /** + * Returns a size of the stack. + * + * @return an integer value that is a size of stack + */ + int size(); + + /** + * Checks if the stack is empty. + * + * @return {@code true} if the stack is empty, returns {@code false} if it's not + */ + boolean isEmpty(); +} diff --git a/6-0-test-driven-development/src/test/java/com/bobocode/stack/StackTest.java b/6-0-test-driven-development/src/test/java/com/bobocode/stack/StackTest.java new file mode 100644 index 00000000..ca2774b0 --- /dev/null +++ b/6-0-test-driven-development/src/test/java/com/bobocode/stack/StackTest.java @@ -0,0 +1,4 @@ +package com.bobocode.stack; + +public class StackTest { +} diff --git a/pom.xml b/pom.xml index ff1d087e..43c0946f 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ 4-0-object-oriented-programming 5-0-functional-programming java-fundamentals-util + 6-0-test-driven-development @@ -31,6 +32,23 @@ junit-jupiter-engine 5.7.0 + + org.hamcrest + hamcrest-library + 1.3 + test + + + org.assertj + assertj-core + 3.18.1 + test + + + org.hamcrest + hamcrest-library + 1.3 + org.projectlombok lombok @@ -55,5 +73,4 @@ - \ No newline at end of file