Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- private Hashtable phoneBooks;
- public Main(){
- this.phoneBooks = new Hashtable();
- }
- public void tambah(String name, String number){
- if(this.phoneBooks.containsKey(name)){
- System.out.println(name+ " exists.");
- } else {
- this.phoneBooks.put(name, number);
- System.out.println(name+" success.");
- }
- }
- public void cari (String name){
- if(this.phoneBooks.containsKey(name)){
- String number = (String) this.phoneBooks.get(name);
- System.out.println(name+"'s number is "+number+".");
- } else {
- System.out.println("Unknown '"+name+"'");
- }
- }
- public void status(){
- Enumeration names;
- names = this.phoneBooks.keys();
- int total = 0;
- while(names.hasMoreElements()) {
- String str = (String) names.nextElement();
- System.out.println(str + " " + this.phoneBooks.get(str));
- total++;
- }
- System.out.println("Total "+total+" kontak.");
- }
- public void hapus_entry(String name){
- if(this.phoneBooks.containsKey(name)){
- this.phoneBooks.remove(name);
- System.out.println(name+" deleted.");
- } else {
- System.out.println("Unknown'"+name+"'");
- }
- }
- public static void main(String[] args ) throws IOException {
- Scanner scanner = new Scanner(System.in);
- Main phoneBook = new Main();
- while(true){
- System.out.print("Choose category\n");
- System.out.print("1. Add contact \n");
- System.out.print("2. Search contact \n");
- System.out.print("3. Status \n");
- System.out.print("4. Delete contact \n");
- System.out.print("5. Exit \n");
- System.out.print("Command: ");
- int command = scanner.nextInt();
- if(command==1){
- System.out.print("Name: ");
- String name = scanner.next();
- System.out.print("Number: ");
- String number = scanner.next();
- phoneBook.tambah(name, number);
- }
- else if(command==2){
- System.out.print("Name: ");
- String name = scanner.next();
- phoneBook.cari(name);
- }
- else if(command==3){
- phoneBook.status();
- }
- else if(command==4){
- System.out.print("Name: ");
- String name = scanner.next();
- phoneBook.hapus_entry(name);
- }
- else if(command==5){
- break;
- }
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment