Skip to content

Commit 36b4a5f

Browse files
committed
Add the second variant of the todo-list
1 parent 01a047d commit 36b4a5f

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
// Task 5.2
5+
public class Main2 {
6+
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in);
9+
ToDoList toDoList = new ToDoList();
10+
11+
while (true) {
12+
UserInput userInput = parse(scanner.nextLine());
13+
switch (userInput.getCommand()) {
14+
case "LIST":
15+
System.out.println(toDoList.list());
16+
break;
17+
case "ADD":
18+
toDoList.add(userInput.getIndex(), userInput.getTask());
19+
break;
20+
case "EDIT":
21+
toDoList.edit(userInput.getIndex(), userInput.getTask());
22+
break;
23+
case "DELETE":
24+
toDoList.delete(userInput.getIndex());
25+
break;
26+
}
27+
}
28+
}
29+
30+
private static UserInput parse(String input) {
31+
String[] split = input.split(" ");
32+
if (split.length == 1) return new UserInput(input.toUpperCase(), null, null);
33+
try {
34+
return new UserInput(split[0].toUpperCase(), Integer.valueOf(split[1]), String.join(" ",
35+
Arrays.copyOfRange(split, 2, split.length)));
36+
} catch (NumberFormatException e) {
37+
return new UserInput(split[0].toUpperCase(), null, String.join(" ",
38+
Arrays.copyOfRange(split, 1, split.length)));
39+
}
40+
}
41+
42+
private static class UserInput {
43+
private final String command;
44+
private final Integer index;
45+
private final String task;
46+
47+
public UserInput(String command, Integer index, String task) {
48+
this.command = command;
49+
this.index = index;
50+
this.task = task;
51+
}
52+
53+
public String getCommand() {
54+
return command;
55+
}
56+
57+
public Integer getIndex() {
58+
return index;
59+
}
60+
61+
public String getTask() {
62+
return task;
63+
}
64+
}
65+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
import java.util.stream.IntStream;
5+
6+
public class ToDoList {
7+
8+
private final List<String> todoList = new ArrayList<>();
9+
10+
public void add(String todo) {
11+
todoList.add(todo);
12+
}
13+
14+
public void add(Integer index, String todo) {
15+
if (exist(index)) todoList.add(index, todo);
16+
else add(todo);
17+
}
18+
19+
public void edit(Integer index, String todo) {
20+
if (exist(index)) todoList.set(index, todo);
21+
}
22+
23+
public void delete(int index) {
24+
if (exist(index)) todoList.remove(index);
25+
}
26+
27+
public String list() {
28+
return IntStream.range(0, todoList.size())
29+
.mapToObj(i -> i + " - " + todoList.get(i))
30+
.collect(Collectors.joining("\n"));
31+
}
32+
33+
private boolean exist(Integer index) {
34+
return index != null && index >= 0 && index < todoList.size();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)