Skip to content

Commit a5beda9

Browse files
author
coursar
committed
feat(collections)
1 parent 6e035f7 commit a5beda9

File tree

10 files changed

+348
-1
lines changed

10 files changed

+348
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636

3737
4.2. [x] [Интерфейсы для организации малой связности. Обобщённое программирование (Generics)](interfaces)
3838

39-
4.3. [ ] [Collections Framework. CRUD и тестирование систем, управляющих набором объектов](collections)
39+
4.3. [x] [Collections Framework. CRUD и тестирование систем, управляющих набором объектов](collections)

collections/collections/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>collections</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.projectlombok</groupId>
20+
<artifactId>lombok</artifactId>
21+
<version>1.18.12</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter</artifactId>
27+
<version>5.4.2</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mockito</groupId>
32+
<artifactId>mockito-junit-jupiter</artifactId>
33+
<version>3.3.3</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-surefire-plugin</artifactId>
43+
<version>2.22.2</version>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ru.netology.domain;
2+
3+
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.NoArgsConstructor;
6+
7+
@NoArgsConstructor
8+
@Data
9+
@EqualsAndHashCode(callSuper = true)
10+
public class Book extends Product {
11+
private String author;
12+
private int pages;
13+
14+
public Book(int id, String name, int price, double rating, String author, int pages) {
15+
super(id, name, price, rating);
16+
this.author = author;
17+
this.pages = pages;
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ru.netology.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@NoArgsConstructor
8+
@AllArgsConstructor
9+
@Data
10+
public class Product implements Comparable<Product> {
11+
private int id;
12+
private String name;
13+
private int price;
14+
private double rating;
15+
16+
@Override
17+
public int compareTo(Product o) {
18+
return id - o.id;
19+
}
20+
}
21+
22+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ru.netology.generic;
2+
3+
public class ParametrizedMethod {
4+
public <T> void method(T param) { }
5+
6+
public static <T> void staticMethod(T param) { }
7+
8+
}
9+
10+
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.netology.generic;
2+
3+
public class ParametrizedType<T> {
4+
}
5+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package ru.netology.repository;
2+
3+
import ru.netology.domain.Product;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.List;
8+
9+
// FIXME: ВАЖНО не оставляйте код в таком виде (закомментированном) в ваших ДЗ!
10+
// FIXME: мы так сделали ТОЛЬКО для удобства демонстрации
11+
12+
// first version without wildcard
13+
//public class ProductRepository {
14+
// private Collection<Product> items = new ArrayList<>();
15+
//
16+
// public Collection<Product> getAll() {
17+
// return items;
18+
// }
19+
//
20+
// public Product getById(int id) {
21+
// for (Product item : items) {
22+
// if (item.getId() == id) {
23+
// return item;
24+
// }
25+
// }
26+
// return null;
27+
// }
28+
//
29+
// public boolean add(Product item) {
30+
// return items.add(item);
31+
// }
32+
//
33+
// public boolean remove(Product item) {
34+
// return items.remove(item);
35+
// }
36+
//
37+
// public boolean addAll(Collection<Product> items) {
38+
// return this.items.addAll(items);
39+
// }
40+
//
41+
// public boolean removeAll(Collection<Product> items) {
42+
// return this.items.removeAll(items);
43+
// }
44+
//}
45+
46+
// second version with wildcard
47+
//public class ProductRepository {
48+
// private Collection<Product> items = new ArrayList<>();
49+
//
50+
// public Collection<Product> getAll() {
51+
// return items;
52+
// }
53+
//
54+
// public Product getById(int id) {
55+
// for (Product item : items) {
56+
// if (item.getId() == id) {
57+
// return item;
58+
// }
59+
// }
60+
// return null;
61+
// }
62+
//
63+
// public boolean add(Product item) {
64+
// return items.add(item);
65+
// }
66+
//
67+
// public boolean remove(Product item) {
68+
// return items.remove(item);
69+
// }
70+
//
71+
// public boolean addAll(Collection<? extends Product> items) {
72+
// return this.items.addAll(items);
73+
// }
74+
//
75+
// public boolean removeAll(Collection<? extends Product> items) {
76+
// return this.items.removeAll(items);
77+
// }
78+
//}
79+
80+
// third version with List
81+
public class ProductRepository {
82+
private List<Product> items = new ArrayList<>();
83+
84+
public List<Product> getAll() {
85+
return items;
86+
}
87+
88+
public Product getById(int id) {
89+
for (Product item : items) {
90+
if (item.getId() == id) {
91+
return item;
92+
}
93+
}
94+
return null;
95+
}
96+
97+
public boolean add(Product item) {
98+
//
99+
return items.add(item);
100+
//
101+
}
102+
103+
public boolean remove(Product item) {
104+
//
105+
return items.remove(item);
106+
//
107+
}
108+
109+
public boolean addAll(Collection<? extends Product> items) {
110+
return this.items.addAll(items);
111+
}
112+
113+
public boolean removeAll(Collection<? extends Product> items) {
114+
return this.items.removeAll(items);
115+
}
116+
}
117+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.netology.repository;
2+
3+
import org.junit.jupiter.api.Nested;
4+
5+
public class CRUDRepositoryTest {
6+
@Nested
7+
public class Empty {
8+
}
9+
10+
@Nested
11+
public class SingleItem {
12+
}
13+
14+
@Nested
15+
public class MultipleItems {
16+
}
17+
}
18+
19+
20+
21+
22+
23+
24+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ru.netology.repository;
2+
3+
import org.junit.jupiter.api.Test;
4+
import ru.netology.domain.Book;
5+
import ru.netology.domain.Product;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
11+
class ProductRepositoryTest {
12+
private ProductRepository repository = new ProductRepository();
13+
14+
@Test
15+
void shouldAddProduct() {
16+
repository.add(new Product());
17+
}
18+
19+
@Test
20+
void shouldAddMultipleProducts() {
21+
Collection<Product> products = new ArrayList<>();
22+
products.add(new Product());
23+
products.add(new Product());
24+
25+
repository.addAll(products);
26+
}
27+
28+
@Test
29+
void shouldAddBook() {
30+
repository.add(new Book());
31+
}
32+
33+
@Test
34+
void shouldAddMultipleBooks() {
35+
Collection<Book> books = new ArrayList<>();
36+
books.add(new Book());
37+
books.add(new Book());
38+
39+
boolean removed = repository.addAll(books);
40+
}
41+
}
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ru.netology.repository;
2+
3+
import org.junit.jupiter.api.Test;
4+
import ru.netology.domain.Book;
5+
import ru.netology.domain.Product;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
10+
11+
class ProductRepositoryTestWithListOf {
12+
private ProductRepository repository = new ProductRepository();
13+
14+
@Test
15+
void shouldAddProduct() {
16+
repository.add(new Product());
17+
}
18+
19+
@Test
20+
void shouldAddMultipleProducts() {
21+
Collection<Product> products = new ArrayList<>();
22+
products.add(new Product());
23+
products.add(new Product());
24+
repository.addAll(products);
25+
26+
repository.addAll(List.of(new Product(), new Product()));
27+
}
28+
29+
@Test
30+
void shouldAddBook() {
31+
repository.add(new Book());
32+
}
33+
34+
@Test
35+
void shouldAddMultipleBooks() {
36+
boolean removed = repository.addAll(List.of(new Product(), new Product()));
37+
}
38+
}
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+

0 commit comments

Comments
 (0)