-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
115 lines (98 loc) · 3.35 KB
/
Main.java
File metadata and controls
115 lines (98 loc) · 3.35 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
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Todo {
Scanner scan = new Scanner(System.in);
ArrayList<String> tasks = new ArrayList<String>();
File file = new File("C:\\Users\\Moeez\\Desktop\\java\\Project4\\todo.txt");
// lines seperator
public void seperator() {
System.out.println("*********************");
}
// Add Task method
public void addTask() {
seperator();
System.out.print("Enter Task: ");
tasks.add(scan.nextLine());
try (FileWriter filewriter = new FileWriter("todo.txt")) {
for (String t : tasks) {
filewriter.write(t + System.lineSeparator());
}
filewriter.close();
} catch (IOException e) {
System.out.println("Something went wrong while writing to the file");
}
System.out.println("Task added successfully");
seperator();
}
// Delete Task method
public void deleteTask(int index) {
seperator();
System.out.println("Task removed at index: " + index + " " + tasks.remove(index));
seperator();
}
// ViewTask method
public void viewTask() {
seperator();
// try catch is used to read data from the file
try {
Scanner scan = new Scanner(file);
while (scan.hasNextLine()) { // hasNextLine() reads a full line in a file
String line = scan.nextLine();
System.out.println(line); // Print each line to the console
}
scan.close();
} catch (IOException e) {
System.out.println("File not found: " + e.getMessage());
}
seperator(); // this is a function used for styling
}
// if you don't want to print data manually from the arraylist then comment the
// try catch part and uncomment it
// if (tasks.isEmpty()) {
// System.out.println("No Tasks Found, First Add a Task");
// } else {
// for (int i = 0; i < tasks.size(); i++) {
// System.out.println(i + ". " + tasks.get(i));
// }
// seperator();
// }
}
class Main {
public static void main(String[] args) {
Todo t = new Todo();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to todolist app");
boolean keepRunning = true;
while (keepRunning) {
System.out.println("1. Add Task");
System.out.println("2. Remove Task");
System.out.println("3. View Task");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
int n = scan.nextInt();
switch (n) {
case 1:
t.addTask();
break;
case 2:
System.out.println("Enter task number");
t.deleteTask(scan.nextInt());
break;
case 3:
t.viewTask();
break;
case 4:
System.out.println("Exiting... Thank you for using todo");
keepRunning = false;
break;
default:
System.out.println("Invalid input");
break;
}
}
scan.close();
}
}