generated from admin/template
e
This commit is contained in:
+58
-119
@@ -1,152 +1,91 @@
|
|||||||
var pokemonList = new List<Pokemon>();
|
var pikachu = new Pikachu("qwe", 12, 3);
|
||||||
|
Console.WriteLine(pikachu.ToString());
|
||||||
|
|
||||||
for (byte i = 1; i < 6; Console.WriteLine($"Added pokemon {i++}"))
|
|
||||||
{
|
|
||||||
if (i < 3)
|
|
||||||
{
|
|
||||||
pokemonList.Add(new Pokemon($"Pokemon {i}", (byte)(1 * i), (short)(100 * i)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (i < 5)
|
|
||||||
{
|
|
||||||
pokemonList.Add(new OtherPokemon($"Other pokemon {i}", (byte)(1 * i), (short)(100 * i)));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pokemonList.Add(new OtherPokemon($"", (byte)(1 * i), (short)(100 * i)));
|
|
||||||
}
|
|
||||||
Console.WriteLine();
|
|
||||||
|
|
||||||
foreach (var pokemon in pokemonList)
|
|
||||||
{
|
|
||||||
pokemon.ShowInfo();
|
|
||||||
pokemon.Attack();
|
|
||||||
|
|
||||||
switch (pokemon)
|
abstract class Pokemon
|
||||||
{
|
{
|
||||||
case IEvolvable ev:
|
public Pokemon() : this(DEFAULT_NAME, DEFAULT_HP, DEFAULT_LVL) {}
|
||||||
ev.Evolve();
|
public Pokemon(string name) : this(name, hp: DEFAULT_HP, DEFAULT_LVL) {}
|
||||||
break;
|
public Pokemon(string name, short hp) : this(name, hp, DEFAULT_LVL) {}
|
||||||
case IMovable mov:
|
|
||||||
mov.Move();
|
public Pokemon(string name, short hp, byte level)
|
||||||
break;
|
{
|
||||||
default:
|
Name = name;
|
||||||
Console.WriteLine("Pokemon don't have an IEvolvable or IMovable.");
|
Hp = hp;
|
||||||
break;
|
Level = level;
|
||||||
}
|
//Count++
|
||||||
Console.WriteLine('\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Battle count: {Pokemon.BattleCount}");
|
protected string name;
|
||||||
|
protected short hp;
|
||||||
|
protected byte level;
|
||||||
|
|
||||||
|
protected const string DEFAULT_NAME = "UNKNOWN";
|
||||||
|
|
||||||
// Classes
|
|
||||||
class Pokemon
|
|
||||||
{
|
|
||||||
// CONSTANTS
|
|
||||||
protected const byte
|
|
||||||
MIN_LEVEL = 1,
|
|
||||||
MAX_LEVEL = 100;
|
|
||||||
protected const short
|
protected const short
|
||||||
MIN_HP = 0,
|
DEFAULT_HP = 100,
|
||||||
MAX_HP = 2500;
|
MAX_HP = 999,
|
||||||
protected const string DEFAULT_NAME = "Unknown";
|
MIN_HP = 0;
|
||||||
|
protected const byte
|
||||||
|
DEFAULT_LVL = 1,
|
||||||
|
MAX_LVL = 100,
|
||||||
|
MIN_LVL = 1;
|
||||||
|
|
||||||
|
public string Name
|
||||||
// FIELDS
|
|
||||||
protected string name = DEFAULT_NAME;
|
|
||||||
protected byte level = MIN_LEVEL;
|
|
||||||
protected short hp = MIN_HP;
|
|
||||||
|
|
||||||
protected static byte battleCount;
|
|
||||||
|
|
||||||
|
|
||||||
// PROPS
|
|
||||||
public virtual string Name
|
|
||||||
{
|
{
|
||||||
get => name;
|
get => name;
|
||||||
set => name = value ?? DEFAULT_NAME;
|
set => name = !(string.IsNullOrEmpty(value))
|
||||||
}
|
? value
|
||||||
public byte Level
|
: throw new Exception("Name cannot be empty or Null");
|
||||||
{
|
|
||||||
get => level;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value > MAX_LEVEL) level = MAX_LEVEL;
|
|
||||||
else if(value < MIN_LEVEL) level = MIN_LEVEL;
|
|
||||||
else level = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
public short Hp
|
public short Hp
|
||||||
{
|
{
|
||||||
get => hp;
|
get => hp;
|
||||||
set
|
set => hp = value > MAX_HP || value < MIN_HP
|
||||||
|
? value
|
||||||
|
: throw new Exception($"Hp cannot be less than {MIN_HP} and greater than {MAX_HP}");
|
||||||
|
}
|
||||||
|
public byte Level
|
||||||
{
|
{
|
||||||
if (value > MAX_HP) hp = MAX_HP;
|
get => level;
|
||||||
else if(value < MIN_HP) hp = MIN_HP;
|
set => level = value > MAX_LVL || value < MIN_LVL
|
||||||
else hp = value;
|
? value
|
||||||
}
|
: throw new Exception($"Hp cannot be less than {MIN_LVL} and greater than {MAX_LVL}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte BattleCount => battleCount;
|
public abstract override string ToString();
|
||||||
|
public abstract void ShowInfo();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IAttack
|
||||||
// CONSTRUCTORS
|
|
||||||
public Pokemon(string name)
|
|
||||||
{
|
{
|
||||||
Name = name;
|
public void Attack();
|
||||||
battleCount++;
|
|
||||||
}
|
}
|
||||||
public Pokemon(string name, byte level) : this(name) => Level = level;
|
|
||||||
public Pokemon(string name, byte level, short hp) : this(name, level) => Hp = hp;
|
|
||||||
|
|
||||||
|
class Pikachu : Pokemon, IAttack
|
||||||
// METHODS
|
|
||||||
public override string ToString() => $"Pokemon: {Name}\tLevel: {Level}\tHp: {Hp}";
|
|
||||||
public virtual void Attack() => Console.WriteLine($"{Name} is attacking!");
|
|
||||||
public void ShowInfo() => Console.WriteLine($"=== INFORMATION ===\nPokemon: {Name}\t(level: {Level} | hp: {Hp})");
|
|
||||||
}
|
|
||||||
class OtherPokemon : Pokemon
|
|
||||||
{
|
{
|
||||||
// CONSTRUCTORS
|
public Pikachu(string name, short hp, byte level) : base() {}
|
||||||
public OtherPokemon(string name) : base(name){}
|
|
||||||
public OtherPokemon(string name, byte level) : base(name, level){}
|
|
||||||
public OtherPokemon(string name, byte level, short hp) : base(name, level, hp){}
|
|
||||||
|
|
||||||
|
public override string ToString() => $"Pikachu({Name}, {Hp},{Level})";
|
||||||
// METHODS
|
public override void ShowInfo() => Console.WriteLine($"|INFO|==============\nName: {Name}\nHp: {Hp}\nLevel: {Level}");
|
||||||
public override void Attack() => Console.WriteLine($"{Name} is attacks in a different way!");
|
public void Attack() => Console.WriteLine("Electro");
|
||||||
}
|
}
|
||||||
|
|
||||||
class Charmander : Pokemon, IEvolvable, IMovable
|
class Squirtle : Pokemon, IAttack
|
||||||
{
|
{
|
||||||
// CONSTANTS
|
public Squirtle() : base() {}
|
||||||
private const string NAME = "Charmander";
|
|
||||||
|
|
||||||
|
public override string ToString() => $"Squirtle({Name}, {Hp},{Level})";
|
||||||
// FIELDS
|
public override void ShowInfo() => Console.WriteLine($"|INFO|==============\nName: {Name}\nHp: {Hp}\nLevel: {Level}");
|
||||||
private new string name;
|
public void Attack() => Console.WriteLine("Aqua");
|
||||||
|
|
||||||
|
|
||||||
// PROPS
|
|
||||||
public override string Name => name;
|
|
||||||
|
|
||||||
|
|
||||||
// CONSTRUCTORS
|
|
||||||
public Charmander(string name) : base(name) => this.name = NAME;
|
|
||||||
public Charmander(string name, byte level) : base(name, level) => this.name = NAME;
|
|
||||||
public Charmander(string name, byte level, short hp) : base(name, level, hp) => this.name = NAME;
|
|
||||||
|
|
||||||
// METHODS
|
|
||||||
public void Move() => Console.WriteLine($"{Name} is moving!");
|
|
||||||
public void Evolve() => Console.WriteLine($"{Name} is evolving!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interfaces
|
class Charmander : Pokemon, IAttack
|
||||||
interface IEvolvable
|
|
||||||
{
|
{
|
||||||
void Evolve();
|
public Charmander() : base() {}
|
||||||
}
|
|
||||||
|
|
||||||
interface IMovable
|
public override string ToString() => $"Charmander({Name}, {Hp},{Level})";
|
||||||
{
|
public override void ShowInfo() => Console.WriteLine($"|INFO|==============\nName: {Name}\nHp: {Hp}\nLevel: {Level}");
|
||||||
void Move();
|
public void Attack() => Console.WriteLine("Chair");
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user