-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteTakingApp.java
More file actions
105 lines (94 loc) · 3.63 KB
/
NoteTakingApp.java
File metadata and controls
105 lines (94 loc) · 3.63 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Note {
String title;
String content;
Note(String title, String content) {
this.title = title;
this.content = content;
}
@Override
public String toString() {
return "Title: " + title + "\nContent: " + content;
}
}
public class NoteTakingApp {
private static final List<Note> notes = new ArrayList<>(); // Make notes final
public static void main(String[] args) {
// Using try-with-resources to ensure the scanner is closed properly
try (Scanner scanner = new Scanner(System.in)) {
int choice;
do {
System.out.println("\nNote-Taking Application Menu:");
System.out.println("1. Add Note");
System.out.println("2. View Notes");
System.out.println("3. Update Note");
System.out.println("4. Delete Note");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Using traditional switch statement
switch (choice) {
case 1:
addNote(scanner);
break;
case 2:
viewNotes();
break;
case 3:
updateNote(scanner);
break;
case 4:
deleteNote(scanner);
break;
case 5:
System.out.println("Exiting the application. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}
}
private static void addNote(Scanner scanner) {
System.out.print("Enter note title: ");
String title = scanner.nextLine();
System.out.print("Enter note content: ");
String content = scanner.nextLine();
notes.add(new Note(title, content));
System.out.println("Note added!");
}
private static void viewNotes() {
System.out.println("Your Notes:");
for (int i = 0; i < notes.size(); i++) {
System.out.println((i + 1) + ". " + notes.get(i));
}
}
private static void updateNote(Scanner scanner) {
System.out.print("Enter note number to update: ");
int updateIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
if (updateIndex >= 0 && updateIndex < notes.size()) {
System.out.print("Enter new title: ");
String newTitle = scanner.nextLine();
System.out.print("Enter new content: ");
String newContent = scanner.nextLine();
notes.set(updateIndex, new Note(newTitle, newContent));
System.out.println("Note updated!");
} else {
System.out.println("Invalid note number.");
}
}
private static void deleteNote(Scanner scanner) {
System.out.print("Enter note number to delete: ");
int deleteIndex = scanner.nextInt() - 1;
if (deleteIndex >= 0 && deleteIndex < notes.size()) {
notes.remove(deleteIndex);
System.out.println("Note deleted!");
} else {
System.out.println("Invalid note number.");
}
}
}