Skip to content

Commit e23c10f

Browse files
committed
Программа полностью готова, осталось написать тесты
1 parent c1995ae commit e23c10f

File tree

6 files changed

+416
-208
lines changed

6 files changed

+416
-208
lines changed

savedData.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Client{name='Альберт Большаков', phoneNumber='+79141544234', idNumber=795}
2+
Client{name='Антон Германович Федосеев', phoneNumber='+79512917697', idNumber=47}
3+
Client{name='Белозерова Ольга', phoneNumber='+79132151876', idNumber=906}
4+
Client{name='Большакова Маргарита', phoneNumber='+79600176461', idNumber=358}
5+
Client{name='Виктория Гущина', phoneNumber='+79660851640', idNumber=872}
6+
Client{name='Даниил Маслов', phoneNumber='+79769435383', idNumber=861}
7+
Client{name='Денисова Юлия', phoneNumber='+79395588731', idNumber=328}
8+
Client{name='Елисеев Денис Егорович', phoneNumber='+79569043128', idNumber=762}
9+
Client{name='Кира Денисовна Гордеева', phoneNumber='+79369345223', idNumber=97}
10+
Client{name='Комарова Дарья Макаровна', phoneNumber='+79009560450', idNumber=538}
11+
Client{name='Лобанов Вячеслав Васильевич', phoneNumber='+79633737514', idNumber=122}
12+
Client{name='Марина Альбертовна Белова', phoneNumber='+79111698201', idNumber=942}
13+
Client{name='Матвей Горбунов', phoneNumber='+79544728055', idNumber=172}
14+
Client{name='Матвей Григорьевич Ефремов', phoneNumber='+79467628131', idNumber=266}
15+
Client{name='Осипов Степан Макарович', phoneNumber='+79600704787', idNumber=290}
16+
Client{name='Савина Лариса Дмитриевна', phoneNumber='+79024370933', idNumber=22}
17+
Client{name='Смирнов Никита Лаврентьевич', phoneNumber='+79956621645', idNumber=691}
18+
Client{name='Соколова Татьяна', phoneNumber='+79988936291', idNumber=758}
19+
Client{name='Чернов Борис Геннадьевич', phoneNumber='+79574148315', idNumber=697}
20+
Client{name='Ярослав Георгиевич Максимов', phoneNumber='+79171026484', idNumber=964}
21+

