如何使用hashmap和arrylist实现此Java程序

时间:2015-06-03 08:56:24

标签: java arraylist hashmap implementation

在厨房里有橱柜和冰箱,每个橱柜我可以放出产品(奶酪,牛奶和鸡蛋)

这是我的Kitchen课程,我使用hashmap创建了如何在厨房中获取橱柜和冰箱的说明;

import java.util.HashMap;

public class  Kitchen{
    HashMap<String,KitchenCabinet > store = new HashMap<String, KitchenCabinet >();`

    public void  putKitchenCabinet (String name, `KitchenCabinet newKuechenSchrank) {
        store.put(name, newKitchenCabinet);
    }
    public KitchenCabinet getKitchenCabinet (String name){
        KitchenCabinet out = this.store.get(name);
        return out;
    }
}

我还为每个产品(奶酪,牛奶和鸡蛋)创建了一个单独的类,并创建了一个抽象的Product类,它扩展到了另一类产品,然后我在类KitchenCabinet中使用了一个Arraylist

这是我的KitchenCabinet课程

import java.util.ArrayList;

public  class KitchenCabinet {

    //List of Products here 
    Products vaildProducts;

    ArrayList<Products> superProducts = new ArrayList<Products>();

    public void addProducts(Products myProducts) {
        this.superProducts.add(myProducts);
    }

    public Products getProducts() {
        Products out = this.superProducts.remove(0);
        return out;
    }
}

我如何实施以下内容?

  1. 厨房有2个橱柜层(左,右和冰箱)

  2. 冰箱内的2x奶酪

  3. 左侧2x牛奶

  4. 右边3x牛奶

  5. 1x左边的鸡蛋

  6. 和问题

    • 厨房里有多少产品。

    • 厨房里有多少牛奶。

    • 产品X在哪个柜子里。

    请任何人,我需要你的帮助来实施这个计划 我对编程非常陌生,介于两者之间,我迷失了。非常感谢提前

1 个答案:

答案 0 :(得分:0)

所以这是你所展示的解决方案。我建议你不要把这个答案作为你的家庭作业提交,因为你的老师可能会在网上寻找帮助的学生。使用它作为您自己的更好实施的起点,因为这只是草稿。

需要注意的是,请查看java的一些命名约定。例如,类通常是单数,而集合是复数(这取决于讨论,当然)。当您使用更复杂的设计时,正确的命名可以帮助您定位代码。

使用钻石符号(我认为可以在java 7中找到?)用于您的系列,因为它可以使您的线条更短更清晰。

//do
HashMap<String,Storage> storages = new HashMap<>();
//don't
HashMap<String,Storage> storages = new HashMap<String,Storage>();

如果您有专门的课程,请使用私人关键字隐藏其成员,特别是如果您不打算直接访问它,在大多数情况下也不想这样做。

密切关注程序的过度工程。例如,在下面的代码中,没有必要为每个产品创建新类,并且通常没有程序员会这样做,因为这些类不会添加任何功能,只会提高代码的可读性。

不要实施您不需要的东西。在您的示例中,您实现了从厨房中删除项目的方法,这是set tasks不需要的。这并不意味着它对运动有害,但通常是浪费时间,你认为需要的一半方法需要改变或根本不用。

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

希望它有所帮助, 问候。