Advertisement
CR7CR7

TreasureHunt

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