src/main/java/input/CustomCollection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public class CustomCollection<T> implements Iterable<T> {
3838
/** Количество фактически хранящихся элементов в коллекции. */
3939
private int size;
4040

41+
public Object[] getElements() {
42+
return elements;
43+
}
44+
45+
public int getSize() {
46+
return size;
47+
}
48+
4149
public CustomCollection() {
4250
this.elements = new Object[DEFAULT_CAPACITY];
4351
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package output;
2+
3+
import java.io.*;
4+
5+
public class FileDataWriter {
6+
7+
private final File file = new File("savedData.txt");
8+
9+
public void writeDataToFile(String data){
10+
try {
11+
file.createNewFile();
12+
} catch (IOException e) {
13+
throw new RuntimeException(e);
14+
}
15+
16+
try(Writer writer = new BufferedWriter(new FileWriter(file, true))){
17+
writer.append(data);
18+
} catch (Exception e){
19+
e.printStackTrace();
20+
}
21+
}
22+
}

src/main/java/userInterface/AppController.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import enums.Field;
55
import input.CustomCollection;
66
import input.InputManager;
7+
import output.FileDataWriter;
78
import sorting.MergeSortDefaultStrategy;
89
import sorting.MergeSortDynamicStrategy;
910
import sorting.SortingManager;
@@ -15,43 +16,49 @@ public class AppController {
1516
private final InputManager inputManager = new InputManager();
1617
private final CustomCollection<Client> fullList = new CustomCollection<>();
1718
private final SortingManager sortingManager = new SortingManager();
19+
private final FileDataWriter fileDataWriter = new FileDataWriter();
20+
ConcurrentCounter concurrentCounter = new ConcurrentCounter();
1821

1922
public void startDefaultSorting(){
2023
sortingManager.setStrategy(new MergeSortDefaultStrategy());
2124
sortingManager.getCurrentStrategy().sort(fullList);
22-
showAllClients();
25+
showAndWriteAllClients();
2326
}
2427

2528
public void startEvenIdsSorting(){
2629
sortingManager.setStrategy(new MergeSortDefaultStrategy());
2730
sortingManager.getCurrentStrategy().sortEvenValuesOnly(fullList);
28-
showAllClients();
31+
showAndWriteAllClients();
2932
}
3033

3134
public void startDynamicSorting(Field field){
3235
sortingManager.setStrategy(new MergeSortDynamicStrategy(field, true));
3336
sortingManager.getCurrentStrategy().sort(fullList);
34-
showAllClients();
37+
showAndWriteAllClients();
3538
}
3639

3740
public CustomCollection<Client> getFullList() {
3841
return fullList;
3942
}
4043

41-
public void showAllClients(){
44+
public void showAndWriteAllClients(){
4245
for(Client client : fullList){
4346
System.out.println(client);
47+
fileDataWriter.writeDataToFile(client + "\n");
4448
}
49+
fileDataWriter.writeDataToFile("\n");
4550
}
4651

4752
public void startFileReaderStrategy(String path){
4853
inputManager.setStrategy(inputManager.createFileStrategy(path));
4954
try {
5055
CustomCollection<Client> fromFileList = inputManager.loadData();
56+
int countOfAlexes = concurrentCounter.countAlexes(fromFileList);
5157
fullList.addAll(fromFileList);
5258
for(Client client : fromFileList){
5359
System.out.println(client);
5460
}
61+
System.out.println("Добавлено Алексеев: " + countOfAlexes);
5562
} catch (IOException e) {
5663
throw new RuntimeException(e);
5764
}
@@ -61,10 +68,12 @@ public void startManualInputStrategy(){
6168
inputManager.setStrategy(inputManager.createManualStrategy());
6269
try {
6370
CustomCollection<Client> manualList = inputManager.loadData();
71+
int countOfAlexes = concurrentCounter.countAlexes(manualList);
6472
fullList.addAll(manualList);
6573
for(Client client : manualList){
6674
System.out.println(client);
6775
}
76+
System.out.println("Добавлено Алексеев: " + countOfAlexes);
6877
} catch (IOException e) {
6978
throw new RuntimeException(e);
7079
}
@@ -74,10 +83,12 @@ public void startRandomDataStrategy(int count){
7483
inputManager.setStrategy(inputManager.createRandomStrategy(count));
7584
try {
7685
CustomCollection<Client> randomList = inputManager.loadData();
86+
int countOfAlexes = concurrentCounter.countAlexes(randomList);
7787
fullList.addAll(randomList);
7888
for(Client client : randomList){
7989
System.out.println(client);
8090
}
91+
System.out.println("Добавлено Алексеев: " + countOfAlexes);
8192
} catch (IOException e) {
8293
throw new RuntimeException(e);
8394
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package userInterface;
2+
3+
import dto.Client;
4+
import input.CustomCollection;
5+
6+
import java.util.concurrent.*;
7+
8+
public class ConcurrentCounter {
9+
10+
public int countAlexes(CustomCollection<Client> clients){
11+
CustomCollection<Client> clients1 = new CustomCollection<>();
12+
CustomCollection<Client> clients2 = new CustomCollection<>();
13+
14+
for (int i = 0; i < clients.size()/2 ; i++){
15+
clients1.add(clients.get(i));
16+
}
17+
18+
for (int i = clients.size()/2; i < clients.size(); i++){
19+
clients2.add(clients.get(i));
20+
}
21+
22+
ExecutorService executorService = Executors.newFixedThreadPool(2);
23+
24+
Future<Long> count1 = executorService.submit(new Callable<Long>() {
25+
@Override
26+
public Long call() throws Exception {
27+
return clients1.stream().filter(c-> c.getName().contains("Алексей")).count();
28+
}
29+
});
30+
31+
Future<Long> count2 = executorService.submit(new Callable<Long>() {
32+
@Override
33+
public Long call() throws Exception {
34+
return clients2.stream().filter(c-> c.getName().contains("Алексей")).count();
35+
}
36+
});
37+
38+
executorService.shutdown();
39+
40+
try {
41+
long result = count1.get() + count2.get();
42+
return Math.toIntExact(result);
43+
} catch (InterruptedException e) {
44+
throw new RuntimeException(e);
45+
} catch (ExecutionException e) {
46+
throw new RuntimeException(e);
47+
}
48+
49+
}
50+
}

0 commit comments

Comments
 (0)