-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodosApp.java
More file actions
164 lines (127 loc) · 4.88 KB
/
TodosApp.java
File metadata and controls
164 lines (127 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;
public class TodosApp {
private static Scanner scanner = new Scanner(System.in); // this is used by userInput function keep that in mind.
// It's probably not a good thing
private static ArrayList<Todo> todos = new ArrayList<>();
public static void main(String[] args) {
boolean running = true;
while (running) {
displayTodos();
showMenu();
String userChoice = getUserInput();
switch (userChoice) {
case "1":
addTodo();
break;
case "2":
completeTodo();
break;
case "3":
deleteTodo();
break;
case "4":
running = false;
break;
}
}
System.out.println("Not running anymore ;-;");
}
private static void showMenu() {
String template = """
\n--- TODOS App Menu ---
1. Add Todo
2. Mark Todo as completed %s
3. Delete Todo %s
4. Quit!
""";
String message = String.format(template,
todos.isEmpty() ? "(disabled)" : "",
todos.isEmpty() ? "(disabled)" : "");
System.out.println(message);
}
private static void addTodo() {
System.out.println("Enter todo:");
String title = getUserInput();
todos.add(new Todo(title));
System.out.println("Todo named: " + title + " was added bitch. ");
}
private static void displayTodos() {
if (!todos.isEmpty()) {
System.out.println("Your todos: ");
for (int i = 0; i < todos.size(); i++) {
System.out.println(i + 1 + ". " + todos.get(i).returnTodoString());
}
} else {
System.out.println("You have nothing to do. Time to relax 8-)");
}
}
private static void completeTodo() {
if (todos.isEmpty()) {
System.out.println("You don't have todos to manage! \n create some by chosing option one in the menu ");
return;
}
displayTodos();
System.out.println("What have you done?: ");
String userChoice = getUserInput();
boolean validation = validateUserInputInt(userChoice);
if (validation == false) {
return;
}
int parsedUserChoice = Integer.parseInt(userChoice) - 1;
Todo todo = todos.get(parsedUserChoice);
todo.toggleComplete();
}
public static void deleteTodo() {
if (todos.isEmpty()) {
System.out.println("You don't have todos to manage! \n create some by chosing option one in the menu ");
return;
}
displayTodos();
System.out.println("What todo you want to delete?: ");
String userChoice = getUserInput();
boolean validation = validateUserInputInt(userChoice);
if (validation == false) {
return;
}
int parsedUserChoice = Integer.parseInt(userChoice) - 1;
System.out.println("Removed todo: " + todos.get(parsedUserChoice).returnTodoString());
todos.remove(parsedUserChoice);
}
private static String getUserInput() {
return scanner.nextLine().trim();
}
// wanned to make external function to validate user input. It isn't so good as
// I thought It would be xD and it's redundant xD
private static boolean validateUserInputInt(String userInput) {
String regex = "^[1-9]\\d{0,8}$"; // you can make it so it dynamically adds the size of the todos to it, but I
// was too lazy for that there are other todos apps to be coded in other
// languages / framewroks etc.
if (!userInput.matches(regex)) {
System.out.println("You can only use positive numbers,\n and you can't go higher than: 999999999. ");
return false;
}
int validatedUserInput = Integer.parseInt(userInput) - 1;
if (validatedUserInput > (todos.size() - 1)) {
System.out.println("There is no todo with this number. ");
return false;
}
return true;
}
}
class Todo {
public String title;
public boolean checked = false;
public String id = UUID.randomUUID().toString();
public Todo(String title) {
this.title = title;
}
public void toggleComplete() {
this.checked = !this.checked;
System.out.println("Marked as completed: " + this.title);
}
public String returnTodoString() {
return this.title + (this.checked ? " [x] (completed) " : " [ ] (not completed)");
}
}