如何使用hashmap和arrylist实现这个Java程序
厨房里有橱柜和冰箱,我可以在每个橱柜里放和取出产品(奶酪、牛奶和鸡蛋) 这是我的厨房课程,我使用hashmap创建了一个关于如何在厨房中获取橱柜和冰箱的说明如何使用hashmap和arrylist实现这个Java程序,java,arraylist,hashmap,implementation,Java,Arraylist,Hashmap,Implementation,厨房里有橱柜和冰箱,我可以在每个橱柜里放和取出产品(奶酪、牛奶和鸡蛋) 这是我的厨房课程,我使用hashmap创建了一个关于如何在厨房中获取橱柜和冰箱的说明 import java.util.HashMap; public class Kitchen{ HashMap<String,KitchenCabinet > store = new HashMap<String, KitchenCabinet >();` public void putKit
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;
}
}
import java.util.HashMap;
公共厨房{
HashMap store=新的HashMap()`
公共无效putKitchenCabinet(字符串名称,`KitchenCabinet newKuechenSchrank){
储存。放置(名称,新厨柜);
}
公共KitchenCabinet getKitchenCabinet(字符串名称){
KitchenCabinet out=this.store.get(name);
返回;
}
}
我还为每个产品(奶酪、牛奶和鸡蛋)创建了一个单独的类,并创建了一个抽象的产品类,该类扩展到其他产品类,然后我在KitchenCabinet类中使用了一个Arraylist
这是我的厨柜课
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;
}
}
import java.util.ArrayList;
公共级厨柜{
//产品清单在这里
产品和产品;
ArrayList超级产品=新的ArrayList();
公共产品(产品我的产品){
this.superProducts.add(myProducts);
}
公共产品{
Products out=此.superProducts.remove(0);
返回;
}
}
我如何实现以下内容
- 厨房里有多少种产品
- 厨房里有多少牛奶
- 产品X在哪个柜中
我对编程非常陌生,在这两者之间,我迷路了。提前非常感谢这里是您展示的解决方案。我建议你不要把这个答案作为家庭作业提交,因为你的老师可能会在网上查找寻求帮助的学生。使用它作为您自己更好的实现的起点,因为这只是一个粗略的草案 需要注意的重要一点是,查找java的一些命名约定。例如,类通常是单数的,而集合是复数的(当然,这取决于讨论)。当您使用更复杂的设计时,正确的命名有助于您在代码中定位 对您的集合使用菱形表示法(我想在Java7中可以使用),因为它使您的行更短、更清晰
//do
HashMap<String,Storage> storages = new HashMap<>();
//don't
HashMap<String,Storage> storages = new HashMap<String,Storage>();
//do
HashMap storages=新的HashMap();
//不要
HashMap storages=新的HashMap();
当您有某个专用类时,请使用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");}}
import java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公营厨房考试{
公共静态void main(字符串[]args){
//初始化家具
厨房=新厨房();
厨房、储藏室(“冰箱”);
厨房。createStorage(“左橱柜”);
厨房。createStorage(“右橱柜”);
//把产品放在厨房里
厨房。放(“冰箱”,新奶酪());
厨房。放(“冰箱”,新奶酪());
厨房。放(“左柜”,新牛奶());
厨房。放(“左柜”,新牛奶());
厨房。放(“左橱柜”,新鸡蛋());
厨房。摆放(“右橱柜”,新牛奶();
厨房。摆放(“右橱柜”,新牛奶();
厨房。摆放(“右橱柜”,新牛奶();
//厨房里有多少种产品
映射内容=kitchen.find(null);
整数金额=0;
for(列表值:contents.values()){
amount+=value.size();
}
System.out.println(“厨房里是”+amount+“产品”);
//有多少个奶瓶
内容=厨房。查找(“牛奶”);
金额=0;
for(列表值:contents.values()){
amount+=value.size();
}
System.out.println(“厨房里是”+数量+“奶瓶”);
//哪个内阁是什么
contents=kitchen.find(空);
for(Map.Entry:contents.entrySet()){
System.out.println(“In”+entry.getKey()+“是”+Arrays.toString(entry.getValue().toArray()));
}
}
}
班级厨房{
私有最终HashMap存储=新HashMap();
public void createStorage(字符串名称){
storages.put(名称,new Storage());
}
public void put(字符串storageName,Product){
如果(!storages.containsKey(storageName)){
抛出新的RuntimeException(“未找到存储!”);
}
storages.get(storageName).put(product);
}
//查找所有存储中出现的特定产品
公共地图查找(字符串productName){
找到的映射=新的HashMap();
for(字符串storageName:storages.keySet()){
found.put(storageName,storages.get(storageName.find(productName));
}
发现退货;
}
}
//一般存放的地方,冰箱没有功能上的区别
//一个