Advertisement
veronikaaa86

02. Treasure Hunt

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