Skip to content

Commit cfa6cee

Browse files
author
Никита
committed
- Реализован интерфейс CollectionInterface
- Исправлен метод remove() в интерфейсе теперь void removeByIndex(int index) - Рефакторинг: addAll() теперь использует add() - Исправление generic-типов в интерфейсе
1 parent 5e069dc commit cfa6cee

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package input;
22

3-
public interface CollectionInterface<T> extends Iterable<String>{
4-
boolean add(T car); // important
5-
boolean remove(T car); // important
6-
boolean removeAt(int index);
3+
public interface CollectionInterface<T> extends Iterable<T>{
4+
void add(T element); // important
5+
void removeByIndex(int index); // important
76
void clear(); // important
87
T get(int index);
9-
boolean add(T car, int index);
108
int size(); // important
11-
boolean contains(T car);
129
}

src/main/java/input/CustomCollection.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @param <T> тип элементов, хранимых в коллекции
2727
*/
28-
public class CustomCollection<T> implements Iterable<T> {
28+
public class CustomCollection<T> implements CollectionInterface<T> {
2929
/** Коэффициент увеличения емкости при расширении массива. */
3030
private static final float GROWTH_FACTOR = 1.5f;
3131

@@ -49,7 +49,7 @@ public CustomCollection(int initialCapacity) {
4949
this.elements = new Object[initialCapacity];
5050
}
5151

52-
public boolean add(T element) {
52+
public void add(T element) {
5353
// Проверяем достаточно ли места для добавления
5454
if (size == elements.length) {
5555
// Если недостаточно, то увеличиваем размер списка
@@ -59,7 +59,6 @@ public boolean add(T element) {
5959
elements[size] = element;
6060
// Увеличиваем число элементов в массиве
6161
size++;
62-
return true;
6362
}
6463

6564
public void removeByIndex(int index) {
@@ -82,6 +81,7 @@ public void removeByIndex(int index) {
8281
elements[--size] = null;
8382
}
8483

84+
// Не используется можно удалить
8585
public void clear() {
8686
for (int i = 0; i < size; i++) {
8787
elements[i] = null;
@@ -127,26 +127,15 @@ public Stream<T> stream() {
127127
* в процессе выполнения операции.
128128
*
129129
* @param collection коллекция, содержащая элементы для добавления
130-
* @return {@code true} если эта коллекция изменилась в результате вызова
131130
*/
132-
public boolean addAll(CustomCollection<? extends T> collection) {
133-
// Проверяем, что переданная коллекция не null и не пустая
131+
public void addAll(CustomCollection<? extends T> collection) {
134132
if (collection == null || collection.isEmpty()) {
135-
// Возвращаем false, так как нечего добавлять
136-
return false;
133+
return;
137134
}
138135

139-
int requiredCapacity = size + collection.size();
140-
if (requiredCapacity > elements.length) {
141-
increaseCapacity(requiredCapacity);
142-
}
143136
for (T element : collection) {
144-
// Добавляем элемент в конец массива
145-
// elements[size] = element - добавляет элемент на текущую позицию
146-
// size++ - увеличивает счетчик элементов
147-
elements[size++] = element;
137+
add(element);
148138
}
149-
return true;
150139
}
151140

152141
public int size() {

src/main/java/input/Strategy/ManualInputReaderStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ private String promptForString(String message, boolean required) {
246246
}
247247
}
248248

249-
class StopInputException extends RuntimeException {
249+
static class StopInputException extends RuntimeException {
250250
public StopInputException() {
251251
super("Ввод прерван пользователем");
252252
}
253253
}
254-
}
254+
}

0 commit comments

Comments
 (0)