我正在做一个虚拟动物园。我的动物园有一个食品店,里面有一系列的食物和每种食物的数量。这是我的食品商店类:
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个项目从动物园的食品店移动到围栏食品店。我在想我该怎么做呢?谢谢
发布于 2015-12-09 22:49:48
您的takeFood
方法有点混乱。
我相信您想要向takeFood
方法传递一个quantity
,就像您对addFood
方法所做的那样。
使用您当前的代码,您可以检查映射是否包含food
密钥。如果没有,您将尝试删除具有该键的项,这将不会执行任何操作,因为您的map不包含该键。如果您的映射包含food
键,则获取该键的值,然后使用两个参数调用散列映射上的remove
函数。但是,散列映射的remove
函数没有接受两个参数。这些代码甚至不应该被编译。remove
方法将简单地从映射中取出带有您传递给它的键的项。
您要做的是从映射中获取当前值,然后如果传递给takeFood
方法的quantity
大于当前值,则抛出某种异常或返回某种消息。如果请求的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
}
}
}
https://stackoverflow.com/questions/34181285
复制相似问题