-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTodo.java
More file actions
179 lines (173 loc) · 6.11 KB
/
Todo.java
File metadata and controls
179 lines (173 loc) · 6.11 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.mycompany.todo;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class Todo {
//Gui
public static DefaultListModel<String> listModel = new DefaultListModel<>();
public static JList<String> taskList = new JList<>(listModel);
public static JTextField inputField = new JTextField(20);
public static void createGUI() {
//------------------------------------------------------------------------------
JFrame frame = new JFrame("Todo App");
frame.setSize(400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout()); // Make The app have sections
//------------------------------------------------------------------------------
// Top (input + add button)
JPanel topPanel = new JPanel();
JButton addButton = new JButton("Add Task");
//Intput's (Button & Field).
topPanel.add(inputField);
topPanel.add(addButton);
//------------------------------------------------------------------------------
// Center (task list)
refreshList();
JScrollPane scrollPane = new JScrollPane(taskList);
//------------------------------------------------------------------------------
// Bottom (delete button)
JButton deleteButton = new JButton("Delete Selected");
//------------------------------------------------------------------------------
// Set the location of Every Section.
frame.add(topPanel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(deleteButton, BorderLayout.SOUTH);
//------------------------------------------------------------------------------
// 🔥 Actions
addButton.addActionListener(e -> {
String text = inputField.getText().trim();
if (!text.isEmpty()) {
arr.add(text);
saveTasks();
refreshList();
inputField.setText("");
}
});
deleteButton.addActionListener(e -> {
int index = taskList.getSelectedIndex();
if (index != -1) {
int confirm = JOptionPane.showConfirmDialog(
frame,
"Are You Sure you want to delete this Task?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION
);
if (confirm == JOptionPane.YES_OPTION){
arr.remove(index);
saveTasks();
refreshList();
}
} else {
JOptionPane.showMessageDialog(frame, "Select a task first.");
}
});
frame.setVisible(true);
}
public static void refreshList() {
listModel.clear();
for (String task : arr) {
listModel.addElement(task);
}
}
//End of GUI--------------------------------------------------------------------
//Buffer writer to write the file in the System and save it.
public static void saveTasks(){
try{
BufferedWriter writer = new BufferedWriter(new FileWriter("tasks.txt"));
for (String task : arr){
writer.write(task);
writer.newLine();
}
writer.close();
}catch(IOException e){
System.out.println("Error Saving Tasks.");
}
}
public static void loadTasks(){
try{
BufferedReader reader = new BufferedReader(new FileReader("tasks.txt"));
String line;
while ((line = reader.readLine())!= null){
arr.add(line);
}
reader.close();
}
catch(IOException e){
System.out.println("No Previous tasks found.");
}
}
public static ArrayList<String> arr = new ArrayList<>();
public static Scanner input = new Scanner(System.in);
public static void task(){
System.out.println("\nEnter the Task: ");
arr.add(input.nextLine());
saveTasks();
System.out.println("\nTask Added Successfully.\n");
}
public static void view(){
if(arr.isEmpty()){
System.out.println("No tasks available.");
} else {
for(int i = 0; i < arr.size(); i++){
System.out.println((i+1) + " - " + arr.get(i));
}
System.out.println("______________________________________________");
}
}
public static void del(int del){
if(del>=0&&del<arr.size()){
arr.remove((del-1));
saveTasks();
System.out.println("\nTask Deleted Successfully.\n");
}
else{
System.out.println("\nInvalid Index.\n ");}
}
public static void main(String[] args) {
loadTasks();
createGUI();
int choice = 0 ;
System.out.println("Welcome At the Todo List App: ");
do{
System.out.println("\nChoose an option:");
System.out.println("1- Add Task");
System.out.println("2- View Tasks");
System.out.println("3- Delete Task");
System.out.println("0- Exit");
System.out.println("------------------------------------\n");
if(input.hasNextInt()){
choice = input.nextInt();
}
else{
System.out.println("Invalid input. Please Enter Numbers Only...");
input.nextLine();
continue;
}
input.nextLine();
switch(choice) {
case 1:
task();
break;
case 2:
System.out.println();
view();
break;
case 3:
int ch ;
System.out.println("\nEnter the number Item You need to delete: ");
view();
ch = input.nextInt();
input.nextLine();
del(ch);
break;
case 0:
break;
default:
System.out.println("Invalid Input. ");
}
}while(choice!=0);
System.out.println("Goodbye!");
}
}