Skip to content

Commit

Permalink
Merge pull request bobocode-projects#19 from bobocode-projects/GP-32_…
Browse files Browse the repository at this point in the history
…Merge_Stack_into_main

Gp 32 merge stack into main
  • Loading branch information
tboychuk authored Jan 13, 2021
2 parents 0847f6c + 0512e6c commit dd5a328
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<T>}.
*
* @param <T> generic type parameter
*/
public class LinkedStack<T> implements Stack<T> {

/**
* This method creates a stack of provided elements
*
* @param elements elements to add
* @param <T> generic type
* @return a new stack of elements that were passed as method parameters
*/
public static <T> LinkedStack<T> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>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<T>` 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))

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bobocode.stack;

public interface Stack<T> {

void push(T element);

T pop();

int size();

boolean isEmpty();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bobocode.stack.exception;

public class EmptyStackException extends RuntimeException{

}
Original file line number Diff line number Diff line change
@@ -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<Integer> 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);
}
}
25 changes: 25 additions & 0 deletions 6-0-test-driven-development/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>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


19 changes: 19 additions & 0 deletions 6-0-test-driven-development/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-fundamentals-course</artifactId>
<groupId>com.bobocode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>6-0-test-driven-development</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>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 <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=20/>)](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)

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bobocode.stack;

/**
*
* Stack is a data structure that follows "last in, first out" rule (LIFO).
*/
public interface Stack<T> {
/**
* 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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.bobocode.stack;

public class StackTest {
}
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>4-0-object-oriented-programming</module>
<module>5-0-functional-programming</module>
<module>java-fundamentals-util</module>
<module>6-0-test-driven-development</module>
</modules>

<dependencies>
Expand All @@ -31,6 +32,23 @@
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -55,5 +73,4 @@
</dependency>
</dependencies>


</project>

0 comments on commit dd5a328

Please sign in to comment.