Skip to content

Commit 54f3773

Browse files
committed
Merge branch 'main' into exercise/completed
2 parents 373d7e3 + 496ea59 commit 54f3773

File tree

8 files changed

+204
-6
lines changed

8 files changed

+204
-6
lines changed

2-0-data-structures-and-algorithms/src/main/java/com/bobocode/linked_list/LinkedList.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package com.bobocode.linked_list;
22

33

4-
54
import com.bobocode.util.ExerciseNotCompletedException;
65

7-
import java.util.NoSuchElementException;
8-
96
/**
107
* {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as
11-
* inner static class {@link Node<T>}. In order to keep track on nodes, {@link LinkedList} keeps a reference to a head node.
8+
* inner static class {@link Node<T>}.
129
*
1310
* @param <T> generic type parameter
1411
*/
@@ -26,7 +23,7 @@ public static <T> List<T> of(T... elements) {
2623
}
2724

2825
/**
29-
* Adds an element to the end of the list
26+
* Adds an element to the end of the list.
3027
*
3128
* @param element element to add
3229
*/
@@ -75,7 +72,7 @@ public T get(int index) {
7572
* Returns the first element of the list. Operation is performed in constant time O(1)
7673
*
7774
* @return the first element of the list
78-
* @throws NoSuchElementException if list is empty
75+
* @throws java.util.NoSuchElementException if list is empty
7976
*/
8077
@Override
8178
public T getFirst() {
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Java OOP exercises
2+
The list of exercises dedicated to training your OOP skills in Java
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+
* [Flights search](https://github.com/bobocode-projects/java-oop-exercises/tree/master/flight-search-simple)
14+

4-0-object-oriented-programming/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6+
<dependencies>
7+
<dependency>
8+
<groupId>com.bobocode</groupId>
9+
<artifactId>java-fundamentals-util</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
<scope>compile</scope>
12+
</dependency>
13+
</dependencies>
614

715
<parent>
816
<groupId>com.bobocode</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Flight search exercise :muscle:
2+
Improve your OOP skills
3+
### Task
4+
`FlighService` and package `service` represent a component of the system that holds a business logic. `FlightDao`
5+
and package `data` represent a component of the system that implements data access layer. Your job is to implement
6+
the *todo* section of those classes **following OOP design principles.**
7+
8+
To verify your implementation, run `FlightSeriviceTest.java`
9+
10+
### Pre-conditions :heavy_exclamation_mark:
11+
You're supposed to be familiar with Java and OOP
12+
13+
### How to start :question:
14+
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
15+
* If you don't have enough knowladge about this domain, check out the [links below](#related-materials-information_source)
16+
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation
17+
18+
### Related materials :information_source:
19+
* [SOLID](https://en.wikipedia.org/wiki/SOLID)
20+
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.bobocode.flight_search.data;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/**
9+
* {@link FlightDao} represents a Data Access Object (DAO) for flights. The implementation is simplified, so it just
10+
* uses {@link HashSet} to store flight numbers.
11+
* <p>
12+
* todo: 1. Implement a method {@link FlightDao#register(String)} that store new flight number into the set
13+
* todo: 2. Implement a method {@link FlightDao#findAll()} that returns a set of all flight numbers
14+
*/
15+
public class FlightDao {
16+
private Set<String> flights = new HashSet<>();
17+
18+
/**
19+
* Stores a new flight number
20+
*
21+
* @param flightNumber a flight number to store
22+
* @return {@code true} if a flight number was stored, {@code false} otherwise
23+
*/
24+
public boolean register(String flightNumber) {
25+
throw new ExerciseNotCompletedException();// todo: implement this method
26+
}
27+
28+
/**
29+
* Returns all stored flight numbers
30+
*
31+
* @return a set of flight numbers
32+
*/
33+
public Set<String> findAll() {
34+
throw new ExerciseNotCompletedException();// todo: implement this method
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.bobocode.flight_search.factory;
2+
3+
import com.bobocode.flight_search.service.FlightService;
4+
import com.bobocode.util.ExerciseNotCompletedException;
5+
6+
/**
7+
* {@link FlightServiceFactory} is used to create an instance of {@link FlightService}
8+
* <p>
9+
* todo: 1. Implement method {@link FlightServiceFactory#creteFlightService()}
10+
*/
11+
public class FlightServiceFactory {
12+
13+
/**
14+
* Create a new instance of {@link FlightService}
15+
*
16+
* @return FlightService
17+
*/
18+
public FlightService creteFlightService() {
19+
throw new ExerciseNotCompletedException();
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.bobocode.flight_search.service;
2+
3+
import com.bobocode.flight_search.data.FlightDao;
4+
import com.bobocode.util.ExerciseNotCompletedException;
5+
6+
import java.util.List;
7+
8+
/**
9+
* {@link FlightService} provides an API that allows to manage flight numbers
10+
* <p>
11+
* todo: 1. Using {@link FlightDao} implement method {@link FlightService#registerFlight(String)}
12+
* todo: 2. Using {@link FlightDao} implement method {@link FlightService#searchFlights(String)}
13+
*/
14+
public class FlightService {
15+
16+
/**
17+
* Adds a new flight number
18+
*
19+
* @param flightNumber a flight number to add
20+
* @return {@code true} if a flight number was added, {@code false} otherwise
21+
*/
22+
public boolean registerFlight(String flightNumber) {
23+
throw new ExerciseNotCompletedException();
24+
}
25+
26+
/**
27+
* Returns all flight numbers that contains a provided key.
28+
*
29+
* @param query a search query
30+
* @return a list of found flight numbers
31+
*/
32+
public List<String> searchFlights(String query) {
33+
throw new ExerciseNotCompletedException();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.bobocode.flight_search;
2+
3+
import com.bobocode.flight_search.factory.FlightServiceFactory;
4+
import com.bobocode.flight_search.service.FlightService;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
public class FlightServiceTest {
12+
13+
private FlightService flightService = new FlightServiceFactory().creteFlightService();
14+
15+
@Test
16+
public void testRegisterFlight() {
17+
boolean registered = flightService.registerFlight("PR344");
18+
19+
assertTrue(registered);
20+
}
21+
22+
@Test
23+
public void testRegisterSameFlightTwice() {
24+
flightService.registerFlight("RB122");
25+
26+
boolean registeredSecondTime = flightService.registerFlight("RB122");
27+
28+
assertFalse(registeredSecondTime);
29+
}
30+
31+
@Test
32+
public void testSearchExistingFlightByFullNumber() {
33+
flightService.registerFlight("OL234");
34+
flightService.registerFlight("KM23234");
35+
flightService.registerFlight("LTE114");
36+
flightService.registerFlight("BRT14");
37+
38+
List<String> foundFlights = flightService.searchFlights("LTE114");
39+
40+
assertEquals(1, foundFlights.size());
41+
assertEquals("LTE114", foundFlights.get(0));
42+
}
43+
44+
@Test
45+
public void testSearchNonExistingFlight() {
46+
List<String> foundFlights = flightService.searchFlights("XXX");
47+
48+
assertNotNull(foundFlights);
49+
}
50+
51+
@Test
52+
public void testSearchFlights() {
53+
flightService.registerFlight("OR1214");
54+
flightService.registerFlight("BTR14");
55+
flightService.registerFlight("BMK198");
56+
flightService.registerFlight("RLR198");
57+
58+
List<String> foundFlights = flightService.searchFlights("R1");
59+
60+
assertTrue(foundFlights.contains("OR1214"));
61+
assertTrue(foundFlights.contains("BTR14"));
62+
assertTrue(foundFlights.contains("RLR198"));
63+
assertEquals(3, foundFlights.size());
64+
}
65+
}

0 commit comments

Comments
 (0)