Skip to content

Commit c2c703e

Browse files
authored
Added labs 7, 8, 9
1 parent e72ee9c commit c2c703e

File tree

93 files changed

+3683
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3683
-0
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.5.RELEASE</version>
9+
<relativePath/>
10+
</parent>
11+
<groupId>ua.lviv.iot.constructiongoods</groupId>
12+
<artifactId>lab-7-java-unit-testing</artifactId>
13+
<packaging>jar</packaging>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
<url>http://maven.apache.org</url>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-jpa</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-hateoas</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.postgresql</groupId>
36+
<artifactId>postgresql</artifactId>
37+
<scope>runtime</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.hsqldb</groupId>
42+
<artifactId>hsqldb</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
<exclusions>
51+
<exclusion>
52+
<groupId>org.junit.vintage</groupId>
53+
<artifactId>junit-vintage-engine</artifactId>
54+
</exclusion>
55+
</exclusions>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.springframework.restdocs</groupId>
60+
<artifactId>spring-restdocs-mockmvc</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.junit.jupiter</groupId>
66+
<artifactId>junit-jupiter-engine</artifactId>
67+
<version>5.6.0</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.junit.vintage</groupId>
72+
<artifactId>junit-vintage-engine</artifactId>
73+
<version>5.2.0</version>
74+
<scope>test</scope>
75+
</dependency>
76+
77+
<dependency>
78+
<groupId>com.github.spotbugs</groupId>
79+
<artifactId>spotbugs</artifactId>
80+
<version>4.0.0-RC3</version>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-checkstyle-plugin</artifactId>
86+
<version>3.1.1</version>
87+
<type>maven-plugin</type>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-pmd-plugin</artifactId>
93+
<version>3.13.0</version>
94+
<type>maven-plugin</type>
95+
</dependency>
96+
97+
<dependency>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-project-info-reports-plugin</artifactId>
100+
<version>3.0.0</version>
101+
<type>maven-plugin</type>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-site-plugin</artifactId>
107+
<version>3.8.2</version>
108+
<type>maven-plugin</type>
109+
</dependency>
110+
</dependencies>
111+
112+
<build>
113+
<plugins>
114+
<plugin>
115+
<groupId>org.asciidoctor</groupId>
116+
<artifactId>asciidoctor-maven-plugin</artifactId>
117+
<version>1.5.8</version>
118+
<executions>
119+
<execution>
120+
<id>generate-docs</id>
121+
<phase>prepare-package</phase>
122+
<goals>
123+
<goal>process-asciidoc</goal>
124+
</goals>
125+
<configuration>
126+
<backend>html</backend>
127+
<doctype>book</doctype>
128+
</configuration>
129+
</execution>
130+
</executions>
131+
<dependencies>
132+
<dependency>
133+
<groupId>org.springframework.restdocs</groupId>
134+
<artifactId>spring-restdocs-asciidoctor</artifactId>
135+
<version>${spring-restdocs.version}</version>
136+
</dependency>
137+
</dependencies>
138+
</plugin>
139+
140+
<plugin>
141+
<groupId>org.springframework.boot</groupId>
142+
<artifactId>spring-boot-maven-plugin</artifactId>
143+
</plugin>
144+
145+
<plugin>
146+
<groupId>org.apache.maven.plugins</groupId>
147+
<artifactId>maven-compiler-plugin</artifactId>
148+
<version>3.8.1</version>
149+
<configuration>
150+
<release>13</release>
151+
</configuration>
152+
</plugin>
153+
154+
<plugin>
155+
<groupId>org.apache.maven.plugins</groupId>
156+
<artifactId>maven-surefire-plugin</artifactId>
157+
<version>2.22.2</version>
158+
<configuration>
159+
<argLine>
160+
--illegal-access=permit
161+
</argLine>
162+
</configuration>
163+
</plugin>
164+
165+
<plugin>
166+
<groupId>com.github.spotbugs</groupId>
167+
<artifactId>spotbugs-maven-plugin</artifactId>
168+
<version>3.1.12.2</version>
169+
</plugin>
170+
171+
<plugin>
172+
<groupId>org.apache.maven.plugins</groupId>
173+
<artifactId>maven-site-plugin</artifactId>
174+
<version>3.7.1</version>
175+
</plugin>
176+
</plugins>
177+
</build>
178+
179+
180+
181+
<reporting>
182+
<plugins>
183+
<plugin>
184+
<groupId>org.apache.maven.plugins</groupId>
185+
<artifactId>maven-project-info-reports-plugin</artifactId>
186+
<version>3.0.0</version>
187+
</plugin>
188+
189+
<plugin>
190+
<groupId>org.apache.maven.plugins</groupId>
191+
<artifactId>maven-surefire-plugin</artifactId>
192+
<version>2.22.2</version>
193+
</plugin>
194+
195+
<plugin>
196+
<groupId>org.apache.maven.plugins</groupId>
197+
<artifactId>maven-site-plugin</artifactId>
198+
<version>3.7.1</version>
199+
</plugin>
200+
201+
<plugin>
202+
<groupId>org.apache.maven.plugins</groupId>
203+
<artifactId>maven-pmd-plugin</artifactId>
204+
<version>3.13.0</version>
205+
</plugin>
206+
207+
<plugin>
208+
<groupId>com.github.spotbugs</groupId>
209+
<artifactId>spotbugs-maven-plugin</artifactId>
210+
<version>3.1.12.2</version>
211+
</plugin>
212+
213+
<plugin>
214+
<groupId>org.apache.maven.plugins</groupId>
215+
<artifactId>maven-checkstyle-plugin</artifactId>
216+
<version>3.1.1</version>
217+
<reportSets>
218+
<reportSet>
219+
<reports>
220+
<report>checkstyle</report>
221+
</reports>
222+
</reportSet>
223+
</reportSets>
224+
<configuration>
225+
<consoleOutput>true</consoleOutput>
226+
<encoding>UTF-8</encoding>
227+
<configLocation>google_checks.xml</configLocation>
228+
</configuration>
229+
</plugin>
230+
231+
</plugins>
232+
</reporting>
233+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ua.lviv.iot.constructiongoods.manager;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import ua.lviv.iot.constructiongoods.model.AbstractConstructionGoods;
6+
7+
public class ConstructionGoodsManager {
8+
9+
public ConstructionGoodsManager() {
10+
}
11+
12+
public List<AbstractConstructionGoods> findGoodsCheaperThan(double price,
13+
List<AbstractConstructionGoods> goods) {
14+
List<AbstractConstructionGoods> result = new LinkedList<>();
15+
for (int goodFromTheList = 0; goodFromTheList < goods.size(); goodFromTheList++) {
16+
if (goods.get(goodFromTheList).getPriceInUah() < price) {
17+
result.add(goods.get(goodFromTheList));
18+
System.out.println("Found good #" + (goodFromTheList + 1) + " matching the criteria"
19+
+ " with price " + goods.get(goodFromTheList).getPriceInUah());
20+
} else {
21+
System.out.println("Good #" + (goodFromTheList + 1) + " does not match the criteria");
22+
}
23+
}
24+
return result;
25+
}
26+
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ua.lviv.iot.constructiongoods.manager;
2+
3+
import java.io.Serializable;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
import ua.lviv.iot.constructiongoods.model.AbstractConstructionGoods;
8+
9+
public class ConstructionGoodsManagerUtils {
10+
11+
//static inner class
12+
static class ConstructionGoodsSorterByWeight implements Comparator<AbstractConstructionGoods>,
13+
Serializable {
14+
@Override
15+
public int compare(AbstractConstructionGoods firstGood,
16+
AbstractConstructionGoods secondGood) {
17+
return firstGood.getWeightInKilograms() - secondGood.getWeightInKilograms();
18+
}
19+
}
20+
21+
private static final ConstructionGoodsSorterByWeight CONSTRUCTION_GOODS_SORTER_BY_WEIGHT =
22+
new ConstructionGoodsSorterByWeight();
23+
24+
//sort using static inner class
25+
public static void sortByWeight(List<AbstractConstructionGoods> goods, SortType sortType) {
26+
goods.sort(sortType == SortType.ASCENDING ? CONSTRUCTION_GOODS_SORTER_BY_WEIGHT :
27+
CONSTRUCTION_GOODS_SORTER_BY_WEIGHT.reversed());
28+
}
29+
30+
//inner class
31+
class ConstructionGoodsSorterByWidth implements Comparator<AbstractConstructionGoods> {
32+
@Override
33+
public int compare(AbstractConstructionGoods firstGood, AbstractConstructionGoods secondGood) {
34+
if (Double.compare(firstGood.getWidthInCentimeters(),
35+
secondGood.getWidthInCentimeters()) < 0) {
36+
return -1;
37+
}
38+
if (Double.compare(firstGood.getWidthInCentimeters(),
39+
secondGood.getWidthInCentimeters()) > 0) {
40+
return 1;
41+
}
42+
return 0;
43+
}
44+
}
45+
46+
//sort using inner class
47+
public static void sortByWidth(List<AbstractConstructionGoods> goods, SortType sortType) {
48+
ConstructionGoodsManagerUtils manager = new ConstructionGoodsManagerUtils();
49+
ConstructionGoodsSorterByWidth sorterByWidth = manager.new ConstructionGoodsSorterByWidth();
50+
goods.sort(sortType == SortType.ASCENDING ? sorterByWidth : sorterByWidth.reversed());
51+
}
52+
53+
//sort using anonymous class
54+
public static void sortByProducerName(List<AbstractConstructionGoods> goods, SortType sortType) {
55+
Comparator<AbstractConstructionGoods> comparator = new Comparator<AbstractConstructionGoods>() {
56+
@Override
57+
public int compare(AbstractConstructionGoods firstGood,
58+
AbstractConstructionGoods secondGood) {
59+
return firstGood.getProducerName().compareTo(secondGood.getProducerName());
60+
}
61+
};
62+
goods.sort(sortType == SortType.ASCENDING ? comparator : comparator.reversed());
63+
}
64+
65+
//sort using lambda
66+
public static void sortByColor(List<AbstractConstructionGoods> goods, SortType sortType) {
67+
Comparator<AbstractConstructionGoods> comparator =
68+
(firstGood, secondGood) -> firstGood.getColor().compareTo(secondGood.getColor());
69+
goods.sort(sortType == sortType.DESCENDING ? comparator : comparator.reversed());
70+
}
71+
72+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ua.lviv.iot.constructiongoods.manager;
2+
3+
public enum SortType {
4+
ASCENDING, DESCENDING;
5+
}

0 commit comments

Comments
 (0)