Copy import java.util.*;
public class NotebookApp{
private final List<Note> notes = new ArrayList<>();
private final Scanner scanner = new Scanner(System.in);
public static void main(String[] args){
new NotebookApp().run();
}
private void run(){
int choice;
do {
System.out.println("\n=== Personal Notebook ===");
System.out.println("1. Tambah Catatan");
System.out.println("2. Lihat Semua Catatan");
System.out.println("3. Lihat Catatan (grup inisial)");
System.out.println("4. Cari Catatan (berdasarkan judul)");
System.out.println("5. Hapus Catatan (berdasarkan judul)");
System.out.println("6. Keluar");
System.out.print("Pilih menu: ");
choice = readInt();
switch (choice) {
case 1 -> addNote();
case 2 -> listNotes();
case 3 -> listGroupedByInitial();
case 4 -> searchNotes();
case 5 -> deleteNote();
case 6 -> System.out.println("Keluar dari Notebook. Sampai jumpa!");
default -> System.out.println("Pilihan tidak valid, coba lagi.");
}
} while (choice != 6);
scanner.close();
}
private int readInt(){
while (true){
String line = scanner.nextLine();
try{ return Integer.parseInt(line.trim()); }
catch (NumberFormatException e) { System.out.print("Masukkan angka: "); }
}
}
private void addNote(){
System.out.print("Masukkan judul catatan: ");
String title = scanner.nextLine();
System.out.print("Masukkan isi catatan: ");
String content = scanner.nextLine();
notes.add(new Note(title, content));
System.out.println("Catatan berhasil ditambahkan!");
}
private void listNotes(){
if (notes.isEmpty()){
System.out.println("Belum ada catatan.");
return;
}
System.out.println("\nDaftar Catatan:");
for (int i = 0; i < notes.size(); i++){
System.out.println((i + 1) + ". " + notes.get(i).getTitle());
}
}
private void listGroupedByInitial() {
if (notes.isEmpty()) {
System.out.println("Belum ada catatan.");
return;
}
Map<Character, List<Note>> groups = new TreeMap<>();
for (Note n : notes){
char key = n.getTitle().isEmpty() ? '#' : Character.toUpperCase(n.getTitle().charAt(0));
groups.computeIfAbsent(key, k -> new ArrayList<>()).add(n);
}
System.out.println("\nCatatan Tergrup (berdasarkan huruf awal judul):");
for (Map.Entry<Character, List<Note>> e : groups.entrySet()){
System.out.println("\n[" + e.getKey() + "]");
for (Note n : e.getValue()){
System.out.println("- " + n.getTitle());
}
}
}
private void searchNotes(){
System.out.print("Masukkan judul catatan yang dicari: ");
String searchTitle = scanner.nextLine().toLowerCase();
boolean found = false;
for (Note note : notes){
if (note.getTitle().toLowerCase().contains(searchTitle)){
System.out.println("\nCatatan ditemukan:\n" + note);
found = true;
}
}
if (!found) System.out.println("Catatan tidak ditemukan.");
}
private void deleteNote(){
System.out.print("Masukkan judul catatan yang akan dihapus: ");
String deleteTitle = scanner.nextLine().toLowerCase();
boolean removed = notes.removeIf(n -> n.getTitle().toLowerCase().equals(deleteTitle));
System.out.println(removed ? "Catatan berhasil dihapus." : "Catatan dengan judul tersebut tidak ditemukan.");
}
}