generated from clean-code-workshop/sigrid-training-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added solution from the code workshop
- Loading branch information
1 parent
1ac8079
commit c12a797
Showing
3 changed files
with
45 additions
and
46 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
training-assignments/src/java/eu/sig/training/ch03-complexity/flags/Flag.java
This file contains 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
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; | ||
} | ||
} |
69 changes: 23 additions & 46 deletions
69
training-assignments/src/java/eu/sig/training/ch03-complexity/flags/FlagFactory.java
This file contains 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
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[] | ||
|
||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
training-assignments/src/java/eu/sig/training/ch03-complexity/flags/Nationality.java
This file contains 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
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 | ||
} |