Advertisement
Samuel_Berkat_Hulu

hashdhsdf

Jun 22nd, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class HashTable2 here.
  4.  *
  5.  * Samuel Berkat Hulu
  6.  * @version 5.0 Tugas Struktur Data
  7.  */
  8.  
  9. import java.util.*;
  10. public class HastablePhonenumberApp
  11. {
  12.     public static int hashFunction(String name){
  13.         int hash = 0;
  14.         for(int i=0; i<name.length(); i++){
  15.             hash = hash*10 + name.charAt(i);
  16.         }
  17.         hash = hash % 499;
  18.         return hash;
  19.     }
  20.    
  21.     public static void main(String args[]){
  22.         Hashtable<Integer, String> phonebook = new Hashtable<>();
  23.         List<String> names = new ArrayList<String>();
  24.         Scanner sc = new Scanner(System.in);
  25.        
  26.         int choice = -1;
  27.         String number;
  28.         String name;
  29.         while(choice != 0){
  30.             System.out.println("Silahkan  pilih:\n1. Tambahkan kontak\n2. Cari kontak\n3. Hapus kontak\n4. Lihat semua\n0. Keluar");
  31.             choice = sc.nextInt();
  32.             switch(choice){
  33.                 case 1:
  34.                     System.out.println("Masukkan nomor telepon:");
  35.                     number = sc.next();
  36.                     System.out.println("Masukkan nama kontak:");
  37.                     name = sc.next();
  38.                     names.add(name);
  39.                     phonebook.put(hashFunction(name), number);
  40.                     System.out.println("Kontak telah ditambahkan");
  41.                     break;
  42.                 case 2:
  43.                     System.out.println("Masukkan nama kontak:");
  44.                     name = sc.next();
  45.                     number = phonebook.get(hashFunction(name));
  46.                     System.out.println("Nomor telepon: " +  number);
  47.                     break;
  48.                 case 3:
  49.                     System.out.println("Masukkan nama kontak:");
  50.                     name = sc.next();
  51.                     names.remove(name);
  52.                     phonebook.remove(hashFunction(name));
  53.                     System.out.println("Kontak telah dihapus");
  54.                     break;
  55.                 case 4:
  56.                     if(phonebook.size() > 0){
  57.                         System.out.println(phonebook.size() + " kontak tersimpan");
  58.                         for(String i : names){
  59.                             System.out.println(i + ": " + phonebook.get(hashFunction(i)));
  60.                         }
  61.                     }
  62.                     else
  63.                         System.out.println("Tidak ada kontak yang tersimpan");
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement