Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created January 8, 2016 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIRHAMY/673336e968d1fbfd238f to your computer and use it in GitHub Desktop.
Save SIRHAMY/673336e968d1fbfd238f to your computer and use it in GitHub Desktop.
My WorldProblem solution.
public class City implements Municipality{
private int population;
private String name;
public City(String name, int population) {
this.name = name;
this.population = population;
}
@Override
public int getPopulation() {
return population;
}
@Override
public String getString() {
return name;
}
}
public class Country implements Municipality{
private String name;
private Municipality[] municipalities;
public Country(String name, Municipality... entities) {
this.name = name;
municipalities = entities;
}
@Override
public int getPopulation() {
int result = 0;
for(Municipality mun: municipalities) {
result += mun.getPopulation();
}
return result;
}
@Override
public String getString() {
StringBuffer result = new StringBuffer();
result.append(name);
for(Municipality mun: municipalities) {
result.append(" " + mun.getString());
}
return result.toString();
}
}
public class District implements Municipality{
private int population;
private String name;
public District(String name, int population) {
this.name = name;
this.population = population;
}
@Override
public int getPopulation() {
return population;
}
@Override
public String getString() {
return name;
}
}
public class Main {
public static void main(String[] args) {
World myWorld = new World(
new Country("USA",
new State("NY",
new City("New York", 8143197)),
new City("LA", 3844829),
new City("Chicago", 2842518),
new District("Washington D.C.", 658893)),
new Country("Germany",
new City("Berlin", 3336026),
new City("Hamburg", 1605606),
new City("Munich", 831937)),
new Country("Tamriel",
new State("Skyrim",
new City("Dragonsreach", 580),
new City("Falkreath", 390),
new District("Sheogorath's Realm", 666)),
new State("Morrowind",
new City("Seyda Neen", 46))));
myWorld.printPopulation();
System.out.println();
myWorld.printWorld();
}
}
public interface Municipality {
public String getString();
public int getPopulation();
}
public class State implements Municipality{
private String name;
private Municipality[] municipalities;
public State(String name, Municipality... entities) {
this.name = name;
municipalities = entities;
}
@Override
public int getPopulation() {
int result = 0;
for(Municipality mun: municipalities) {
result += mun.getPopulation();
}
return result;
}
@Override
public String getString() {
StringBuffer result = new StringBuffer();
result.append(name);
for(Municipality mun: municipalities) {
result.append(" " + mun.getString());
}
return result.toString();
}
}
public class World {
private Country[] countries;
public World(Country... entities) {
countries = entities;
}
public void printPopulation() {
int result = 0;
for(Country c: countries) {
result += c.getPopulation();
}
System.out.format("World Population: %d", result);
}
public void printWorld() {
StringBuffer result = new StringBuffer();
for(Country c: countries) {
result.append(c.getString() + " ");
}
System.out.println("World to String: " + result.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment