I want to take users input to create a list of tasks as shown in the next code:
class Task {
private int start;
private int finish;
public Task(int start, int finish) {
this.start = start;
this.finish = finish;
}
public int getFinish() {
return finish;
}
public int getStart() {
return start;
}
@Override
public String toString() {
return "(" + getStart() + ", " + getFinish() + ")";
}
}
As you can see a Task is composed by two int one for the start and one for the finish.
Then I have a class that select the tasks with certain conditions and return a list of tasks:
class AS {
public static List<Task> activitySelector(List<Task> tasks) {
int k = 0;
Set<Integer> result = new HashSet<>();
result.add(0);
Collections.sort(tasks, Comparator.comparingInt(Task::getStart));
for (int i = 1; i < tasks.size(); i++) {
if (tasks.get(i).getStart() >= tasks.get(k).getFinish()) {
result.add(i);
k = i;
}
}
return result.stream().map(tasks::get).collect(Collectors.toList());
}
Then I need the user to type in terminal the numbers (start, finish) to create the list of tasks, but I'm stuck on how to get user input and sort each input into the correct way. I know i can declare the list locally like:
List<Task> tasks = Arrays.asList(new Task(1, 4), new Task(1, 3),
new Task(2, 5), new Task(3, 7),
new Task(4, 7), new Task(6, 9),
new Task(7, 8));
But I don't know how to create List<Task> from user input. Any idea on how I can achieve this?
Scannerfrom terminal but not succeededintgiven by user and store each in the form of theTaskconstructor which is(start, finish)new Task(startInt, finshInt)?