2015-12-09 58 views
0

我正在制作一个虚拟动物园。我的动物园有一个食品店,其中包含一系列食品和每种食品的数量。这里是我foodstore类:如何将项目从一个哈希映射移动到另一个哈希映射?

public class Foodstore { 

Map<String, Integer> register = new HashMap<String, Integer>(); //hashmap called register declaration 

public void addFood(String food, int quantity) { 
    if (register.containsKey(food)) {  //if register contains this food 
     Integer newAmount = register.get(food) + quantity; //assign new amount to newAmount 
     register.put(food, newAmount); //add this amount of the type of food 
    } 
    else { 
     register.put(food, quantity); 
    } 

} 

public void takeFood(String food) { 
    if (register.containsKey(food)) { //if register contains this food 
     Integer newAmount = register.get(food); //assign new amount to newAmount 
     register.remove(food, newAmount); //remove this amount of food 
    } 
    else { 
     register.remove(food); 
    } 
} 

}

我也有包含自己的食品商店的防护等级,这里是我创建的外壳食品店的HashMap

Foodstore store = new Foodstore(); 

在我动物园我有动物园管理员谁拥有aMonthPasses方法,需要允许他们从动物园foodstore移动多达20个项目到圈地食品商店。我想知道如何去做这件事?谢谢

+1

在'java.util.HashMap'没有方法'删除()'。 – hotzst

回答

0

你的takeFood方法有点搞砸了。

我相信你想通过quantity到你的takeFood方法,就像你用addFood方法做的一样。

使用您当前的代码,检查地图是否包含您的密钥food。如果没有,您尝试使用该键移除该项目,由于您的地图不包含该键,该项目不会执行任何操作。如果您的地图确实包含密钥food,您将获取该密钥的值,然后使用两个参数调用散列图上的remove函数。但是,哈希映射的remove函数不需要两个参数。该代码甚至不应该编译。 remove方法将简单地使用您传递给它的关键字将项目从地图中取出。

你想要做的是从地图获取当前值,然后如果quantity传入takeFood方法的值大于当前值,则抛出某种异常或返回某种类型的消息。如果请求的quantity小于您食品商店中的当前金额,则将当前值减去quantity,然后将该新金额返回到您的地图中的food密钥。此外,如果您的散列映射中不存在您的密钥food,而不是尝试remove一个不存在于您的散列映射中的密钥,您将希望引发另一个异常或返回某种消息,指示没有在您的Foodstore中输入food

这里是会是什么样子:

public void takeFood(String food, int quantity) { 
    if (register.containsKey(food)) { //if register contains this food 
     Integer oldAmount = register.get(food); //get old amount from register 
     if (quantity > oldAmount) { 
      throw new Exception("Not enough " + food + " in foodstore. There is only " + oldAmount + " " + food + "."); //there is not that much in the foodstore - throw an error 
     } else {     
      Integer newAmount = oldAmount - quantity; //decrement the quantity from the old amount 
      register.put(food, newAmount); //set the value in the hash map to new amount 
     } 
    } 
    else { 
     throw new Exception("There is no " + food + " in the foodstore."); 
    } 
} 

然后在您的Zookeeper类,我猜你必须参考你的动物园的Foodstore然后Enclosure。然后在你的Enclosure课上,它的食品店需要通过公共方法访问。

public class Enclosure { 
    private Foodstore foodstore = new Foodstore(); 

    public Foodstore getFoodstore { 
     return foodstore; 
    } 
} 

然后在您的Zookeeper类,你可以移动食物四周是这样的:

public class Zookeeper { 
    private Foodstore zooFoodstore; 
    private Enclosure enclosure; 

    //constructor to pass in references to the zoo's foodstore and the enclosure 
    public Zookeeper(Foodstore zooFoodstore, Enclosure enclosure) { 
     this.zooFoodstore = zooFoodstore; 
     this.lionEnclosure = lionEnclosure; 
    } 

    public void aMonthPasses() { 
     int quantity = 20; //get the amount of food you want to transfer here 
     String food = "meat"; //get the type of food you want to transfer here 
     try { 
      zooFoodstore.takeFood(food, quantity); 
      lionEnclosure.getFoodstore().addFood(food, quantity); 
     } catch (Exception ex) { 
      //food didn't exist in zoo foodstore or there wasn't enough 
     } 
    } 
}