因此,这是您所展示内容的解决方案。我建议您不要将此答案作为作业提交,因为您的老师可能会在 Internet 上查找寻求帮助的学生。将其作为您自己更好实施的起点,因为这只是草稿。
需要注意的是,查找一些 java 的命名约定。例如,类通常是单数的,而集合是复数的(当然,这要讨论)。当您处理更复杂的设计时,正确的命名有助于您在代码中定位。
为您的收藏使用菱形符号(我认为在 java 7 中可用?),因为它使您的线条更短更清晰。
//do
HashMap<String,Storage> storages = new HashMap<>();
//don't
HashMap<String,Storage> storages = new HashMap<String,Storage>();
当你有专门的类时,使用 private 关键字隐藏它的成员,特别是如果你不打算直接访问它,在大多数情况下也不应该这样做。
密切注意程序的过度设计。例如,在下面的代码中,没有必要为每个产品创建新类,通常没有程序员会这样做,因为这些类不会添加任何功能,只会增加代码的可读性。
不要实现你不需要的东西。在您的示例中,您实现了从厨房中删除项目的方法,这不是设置任务所必需的。这并不意味着它不利于锻炼,但通常这是浪费时间,而且您认为需要更改的方法中有一半将需要更改或根本不会使用。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class KitchenTest {
public static void main(String[] args){
//initialize furniture
Kitchen kitchen = new Kitchen();
kitchen.createStorage("Refrigerator");
kitchen.createStorage("Left Cabinet");
kitchen.createStorage("Right Cabinet");
//put products in the kitchen
kitchen.put("Refrigerator", new Cheese());
kitchen.put("Refrigerator", new Cheese());
kitchen.put("Left Cabinet", new Milk());
kitchen.put("Left Cabinet", new Milk());
kitchen.put("Left Cabinet", new Egg());
kitchen.put("Right Cabinet", new Milk());
kitchen.put("Right Cabinet", new Milk());
kitchen.put("Right Cabinet", new Milk());
//how many products in the kitchen
Map<String, List<Product>> contents = kitchen.find(null);
int amount = 0;
for (List<Product> value : contents.values()) {
amount+=value.size();
}
System.out.println("In the kitchen is "+amount+" products.");
//how many milk bottles
contents = kitchen.find("Milk");
amount = 0;
for (List<Product> value : contents.values()) {
amount+=value.size();
}
System.out.println("In the kitchen is "+amount+" milk bottles.");
//in which cabinet is what
contents = kitchen.find(null);
for (Map.Entry<String, List<Product>> entry : contents.entrySet()) {
System.out.println("In "+entry.getKey()+" is "+Arrays.toString(entry.getValue().toArray()));
}
}
}
class Kitchen{
private final HashMap<String,Storage> storages = new HashMap<>();
public void createStorage(String name) {
storages.put(name, new Storage());
}
public void put(String storageName, Product product){
if(!storages.containsKey(storageName)){
throw new RuntimeException("Storage not found!");
}
storages.get(storageName).put(product);
}
//find specific product occurence in all storages
public Map<String, List<Product>> find(String productName){
Map<String, List<Product>> found = new HashMap<>();
for (String storageName : storages.keySet()) {
found.put(storageName, storages.get(storageName).find(productName));
}
return found;
}
}
//generic storage place, there is no functional difference between refrigerator
//and cabinets
class Storage {
private final ArrayList<Product> contents = new ArrayList<>();
public void put(Product myProducts) {
this.contents.add(myProducts);
}
//iterate over all contents of storage and storages found items in list
public List<Product> find(String name) {
List<Product> found = new ArrayList<>();
for (Product product : contents) {
//if you put name as null, you will get all products in the storage
if(name==null || product.name.equals(name)){
found.add(product);
}
}
return found;
}
}
//recognized products by name and class
class Product{
public final String name;
public Product(String name){
this.name=name;
}
@Override
public String toString(){return name;}
}
class Milk extends Product{public Milk(){super("Milk");}}
class Cheese extends Product{public Cheese(){super("Cheese");}}
class Egg extends Product{public Egg(){super("Egg");}}
希望对你有帮助,
问候。