borovaneca

TreasureHunt

Oct 21st, 2023
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package Fundamentals.Arrays.Exercise;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class TreasureHunt {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] treasureChest = scanner.nextLine().split("\\|");
  11.  
  12.         String command = scanner.nextLine();
  13.  
  14.         while (!command.equals("Yohoho!")) {
  15.             String[] commandParts = command.split(" ");
  16.  
  17.             switch (commandParts[0]) {
  18.                 case "Loot":
  19.                     for (int i = 1; i < commandParts.length; i++) {
  20.                         boolean alreadyContained = false;
  21.                         for (int j = 0; j < treasureChest.length; j++) {
  22.                             if (commandParts[i].equals(treasureChest[j])) {
  23.                                 alreadyContained = true;
  24.                                 break;
  25.                             }
  26.                         }
  27.                         if (!alreadyContained) {
  28.                             String newChest = commandParts[i] + " " + String.join(" ", treasureChest);
  29.                             treasureChest = newChest.split(" ");
  30.                         }
  31.                     }
  32.                     break;
  33.                 case "Drop":
  34.                     int position = Integer.parseInt(commandParts[1]);
  35.  
  36.                     if (position <= treasureChest.length - 1 && position >= 0) {
  37.                         String dropItem = treasureChest[position];
  38.                         for (int i = position; i < treasureChest.length - 1; i++) {
  39.                             treasureChest[i] = treasureChest[i + 1];
  40.                         }
  41.                         treasureChest[treasureChest.length - 1] = dropItem;
  42.  
  43.                     } else {
  44.                         break;
  45.                     }
  46.                     break;
  47.                 case "Steal":
  48.                     int numberOfStealingItems = Integer.parseInt(commandParts[1]);
  49.  
  50.                     if (numberOfStealingItems >= 0 && numberOfStealingItems < treasureChest.length) {
  51.                         for (int i = 0; i < numberOfStealingItems; i++) {
  52.                             System.out.print(treasureChest[treasureChest.length - numberOfStealingItems + i]);
  53.                             if (i != numberOfStealingItems - 1) {
  54.                                 System.out.print(", ");
  55.                             }
  56.                         }
  57.                         String[] tempChest = new String[treasureChest.length - numberOfStealingItems];
  58.                         for (int i = 0; i < tempChest.length; i++) {
  59.                             tempChest[i] = treasureChest[i];
  60.                         }
  61.                         treasureChest = tempChest;
  62.  
  63.                     } else if (numberOfStealingItems >= 0) {
  64.                         for (int i = 0; i < treasureChest.length; i++) {
  65.                             System.out.print(treasureChest[i]);
  66.                             if (i != treasureChest.length - 1) {
  67.                                 System.out.print(", ");
  68.                             }
  69.                         }
  70.                         treasureChest = new String[0];
  71.                     }
  72.                     System.out.println();
  73.                     break;
  74.  
  75.             }
  76.  
  77.  
  78.             command = scanner.nextLine();
  79.         }
  80.  
  81.         String treasureCount = String.join("", treasureChest);
  82.         int charCounter = 0;
  83.         for (int i = 0; i < treasureCount.length(); i++) {
  84.             charCounter++;
  85.         }
  86.         double averageTreasure = (1.0 * charCounter) / treasureChest.length;
  87.         if (charCounter > 0) {
  88.             System.out.printf("Average treasure gain: %.2f pirate credits.", averageTreasure);
  89.         } else {
  90.             System.out.println("Failed treasure hunt.");
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment