I've written a console-based demo while learning about the Solid principles. Along the way, I've become exceedingly paranoid about whether I'm writing good code or not.
Here is the code I have so far:
public class Entity { //Base template for all game entities...
private String name;
public Entity(String name)
{
this.name = name;
displayEntityName();
}
public String getName()
{
return name;
}
public void setName(String newName)
{
name = newName;
}
private void displayEntityName()
{
System.out.println(name + " entity created...");
}
public class Player extends Entity {
private String[] inventory; //Player will have an inventory.
private int itemsInInventory;
private static final Random RAND = new Random();
public Player(String name) {
super(name);
inventory = new String[25]; //Default inventory size.
}
public void attack(Enemy enemy) //Player can attack all enemy types.
{
System.out.println(super.getName() + " attacked the enemy...");
}
public void findLegendaryWeapon()
{
inventory[0] = WeaponConstants.LEGENDARY_WEAPONS
[RAND.nextInt(WeaponConstants.LEGENDARY_WEAPONS.length)];
itemsInInventory++;
}
public void equiptWeapon()
{
System.out.println(super.getName() + " equipted a weapon...");
}
public void searchChest()
{
System.out.println(super.getName() + " searched the chest and found nothing...");
}
}
public enum WeaponConstants{ //Legendary weapon constants.
BOW_OF_THE_MAJOR_ARCHER(), BLOOD_LETTER,
DAWNFANG, FROZEN_TOUCH;
public static final String[] LEGENDARY_WEAPONS = {DAWNFANG.name(),
BLOOD_LETTER.name(), FROZEN_TOUCH.name(), BOW_OF_THE_MAJOR_ARCHER.name()};
}
public interface Enemy { //Enemy interface...
void attack(Player player);
}
public class Ghoul extends Entity implements Enemy { //An example of a standard enemy class.
public Ghoul(String name) {
super(name);
}
@Override
public void attack(Player player) {
System.out.println(super.getName() + " attacked " + player.getName());
}
}
As mentioned above, I mainly want to know if I'm violating any of the Solid principles. My main concern is with my player class. I have quite a few methods in the class that do different types of things but they are all related to the player. Is this in any way violating the SRP principle?
Also, I wanted to know if my abstractions are correct and well-designed. Does this represent good coding style when used to develop more serious apps? I only have one year of experience, so I'm trying to solidify good practices early on. All input would be appreciated.