Untitled

mail@pastecode.io avatar
unknown
java
3 years ago
8.8 kB
12
Indexable
Never
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tutor_05032022;

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

/**
 *
 * @author chris
 */
public class Tutor_05032022 {
    
    /**
     * @param args the command line arguments
     */
    
    // List question
    // 1. Cara Buat Menu yang benar dan cepat
    
    // 2. Encapsulation itu apa *
    
    // 3. Hapus array *
    
    // 4. Nyambungin menu admin koki karyawan (semacam)
    
    // 5. Random
    
//        int[] angka = {1,2,3,4};
//        // -> Hapus 3 jadi {1,2,4}
//        for (int i : angka) {
//            System.out.println(i);
//        }
//
//        System.out.print("Angka yang mau dihapus : ");
//        int valueHapus = Integer.parseInt(in.nextLine());
//        int[] temp = removeArray(angka, valueHapus);
    
    // PENGECEKAN APAKAH SEMUA NILAI E 0
    // Lek misal e semua nilai e 0 SEMUA, muncul no pesan "ERROR, VALUE GADA"
    // Lek misale nilai e gak 0 SEMUA, REPLACE ANGKA DENGAN TEMP
    // angka = temp
    
//        for (int i : angka) {
//            System.out.println(i);
//        }
    
//        ArrayList<String> arlist = new ArrayList<>();
//        arlist.add("1");
//        arlist.add("2");
//        arlist.add("3");
//        System.out.println(arlist);
//        arlist.remove(2); //REMOVE OBJECT / STRING KE NILAI 2
//        System.out.println(arlist);
    
    // Sistem Library
    // 1. Login admin / user --> username & password "admin"
    // 2. Admin input buku
    // (Buku ada nilai kayak nama , judul, tahun terbit seacamnnya)
    // 3. User bisa pinjam buku
    // (User ada nilai kayak semacamnya)
    // 4. Menu2 biasa, lihat buku lihat ini liaht itu lihat apalah
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        
        String inp_menu = "-1";
        
        while(!inp_menu.equals("3")){
            System.out.println("MENU");
            System.out.println("1. Login");
            System.out.println("2. Register");
            System.out.println("3. Exit");
            
            if(inp_menu.equals("1")){
                // Login
                System.out.print("Masukkan Username : ");
                String inp_username = in.nextLine();
                System.out.print("Masukkan Password : ");
                String inp_pass = in.nextLine();
                if(inp_username.equals("admin") && inp_pass.equals("admin")){
                    //SHOW MENU ADMIN
                    String inp_admin = "";
                    while(!inp_admin.equals("4")){
                        System.out.println("1. Lihat User");
                        System.out.println("2. Lihat Semua Buku");
                        System.out.println("3. Tambah Buku");
                        System.out.println("4. Exit");
                        inp_admin = in.nextLine();
                        
                        if(inp_admin.equals("1")){
                            int ctr = 1;
                            for (User user : User.getListUser()) {
                                System.out.println(String.format("%s. Name : %s\nPassword : %s\nUsername : %s", ctr,user.getNama(), user.getPassword(), user.getUsername()));
                                ctr++;
                            }
                        }else if(inp_admin.equals("2")){
                            int ctr = 1;
                            for (Buku buku : Buku.getListBuku()) {
                                System.out.println(String.format("%s. Judul : %s",ctr, buku.getJudul()));
                                ctr++;
                            }
                        }else if(inp_admin.equals("3")){
                            System.out.print("Judul : ");
                            String judul = in.nextLine();
                            System.out.print("Penerbit : ");
                            String penerbit = in.nextLine();
                            System.out.print("Tahun Terbit : ");
                            int tahunTerbit = Integer.parseInt(in.nextLine());
                            
                            Buku b = new Buku(judul, penerbit, tahunTerbit);
                            Buku.getListBuku().add(b);
                        }
                    }
                }else{
                    User u_login = null;
                    for (User u : User.getListUser()) {
                        if(u.getUsername().equals(inp_username) && u.getPassword().equals(inp_pass)){
                            u_login = u;
                        }
                    }
                    if(u_login == null){
                        System.out.println(" Username / Password gada");
                    }else{
                        // SHOW MENU USER
                        String inp_user = "";
                        
                        while(!inp_user.equals("3")){
                            System.out.println("1. Lihat daftar pinjam buku");
                            System.out.println("2. Pinjam buku");
                            System.out.println("3. Exit");
                            inp_user = in.nextLine();
                            if(inp_user.equals("1")){
                                // 1. Buku apa
                                // 2. Buku apa
                                int ctr = 1;
                                for (Buku buku : u_login.getListBuku()) {
                                    System.out.println(String.format("%s. %s", ctr,buku.getJudul()));
                                    ctr++;
                                }
                            }else if(inp_user.equals("2")){
                                String inp_pinjam = "";
                                int idx_pinjam = -1;
                                while(!inp_pinjam.equals(String.valueOf(Buku.getListBuku().size()+1))){
                                    int ctr = 1;
                                    for (Buku buku : Buku.getListBuku()) {
                                        System.out.println(String.format("%s. %s",ctr,buku.getJudul()));
                                        ctr++;
                                    }
                                    // ASUMSI PASTI ADA BUKUNYA
                                    System.out.print("Input buku yang mau dipinjam : ");
                                    inp_pinjam = in.nextLine();
                                    if(!inp_pinjam.equals(String.valueOf(Buku.getListBuku().size()+1))){
                                        idx_pinjam = Integer.parseInt(inp_pinjam);
                                        inp_pinjam = String.valueOf(Buku.getListBuku().size()+1);
                                    }
                                }
                                if(idx_pinjam != -1){
                                    Buku bukuPinjam = Buku.getListBuku().get(idx_pinjam-1);
                                    u_login.getListBuku().add(bukuPinjam);
                                    System.out.println("SUKSES PINJAM");
                                }
                            }
                        }
                    }
                }
                
                
            }else if(inp_menu.equals("2")){
                // Register
                System.out.println("Masukkan Nama : ");
                String inp_nama = in.nextLine();
                System.out.println("Masukkan Username : ");
                String inp_username = in.nextLine();
                System.out.println("Masukkan Password : ");
                String inp_pass = in.nextLine();
                
                User u = new User(inp_nama, inp_username, inp_pass);
                User.addUser(u);
            }
            
            inp_menu = in.nextLine();
        }
    }
    
    public static int[] removeArray(int[] arr, int val){
        
        int idx = -1;
        
        //Ndapetno idx
        for(int i = 0; i < arr.length; i++){
            if(arr[i] == val){
                idx = i;
            }
        }
        
        int ctr = 0;
        int[] arrBaru = new int[arr.length-1];
        
        if(idx != -1){
            for(int i = 0; i < arr.length; i++){
                if(i != idx){
                    arrBaru[ctr] = arr[i];
                    ctr++;
                }
            }
        }
        return arrBaru;
    }
    
}