Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.8 kB
3
Indexable
Never
package com.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class latihan {
    public static void main(String[] args) {
        int menu;
        boolean validateInt = false;
        HashMap<Integer,String> HashMapPerson = new HashMap<>();
        HashMapPerson.put(1,"andi");
        HashMapPerson.put(2,"budi");
        HashMapPerson.put(3,"caca");
        while (!validateInt) {
           try {
               Scanner scan = new Scanner(System.in);
               System.out.println("===========================");
               System.out.println("1. tambah data: ");
               System.out.println("2. tampilkan data: ");
               System.out.println("3. cari data");
               System.out.println("masukan pilihan: ");
               menu = scan.nextInt();
               validateInt = true;
               switch (menu) {
                   case 1:
                         String nama;

                         boolean input1 = false;
                           scan.nextLine();
                           for (int hash = 4; hash > HashMapPerson.size(); hash++) {
                               while (!input1) {
                                   System.out.print("masukan nama: ");
                                   nama = scan.nextLine();
                                   HashMapPerson.put(hash, nama);
                                   System.out.print("Input lagi ? (Y/N) : ");
                                   String confirm = scan.nextLine();
                                   if (confirm.equals("y") || confirm.equals("Y")) {
                                       input1 = false;
                                   }else{
                                       input1 = true;
                                       validateInt = false;
                                   }
                                }
                             }
                       break;

                   case 2:
                       int no = 1;
                       for (Map.Entry value: HashMapPerson.entrySet()) {
                           System.out.print(no++ +". ");
                           System.out.println(value);
                       }
                       validateInt = false;
                       break;
                   case 3:
                       boolean case3 = false;
                       scan.nextLine();
                       while (!case3) {
                           System.out.print("masukan nama yang ingin dicari: ");
                           String filter = scan.nextLine();
                           String cekData = HashMapPerson.containsValue(filter) ? filter + " ditemukan"
                                   : filter + " tidak ditemukan";
                           System.out.println(cekData);
                           System.out.print("Cari lagi ? (Y/N) : ");
                           String confirm = scan.nextLine();
                           if (confirm.equals("y") || confirm.equals("Y")) {
                               case3 = false;
                           }else{
                               case3 = true;
                               validateInt = false;
                           }
                       }
                       break;
                   case 4:
                       System.exit(0);
                       break;
                   default:
                       System.out.println("masukan pilihan yang tertera");
                       validateInt = false;
               }
           }catch (Exception ex){
               System.out.println("Inputan yang anda masukan salah");
               validateInt = false;
           }
        }

    }
}