Quick actions

cmd+k|ctrl+k

Navigation

Languages

Perpustakaan Sederhana dengan ArrayList

Snippet info

Language

Java

Visibility

public

Author

riyanto.droider

Created

2022-12-05T02:24:28.873989Z

Updated

2025-01-12T03:49:47.544274Z

package pertemuan10;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class BukuApp {

    private static int pilihan;
    private static ArrayList<Buku> arrayList;
    private static InputStreamReader isr;
    private static BufferedReader input;


    public static void main(String[] args) throws IOException {
        isr = new InputStreamReader(System.in);
        input = new BufferedReader(isr);

        arrayList = new ArrayList<>();

        do {
            tampilMenu();
            System.out.print("Pilih nomor menu [0-4]: ");
            pilihan = Integer.parseInt(input.readLine());

            switch (pilihan) {
                case 1: tampilBuku(); break;
                case 2: tambahBuku(); break;
                case 3: ubahBuku(); break;
                case 4: hapusBuku(); break;
            }
        }
        while (pilihan != 0);
    }

    private static void ubahBuku() throws IOException {
        System.out.print("Masukkan ID Buku yang akan diubah: ");
        String idBuku = input.readLine();

        for (Buku buku: arrayList) {
            if (idBuku.equalsIgnoreCase(buku.getIdBuku())) {
                System.out.print("Masukkan judul buku baru      : ");
                buku.setJudul(input.readLine());

                System.out.print("Masukkan nama pengarang baru  : ");
                buku.setPengarang(input.readLine());

                System.out.print("Masukkan tahun                : ");
                buku.setTahun(input.readLine());

                arrayList.set(arrayList.indexOf(buku), buku);
            }
        }
    }

    static void tampilMenu() {
        System.out.println("---------------------------");
        System.out.println("0. Keluar\n1. Tampil buku\n2. Tambah buku\n3. Ubah buku\n4. Hapus buku");
        System.out.println("---------------------------");
    }

    static void tampilBuku() {
        if ( ! arrayList.isEmpty()) {
            int i = 1;
            for (Buku buku: arrayList) {
                System.out.println(
                    String.format("%d\t %s\t %s\t %s\t %s",
                        i++,
                        buku.getIdBuku(),
                        buku.getJudul(),
                        buku.getPengarang(),
                        buku.getTahun())
                );
            }
        } else {
            System.out.println("Data buku masih kosong!");
        }
    }

    static void hapusBuku() throws IOException {
        System.out.print("Masukkan ID Buku    : ");
        String idBuku = input.readLine();

        boolean berhasil = false;

        for (Buku buku: arrayList) {
            if (idBuku.equalsIgnoreCase(buku.getIdBuku())) {
                arrayList.remove(buku);
                berhasil = true;
            } else {
                berhasil = false;
            }
        }

        if (berhasil) {
            System.out.printf("Buku dengan ID %s berhasil dihapus\n", idBuku);
        } else {
            System.out.println("Gagal dihapus. ID buku tidak ada");
        }
    }

    static void tambahBuku() throws IOException {
        System.out.print("Masukkan ID Buku      : ");
        String idBuku = input.readLine();

        System.out.print("Masukkan judul buku   : ");
        String judul = input.readLine();

        System.out.print("Masukkan pengarang    : ");
        String pengarang = input.readLine();

        System.out.print("Masukkan tahun terbit : ");
        String tahun = input.readLine();

        arrayList.add(new Buku(idBuku, judul, pengarang, tahun));
    }

}
INFO