Java - 确保集合中的项目只能使用一次

时间:2017-06-02 13:00:15

标签: java if-statement hashmap conditional-statements aggregation

所以我试图确保如果静态hashmap中的某个项被多次使用,那么它不允许你再次使用它。该计划背后的想法是,一个侦察小队每个小队不能有一个以上的“重型锚杆”和“导弹发射器”。请注意,“重型bolter”和“missle launcher”是hashmap集合中的两个hashmap键。

所以我有一个名为WeaponList的类,它包含Ranged武器的静态hashmap。然后我有一个Scout类,它允许Scout从构造函数中的默认武器中升级它的远程武器。但是,WeaponList类hashmap中的某些项目只允许升级到一次。因此,如果Scout升级为“重型弩机”,并且已经有一个已经拥有该武器的小队成员,则不应进行升级。

有办法做到这一点吗?我已经在Scout类的upgradeRanged方法中尝试过这行代码。 if (WeaponList.getRanged("Heavy Bolter") > 1))但那不行,因为我认为使用'>'符号在原始类型和引用类型之间不起作用。

我还应该提到我有另一个名为ScoutSquad的类,它是Scouts的聚合。在主程序中,您创建ScoutSquad对象引用,然后将Scout对象添加到ScoutSquad。每个侦察兵都可以单独升级他们的默认武器。 (我可能正在考虑使用ScoutSquad类来确保来自HashMap的Ranged武器不能多次使用)。

任何有关此问题的帮助都将被证实,如果对问题的任何混淆都知道并且我会做出合适的更改。

我现在将提供相关类的代码:

WeaponList Class

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类(专注于upgradeRanged方法)

public class Scout
{
    private Weapon ranged;
    private int points;

    //DEFAULT SCOUT
    public Scout() 
    {       
        ranged = new Ranged("Bolt Pistol", 0);

        points = 13; 
        points = points + ranged.getWeaponPoints();      
    }

    public void rangedUpgrade(Ranged ranged) 
    {    
        switch(ranged.toString())
        {
            case "Bolter" : 
            case "Shotgun" :
            case "Sniper Rifle" : 
            case "Heavy Bolter" : 
            case "Missle Launcher" :            
               this.ranged = ranged; 
               points = points + ranged.getWeaponPoints();
            break;        
            default :
                System.out.println("A Scout can't have a " + ranged);                   
        }

        if (WeaponList.getRanged("Heavy Bolter") > 1))
        {

        }
    }  

    public Weapon getRangedUpgrade()
    {
        return ranged;
    }

    @Override
    public String toString()
    {
        return "|| " + "Ranged Weapon: " + ranged + " || " + "Melee Weapon: " + melee + " || " + "Points: " + points;
    }
}

主程序:

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("Heavy Bolter"));

        System.out.println(scouts.getScoutSquad());

      input.close();
    }   
}

2 个答案:

答案 0 :(得分:0)

如果HashMap.get()中没有该项,则

null会返回Map。因此你可以这样做:

if(WeaponList.getRanged("Heavy Bolter") != null) {
    // Already has
else {
    // Doesn't have
}

答案 1 :(得分:0)

将此添加到 ScoutSquad 类中:

public boolean checkUses(String weapon) {
  for (Scout scout : scouts)
    if (scout.getRangedUpgrade() == WeaponList.getRanged(weapon)
      return true;
  return false;
}

更改 Scout 类的 rangedUpgrade 方法:

public void rangedUpgrade(Ranged ranged) 
        {    
            switch(ranged.toString())
            {
                case "Heavy Bolter" : 
                case "Missle Launcher" :            
                  if (getSquad().checkUses(ranged.toString())) {
                    System.out.println(
                    "This scout's squad is already using a " + ranged);
                    break;
                  }
                case "Bolter" : 
                case "Shotgun" :
                case "Sniper Rifle" : 
                    this.ranged = ranged; 
                    points = points + ranged.getWeaponPoints();
                break;        
                default :
                    System.out.println("A Scout can't have a " + ranged);                   
                break;
            }
        }