如何将项目从一个哈希映射移动到另一个哈希映射?

时间:2015-12-09 14:28:59

标签: java hashmap

我正在制作一个虚拟动物园。我的动物园有一个食品店,里面有各种食物和各种食物的数量。这是我的食品店类:

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);
    }
}

}

我还有一个Enclosure类,其中包含自己的食品商店,以下是我如何创建商店食品商店的hashmap

Foodstore store = new Foodstore();

在我的动物园里,我有动物园管理员,他们有一个aMonthPasses方法,需要允许他们从动物园食品店到外围食品店最多移动20件物品。我想知道我会怎么做呢?感谢

1 个答案:

答案 0 :(得分:0)

你的takeFood方法有点混乱。

我认为您希望将quantity传递给takeFood方法,就像使用addFood方法一样。

使用您当前的代码,检查地图是否包含您的food密钥。如果没有,您尝试使用该键删除该项,这将不会执行任何操作,因为您的地图不包含该键。如果您的地图确实包含food密钥,那么您将获得该密钥的值,然后使用两个参数调用哈希映射上的remove函数。但是,哈希映射的remove函数不带两个参数。该代码甚至不应该编译。 remove方法只会使用您传入其中的密钥将该项目从地图中删除。

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

这是什么样的:

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课程中,我猜测你有动物园FoodstoreEnclosure的引用。然后在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
        }
    }
}