arraylist - How do I implement this Java program using hashmap and arrylist -


in kitchen cupboards , refrigerator, in each of cupboard can put , out products ( cheese, milk , eggs)

here kitchen class used hashmap create instruction on how go getting cupboard , refrigerator in kitchen;

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

i have created separate class each of products ( cheese, milk , eggs) , created abstract product class extends other class of products, used arraylist in class kitchencabinet

here kitchencabinet class

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

how implement following?

  1. kitchen has 2 cabinet layers (left, right , refrigerator)

  2. 2x cheese inside refrigerator

  3. 2x milk in left

  4. 3x milk in right

  5. 1x egg in left

and question

  • how many products in kitchen.

  • how many milk in kitchen.

  • in cabinet product x.

please anybody, need implement program.
new programming , in between, i'm lost. in advance

so here solution showed. advise not submit answer homework, teacher check internet students seeking help. use starting point own, better implementation, rough draft.

important note, naming conventions java. instance classes singular, while collections plural (it discussion, sure). when working more complicated design, correct naming helps orientate in code.

use diamond notation (available in java 7 think?) collections, makes lines shorter , clearer.

//do hashmap<string,storage> storages = new hashmap<>(); //don't hashmap<string,storage> storages = new hashmap<string,storage>(); 

when have dedicated class something, hide members using private keyword, if don't plan access directly, nor should want cases.

keep eye on overengineering of programs. instance, in code below, not necessary create new class each product, , no programmer it, since classes not add functionality whatsoever, , increase readability of code.

do not implement things not need. in example, implemented method removes item kitchen, not required set tasks. not mean bad exercise purposes, waste of time , half of methods think need need changed or not used @ all.

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 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 kitchen         map<string, list<product>> contents = kitchen.find(null);         int amount = 0;         (list<product> value : contents.values()) {             amount+=value.size();         }         system.out.println("in kitchen "+amount+" products.");          //how many milk bottles         contents = kitchen.find("milk");         amount = 0;         (list<product> value : contents.values()) {             amount+=value.size();         }         system.out.println("in kitchen "+amount+" milk bottles.");          //in cabinet         contents = kitchen.find(null);         (map.entry<string, list<product>> entry : contents.entryset()) {             system.out.println("in "+entry.getkey()+" "+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 storages     public map<string, list<product>> find(string productname){         map<string, list<product>> found = new hashmap<>();         (string storagename : storages.keyset()) {             found.put(storagename, storages.get(storagename).find(productname));         }         return found;     } }  //generic storage place, there 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 on contents of storage , storages found items in list     public list<product> find(string name) {         list<product> found = new arraylist<>();         (product product : contents) {             //if put name null, products in storage             if(name==null || product.name.equals(name)){                 found.add(product);             }         }         return found;     } }  //recognized products name , 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");}} 

hope helps, regards.


Comments