Skip to content

Instantly share code, notes, and snippets.

@nikhilsaraf
Created July 16, 2020 20:42
Show Gist options
  • Save nikhilsaraf/23feff2ed32e4c673645ad82edcbf1ee to your computer and use it in GitHub Desktop.
Save nikhilsaraf/23feff2ed32e4c673645ad82edcbf1ee to your computer and use it in GitHub Desktop.
/**
AddressBook is a model for how to store the address or phone number for individuals.
*/
class AddressBook {
private Map<String, String> map;
public AddressBook(Map<String, String> map) {
this.map = map;
}
/**
sample code that creates an AddressBook using a Map<String, String> as input.
Usage:
abook = AddressBook.makeSimple();
phoneNumber = abook.get("Ava"); // (415)-730-xxxx
*/
static AddressBook makeSimple() {
Map<String, String> map = new HashMap<>();
map.put("Ava", "(415)-730-xxxx");
map.put("Bob", "(512)-349-xxxx");
map.put("Carl", "(917)-123-xxxx");
// use the constructor to create the AddressBook
return new AddressBook(map);
}
public String get(String name) {
// reference the internal map data structure to get the result
return this.map.get(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment