D
D
dc65k2021-10-07 15:54:57
Java
dc65k, 2021-10-07 15:54:57

How to correctly complete the solution of the problem about currencies (Java)?

Hello. Tell me how to finish the solution of this problem correctly (lines // ????)?
Result
The first value of the functions of all buy operations.
The second sum of all sell transactions.

And the second point, how to specify Generics?

List list1 = new ArrayList<>();
list1.add("usd");
list1.add("buy");
list1.add(10000);


package main.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    /*
    output:
    {
        usd: [10000, 30000],
        gbd: [9000, 0],
        eur: [0, 7000],
        uah: [10000, 0],
    }

     */

    public static void getCurrencyData(List data) {
        Map<String, List> map = new HashMap<>();

        List<Integer> initialValues = new ArrayList<>();
        initialValues.add(0);
        initialValues.add(0);

        for (Object currentValue : data) {
            List current = (List) currentValue;

            String currency = (String) current.get(0);
            String type = (String) current.get(1);
            Integer amount = (Integer) current.get(2);

            if (!map.containsKey(currency)) {
                map.put(currency, initialValues);
            }

            if (map.containsKey(currency) && type == "buy") {
//                Integer value = (Integer) map.get(currency).get(0);
//                System.out.println("value " + value);
//                System.out.println("currency " + currency + " value + amount " + (value + amount));
//                System.out.println("=====");

//                map.get(currency).set(0, value + amount); // ????
            } else {
                Integer value = (Integer) map.get(currency).get(1);
                System.out.println("value " + value);
                System.out.println("currency " + currency + " value + amount " + (value + amount));
                System.out.println("=====");

//                map.get(currency).set(1, value + amount); // ????
            }
        }

        System.out.println("map " + map);

    }

    public static void main(String[] args) {
        List data = new ArrayList<>();

        List list1 = new ArrayList<>();
        list1.add("usd");
        list1.add("buy");
        list1.add(10000);

        List list2 = new ArrayList<>();
        list2.add("usd");
        list2.add("sell");
        list2.add(5000);

        List list3 = new ArrayList<>();
        list3.add("gbd");
        list3.add("buy");
        list3.add(9000);

        List list4 = new ArrayList<>();
        list4.add("eur");
        list4.add("sell");
        list4.add(7000);

        List list5 = new ArrayList<>();
        list5.add("uah");
        list5.add("buy");
        list5.add(10000);

        List list6 = new ArrayList<>();
        list6.add("usd");
        list6.add("sell");
        list6.add(25000);

        data.add(list1);
        data.add(list2);
        data.add(list3);
        data.add(list4);
        data.add(list5);
        data.add(list6);

        getCurrencyData(data);
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question