Last active
January 27, 2020 14:53
-
-
Save benjjo/e265a4461e934f714fc8c18d67879734 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.HashMap; | |
| /** | |
| * HashMap example to show the mapping of a String to an Integer | |
| * | |
| * @author benjo charlie | |
| * @version 1.2 | |
| * Changelog: Changed Map to Static Final. | |
| */ | |
| public class CountryAgeOfMagority | |
| { | |
| // instance variables - String to Integer type reference | |
| private static final HashMap<String, Integer> ageOfMagority = new HashMap<>(); | |
| /** | |
| * Constructor for objects of class CountryAgeOfMagority | |
| */ | |
| public CountryAgeOfMagority() | |
| { | |
| //this.ageOfMagority = new HashMap<>(); | |
| this.mapCodeToAge(); | |
| } | |
| /** | |
| * Getter method for the HashMap String | |
| */ | |
| public String getAgeOfMagority() | |
| { | |
| return "" + CountryAgeOfMagority.ageOfMagority; | |
| } | |
| /** | |
| * Instance method to create the HashMap of country codes and ages. | |
| */ | |
| private void mapCodeToAge() { | |
| // Add keys and values (Country, Age) | |
| CountryAgeOfMagority.ageOfMagority.put("IR", 14); | |
| CountryAgeOfMagority.ageOfMagority.put("ID", 15); | |
| CountryAgeOfMagority.ageOfMagority.put("KH", 16); | |
| CountryAgeOfMagority.ageOfMagority.put("KP", 17); | |
| CountryAgeOfMagority.ageOfMagority.put("AU", 18); | |
| CountryAgeOfMagority.ageOfMagority.put("CA", 19); | |
| CountryAgeOfMagority.ageOfMagority.put("JP", 20); | |
| CountryAgeOfMagority.ageOfMagority.put("US", 21); | |
| System.out.println(ageOfMagority); | |
| } | |
| /** | |
| * Setter for the map | |
| */ | |
| public void setMap(String newCountry, int newAge) { | |
| CountryAgeOfMagority.ageOfMagority.put(newCountry, newAge); | |
| } | |
| /** | |
| * Getter method to retreive the age information from the counrty code. | |
| */ | |
| public int getAgeFromKey(String key) { | |
| return CountryAgeOfMagority.ageOfMagority.get(key); | |
| } | |
| /** | |
| * Returns all instance variable values. | |
| */ | |
| public String toString() { | |
| return "" + CountryAgeOfMagority.ageOfMagority; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment