【问题标题】:trying to calculate profit in java试图在java中计算利润
【发布时间】:2018-08-17 02:45:47
【问题描述】:

第一个参数是一个HashMap,表示我们商店中每件商品的售价(即给定的)。

第二个参数也是一个 HashMap,但这个参数代表我们为库存购买每件商品的成本。

第三个参数也是一个HashMap,但是这个代表一个顾客购物车。

此方法计算并返回当天的总利润。请注意,销售商品的利润是客户的成本(第一个参数中的价格)减去我们购买商品的成本(第二个参数)。在所有计算中,使用以下价格。

这是我做的,不知道我做错了什么。还有一种方法可以在不使用入口集的情况下做到这一点吗?

public static double computeProfit(HashMap<String, Double> sellingPrice, HashMap<String, Double> inventory, ArrayList<HashMap<String, Integer>> orders) {

    double ans = 0;
    for (HashMap<String, Integer> order : orders) {
        for (Map.Entry<String, Integer> itemToQuantity : order.entrySet()) {
            String item = itemToQuantity.getKey();
            Integer quantityPurchased = itemToQuantity.getValue();
            inventory.put(item, inventory.get(item) * quantityPurchased);
        }
    }

    for (Entry<String, Double> itemInInventory : inventory.entrySet()) {
        String itemName = itemInInventory.getKey();
        Double reQuantity = itemInInventory.getValue();
        ans = ans -(sellingPrice.get(itemName)+ reQuantity);
    }

    return ans;
}
public static ArrayList<HashMap<String,Integer>> allOrders(){

    ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
    HashMap<String, Integer> cart0 = new HashMap<>();
    cart0.put("frozen dinner", 4);
    cart0.put("yogurt", 4);
    cart0.put("milk", 3);
    allOrders.add(cart0);
    HashMap<String, Integer> cart1 = new HashMap<>();
    cart1.put("yogurt", 4);
    cart1.put("strawberries", 3);
    cart1.put("apples", 4);
    allOrders.add(cart1);

    return allOrders;
}
public static HashMap<String,Double>sellingPrice(){    
    HashMap<String,Double>priceList = new HashMap<String,Double>();
    priceList.put("eggs", 1.79);
    priceList.put("orange juice",2.5);
    priceList.put("yogurt",1.99);
    priceList.put("bread",2.49);
    priceList.put("butter",2.39);
    priceList.put("peppers",1.49);
    priceList.put("chips",2.95);
    priceList.put("chocolate chips",2.39);
    priceList.put("popcorn",1.99);
    priceList.put("tomato sauce",0.99);
    priceList.put("frozen pizza",5.49);
    priceList.put("milk",2.09);
    priceList.put("bananas",0.49);
    priceList.put("hot dog",1.29);
    priceList.put("relish",0.99);   
    priceList.put("frozen dinner",2.5);
    priceList.put("cereal",3.25);
    priceList.put("tuna fish",0.99);
    priceList.put("coffee",2.0);
    priceList.put("pasta",0.99);
    priceList.put("strawberries",3.5);
    priceList.put("apples",1.29);
    priceList.put("sugar",1.99);
    priceList.put("ketchup",2.89);

    return sellingPrice();
}
public static HashMap<String, Double> purchasePrice(){  
    HashMap<String,Double>cart = new HashMap <String,Double>();
    cart.put("eggs", 1.2);
    cart.put("orange juice", 1.0);
    cart.put("yogurt", 1.0);
    cart.put("bread", 1.5);
    cart.put("butter", 1.95);
    cart.put("peppers", 0.99);
    cart.put("chips", 0.9);
    cart.put("chocolate chips", 1.79);
    cart.put("popcorn", 0.99);
    cart.put("tomato sauce", 0.4);
    cart.put("frozen pizza", 2.6);
    cart.put("milk", 1.89);
    cart.put("bananas", 0.39);
    cart.put("hot dog", 0.79);
    cart.put("relish", 0.49);
    cart.put("frozen dinner", 1.5);
    cart.put("cereal", 1.55);
    cart.put("tuna fish", 0.49);
    cart.put("coffee", 0.6);
    cart.put("pasta", 0.5);
    cart.put("strawberries", 1.99);
    cart.put("apples", 0.99);
    cart.put("sugar", 1.5);
    cart.put("sugar", 0.98);

    return purchasePrice();
}
public static void main(String [] args){
    computeProfit(sellingPrice(),purchasePrice(),allOrders());   
}

我也试过了,我这里做错了什么?

