Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
abstractmethod solution in Uncategorized category for Army Units by GlulkAlex
#import abc
#from abc import ABC
from abc import ABCMeta, abstractmethod
class Swordsman(object):
# class attr
_specialization = "swordsman"
def __init__(
self
# Knight
#, unit_Type: str
# Jaks
, unit_Name: str
# European
#, army_Type: str
# basic class: swordsman
#, specialization: str
):
""" instance props """
self._type = None#unit_Type
self._name = unit_Name
self._army_Type = None#army_Type
def introduce( self ) -> str:
return ( f"{self._type} {self._name}, "
f"{self._army_Type} {self._specialization}" )
class Lancer(object):
_specialization = "lancer"
def __init__(
self
, unit_Name: str
):
""" instance props """
self._type = None#unit_Type
self._name = unit_Name
self._army_Type = None#army_Type
def introduce( self ) -> str:
return ( f"{self._type} {self._name}, " +
f"{self._army_Type} {self._specialization}" )
class Archer(object):
_specialization = "archer"
def __init__(
self
, unit_Name: str
):
""" instance props """
self._type = None#unit_Type
self._name = unit_Name
self._army_Type = None#army_Type
def introduce( self ) -> str:
return ( f"{self._type} {self._name}, " +
f"{self._army_Type} {self._specialization}" )
class Army(
#object
metaclass=ABCMeta
):
@abstractmethod
def train_swordsman(
self
#, unit_Type: str
, unit_Name: str
#, army_Type: str
) -> Swordsman:
""" abstract methods may have an implementation """
return NotImplementedError()
@abstractmethod
def train_lancer(
self
, unit_Name: str
) -> Lancer:
""" abstract methods may have an implementation """
return NotImplementedError()
@abstractmethod
def train_archer(
self
, unit_Name: str
) -> Archer:
""" abstract methods may have an implementation """
return NotImplementedError()
class AsianArmy( Army ):
_army_Type = "Asian"
def train_swordsman( self, name: str ) -> Swordsman:
unit = Swordsman( name )
unit._army_Type = self._army_Type
unit._type = "Samurai"
return unit
def train_lancer( self, name: str ) -> Lancer:
unit = Lancer( name )
unit._army_Type = self._army_Type
unit._type = "Ronin"
return unit
def train_archer( self, name: str ) -> Archer:
unit = Archer( name )
unit._army_Type = self._army_Type
unit._type = "Shinobi"
return unit
class EuropeanArmy( Army ):
_army_Type = "European"
def train_swordsman( self, name: str ) -> Swordsman:
unit = Swordsman( name )
unit._army_Type = self._army_Type
unit._type = "Knight"
return unit
def train_lancer( self, name: str ) -> Lancer:
unit = Lancer( name )
unit._army_Type = self._army_Type
unit._type = "Raubritter"
return unit
def train_archer( self, name: str ) -> Archer:
unit = Archer( name )
unit._army_Type = self._army_Type
unit._type = "Ranger"
return unit
if __name__ == '__main__':
# These "asserts"
# using only for self-checking
# and not necessary for auto-testing
my_army = EuropeanArmy()
enemy_army = AsianArmy()
soldier_1 = my_army.train_swordsman("Jaks")
soldier_2 = my_army.train_lancer("Harold")
soldier_3 = my_army.train_archer("Robin")
soldier_4 = enemy_army.train_swordsman("Kishimoto")
soldier_5 = enemy_army.train_lancer("Ayabusa")
soldier_6 = enemy_army.train_archer("Kirigae")
expected = "Knight Jaks, European swordsman"
actual = soldier_1.introduce()
assert actual == expected, ( "EuropeanArmy.train_swordsman: " +
f"{actual} != {expected}" )
expected = "Raubritter Harold, European lancer"
actual = soldier_2.introduce()
assert actual == expected, ( "EuropeanArmy.train_lancer: " +
f"{actual} != {expected}" )
expected = "Ranger Robin, European archer"
actual = soldier_3.introduce()
assert actual == expected, ( "EuropeanArmy.train_archer: " +
f"{actual} != {expected}" )
assert soldier_4.introduce() == "Samurai Kishimoto, Asian swordsman"
assert soldier_5.introduce() == "Ronin Ayabusa, Asian lancer"
assert soldier_6.introduce() == "Shinobi Kirigae, Asian archer"
print("Coding complete? Let's try tests!")
June 22, 2018