├── .gitignore
├── README.md
├── account-data
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── bobocode
│ ├── data
│ └── Accounts.java
│ └── model
│ ├── Account.java
│ ├── CreditAccount.java
│ └── Sex.java
├── crazy-lambdas
├── README.md
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ └── CrazyLambdas.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── CrazyLambdasTest.java
├── crazy-optionals
├── README.md
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ ├── CrazyOptionals.java
│ │ ├── exception
│ │ └── AccountNotFoundException.java
│ │ └── function
│ │ ├── AccountProvider.java
│ │ ├── AccountService.java
│ │ └── CreditAccountProvider.java
│ └── test
│ └── java
│ └── com
│ └── CrazyOptionalsTest.java
├── crazy-streams
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com.bobocode
│ │ ├── CrazyStreams.java
│ │ └── exception
│ │ └── EntityNotFoundException.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── CrazyStreamsTest.java
├── declarative-sum-of-squares
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ ├── SumOfSquares.java
│ │ └── exception
│ │ └── InvalidRangeException.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── SumOfSquareTest.java
├── file-reader
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ └── FileReaders.java
│ └── test
│ ├── java
│ └── com
│ │ └── bobocode
│ │ └── FileReadersTest.java
│ └── resources
│ ├── empty.txt
│ ├── lines.txt
│ └── simple.txt
├── file-stats
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ ├── FileStats.java
│ │ └── FileStatsException.java
│ └── test
│ ├── java
│ └── com
│ │ └── bobocode
│ │ └── FileStatsTest.java
│ └── resources
│ ├── scosb.txt
│ └── sotl.txt
├── lambda-math-functions
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com.bobocode
│ │ ├── FunctionMap.java
│ │ ├── Functions.java
│ │ └── InvalidFunctionNameException.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── FunctionsTest.java
├── linked-list
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ ├── LinkedList.java
│ │ └── List.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── LinkedListTest.java
├── linked-queue
├── README.MD
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── bobocode
│ │ ├── LinkedQueue.java
│ │ └── Queue.java
│ └── test
│ └── java
│ └── com
│ └── bobocode
│ └── QueueTest.java
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | **/*.iml
3 | **/target
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #
Java Core exercises
2 | The list of exercises dedicated to training your Java SE skills
3 |
4 | ### No pain, No gain :heavy_exclamation_mark:
5 |
6 | > Skill is only developed by hours and hours and hours of beating on your craft
7 |
8 | Working on real problems, you're focused on finding a solution. Learning new things, you're trying to understand how it works.
9 | It is important to have a different type of activities, which purpose is improving your skill
10 |
11 | ***An exercise** is a predefined task that you continuously implement to improve a certain skill* :muscle:
12 | ##
13 | * [Linked queue](https://github.com/bobocode-projects/java-core-exercises/tree/master/linked-queue)
14 | * [Linked List](https://github.com/bobocode-projects/java-core-exercises/tree/master/linked-list)
15 | * [File Stats](https://github.com/bobocode-projects/java-core-exercises/tree/master/file-stats)
16 | * [File Reader](https://github.com/bobocode-projects/java-core-exercises/tree/master/file-reader)
17 |
18 | ### Related information
19 | * [Java: The Complete Reference, 9th Edition](https://www.amazon.com/Java-Complete-Reference-Herbert-Schildt/dp/0071808558/ref=sr_1_1?ie=UTF8&qid=1540376597&sr=8-1&keywords=java+complete+reference+9th+edition) :orange_book:
20 | * **Classes**
21 | * Nested and Inner classes - **pp. 149 - 151**
22 |
23 | * **Generics**
24 | * Bounded types - **pp. 346 - 349**
25 | * Wildcards - **pp. 349 - 356**
26 | * Type erasure - **p. 373**
27 | * Generic method - **pp. 356 - 359**
28 | * Generic interfaces - **pp. 360 - 362** :heavy_exclamation_mark:
29 | * Restrictions on static members - **p. 377** :heavy_exclamation_mark:
30 |
--------------------------------------------------------------------------------
/account-data/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | java-core-exercises
7 | com.bobocode
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | account-data
13 |
14 |
15 |
16 | io.codearte.jfairy
17 | jfairy
18 | 0.5.9
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/account-data/src/main/java/com/bobocode/data/Accounts.java:
--------------------------------------------------------------------------------
1 | package com.bobocode.data;
2 |
3 | import com.bobocode.model.Account;
4 | import com.bobocode.model.CreditAccount;
5 | import com.bobocode.model.Sex;
6 | import io.codearte.jfairy.Fairy;
7 | import io.codearte.jfairy.producer.person.Person;
8 |
9 | import java.math.BigDecimal;
10 | import java.time.LocalDate;
11 | import java.util.List;
12 | import java.util.concurrent.ThreadLocalRandom;
13 | import java.util.stream.Stream;
14 |
15 | import static java.util.stream.Collectors.toList;
16 |
17 | public class Accounts {
18 | public static int MAX_BALANCE_VALUE = 200_000;
19 |
20 | public static Account generateAccount() {
21 | Person person = generatePerson();
22 | Account account = convertToAccount(person);
23 | fillCommonRandomFields(account);
24 | return account;
25 | }
26 |
27 | public static CreditAccount generateCreditAccount() {
28 | Person person = generatePerson();
29 | CreditAccount account = convertToCreditAccount(person);
30 | fillCommonRandomFields(account);
31 | account.setCreditBalance(randomBigDecimal(MAX_BALANCE_VALUE));
32 | return account;
33 | }
34 |
35 | public static List generateAccountList(int size) {
36 | return Stream.generate(Accounts::generateAccount)
37 | .limit(size)
38 | .collect(toList());
39 | }
40 |
41 | public static List generateCreditAccountList(int size) {
42 | return Stream.generate(Accounts::generateCreditAccount)
43 | .limit(size)
44 | .collect(toList());
45 | }
46 |
47 | private static Person generatePerson() {
48 | Fairy fairy = Fairy.create();
49 | return fairy.person();
50 | }
51 |
52 | private static Account convertToAccount(Person person) {
53 | Account account = new Account();
54 | fillAccount(account, person);
55 | return account;
56 | }
57 |
58 | private static CreditAccount convertToCreditAccount(Person person) {
59 | CreditAccount account = new CreditAccount();
60 | fillAccount(account, person);
61 | return account;
62 | }
63 |
64 | private static void fillAccount(Account account, Person person) {
65 | account.setFirstName(person.getFirstName());
66 | account.setLastName(person.getLastName());
67 | account.setEmail(person.getEmail());
68 | account.setBirthday(LocalDate.of(
69 | person.getDateOfBirth().getYear(),
70 | person.getDateOfBirth().getMonthOfYear(),
71 | person.getDateOfBirth().getDayOfMonth()));
72 | account.setSex(Sex.valueOf(person.getSex().name()));
73 | }
74 |
75 | private static void fillCommonRandomFields(Account account) {
76 | BigDecimal balance = randomBigDecimal(MAX_BALANCE_VALUE);
77 | account.setBalance(balance);
78 | account.setCreationDate(LocalDate.now());
79 | }
80 |
81 | private static BigDecimal randomBigDecimal(int max) {
82 | return BigDecimal.valueOf(ThreadLocalRandom.current().nextInt(max));
83 | }
84 | }
85 |
86 |
--------------------------------------------------------------------------------
/account-data/src/main/java/com/bobocode/model/Account.java:
--------------------------------------------------------------------------------
1 | package com.bobocode.model;
2 |
3 | import lombok.*;
4 |
5 | import java.math.BigDecimal;
6 | import java.time.LocalDate;
7 | import java.time.LocalDateTime;
8 |
9 | @NoArgsConstructor
10 | @AllArgsConstructor(access = AccessLevel.PUBLIC)
11 | @Getter
12 | @Setter
13 | @ToString
14 | @EqualsAndHashCode(of = "email")
15 | public class Account {
16 | private Long id;
17 | private String firstName;
18 | private String lastName;
19 | private String email;
20 | private LocalDate birthday;
21 | private Sex sex;
22 | private LocalDate creationDate;
23 | private BigDecimal balance = BigDecimal.ZERO;
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/account-data/src/main/java/com/bobocode/model/CreditAccount.java:
--------------------------------------------------------------------------------
1 | package com.bobocode.model;
2 |
3 | import lombok.AccessLevel;
4 | import lombok.AllArgsConstructor;
5 | import lombok.NoArgsConstructor;
6 | import lombok.Setter;
7 |
8 | import java.math.BigDecimal;
9 | import java.util.Optional;
10 |
11 | @NoArgsConstructor
12 | @AllArgsConstructor(access = AccessLevel.PUBLIC)
13 | @Setter
14 | public class CreditAccount extends Account {
15 | private BigDecimal creditBalance;
16 |
17 | public Optional getCreditBalance() {
18 | return Optional.ofNullable(creditBalance);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/account-data/src/main/java/com/bobocode/model/Sex.java:
--------------------------------------------------------------------------------
1 | package com.bobocode.model;
2 |
3 | public enum Sex {
4 | MALE,
5 | FEMALE
6 | }
7 |
--------------------------------------------------------------------------------
/crazy-lambdas/README.md:
--------------------------------------------------------------------------------
1 | #
Crazy lambda exercise :muscle:
2 | Improve your lambda skills
3 | ### Task
4 | `CrazyLambdas` class consists of static methods that return various functions, operations and predicates.
5 | Your job is to implement the *todo* section of that class using **Lambda expressions** and **method reference**.
6 | To verify your implementation, run `CrazyLambdasTest.java`
7 |
8 | ### Pre-conditions :heavy_exclamation_mark:
9 | You're supposed to be familiar with Java 8
10 |
11 | ### How to start :question:
12 | * Just clone the repository and start implementing the **todo** section, verify your changes by running tests
13 | * If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
14 | * Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
15 |
16 | ### Related materials :information_source:
17 | * [Lambda tutorial](https://github.com/bobocode-projects/java-8-tutorial/tree/master/lambdas)
18 | * [State of lambda (JSR 335)](http://htmlpreview.github.io/?https://github.com/bobocode-projects/resources/blob/master/java8/lambda/sotl.html)
19 |
20 |
--------------------------------------------------------------------------------
/crazy-lambdas/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | java-core-exercises
7 | com.bobocode
8 | 1.0-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | crazy-lambdas
13 |
14 |
15 |
--------------------------------------------------------------------------------
/crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java:
--------------------------------------------------------------------------------
1 | package com.bobocode;
2 |
3 | import java.math.BigDecimal;
4 | import java.util.Map;
5 | import java.util.function.*;
6 |
7 | public class CrazyLambdas {
8 |
9 | /**
10 | * Returns {@link Supplier} that always supply "Hello"
11 | *
12 | * @return a string supplier
13 | */
14 | public static Supplier helloSupplier() {
15 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
16 | }
17 |
18 | /**
19 | * Returns a {@link Predicate} of string that checks if string is empty
20 | *
21 | * @return a string predicate
22 | */
23 | public static Predicate isEmptyPredicate() {
24 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
25 | }
26 |
27 | /**
28 | * Return a {@link Function} that accepts {@link String} and returns that string repeated n time, where n is passed
29 | * as function argument
30 | *
31 | * @return function that repeats Strings
32 | */
33 | public static BiFunction stringMultiplier() {
34 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
35 | }
36 |
37 | /**
38 | * Returns a {@link Function} that converts a {@link BigDecimal} number into a {@link String} that start with
39 | * a dollar sign and then gets a value
40 | *
41 | * @return function that converts adds dollar sign
42 | */
43 | public static Function toDollarStringFunction() {
44 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
45 | }
46 |
47 | /**
48 | * Receives two parameter that represent a range and returns a {@link Predicate} that verifies if string
49 | * length is in the specified range. E.g. min <= length < max
50 | *
51 | * @param min min length
52 | * @param max max length
53 | * @return a string predicate
54 | */
55 | public static Predicate lengthInRangePredicate(int min, int max) {
56 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
57 | }
58 |
59 | /**
60 | * Returns a {@link Supplier} of random integers
61 | *
62 | * @return int supplier
63 | */
64 | public static IntSupplier randomIntSupplier() {
65 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
66 | }
67 |
68 |
69 | /**
70 | * Returns an {@link IntUnaryOperator} that receives an int as a bound parameter, and returns a random int
71 | *
72 | * @return int operation
73 | */
74 | public static IntUnaryOperator boundedRandomIntSupplier() {
75 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
76 | }
77 |
78 | /**
79 | * Returns {@link IntUnaryOperator} that calculates an integer square
80 | *
81 | * @return square operation
82 | */
83 | public static IntUnaryOperator intSquareOperation() {
84 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
85 | }
86 |
87 | /**
88 | * Returns a {@link LongBinaryOperator} sum operation.
89 | *
90 | * @return binary sum operation
91 | */
92 | public static LongBinaryOperator longSumOperation() {
93 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
94 | }
95 |
96 | /**
97 | * Returns a {@link ToIntFunction} that converts string to integer.
98 | *
99 | * @return string to int converter
100 | */
101 | public static ToIntFunction stringToIntConverter() {
102 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
103 | }
104 |
105 | /**
106 | * Receives int parameter n, and returns a {@link Supplier} that supplies {@link IntUnaryOperator}
107 | * that is a function f(x) = n * x
108 | *
109 | * @param n a multiplier
110 | * @return a function supplier
111 | */
112 | public static Supplier nMultiplyFunctionSupplier(int n) {
113 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
114 | }
115 |
116 | /**
117 | * Returns a {@link UnaryOperator} that accepts str to str function and returns the same function composed with trim
118 | *
119 | * @return function that composes functions with trim() function
120 | */
121 | public static UnaryOperator> composeWithTrimFunction() {
122 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
123 | }
124 |
125 | /**
126 | * Receives a {@link Runnable} parameter, and returns a {@link Supplier}. The thread will be started only
127 | * when you call supplier method {@link Supplier#get()}
128 | *
129 | * @param runnable the code you want to tun in new thread
130 | * @return a thread supplier
131 | */
132 | public static Supplier runningThreadSupplier(Runnable runnable) {
133 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
134 | }
135 |
136 | /**
137 | * Returns a {@link Consumer} that accepts {@link Runnable} as a parameter and runs in in a new thread.
138 | *
139 | * @return a runnable consumer
140 | */
141 | public static Consumer newThreadRunnableConsumer() {
142 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
143 | }
144 |
145 | /**
146 | * Returns a {@link Function} that accepts an instance of {@link Runnable} and returns a {@link Supplier} of a
147 | * started {@link Thread} that is created from a given {@link Runnable}
148 | *
149 | * @return a function that transforms runnable into a thread supplier
150 | */
151 | public static Function> runnableToThreadSupplierFunction() {
152 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
153 | }
154 |
155 | /**
156 | * Returns a {@link BiFunction} that has two parameters. First is {@link IntUnaryOperator} which is some integer function.
157 | * Second is {@link IntPredicate} which is some integer condition. And the third is {@link IntUnaryOperator} which is
158 | * a new composed function that uses provided predicate (second parameter of binary function) to verify its input
159 | * parameter. If predicate returns {@code true} it applies a provided integer function
160 | * (first parameter of binary function) and returns a result value, otherwise it returns an element itself.
161 | *
162 | * @return a binary function that receiver predicate and function and compose them to create a new function
163 | */
164 | public static BiFunction functionToConditionalFunction() {
165 | throw new UnsupportedOperationException("It's your job to implement this method"); // todo
166 | }
167 |
168 | /**
169 | * Returns a {@link BiFunction} which first parameter is a {@link Map} where key is a function name, and value is some
170 | * {@link IntUnaryOperator}, and second parameter is a {@link String} which is a function name. If the map contains a
171 | * function by a given name then it is returned by high order function otherwise an identity() is returned.
172 | *
173 | * @return a high-order function that fetches a function from a function map by a given name or returns identity()
174 | */
175 | public static BiFunction