public static double ComputeProfit(HashMap sellPrice, HashMap inventory, ArrayList> orders) {

    double ans = 0;
    int i =0, j = 0;
    for(i=0;i< orders.size();i++) {
        HashMap<String,Integer> temp = orders.get(i);
        for (j=0;j < temp.size();j++) {
            String item = temp.getKey(j);
            Integer quantityPurchased = temp.getValue(j);
            inventory.put(item, inventory.get(item) * quantityPurchased);
        }

        }
        int k=0 ;
        for (k =0 ; k< inventory.size();k++) {
        HashMap <String,Integer>itemInInventory = inventory.get(k);
            String itemName = itemInInventory.getKey(k);
            Double reQuantity = itemInInventory.getValue(k);
            ans = ans -(sellingPrice.get(itemName)+ reQuantity);
        }


        return ans;
}
        public static ArrayList<HashMap<String,Integer>> allOrders(){

            ArrayList<HashMap<String, Integer>> allOrders = new ArrayList<>();
            HashMap<String, Integer> cart0 = new HashMap<>();
            cart0.put("frozen dinner", 4);
            cart0.put("yogurt", 4);
            cart0.put("milk", 3);
            allOrders.add(cart0);
            HashMap<String, Integer> cart1 = new HashMap<>();
            cart1.put("yogurt", 4);
            cart1.put("strawberries", 3);
            cart1.put("apples", 4);
            allOrders.add(cart1);

            return allOrders;
        }

        public static HashMap<String,Double>sellingPrice(){    
            HashMap<String,Double>priceList = new HashMap<String,Double>();
            priceList.put("eggs", 1.79);
            priceList.put("orange juice",2.5);
            priceList.put("yogurt",1.99);
            priceList.put("bread",2.49);
            priceList.put("butter",2.39);
            priceList.put("peppers",1.49);
            priceList.put("chips",2.95);
            priceList.put("chocolate chips",2.39);
            priceList.put("popcorn",1.99);
            priceList.put("tomato sauce",0.99);
            priceList.put("frozen pizza",5.49);
            priceList.put("milk",2.09);
            priceList.put("bananas",0.49);
            priceList.put("hot dog",1.29);
            priceList.put("relish",0.99);   
            priceList.put("frozen dinner",2.5);
            priceList.put("cereal",3.25);
            priceList.put("tuna fish",0.99);
            priceList.put("coffee",2.0);
            priceList.put("pasta",0.99);
            priceList.put("strawberries",3.5);
            priceList.put("apples",1.29);
            priceList.put("sugar",1.99);
            priceList.put("ketchup",2.89);
            return sellingPrice();

        }

        public static HashMap<String, Double> purchasePrice(){


            HashMap<String,Double>cart = new HashMap <String,Double>();
            cart.put("eggs", 1.2);
            cart.put("orange juice", 1.0);
            cart.put("yogurt", 1.0);
            cart.put("bread", 1.5);
            cart.put("butter", 1.95);
            cart.put("peppers", 0.99);
            cart.put("chips", 0.9);
            cart.put("chocolate chips", 1.79);
            cart.put("popcorn", 0.99);
            cart.put("tomato sauce", 0.4);
            cart.put("frozen pizza", 2.6);
            cart.put("milk", 1.89);
            cart.put("bananas", 0.39);
            cart.put("hot dog", 0.79);
            cart.put("relish", 0.49);
            cart.put("frozen dinner", 1.5);
            cart.put("cereal", 1.55);
            cart.put("tuna fish", 0.49);
            cart.put("coffee", 0.6);
            cart.put("pasta", 0.5);
            cart.put("strawberries", 1.99);
            cart.put("apples", 0.99);
            cart.put("sugar", 1.5);
            cart.put("sugar", 0.98);
            return purchasePrice();
        }
        public static void main(String [] args){
            computeProfit(sellingPrice(),purchasePrice(),allOrders());

        }













}

【问题讨论】:

  • 你需要说出你面临的问题
  • 如果你运行这段代码,你应该得到一个 StackOverflowError 因为递归调用

标签: java arraylist hashmap store


【解决方案1】:

您的答案看起来大部分是正确的,您的类型命名只有几个问题(Integer 应该是 int,Double 应该是 double)并且您在代码的一部分中使用 Map.Entry 并稍后使用 Entry。下面的代码修复了您的 computeProfit 函数中的问题,应该可以解决您的问题。如果您有任何问题,请告诉我!

public static double computeProfit(HashMap<String, double> sellingPrice, HashMap<String, double> inventory, ArrayList<HashMap<String, int>> orders) {
    double profit = 0;
    for (HashMap<String, int> order: Orders) {
        for (Map.Entry<String, int> orderEntry: order.entrySet()) {
            double purchasePrice = inventory.get(orderEntry.getKey());
            double salePrice = sellingPrice.get(orderEntry.getKey());
            int purchaseCount = orderEntry.getValue();
            profit += ( salePrice - purchasePrice ) * purchaseCount;
        }
    }
    return profit;
}

【讨论】:

  • @maahi 你能说出你在哪里得到错误吗?在错误声明中应该说明是哪一行造成了错误
  • 在 java.util.HashMap.putVal(HashMap.java:629) 处的 java.util.HashMap.put(HashMap.java:612) 处的线程“主”java.lang.StackOverflowError 中的异常testrun.testrun. sellPrice(testrun.java:50) 在 testrun.testrun. sellPrice(testrun.java:74) 在 testrun.testrun. sellPrice(testrun.java:74) 在 testrun.testrun. sellPrice(testrun.java:74 ) 在 testrun.testrun. sellPrice(testrun.java:74) 在 testrun.testrun. sellPrice(testrun.java:74) 在 testrun.testrun. sellPrice(testrun.java:74) 在 testrun.testrun. sellPrice(testrun.java :74)
  • 我还添加了另一种方法来解决这个问题,你能检查一下吗?
  • 我现在看到了你的问题,在你的 SellingPrice 函数中,你返回了 sellPrice() 由于无限递归导致你的堆栈溢出错误。您需要改为返回 priceList。在 purchasePrice 中也存在同样的问题,您需要退回购物车。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2021-10-11
  • 1970-01-01
  • 2016-10-14
相关资源
最近更新 更多