Advertisement
Guest User

Untitled

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