-
Notifications
You must be signed in to change notification settings - Fork 101
Lesson 41 (set) for review #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Binary-Cat-01
wants to merge
2
commits into
KFalcon2022:for-pr
Choose a base branch
from
Binary-Cat-01:lesson_41_set_for_review
base: for-pr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.walking.lesson41_set.task1.model; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Objects; | ||
|
|
||
| public class Task implements Comparable<Task> { | ||
| private final String name; | ||
| private Instant acceptedAt; | ||
|
|
||
| public Task(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public Instant getAcceptedAt() { | ||
| return acceptedAt; | ||
| } | ||
|
|
||
| public void setAcceptedAt(Instant acceptedAt) { | ||
| this.acceptedAt = acceptedAt; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Task task = (Task) o; | ||
|
|
||
| return Objects.equals(name, task.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return name != null ? name.hashCode() : 0; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Task o) { | ||
| return acceptedAt.compareTo(o.getAcceptedAt()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.walking.lesson41_set.task1.model; | ||
|
|
||
| public enum TaskStatus { | ||
| ACCEPTED, | ||
| EXECUTED, | ||
| CANCELED; | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return this.name().toLowerCase(); | ||
| } | ||
| } |
109 changes: 109 additions & 0 deletions
109
src/com/walking/lesson41_set/task1/service/TaskService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package com.walking.lesson41_set.task1.service; | ||
|
|
||
| import com.walking.lesson41_set.task1.model.Task; | ||
| import com.walking.lesson41_set.task1.model.TaskStatus; | ||
| import com.walking.lesson41_set.task1.util.Logger; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.*; | ||
|
|
||
| public class TaskService { | ||
| private final NavigableSet<Task> tasks; | ||
| private final Logger logger; | ||
|
|
||
| public TaskService() { | ||
| this.tasks = new TreeSet<>(); | ||
| this.logger = new Logger(); | ||
| } | ||
|
|
||
| public TaskService(Collection<? extends Task> incomingTasks) { | ||
| this.tasks = new TreeSet<>(incomingTasks); | ||
| this.logger = new Logger(); | ||
|
|
||
| for (Task incomingTask : incomingTasks) { | ||
| logger.log(getTaskStatusMessage(incomingTask, TaskStatus.ACCEPTED)); | ||
| } | ||
| } | ||
|
|
||
| public List<Task> getAllTasks() { | ||
| return List.copyOf(tasks); | ||
| } | ||
|
|
||
| public boolean acceptSingleTask(Task acceptedTask) { | ||
| if (tasks.add(acceptedTask)) { | ||
| acceptedTask.setAcceptedAt(Instant.now()); | ||
| logger.log(getTaskStatusMessage(acceptedTask, TaskStatus.ACCEPTED)); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public boolean acceptAllTasks(Collection<? extends Task> incomingTasks) { | ||
| if (tasks.addAll(incomingTasks)) { | ||
| for (Task acceptedTask : incomingTasks) { | ||
| acceptedTask.setAcceptedAt(Instant.now()); | ||
| logger.log(getTaskStatusMessage(acceptedTask, TaskStatus.ACCEPTED)); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public Task executeSingleTask() { | ||
| Task executedTask = tasks.pollFirst(); | ||
|
|
||
| if (executedTask != null) { | ||
| logger.log(getTaskStatusMessage(executedTask, TaskStatus.EXECUTED)); | ||
| executedTask.setAcceptedAt(null); | ||
| } | ||
|
|
||
| return executedTask; | ||
| } | ||
|
|
||
| public List<Task> executeMultipleTasks(int taskCount) { | ||
| List<Task> executedTasks = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < taskCount; i++) { | ||
| executedTasks.add(executeSingleTask()); | ||
| } | ||
|
|
||
| return executedTasks; | ||
| } | ||
|
|
||
| public Task cancelNextTask() { | ||
| Task canceledTask = tasks.pollFirst(); | ||
|
|
||
| if (canceledTask != null) { | ||
| logger.log(getTaskStatusMessage(canceledTask, TaskStatus.CANCELED)); | ||
| canceledTask.setAcceptedAt(null); | ||
| } | ||
|
|
||
| return canceledTask; | ||
| } | ||
|
|
||
| public List<Task> cancelMultipleTasks(int taskCount) { | ||
| List<Task> canceledTasks = new ArrayList<>(); | ||
|
|
||
| for (int i = 0; i < taskCount; i++) { | ||
| canceledTasks.add(cancelNextTask()); | ||
| } | ||
|
|
||
| return canceledTasks; | ||
| } | ||
|
|
||
| public Task lookNextTask() { | ||
| return tasks.isEmpty() ? null : tasks.first(); | ||
| } | ||
|
|
||
| public boolean haveTask() { | ||
| return !tasks.isEmpty(); | ||
| } | ||
|
|
||
| private String getTaskStatusMessage(Task task, TaskStatus status) { | ||
| return "Task <%s> %s".formatted(task.getName(), status); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.walking.lesson41_set.task1.util; | ||
|
|
||
| public class Logger { | ||
| public void log(String message) { | ||
| System.out.println(message); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,37 @@ | ||
| package com.walking.lesson41_set.task2; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Scanner; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Реализуйте задачу | ||
| * <a href="https://github.com/KFalcon2022/practical-tasks/tree/master/src/com/walking/lesson26_string_types/task2">...</a>, | ||
| * используя Set. | ||
| */ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| System.out.println("Введите строку, содержашую слова, разделенные пробелом:"); | ||
|
|
||
| String input = scanner.nextLine(); | ||
|
|
||
| scanner.close(); | ||
|
|
||
| int amountUniqueWords = countUniqueWords(input); | ||
|
|
||
| System.out.printf("Количество уникальных слов в строке: %d\n", amountUniqueWords); | ||
| } | ||
|
|
||
| private static int countUniqueWords(String allWords) { | ||
| String[] splittedWords = allWords.trim() | ||
| .toLowerCase() | ||
| .split(" "); | ||
|
|
||
| Set<String> uniqueWords = new HashSet<>(Arrays.asList(splittedWords)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrays.asList() - относительно устаревшая форма. Чаще используют List.of() |
||
|
|
||
| return uniqueWords.size(); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Хорошее решение. Одно из возможных:)