Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class TreasureHunt {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> loot = Arrays
- .stream(scanner.nextLine().split("\\|"))
- .collect(Collectors.toList());
- String input = scanner.nextLine();
- while (!"Yohoho!".equals(input)) {
- List<String> treasure = Arrays
- .stream(input.split("\\s+"))
- .collect(Collectors.toList());
- String command = treasure.get(0);
- switch (command) {
- case "Loot":
- for (int i = 1; i < treasure.size(); i++) {
- if (!loot.contains(treasure.get(i))) {
- loot.add(0, treasure.get(i));
- }
- }
- break;
- case "Drop":
- int dropIndex = Integer.parseInt(treasure.get(1));
- if (dropIndex >= 0 && dropIndex < loot.size()) {
- String item = loot.get(dropIndex);
- loot.remove(dropIndex);
- loot.add(item);
- }
- break;
- case "Steal":
- int count = Integer.parseInt(treasure.get(1));
- if (count > loot.size()) {
- count = loot.size();
- }
- List<String> subList = loot.subList(loot.size() - count, loot.size());
- System.out.println(String.join(", ", subList));
- loot = loot.subList(0, loot.size() - count);
- break;
- }
- input = scanner.nextLine();
- }
- if (!loot.isEmpty()) {
- double sum = 0;
- for (String counter : loot) {
- sum += counter.length();
- }
- double averageSum = sum / loot.size();
- System.out.printf("Average treasure gain: %.2f pirate credits.%n", averageSum);
- } else {
- System.out.println("Failed treasure hunt.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment