From 3990402294a59e6dc1e0401f0bca75a40427ac29 Mon Sep 17 00:00:00 2001 From: Gennady Date: Sat, 31 Aug 2019 08:04:05 +0400 Subject: [PATCH 1/6] feat(ll): Add linked list first solution --- .../main/java/com/bobocode/LinkedList.java | 113 ++++++++++++++++-- 1 file changed, 103 insertions(+), 10 deletions(-) diff --git a/linked-list/src/main/java/com/bobocode/LinkedList.java b/linked-list/src/main/java/com/bobocode/LinkedList.java index bd6176a..595ed18 100644 --- a/linked-list/src/main/java/com/bobocode/LinkedList.java +++ b/linked-list/src/main/java/com/bobocode/LinkedList.java @@ -1,5 +1,9 @@ package com.bobocode; +import org.junit.platform.engine.support.hierarchical.Node; + +import java.util.Arrays; + /** * {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as * inner static class {@link Node}. In order to keep track on nodes, {@link LinkedList} keeps a reference to a head node. @@ -7,6 +11,9 @@ * @param generic type parameter */ public class LinkedList implements List { + private Node head; + private Node lastNode; + private int size = 0; /** * This method creates a list of provided elements @@ -16,7 +23,10 @@ public class LinkedList implements List { * @return a new list of elements the were passed as method parameters */ public static List of(T... elements) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + LinkedList linkedList = new LinkedList(); + Arrays.stream(elements) + .forEach(linkedList::add); + return linkedList; } /** @@ -26,7 +36,17 @@ public static List of(T... elements) { */ @Override public void add(T element) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (head == null) { + head = new Node(element); + head.setNext(head); + lastNode = head; + } else { + Node newNode = new Node(element); + newNode.setNext(head); + lastNode.setNext(newNode); + lastNode = newNode; + } + size++; } /** @@ -38,7 +58,23 @@ public void add(T element) { */ @Override public void add(int index, T element) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (index == 0) { + if (head == null) { + add(element); + } else { + Node newNode = new Node<>(element); + newNode.setNext(head); + head = newNode; + size++; + } + } else { + Node node = findNode(index - 1); + Node next = node.getNext(); + Node newNode = new Node<>(element); + node.setNext(newNode); + newNode.setNext(next); + size++; + } } /** @@ -50,7 +86,8 @@ public void add(int index, T element) { */ @Override public void set(int index, T element) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + Node node = findNode(index); + node.setValue(element); } /** @@ -62,7 +99,7 @@ public void set(int index, T element) { */ @Override public T get(int index) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + return findNode(index).getValue(); } /** @@ -73,7 +110,15 @@ public T get(int index) { */ @Override public void remove(int index) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (index == 0) { + head = findNode(1); + size--; + return; + } + Node node = findNode(index); + Node previousNode = findNode(index - 1); + previousNode.setNext(node.getNext()); + size--; } @@ -84,7 +129,17 @@ public void remove(int index) { */ @Override public boolean contains(T element) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (isEmpty()) { + return false; + } + Node node = head; + while(node.getNext() != head) { + if (node.getValue() == element) { + return true; + } + node = node.getNext(); + } + return false; } /** @@ -94,7 +149,7 @@ public boolean contains(T element) { */ @Override public boolean isEmpty() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + return size == 0; } /** @@ -104,7 +159,19 @@ public boolean isEmpty() { */ @Override public int size() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + return size; + } + + private Node findNode(int index) { + if (index < 0 || index > size - 1 || isEmpty()) { + throw new IndexOutOfBoundsException(); + } + Node node = head; + while(index > 0) { + index--; + node = node.getNext(); + } + return node; } /** @@ -112,6 +179,32 @@ public int size() { */ @Override public void clear() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + head = null; + size = 0; + } + + private class Node { + private T value; + private Node next; + + public Node(T value) { + this.value = value; + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } } } From 4a37fc44759cfa3b10bb47da2e15c16b0d96680d Mon Sep 17 00:00:00 2001 From: Gennady Date: Sat, 31 Aug 2019 10:07:17 +0400 Subject: [PATCH 2/6] feat(ll): Refactor LinkedList class --- .../main/java/com/bobocode/LinkedList.java | 112 ++++++++---------- 1 file changed, 52 insertions(+), 60 deletions(-) diff --git a/linked-list/src/main/java/com/bobocode/LinkedList.java b/linked-list/src/main/java/com/bobocode/LinkedList.java index 595ed18..ec27d82 100644 --- a/linked-list/src/main/java/com/bobocode/LinkedList.java +++ b/linked-list/src/main/java/com/bobocode/LinkedList.java @@ -1,7 +1,5 @@ package com.bobocode; -import org.junit.platform.engine.support.hierarchical.Node; - import java.util.Arrays; /** @@ -12,7 +10,6 @@ */ public class LinkedList implements List { private Node head; - private Node lastNode; private int size = 0; /** @@ -22,8 +19,9 @@ public class LinkedList implements List { * @param generic type * @return a new list of elements the were passed as method parameters */ - public static List of(T... elements) { - LinkedList linkedList = new LinkedList(); + @SafeVarargs + static List of(T... elements) { + List linkedList = new LinkedList<>(); Arrays.stream(elements) .forEach(linkedList::add); return linkedList; @@ -36,17 +34,14 @@ public static List of(T... elements) { */ @Override public void add(T element) { - if (head == null) { - head = new Node(element); - head.setNext(head); - lastNode = head; + if (isEmpty()) { + head = new Node<>(element); + head.next = head; + head.prev = head; + size++; } else { - Node newNode = new Node(element); - newNode.setNext(head); - lastNode.setNext(newNode); - lastNode = newNode; + insertNode(element, head); } - size++; } /** @@ -59,21 +54,13 @@ public void add(T element) { @Override public void add(int index, T element) { if (index == 0) { - if (head == null) { + if (isEmpty()) { add(element); } else { - Node newNode = new Node<>(element); - newNode.setNext(head); - head = newNode; - size++; + head = insertNode(element, head); } } else { - Node node = findNode(index - 1); - Node next = node.getNext(); - Node newNode = new Node<>(element); - node.setNext(newNode); - newNode.setNext(next); - size++; + insertNode(element, findNode(index - 1).next); } } @@ -87,7 +74,7 @@ public void add(int index, T element) { @Override public void set(int index, T element) { Node node = findNode(index); - node.setValue(element); + node.value = element; } /** @@ -99,7 +86,7 @@ public void set(int index, T element) { */ @Override public T get(int index) { - return findNode(index).getValue(); + return findNode(index).value; } /** @@ -111,13 +98,18 @@ public T get(int index) { @Override public void remove(int index) { if (index == 0) { - head = findNode(1); + Node newHead = head.next; + newHead.prev = head.prev; + head.prev.next = newHead; + head = newHead; size--; return; } Node node = findNode(index); - Node previousNode = findNode(index - 1); - previousNode.setNext(node.getNext()); + Node previousNode = node.prev; + + previousNode.next = node.next; + node.next.prev = previousNode; size--; } @@ -133,11 +125,11 @@ public boolean contains(T element) { return false; } Node node = head; - while(node.getNext() != head) { - if (node.getValue() == element) { + while(node.next != head) { + if (node.value == element) { return true; } - node = node.getNext(); + node = node.next; } return false; } @@ -162,49 +154,49 @@ public int size() { return size; } + /** + * Removes all list elements + */ + @Override + public void clear() { + head = null; + size = 0; + } + private Node findNode(int index) { - if (index < 0 || index > size - 1 || isEmpty()) { + if (size - 1 < index || index < 0 || isEmpty()) { throw new IndexOutOfBoundsException(); } Node node = head; while(index > 0) { index--; - node = node.getNext(); + node = node.next; } return node; } - /** - * Removes all list elements - */ - @Override - public void clear() { - head = null; - size = 0; - } + private Node insertNode(T element, Node nextNode) { + Node newNode = new Node<>(element); + Node prevNode = nextNode.prev; - private class Node { - private T value; - private Node next; + newNode.prev = prevNode; + newNode.next = nextNode; - public Node(T value) { - this.value = value; - } + prevNode.next = newNode; + nextNode.prev = newNode; - public T getValue() { - return value; - } + size++; - public void setValue(T value) { - this.value = value; - } + return newNode; + } - public Node getNext() { - return next; - } + private class Node { + private V value; + private Node next; + private Node prev; - public void setNext(Node next) { - this.next = next; + Node(V value) { + this.value = value; } } } From a545c0582dcfdb7a4f5e86c978157e0208c077d0 Mon Sep 17 00:00:00 2001 From: Gennady Date: Sat, 31 Aug 2019 10:21:38 +0400 Subject: [PATCH 3/6] feat(lq): Add implements LinkedQueue --- .../main/java/com/bobocode/LinkedQueue.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/linked-queue/src/main/java/com/bobocode/LinkedQueue.java b/linked-queue/src/main/java/com/bobocode/LinkedQueue.java index 3cde892..8ed94c0 100644 --- a/linked-queue/src/main/java/com/bobocode/LinkedQueue.java +++ b/linked-queue/src/main/java/com/bobocode/LinkedQueue.java @@ -8,6 +8,9 @@ * @param a generic parameter */ public class LinkedQueue implements Queue { + private int size = 0; + private Node head; + private Node tale; /** * Adds an element to the end of the queue. @@ -15,7 +18,14 @@ public class LinkedQueue implements Queue { * @param element the element to add */ public void add(T element) { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (isEmpty()) { + head = new Node<>(element); + tale = head; + } else { + tale.next = new Node<>(element); + tale = tale.next; + } + size++; } /** @@ -24,7 +34,13 @@ public void add(T element) { * @return an element that was retrieved from the head or null if queue is empty */ public T poll() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + if (isEmpty()) { + return null; + } + T value = head.value; + head = head.next; + size--; + return value; } /** @@ -33,7 +49,7 @@ public T poll() { * @return an integer value that is a size of queue */ public int size() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + return size; } /** @@ -42,6 +58,15 @@ public int size() { * @return {@code true} if the queue is empty, returns {@code false} if it's not */ public boolean isEmpty() { - throw new UnsupportedOperationException("This method is not implemented yet"); // todo: implement this method + return size == 0; + } + + private class Node { + private Node next; + private V value; + + Node(V value) { + this.value = value; + } } } From 1d1b4f0f297a54b1b856ccb490180a98b245e494 Mon Sep 17 00:00:00 2001 From: Gennady Date: Sat, 31 Aug 2019 10:30:51 +0400 Subject: [PATCH 4/6] feat(lmf): Add implements Lambda Math func --- .../src/main/java/com.bobocode/Functions.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lambda-math-functions/src/main/java/com.bobocode/Functions.java b/lambda-math-functions/src/main/java/com.bobocode/Functions.java index 80ebc20..09d5076 100644 --- a/lambda-math-functions/src/main/java/com.bobocode/Functions.java +++ b/lambda-math-functions/src/main/java/com.bobocode/Functions.java @@ -1,5 +1,7 @@ package com.bobocode; +import java.util.function.Function; + public class Functions { /** * A static factory method that creates an integer function map with basic functions: @@ -14,8 +16,33 @@ public class Functions { public static FunctionMap intFunctionMap() { FunctionMap intFunctionMap = new FunctionMap<>(); - // todo: add simple functions to the function map (abs, sng, increment, decrement, square) + intFunctionMap.addFunction("abs", Functions::abs); + intFunctionMap.addFunction("sgn", Functions::sgn); + intFunctionMap.addFunction("increment", Functions::increment); + intFunctionMap.addFunction("decrement", Functions::decrement); + intFunctionMap.addFunction("square", Functions::square); return intFunctionMap; } + + private static Integer abs(int number) { + return number < 0 ? number * -1 : number; + } + + private static Integer sgn(int number) { + if (number == 0) return 0; + return number < 0 ? -1 : 1; + } + + private static Integer increment(int number) { + return number + 1; + } + + private static Integer decrement(int number) { + return number - 1; + } + + private static Integer square(int number) { + return number * number; + } } From 931828e72d0e27b2843ad036a375f76cbc9933e3 Mon Sep 17 00:00:00 2001 From: Gennady Date: Sat, 31 Aug 2019 10:43:32 +0400 Subject: [PATCH 5/6] feat(lmf): Add implements Sum of squares --- .../src/main/java/com/bobocode/SumOfSquares.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/declarative-sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java b/declarative-sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java index fc0dfde..8b86c3a 100644 --- a/declarative-sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java +++ b/declarative-sum-of-squares/src/main/java/com/bobocode/SumOfSquares.java @@ -3,6 +3,7 @@ import com.bobocode.exception.InvalidRangeException; +import java.util.stream.IntStream; /** * This class allow to calculate a sum of squares of integer number in a certain range. It was implemented using @@ -25,11 +26,8 @@ static int calculateSumOfSquaresInRange(int startInclusive, int endInclusive) { throw new InvalidRangeException(); } - // todo: refactor using functional approach - int sumOfSquares = 0; - for (int i = startInclusive; i <= endInclusive; i++) { - sumOfSquares += i * i; - } - return sumOfSquares; + return IntStream.range(startInclusive, endInclusive + 1) + .map(i -> i * i) + .sum(); } } From 345cc3dfdb65a3b61ad9117c8b995d1847d34d1d Mon Sep 17 00:00:00 2001 From: Gennady Date: Wed, 4 Sep 2019 21:34:45 +0400 Subject: [PATCH 6/6] feat(co): Add implements CrazyOptional --- .../java/com/bobocode/CrazyOptionals.java | 56 +++++++++++++------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java b/crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java index d81fe02..8522fb1 100644 --- a/crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java +++ b/crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java @@ -11,9 +11,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.math.BigDecimal; -import java.util.List; -import java.util.Optional; -import java.util.OptionalDouble; +import java.util.*; +import java.util.stream.Collectors; public class CrazyOptionals { @@ -24,7 +23,7 @@ public class CrazyOptionals { * @return optional object that holds text */ public static Optional optionalOfString(@Nullable String text) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return Optional.ofNullable(text); } /** @@ -34,7 +33,10 @@ public static Optional optionalOfString(@Nullable String text) { * @param amount money to deposit */ public static void deposit(AccountProvider accountProvider, BigDecimal amount) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + accountProvider.getAccount() + .ifPresent(account -> { + account.setBalance(account.getBalance().add(amount)); + }); } /** @@ -44,7 +46,7 @@ public static void deposit(AccountProvider accountProvider, BigDecimal amount) { * @return optional object that holds account */ public static Optional optionalOfAccount(@Nonnull Account account) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return Optional.of(account); } /** @@ -56,7 +58,7 @@ public static Optional optionalOfAccount(@Nonnull Account account) { * @return account from provider or defaultAccount */ public static Account getAccount(AccountProvider accountProvider, Account defaultAccount) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount().orElse(defaultAccount); } /** @@ -67,7 +69,8 @@ public static Account getAccount(AccountProvider accountProvider, Account defaul * @param accountService */ public static void processAccount(AccountProvider accountProvider, AccountService accountService) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + accountProvider.getAccount() + .ifPresentOrElse(accountService::processAccount, accountService::processWithNoAccount); } /** @@ -78,7 +81,8 @@ public static void processAccount(AccountProvider accountProvider, AccountServic * @return provided or generated account */ public static Account getOrGenerateAccount(AccountProvider accountProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount() + .orElseGet(Accounts::generateAccount); } /** @@ -88,7 +92,8 @@ public static Account getOrGenerateAccount(AccountProvider accountProvider) { * @return optional balance */ public static Optional retrieveBalance(AccountProvider accountProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount() + .map(Account::getBalance); } /** @@ -99,7 +104,7 @@ public static Optional retrieveBalance(AccountProvider accountProvid * @return provided account */ public static Account getAccount(AccountProvider accountProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount().orElseThrow(() -> new AccountNotFoundException("No Account provided!")); } /** @@ -109,7 +114,8 @@ public static Account getAccount(AccountProvider accountProvider) { * @return optional credit balance */ public static Optional retrieveCreditBalance(CreditAccountProvider accountProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount() + .flatMap(CreditAccount::getCreditBalance); } @@ -121,7 +127,8 @@ public static Optional retrieveCreditBalance(CreditAccountProvider a * @return optional gmail account */ public static Optional retrieveAccountGmail(AccountProvider accountProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount() + .filter(account -> account.getEmail().contains("gmail")); } /** @@ -134,7 +141,7 @@ public static Optional retrieveAccountGmail(AccountProvider accountProv * @return account got from either accountProvider or fallbackProvider */ public static Account getAccountWithFallback(AccountProvider accountProvider, AccountProvider fallbackProvider) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accountProvider.getAccount().or(fallbackProvider::getAccount).orElseThrow(NoSuchElementException::new); } /** @@ -145,7 +152,9 @@ public static Account getAccountWithFallback(AccountProvider accountProvider, Ac * @return account with the highest balance */ public static Account getAccountWithMaxBalance(List accounts) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accounts.stream() + .max(Comparator.comparing(Account::getBalance)) + .orElseThrow(NoSuchElementException::new); } /** @@ -155,7 +164,11 @@ public static Account getAccountWithMaxBalance(List accounts) { * @return the lowest balance values */ public static OptionalDouble findMinBalanceValue(List accounts) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + Optional account = accounts.stream() + .min(Comparator.comparing(Account::getBalance)); + + return account.isPresent() ? OptionalDouble.of(account.get().getBalance().doubleValue()) : OptionalDouble.empty(); + } /** @@ -165,7 +178,9 @@ public static OptionalDouble findMinBalanceValue(List accounts) { * @param accountService */ public static void processAccountWithMaxBalance(List accounts, AccountService accountService) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + accounts.stream() + .max(Comparator.comparing(Account::getBalance)) + .ifPresent(accountService::processAccount); } /** @@ -175,7 +190,12 @@ public static void processAccountWithMaxBalance(List accounts, AccountS * @return total credit balance */ public static double calculateTotalCreditBalance(List accounts) { - throw new UnsupportedOperationException("Some people say that method does not work until you implement it"); + return accounts.stream() + .map(CreditAccount::getCreditBalance) + .map(b -> b.orElse(BigDecimal.ZERO).doubleValue()) + .mapToDouble(Double::longValue) + .sum(); + } }