Untitled

 avatar
unknown
plain_text
2 years ago
3.8 kB
4
Indexable
package ee.ttu.algoritmid.accumulator;

import java.util.*;

public class HW02 implements AccumulatorController {
    int usedCapacity = 0;
    List<Price> arrayList = new ArrayList<>();

    @Override
    public Result findStrategy(double[] prices, int capacitance, int hourLimit, double cost) throws IllegalArgumentException {
        arrayList = convertPrices(prices);
        List<List<Price>> chunkList = chunkList(arrayList, 10);

        int count = 0;
        while (count < chunkList.size()) {
            List<Price> subList = convertChunkListToListPrice(chunkList, count);
            strategy(subList, hourLimit, cost, capacitance);
            count++;
        }

        arrayList.sort(Comparator.comparing(Price::getOrderNumber));

        List<Integer> resultTransactions = new ArrayList<>();
        for (Price orderedPrice : arrayList) {
            resultTransactions.add(orderedPrice.getTransaction());
        }

        return new ResultImpl(resultTransactions, prices, cost);
    }

    @Override
    public String uniid() {
        // TODO
        return null;
    }

    private List<Price> convertPrices(double[] prices) {
        List<Price> convertedPrices = new ArrayList<>();
        for (int i = 0; i < prices.length; i++) {
            Price price = new Price(prices[i], i, null);
            convertedPrices.add(price);
        }
        return convertedPrices;
    }

    public static <Price> List<List<Price>> chunkList(List<Price> list, int chunkSize) {
        if (chunkSize <= 0) {
            throw new IllegalArgumentException("Invalid chunk size: " + chunkSize);
        }
        List<List<Price>> chunkList = new ArrayList<>(list.size() / chunkSize);
        for (int i = 0; i < list.size(); i += chunkSize) {
            chunkList.add(list.subList(i, i + chunkSize >= list.size() ? list.size()-1 : i + chunkSize));
        }
        return chunkList;
    }

    public List<Price> convertChunkListToListPrice(List<List<Price>> chunkList, Integer count) {
        return ((chunkList.subList(count, count + 1))
                .stream()
                .flatMap(List::stream)
                .toList());
    }

    public void strategy(List<Price> priceList, Integer hourLimit, double cost, Integer capacitance) {
        List<Price> biggestPrices = new ArrayList<>(priceList);
        biggestPrices.sort(Comparator.comparing(Price::getValue).reversed());

        for (Price price : priceList) {
            int transaction;

            if (price.getBuyer() != 0) {
                transaction = -price.getBuyer();
                arrayList.get(arrayList.indexOf(price)).setTransaction(transaction);
                usedCapacity = usedCapacity - Math.abs(transaction);
            } else {
                if (capacitance - usedCapacity < hourLimit) {
                    transaction = capacitance - usedCapacity;
                } else {
                    transaction = hourLimit;
                }
                if (transaction > 0) {
                    for (Price bigPrice : biggestPrices) {
                        if (price.getValue() + 2 * cost < bigPrice.getValue() && price.getOrderNumber() < bigPrice.getOrderNumber()) {
                            arrayList.get(arrayList.indexOf(bigPrice)).setBuyer(transaction);
                            arrayList.get(arrayList.indexOf(price)).setTransaction(transaction);
                            usedCapacity += transaction;
                            biggestPrices.remove(bigPrice);
                            break;
                        } else {
                            arrayList.get(arrayList.indexOf(price)).setTransaction(0);
                        }
                    }
                }
            }
        }
    }
}
Editor is loading...