Skip to content

Commit

Permalink
added solution from the code workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
daniquemont committed Jun 28, 2024
1 parent 1ac8079 commit c12a797
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.sig.training.ch03;

import java.awt.Color;
import java.util.Arrays;
import java.util.List;

public class Flag {
private final List<Color> colors;

public Flag(Color... colors) {
this.colors = Arrays.asList(colors);
}

public List<Color> getColors() {
return colors;
}
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
package eu.sig.training.ch03;

import static eu.sig.training.ch03.Nationality.BELGIAN;
import static eu.sig.training.ch03.Nationality.DUTCH;
import static eu.sig.training.ch03.Nationality.FRENCH;
import static eu.sig.training.ch03.Nationality.GERMAN;
import static eu.sig.training.ch03.Nationality.ITALIAN;

import java.awt.Color;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlagFactory {
public class FlagFactoryWithMap {

// tag::getFlag[]
private static Map<Nationality, List<Color>> FLAGS =
new HashMap<Nationality, List<Color>>();

static {
FLAGS.put(DUTCH, Arrays.asList(Color.RED, Color.WHITE, Color.BLUE));
FLAGS.put(GERMAN, Arrays.asList(Color.BLACK, Color.RED, Color.YELLOW));
FLAGS.put(BELGIAN, Arrays.asList(Color.BLACK, Color.YELLOW, Color.RED));
FLAGS.put(FRENCH, Arrays.asList(Color.BLUE, Color.WHITE, Color.RED));
FLAGS.put(ITALIAN, Arrays.asList(Color.GREEN, Color.WHITE, Color.RED));
}

public List<Color> getFlagColors(Nationality nationality) {
List<Color> result;
switch (nationality) {
case DUTCH:
result = Arrays.asList(Color.RED, Color.WHITE, Color.BLUE);
break;
case GERMAN:
result = Arrays.asList(Color.BLACK, Color.RED, Color.YELLOW);
break;
case BELGIAN:
result = Arrays.asList(Color.BLACK, Color.YELLOW, Color.RED);
break;
case FRENCH:
result = Arrays.asList(Color.BLUE, Color.WHITE, Color.RED);
break;
case ITALIAN:
result = Arrays.asList(Color.GREEN, Color.WHITE, Color.RED);
break;
case ROMANIA:
result = Arrays.asList(Color.BLUE, Color.YELLOW, Color.RED);
break;
case IRELAND:
result = Arrays.asList(Color.GREEN, Color.WHITE, Color.ORANGE);
break;
case HUNGARIAN:
result = Arrays.asList(Color.RED, Color.WHITE, Color.GREEN);
break;
case BULGARIAN:
result = Arrays.asList(Color.WHITE, Color.GREEN, Color.RED);
break;
case RUSSIA:
result = Arrays.asList(Color.WHITE, Color.BLUE, Color.RED);
break;
case LUXEMBOURG:
result = Arrays.asList(Color.RED, Color.BLUE, Color.LIGHTBLUE);
break;
case SPAIN:
result = Arrays.asList(Color.RED, Color.YELLOW, Color.RED);
break;
case UNCLASSIFIED:
default:
result = Arrays.asList(Color.GRAY);
break;
}
return result;
List<Color> colors = FLAGS.get(nationality);
return colors != null ? colors : Arrays.asList(Color.GRAY);
}
// end::getFlag[]

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package eu.sig.training.ch03;

public enum Nationalities {
DUTCH, GERMAN, BELGIAN, FRENCH, ITALIAN, ROMANIA, IRELAND, HUNGARIAN, BULGARIAN, RUSSIA, LUXEMBOURG, RUSSIA
}

0 comments on commit c12a797

Please sign in to comment.