Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.9 kB
12
Indexable
Never
package Exercise;

import java.util.Scanner;

public class Zadatak10 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String[] loot = scanner.nextLine().split("\\|");

        String command = scanner.nextLine();
        while (!command.equals("Yohoho!")) {
            String[] tokens = command.split(" ");
            String action = tokens[0];

            switch (action) {
                case "Loot":
                    for (int i = 1; i < tokens.length; i++) {
                        String item = tokens[i];
                        if (!containsItem(loot, item)) {
                            loot = insertItemAtBeginning(loot, item);
                        }
                    }
                    break;
                case "Drop":
                    int index = Integer.parseInt(tokens[1]);
                    if (index >= 0 && index < loot.length) {
                        String item = loot[index];
                        loot = removeItem(loot, index);
                        loot = insertItemAtEnd(loot, item);
                    }
                    break;
                case "Steal":
                    int count = Integer.parseInt(tokens[1]);
                    count = Math.min(count, loot.length);
                    String[] stolenItems = new String[count];
                    for (int i = 0; i < count; i++) {
                        stolenItems[i] = loot[loot.length - count + i];
                    }
                    loot = removeItems(loot, loot.length - count, loot.length - 1);
                    System.out.println(String.join(", ", stolenItems));
                    break;
            }

            command = scanner.nextLine();
        }

        if (loot.length == 0) {
            System.out.println("Failed treasure hunt.");
        } else {
            double averageGain = calculateAverageGain(loot);
            System.out.printf("Average treasure gain: %.2f pirate credits.%n", averageGain);
        }
    }

    private static boolean containsItem(String[] arr, String item) {
        for (String element : arr) {
            if (element.equals(item)) {
                return true;
            }
        }
        return false;
    }

    private static String[] insertItemAtBeginning(String[] arr, String item) {
        String[] newArr = new String[arr.length + 1];
        newArr[0] = item;
        for (int i = 0; i < arr.length; i++) {
            newArr[i + 1] = arr[i];
        }
        return newArr;
    }

    private static String[] insertItemAtEnd(String[] arr, String item) {
        String[] newArr = new String[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        newArr[arr.length] = item;
        return newArr;
    }

    private static String[] removeItem(String[] arr, int index) {
        String[] newArr = new String[arr.length - 1];
        for (int i = 0, j = 0; i < arr.length; i++) {
            if (i != index) {
                newArr[j] = arr[i];
                j++;
            }
        }
        return newArr;
    }

    private static String[] removeItems(String[] arr, int startIndex, int endIndex) {
        int length = arr.length - (endIndex - startIndex + 1);
        String[] newArr = new String[length];
        for (int i = 0, j = 0; i < arr.length; i++) {
            if (i < startIndex || i > endIndex) {
                newArr[j] = arr[i];
                j++;
            }
        }
        return newArr;
    }

    private static double calculateAverageGain(String[] arr) {
        int totalLength = 0;
        for (String item : arr) {
            totalLength += item.length();
        }
        return (double) totalLength / arr.length;
    }
}