Reputation: 823
Okay, so I have a class called WeaponList that contains a static HashMap of type <String, Ranged>
(Ranged is another class) The Ranged class also extends another class called Weapon.
Then I have another class called Scout. A Scout is a composition of a Weapon (which Ranged extends remember) and an int of points. I should also point out that the ScoutSquad class is an aggregation of Scouts, with get and set methods.
So in the main program, when you instaniate a Scout Object the default Ranged weapon of each Scout is a "Bolt Pistol" and the default points is 13.
Now in the Scout class theres an option to upgrade your Ranged weapon via the method rangedUpgrade(Ranged ranged). So in the main program using this line of code: scouts.getScout(5).rangedUpgrade(WeaponList.getRanged("Shotgun"));
succesfully upgrades the Scout at index 5, and displays the new Weapon and the scouts new points (as each Weapon has points associated with it)
However currently the Scout can upgrade to any Ranged weapon in the WeaponList classes HashMap. I would like it so the Scout can only upgrade to certain Ranged weapons in the HashMap. I've already tried setting conditions with if statements, but with no success so far. (Lines 27 - 35 of the Scout class)
I'll provide code for all the relavant classes, and hope that someone can help with this.
Thanks.
WeaponList Class
package armoury1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import troops.scouts.Scout;
public class WeaponList
{
static Map<String, Ranged> rangedlist = new HashMap<String, Ranged>()
{{
put("Bolter", new Ranged2H("Bolter", 1));
put("Bolt Pistol", new Ranged("Bolt Pistol", 1));
put("Flamer", new Ranged2H("Flamer", 1));
put("Meltagun", new Ranged2H("Meltagun", 1));
put("Plasma Gun", new Ranged2H("Plasma Gun", 1));
put("Plasma Pistol", new Ranged("Plasma Pistol", 10));
put("Shotgun", new Ranged2H("Shotgun", 10));
put("Sniper Rifle", new Ranged2H("Sniper Rifle", 5));
put("Assault Cannon", new Ranged2H("Assault Cannon", 10));
put("Cyclone Missle Launcher", new Ranged2H("Cyclone Missle Launcher", 10));
put("Heavy Bolter", new Ranged2H("Heavy Bolter", 10));
put("Heavy Flamer", new Ranged2H("Heavy Flamer", 10));
put("Assault Cannon", new Ranged2H("Lascannon", 10));
put("Missle Launcher", new Ranged2H("Missle Launcher", 10));
put("Multi Melta", new Ranged2H("Multi Melta", 10));
put("Plasma Cannon", new Ranged2H("Plasma Cannon", 10));
}};
public static Ranged getRanged(String index)
{
return rangedlist.get(index);
}
public static Map<String, Ranged> getRangedList()
{
return rangedlist;
}
}
Scout Class
package troops.scouts;
import armoury.Wargear;
import armoury1.Melee;
import armoury1.Ranged;
import armoury1.Weapon;
import armoury1.WeaponList;
public class Scout
{
private Weapon ranged, melee;
private int points;
//DEFAULT SCOUT
public Scout()
{
ranged = new Ranged("Bolt Pistol", 0);
melee = new Melee("Close Combat Weapon", 0);
points = 13;
points = points + ranged.getWeaponPoints() + melee.getWeaponPoints();
}
public void rangedUpgrade(Ranged ranged)
{
if (ranged.equals(WeaponList.getRanged("Shotgun")));
{
this.ranged = ranged;
points = points + ranged.getWeaponPoints();
}
else
System.out.println("Can not have this weapon");
}
public void meleeUpgrade(Melee melee)
{
this.melee = melee;
points = points + melee.getWeaponPoints();
}
public Weapon getRangedUpgrade()
{
return ranged;
}
public Weapon getMeleeUpgrade()
{
return melee;
}
@Override
public String toString()
{
return "|| " + "Ranged Weapon: " + ranged + " || " + "Melee Weapon: " + melee + " || " + "Points: " + points;
}
}
Main Program
package main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import armoury1.Melee;
import armoury1.Ranged;
import armoury1.Weapon;
import armoury1.WeaponList;
import troops.scouts.Scout;
import troops.scouts.ScoutSergeant;
import troops.scouts.ScoutSquad;
public class ArmyBuilderDemo
{
public static void main(String[] args) throws Exception
{
ScoutSquad scouts = new ScoutSquad("Squad 1");
Scanner input = new Scanner(System.in);
System.out.println("Enter the squad size: ");
int size = input.nextInt();
for (int i = 0; i < size; i++)
scouts.addScout(new Scout());
System.out.println(scouts.getScoutSquad());
scouts.getScout(5).rangedUpgrade(WeaponList.getRanged("Shotgun"));
scouts.getScout(5).meleeUpgrade(WeaponList.getMelee(3));
System.out.println(scouts.getScoutSquad());
input.close();
}
}
Ranged Class (extends Weapon)
package armoury1;
import java.util.ArrayList;
import java.util.List;
import troops.scouts.Scout;
public class Ranged extends Weapon
{
public Ranged(String name, int points)
{
super(name, points);
}
}
Upvotes: 0
Views: 505
Reputation: 3945
Please implement .equals()
method in Ranged class.
Also, the if condition in rangedUpgrade(Ranged ranged)
that you have put is terminated on the same line.
Please modify the code of this method as:
public void rangedUpgrade(Ranged ranged) {
if (ranged.equals(WeaponList.getRanged("Shotgun"))){
this.ranged = ranged;
points = points + ranged.getWeaponPoints();
} else
System.out.println("Can not have this weapon");
}
}
Upvotes: 1