Created
May 20, 2018 17:50
-
-
Save charsyam/15f5ad2c6703eb48a48f7f83f34e1c90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Map; | |
import java.util.HashMap; | |
class AddressBook { | |
private String name; | |
private String phone; | |
public AddressBook(String name, String phone) { | |
this.name = name; | |
this.phone = phone; | |
} | |
public String toString() { | |
return String.format("%s(%s)", name, phone); | |
} | |
} | |
public class AddressBookManager { | |
private Map<String, AddressBook> books = null; | |
public AddressBookManager() { | |
//Implement to create books HashMap | |
} | |
public void add(String name, String phone) { | |
//Implement to create AddressBook and add into books | |
} | |
public AddressBook remove(String name) { | |
//Implement to remove AddressBook that has name as key | |
//and return it | |
} | |
public void printAll() { | |
//Implement to printAll AddressBook | |
} | |
public AddressBook findByName(String name) { | |
//Implement to find book By Name | |
AddressBook book = null; | |
return book; | |
} | |
public static void showMenu() { | |
System.out.println("===== Menu ====="); | |
System.out.println("1. Add Address"); | |
System.out.println("2. PrintAll"); | |
System.out.println("3. Find By Name"); | |
System.out.println("4. Remove By Name"); | |
System.out.println("5. Quit"); | |
} | |
public static int getMenu(Scanner sc) { | |
return Integer.parseInt(sc.nextLine().trim()); | |
} | |
public static void main(String...args) { | |
Scanner sc = new Scanner(System.in); | |
int count = 0; | |
AddressBookManager bookManager = new AddressBookManager(); | |
while(true) { | |
showMenu(); | |
int menu = getMenu(sc); | |
if (menu == 1) { | |
String name = sc.nextLine(); | |
String phone = sc.nextLine(); | |
bookManager.add(name, phone); | |
} else if (menu == 2) { | |
bookManager.printAll(); | |
} else if (menu == 3) { | |
String name = sc.nextLine(); | |
AddressBook book = bookManager.findByName(name); | |
if (book != null) { | |
System.out.println("Find: " + book.toString()); | |
} else { | |
System.out.println("There is no address : " + name); | |
} | |
} else if (menu == 4) { | |
String name = sc.nextLine(); | |
AddressBook book = bookManager.remove(name); | |
if (book != null) { | |
System.out.println("Delete: " + book.toString()); | |
} else { | |
System.out.println("There is no address : " + name); | |
} | |
} else if (menu == 5) { | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment