-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJournalApp.java
More file actions
101 lines (91 loc) · 3.91 KB
/
Copy pathJournalApp.java
File metadata and controls
101 lines (91 loc) · 3.91 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
import java.util.*;
public class JournalApp {
private static final String STORAGE_FILE = "journal.txt";
public static void main(String[] args) {
JournalStorage storage = new JournalStorage(STORAGE_FILE);
List<JournalEntry> entries = storage.loadEntries();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nJournal App");
System.out.println("1. Add entry");
System.out.println("2. List entries");
System.out.println("3. View entry");
System.out.println("4. Edit entry");
System.out.println("5. Delete entry");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
String choice = scanner.nextLine().trim();
switch (choice) {
case "1" -> addEntry(scanner, storage, entries);
case "2" -> listEntries(entries);
case "3" -> viewEntry(scanner, entries);
case "4" -> editEntry(scanner, storage, entries);
case "5" -> deleteEntry(scanner, storage, entries);
case "6" -> {
System.out.println("Goodbye!");
return;
}
default -> System.out.println("Invalid option.");
}
}
}
private static void addEntry(Scanner scanner, JournalStorage storage, List<JournalEntry> entries) {
System.out.print("Title: ");
String title = scanner.nextLine().trim();
System.out.print("Content: ");
String content = scanner.nextLine().trim();
JournalEntry entry = storage.createEntry(entries, title, content);
System.out.println("Created entry " + entry.getId());
}
private static void listEntries(List<JournalEntry> entries) {
if (entries.isEmpty()) {
System.out.println("No entries yet.");
return;
}
entries.forEach(System.out::println);
}
private static void viewEntry(Scanner scanner, List<JournalEntry> entries) {
JournalEntry entry = findEntry(scanner, entries);
if (entry != null) {
System.out.println("\n" + entry);
System.out.println("Content:");
System.out.println(entry.getContent());
}
}
private static void editEntry(Scanner scanner, JournalStorage storage, List<JournalEntry> entries) {
JournalEntry entry = findEntry(scanner, entries);
if (entry == null) return;
System.out.print("New title (leave blank to keep): ");
String title = scanner.nextLine().trim();
System.out.print("New content (leave blank to keep): ");
String content = scanner.nextLine().trim();
if (!title.isEmpty()) entry.setTitle(title);
if (!content.isEmpty()) entry.setContent(content);
storage.saveEntries(entries);
System.out.println("Updated entry " + entry.getId());
}
private static void deleteEntry(Scanner scanner, JournalStorage storage, List<JournalEntry> entries) {
JournalEntry entry = findEntry(scanner, entries);
if (entry == null) return;
entries.remove(entry);
storage.saveEntries(entries);
System.out.println("Deleted entry " + entry.getId());
}
private static JournalEntry findEntry(Scanner scanner, List<JournalEntry> entries) {
if (entries.isEmpty()) {
System.out.println("No entries available.");
return null;
}
System.out.print("Entry ID: ");
try {
int id = Integer.parseInt(scanner.nextLine().trim());
return entries.stream().filter(e -> e.getId() == id).findFirst().orElseGet(() -> {
System.out.println("Entry not found.");
return null;
});
} catch (NumberFormatException e) {
System.out.println("Invalid ID.");
return null;
}
}
}