File tree Expand file tree Collapse file tree 6 files changed +74
-0
lines changed Expand file tree Collapse file tree 6 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+ public interface Blacksmith {
2+ Weapon manufactureWeapon (WeaponType weaponType );
3+ }
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class ElfBlacksmith implements Blacksmith {
5+
6+ private static final Map <WeaponType , Weapon > ELF_ARSENAL = new HashMap <>();
7+
8+ static {
9+ ELF_ARSENAL .put (WeaponType .SPEAR , new Weapon ("Elven Spear" ));
10+ ELF_ARSENAL .put (WeaponType .AXE , new Weapon ("Elven Axe" ));
11+ }
12+
13+ @ Override
14+ public Weapon manufactureWeapon (WeaponType weaponType ) {
15+ return ELF_ARSENAL .get (weaponType );
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ public class Main {
2+ public static void main (String [] args ) {
3+
4+ Blacksmith blacksmith ;
5+
6+ // Orc weapons
7+ blacksmith = new OrcBlacksmith ();
8+ Weapon weapon1 = blacksmith .manufactureWeapon (WeaponType .SPEAR );
9+ System .out .println ("Orc Blacksmith manufactured: " + weapon1 .getName ());
10+
11+ Weapon weapon2 = blacksmith .manufactureWeapon (WeaponType .AXE );
12+ System .out .println ("Orc Blacksmith manufactured: " + weapon2 .getName ());
13+
14+ // Elf weapons
15+ blacksmith = new ElfBlacksmith ();
16+ Weapon weapon3 = blacksmith .manufactureWeapon (WeaponType .SPEAR );
17+ System .out .println ("Elf Blacksmith manufactured: " + weapon3 .getName ());
18+
19+ Weapon weapon4 = blacksmith .manufactureWeapon (WeaponType .AXE );
20+ System .out .println ("Elf Blacksmith manufactured: " + weapon4 .getName ());
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class OrcBlacksmith implements Blacksmith {
5+
6+ private static final Map <WeaponType , Weapon > ORC_ARSENAL = new HashMap <>();
7+
8+ static {
9+ ORC_ARSENAL .put (WeaponType .SPEAR , new Weapon ("Orcish Spear" ));
10+ ORC_ARSENAL .put (WeaponType .AXE , new Weapon ("Orcish Axe" ));
11+ }
12+
13+ @ Override
14+ public Weapon manufactureWeapon (WeaponType weaponType ) {
15+ return ORC_ARSENAL .get (weaponType );
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ public class Weapon {
2+ private final String name ;
3+
4+ public Weapon (String name ) {
5+ this .name = name ;
6+ }
7+
8+ public String getName () {
9+ return name ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ public enum WeaponType {
2+ SPEAR ,
3+ AXE
4+ }
You can’t perform that action at this time.
0 commit comments