Skip to content

Commit 0dbe6dc

Browse files
authored
Merge branch 'exercise/completed' into GP-26_Add_compleated_solution_to_exercise/completed
2 parents 041c6df + 54f3773 commit 0dbe6dc

File tree

14 files changed

+272
-0
lines changed

14 files changed

+272
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.NoSuchElementException;
44
import java.util.Objects;
55
import java.util.stream.Stream;
6+
import com.bobocode.util.ExerciseNotCompletedException;
67

78
/**
89
* {@link LinkedList} is a list implementation that is based on singly linked generic nodes. A node is implemented as

3-0-java-core/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,19 @@
1+
package com.bobocode.file_reader;
2+
3+
import com.bobocode.util.ExerciseNotCompletedException;
4+
5+
/**
6+
* {@link FileReaders} provides an API that allow to read whole file into a {@link String} by file name.
7+
*/
8+
public class FileReaders {
9+
10+
/**
11+
* Returns a {@link String} that contains whole text from the file specified by name.
12+
*
13+
* @param fileName a name of a text file
14+
* @return string that holds whole file content
15+
*/
16+
public static String readWholeFile(String fileName) {
17+
throw new ExerciseNotCompletedException(); //todo
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bobocode.file_reader;
2+
3+
import com.bobocode.file_reader.FileReaders;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
9+
public class FileReadersTest {
10+
11+
@Test
12+
void testReadWholeFileOnEmptyFile() {
13+
String fileContent = FileReaders.readWholeFile("empty.txt");
14+
15+
assertEquals("", fileContent);
16+
17+
}
18+
19+
@Test
20+
void testReadWholeFileOnFileWithEmptyLines() {
21+
String fileContent = FileReaders.readWholeFile("lines.txt");
22+
23+
assertEquals("Hey!\n" +
24+
"\n" +
25+
"What's up?\n" +
26+
"\n" +
27+
"Hi!", fileContent);
28+
}
29+
30+
@Test
31+
void testReadWholeFile() {
32+
String fileContent = FileReaders.readWholeFile("simple.txt");
33+
34+
assertEquals("Hello!\n" + "It's a test file.", fileContent);
35+
}
36+
}

3-0-java-core/src/test/resources/empty.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hey!
2+
3+
What's up?
4+
5+
Hi!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello!
2+
It's a test file.
+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)