Object Oriented Programming | Week 6: Grouping Object - Membuat Notebook

    Pada pertemuan minggu ke-6, saya mempelajari grouping object dengan manggunakan Array dan ArrayList serta bagaimana cara-cara melakukan iterasi pada tiap-tiap object di dalam Array. Saya mengimplementasikannya dengan membuat sebuah aplikasi notebook interaktif yang sederhana tanpa database. menu yang terdapat pada aplikasi notebook antara lain menambah catatan, melihat list catatan, mencari catatan, menghapus catatan, dan opsi untuk keluar dari aplikasi.




Berikut implementasi classnya:


Note.java:
Note.java berisi atribut-atribut yang dimiliki oleh satu note seperti judul, content/isi, dan juga tanggal pembuatan.


/**
 * Write a description of class Note here.
 *
 * @author Muhammad Zaky Zein
 * @version 5.10.2025
 */

import java.time.LocalDate;

public class Note
{
    private String title;
    private String content;
    private LocalDate date;
    
    Note(String title, String content) {
        this.title = title;
        this.content = content;
        this.date = LocalDate.now();
    }

    public String getTitle() { return title; }
    public String getContent() { return content; }
    public LocalDate getDate() { return date; }

    @Override
    public String toString() {
        return "Title: " + this.title + "\n" + this.date + "\nContent: " + this.content;
    }
}

Notebook.java:
Notebook.java merupakan main entry dari program, class ini merupakan main class untuk menjalankan program yang berfungsi untuk menginisialisasikan serta melakukan grouping object Note, sehingga dapat diakses oleh user.


/**
 * Write a description of class Note here.
 *
 * @author Muhammad Zaky Zein
 * @version 5.10.2025
 */

import java.util.ArrayList;
import java.util.Scanner;

public class Notebook
{
    public static void main(String[] args) {
        ArrayList<Note> notes = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("MyNote App");
            System.out.println("==========");
            System.out.println("1. add new note");
            System.out.println("2. Show all notes");
            System.out.println("3. Find note");
            System.out.println("4. Delete a note");
            System.out.println("5. Exit");
            System.out.print("Select menu: ");
            choice = scanner.nextInt();
            scanner.nextLine();
            
            switch (choice) {
                case 1:
                    System.out.print("Title: ");
                    String title = scanner.nextLine();
                    System.out.print("Content: ");
                    String content = scanner.nextLine();
                    notes.add(new Note(title, content));
                    System.out.println("New Note added successfully");
                    break;
                    case 2:
                    if (notes.isEmpty()) {
                        System.out.println("There is no notes yet");
                    } else {
                        for (int i = 0; i < notes.size(); i++) {
                            System.out.println((i+1) + ". " + notes.get(i).getTitle());
                        }
                    }
                    break;
                case 3:
                    System.out.print("Enter note title: ");
                    String findTitle = scanner.nextLine().toLowerCase();
                    boolean found = false;
                    for (Note note : notes) {
                        if (note.getTitle().toLowerCase().contains(findTitle)) {
                            System.out.println(note.toString());
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        System.out.println("Note not found");
                    }
                    break;
                case 4:
                    System.out.print("Enter note title: ");
                    String deleteTitle = scanner.nextLine().toLowerCase();
                    boolean removed = notes.removeIf(note->note.getTitle().toLowerCase().equals(deleteTitle));
                    if (removed) {
                        System.out.println("Note deleted successfully");
                    } else {
                        System.out.println("Note not found");
                    }
                    break;
                case 5:    
                    System.out.println("Exiting notebook...");
                    break;
                default:
                    System.out.println("Invalid input.");
                    break;
            }
        } while (choice != 5);

        scanner.close();
    }
}

    Pada kode di atas, grouping dilakukan dengan menggunakan ArrayList. Ada beberapa cara untuk melakukan iterasi pada masing-masing groupnya, bisa menggunakan for loop biasa, dan gunakan notes.get(i) untuk mendapatkan object pada index i, dan bisa juga menggunakan for each [for (Note note : notes)].

Demikian yang saya pelajari pada minggu keenam perkuliahan pemrograman berorientasi objek.


Muhammad Zaky Zein
5025241148

Comments