Софтуерно Инженерство
Loading...
Adnankata avatar Adnankata 2 Точки

04. Orders от Maps, Lambda and Stream API - Exercise на JAVA

Здравейте, имам проблем с принтирането на задачата, не разбирам как да умножа листовете накрая, пробвах няколко неуспешни метода.

 

1.Orders

Write a program, which keeps information about products and their prices. Each product has a name, a price and its quantity. If the product doesn't exist yet, add it with its starting quantity.

If you receive a product, which already exists, increase its quantity by the input quantity and if its price is different, replace the price as well.

You will receive products' names, prices and quantities on new lines. Until you receive the command "buy", keep adding items. When you do receive the command "buy", print the items with their names and total price of all the products with that name.

Input

  • Until you receive "buy", the products come in the format: "{name} {price} {quantity}"
  • The product data is always delimited by a single space

Output

  • Print information about each product, following the format:
    "{productName} -> {totalPrice}"
  • Format the average grade to the 2nd decimal place

Examples

Input

Output

Beer 2.20 100

IceTea 1.50 50

NukaCola 3.30 80

Water 1.00 500

buy

Beer -> 220.00

IceTea -> 75.00

NukaCola -> 264.00

Water -> 500.00

Beer 2.40 350

Water 1.25 200

IceTea 5.20 100

Beer 1.20 200

IceTea 0.50 120

buy

Beer -> 660.00

Water -> 250.00

IceTea -> 110.00

CesarSalad 10.20 25

SuperEnergy 0.80 400

Beer 1.35 350

IceCream 1.50 25

buy

CesarSalad -> 255.00

SuperEnergy -> 320.00

Beer -> 472.50

IceCream -> 37.50

Това е кода ми:

https://pastebin.com/ZkwmKjjQ

Благодаря предварително.

 

Тагове:
0
Fundamentals Module
MartinBG avatar MartinBG 2104 Точки

Почти сте се справили със задачата!

За принтирането, трябва да изчислите крайната цена, като умножите цената за 1 бр (намира се на индекс 0) по боря продукти (индекс 1):

        items
                .entrySet()
                .stream()
                .forEach(e -> System.out.printf("%s -> %.2f%n", 
                        e.getKey(), 
                        e.getValue().get(0) * e.getValue().get(1)));

 

Конкретно тук дори няма нужда да използваме stream, защото един .foreach ще свърши работата по-бързо и с по-малко код:

items.forEach((name, data) ->
        System.out.printf("%s -> %.2f%n", name, data.get(0) * data.get(1)));

 

0
24/05/2020 00:22:47
Adnankata avatar Adnankata 2 Точки

Много благодаря!

1