Didart

Treasure Hunt

Nov 18th, 2021
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class TreasureHunt {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         List<String> loot = Arrays
  9.                 .stream(scanner.nextLine().split("\\|"))
  10.                 .collect(Collectors.toList());
  11.  
  12.         String input = scanner.nextLine();
  13.         while (!"Yohoho!".equals(input)) {
  14.             List<String> treasure = Arrays
  15.                     .stream(input.split("\\s+"))
  16.                     .collect(Collectors.toList());
  17.             String command = treasure.get(0);
  18.  
  19.             switch (command) {
  20.                 case "Loot":
  21.                     for (int i = 1; i < treasure.size(); i++) {
  22.                         if (!loot.contains(treasure.get(i))) {
  23.                             loot.add(0, treasure.get(i));
  24.                         }
  25.                     }
  26.                     break;
  27.                 case "Drop":
  28.                     int dropIndex = Integer.parseInt(treasure.get(1));
  29.  
  30.                     if (dropIndex >= 0 && dropIndex < loot.size()) {
  31.                         String item = loot.get(dropIndex);
  32.                         loot.remove(dropIndex);
  33.                         loot.add(item);
  34.                     }
  35.                     break;
  36.                 case "Steal":
  37.                     int count = Integer.parseInt(treasure.get(1));
  38.                     if (count > loot.size()) {
  39.                         count = loot.size();
  40.                     }
  41.                     List<String> subList = loot.subList(loot.size() - count, loot.size());
  42.  
  43.                     System.out.println(String.join(", ", subList));
  44.  
  45.                     loot = loot.subList(0, loot.size() - count);
  46.                     break;
  47.             }
  48.             input = scanner.nextLine();
  49.         }
  50.         if (!loot.isEmpty()) {
  51.             double sum = 0;
  52.             for (String counter : loot) {
  53.                 sum += counter.length();
  54.             }
  55.             double averageSum = sum / loot.size();
  56.  
  57.             System.out.printf("Average treasure gain: %.2f pirate credits.%n", averageSum);
  58.         } else {
  59.             System.out.println("Failed treasure hunt.");
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment