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
56 changes: 38 additions & 18 deletions crazy-optionals/src/main/java/com/bobocode/CrazyOptionals.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -24,7 +23,7 @@ public class CrazyOptionals {
* @return optional object that holds text
*/
public static Optional<String> optionalOfString(@Nullable String text) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
return Optional.ofNullable(text);
}

/**
Expand All @@ -34,7 +33,10 @@ public static Optional<String> 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));
});
}

/**
Expand All @@ -44,7 +46,7 @@ public static void deposit(AccountProvider accountProvider, BigDecimal amount) {
* @return optional object that holds account
*/
public static Optional<Account> optionalOfAccount(@Nonnull Account account) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
return Optional.of(account);
}

/**
Expand All @@ -56,7 +58,7 @@ public static Optional<Account> 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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -88,7 +92,8 @@ public static Account getOrGenerateAccount(AccountProvider accountProvider) {
* @return optional balance
*/
public static Optional<BigDecimal> retrieveBalance(AccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
return accountProvider.getAccount()
.map(Account::getBalance);
}

/**
Expand All @@ -99,7 +104,7 @@ public static Optional<BigDecimal> 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!"));
}

/**
Expand All @@ -109,7 +114,8 @@ public static Account getAccount(AccountProvider accountProvider) {
* @return optional credit balance
*/
public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider accountProvider) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
return accountProvider.getAccount()
.flatMap(CreditAccount::getCreditBalance);
}


Expand All @@ -121,7 +127,8 @@ public static Optional<BigDecimal> retrieveCreditBalance(CreditAccountProvider a
* @return optional gmail account
*/
public static Optional<Account> 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"));
}

/**
Expand All @@ -134,7 +141,7 @@ public static Optional<Account> 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);
}

/**
Expand All @@ -145,7 +152,9 @@ public static Account getAccountWithFallback(AccountProvider accountProvider, Ac
* @return account with the highest balance
*/
public static Account getAccountWithMaxBalance(List<Account> 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);
}

/**
Expand All @@ -155,7 +164,11 @@ public static Account getAccountWithMaxBalance(List<Account> accounts) {
* @return the lowest balance values
*/
public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
throw new UnsupportedOperationException("Some people say that method does not work until you implement it");
Optional<Account> account = accounts.stream()
.min(Comparator.comparing(Account::getBalance));

return account.isPresent() ? OptionalDouble.of(account.get().getBalance().doubleValue()) : OptionalDouble.empty();

}

/**
Expand All @@ -165,7 +178,9 @@ public static OptionalDouble findMinBalanceValue(List<Account> accounts) {
* @param accountService
*/
public static void processAccountWithMaxBalance(List<Account> 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);
}

/**
Expand All @@ -175,7 +190,12 @@ public static void processAccountWithMaxBalance(List<Account> accounts, AccountS
* @return total credit balance
*/
public static double calculateTotalCreditBalance(List<CreditAccount> 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();

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
29 changes: 28 additions & 1 deletion lambda-math-functions/src/main/java/com.bobocode/Functions.java
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -14,8 +16,33 @@ public class Functions {
public static FunctionMap<Integer, Integer> intFunctionMap() {
FunctionMap<Integer, Integer> 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;
}
}
Loading