Skip to content

Conversation

@Binary-Cat-01
Copy link

No description provided.


public static void main(String[] args) {
CounterService counterService = new CounterService();
counterService.displayContent();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

замечание то же, что и ранее: сервис не должен отвечать за способ вывода


Counter[] allCounters = new Counter[counterService.getAllCounters().size() + 5];

counterService.getAllCounters().toArray(allCounters);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

предсоздавать массив в данном случае не обязательно

System.out.println("Названия счетчиков в массиве:");

for (Counter counter : counters) {
if (counter == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тернарка смотрелась бы лаконичнее


private final ArrayList<Counter> counters;

public CounterService(Counter... counters) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вполне можно и параметром принимать лист. Опционально

}

public List<Counter> getAllCounters() {
return counters;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

на будущее - зачастую хорошей практикой принимается отдавать копию коллекции - на случай, если необходимо ограничить бесконтрольное изменение такого хранилища. Тогда добавление и иные изменения коллекции ограничены апи управляющего класса (здесь - сервиса), а на чтение всегда будет отдаваться копия, изменение которой не повлияет на основную коллекцию

}

public Counter getFirst() {
return counters.get(0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

с 21 джавы можно проще

return this.counters.addAll(candidates);
}

public boolean removeIfNotMatch(Collection<? extends Counter> sample) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

чтобы потыкать функциональность листов - норм, но в целом такая семантика вызывает вопросы

List<Counter> uniques = new ArrayList<>();

for (Counter counter : counters) {
if (!uniques.contains(counter)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

у тебя же вроде не переопределен equals(). Будет криво работать

return uniques;
}

public boolean removeAllZeroValue() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

непонятное название, смысл становится прозрачным только после погружения в тело метода

return true;
}

private List<Counter> getZeroValues() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

советую располагать методы в соответствие с модификатором доступа - сначала публичные, потом протектед и т.д.

public Counter increaseCounter(String name, int value) {
Counter counter = getCounterByName(name);

if (counter == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тернарный оператор?

public Counter decreaseCounter(String name, int value) {
Counter counter = getCounterByName(name);

if (counter == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и здесь

public Counter incrementCounter(String name) {
Counter counter = getCounterByName(name);

if (counter == null) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

public void displayContent() {
System.out.printf("Всего счетчиков: %s\n%s\n", counters.size(), "-".repeat(20));

if (counters.isEmpty()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

отписывал выше

this.next = next;
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хорошее решение

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants