I am quite new to java and need help in setting up the implementation of the method in a more sophisticated way - I have written this, but was told I need to use one of the GoF patterns in order to beautify it. Some help as to what I need to do would be most appropriated
protected void setWeapon() {
if (this.getClass().getSimpleName().equals("FighterJet")) {
this.weapon = new GuidedMissileSystem();
System.out.println("FighterJet " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("AttackHelicopter")) {
this.weapon = new GrenadeLauncher();
System.out.println("AttackHelicopter " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Tank")) {
this.weapon = new Cannon();
System.out.println("Tank " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("InfantryMobilityVehicle")) {
this.weapon = new MachineGun();
System.out.println("InfantryMobilityVehicle " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Warship")) {
this.weapon = new RocketLauncher();
System.out.println("Warship " + id + " equipped with " + weapon.getClass().getSimpleName());
} else if (this.getClass().getSimpleName().equals("Submarine")) {
this.weapon = new TorpedoTube();
System.out.println("Submarine " + id + " equipped with " + weapon.getClass().getSimpleName());
}
}
Aucun commentaire:
Enregistrer un commentaire