document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.util.*;
  2.  
  3. public class HashPhonebook{
  4.    
  5.     public static int hashFunction(String nama){
  6.         int hash = 0;
  7.         for(int i=0; i<nama.length(); i++){
  8.             hash = hash*10 + nama.charAt(i);
  9.         }
  10.         hash = hash % 499;
  11.         return hash;
  12.     }
  13.  
  14.     public static void main(String[] args){
  15.         Hashtable<Integer, String> phonebook = new Hashtable<>();
  16.         List<String> str = new ArrayList<>();
  17.         Scanner sc = new Scanner(System.in);
  18.  
  19.         int choice = -1;
  20.         String nomor, nama;
  21.        
  22.         System.out.println("   Silahkan Memilih");
  23.         System.out.println("1. Tambahkan nomor baru");
  24.         System.out.println("2. Cari nomor");
  25.         System.out.println("3. Hapus nomor");
  26.         System.out.println("4. Lihat semua kontak");
  27.         System.out.println("5. Exit\\n");
  28.        
  29.         while(choice != 5){
  30.             System.out.print("Masukkan pilihan : ");
  31.             choice = sc.nextInt();
  32.          
  33.             if (choice == 1){
  34.                 System.out.print("Masukkan nomor telepon : ");
  35.                 nomor = sc.next();
  36.                
  37.                 System.out.print("Masukkan nama kontak : ");
  38.                 nama = sc.next();
  39.                
  40.                 str.add(nama);                
  41.                 phonebook.put(hashFunction(nama), nomor);
  42.                
  43.                 System.out.println("Kontak telah ditambahkan\\n");  
  44.             }
  45.             else if(choice == 2){
  46.                 System.out.print("Masukkan nama kontak : ");
  47.                 nama = sc.next();
  48.                
  49.                 nomor = phonebook.get(hashFunction(nama));
  50.                
  51.                 System.out.println("Nomor telepon : " +  nomor);
  52.                 System.out.print("\\n");  
  53.             }
  54.             else if (choice == 3){
  55.                 System.out.print("Masukkan nama kontak : ");
  56.                 nama = sc.next();
  57.                
  58.                 str.remove(nama);
  59.                 phonebook.remove(hashFunction(nama));
  60.                
  61.                 System.out.println("Kontak telah dihapus\\n");            
  62.             }
  63.             else if (choice == 4){
  64.                 if(phonebook.size() > 0){
  65.                     System.out.println("Terdapat " + phonebook.size() + " kontak tersimpan");
  66.                         for(String i : str){
  67.                             System.out.println(i + " => " + phonebook.get(hashFunction(i)));
  68.                         }
  69.                     System.out.print("\\n");
  70.                 }
  71.                 else
  72.                     System.out.println("Tidak ada kontak yang tersimpan\\n");
  73.             }
  74.         }
  75.     }
  76. }
');