CuBG

Untitled

Feb 26th, 2021
954
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ManOWar_03 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  11.         List<Integer> warShip = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
  12.         int maxHealth = Integer.parseInt(scanner.nextLine());
  13.  
  14.         String input = scanner.nextLine();
  15.         while (!"Retire".equals(input)) {
  16.             String[] command = input.split("\\s+");
  17.  
  18.  
  19.             switch (command[0]) {
  20.                 case "Fire":
  21.                     int fireIndex = Integer.parseInt(command[1]);
  22.                     int damage = Integer.parseInt(command[2]);
  23.  
  24.                     if (validIndex(warShip, fireIndex)) {
  25.                         fire(warShip, fireIndex, damage);
  26.                         if (warShip.get(fireIndex) <= 0) {
  27.                             System.out.println("You won! The enemy ship has sunken.");
  28.                             return;
  29.                         }
  30.                     }
  31.                     break;
  32.                 case "Defend":
  33.                     int startIndex = Integer.parseInt(command[1]);
  34.                     int endIndex = Integer.parseInt(command[2]);
  35.                     int defendDamage = Integer.parseInt(command[3]);
  36.  
  37.                     if (validIndex(pirateShip, startIndex) && validIndex(pirateShip, endIndex)) {
  38.                         defend(pirateShip, startIndex, endIndex, defendDamage);
  39.  
  40.                         for (Integer section : pirateShip) {
  41.                             if(section <= 0) {
  42.                                 System.out.println("You lost! The pirate ship has sunken.");
  43.                                 return;
  44.                             }
  45.                         }
  46.                     }
  47.                     break;
  48.                 case "Repair":
  49.                     int health = Integer.parseInt(command[2]);
  50.                     int repairIndex = Integer.parseInt(command[1]);
  51.                     if (validIndex(warShip, repairIndex)) {
  52.                         repair(pirateShip, repairIndex, health, maxHealth);
  53.                     }
  54.                     break;
  55.                 case "Status":
  56.  
  57.                     int sectionCounter = 0;
  58.                     status(pirateShip, sectionCounter, maxHealth);
  59.                     break;
  60.             }
  61.  
  62.  
  63.             input = scanner.nextLine();
  64.         }
  65.         int pirateShipSectionSum = 0;
  66.         int warshipSectionSum = 0;
  67.  
  68.         for (Integer section : pirateShip) {
  69.             pirateShipSectionSum += section;
  70.         }
  71.         for (Integer section : warShip) {
  72.             warshipSectionSum += section;
  73.         }
  74.         System.out.println("Pirate ship status: " + pirateShipSectionSum);
  75.         System.out.println("Warship status: " + warshipSectionSum);
  76.     }
  77.  
  78.     private static boolean validIndex(List<Integer> ship, int index) {
  79.         return index >= 0 && index < ship.size();
  80.     }
  81.  
  82.     private static void fire(List<Integer> warShip, int index, int damage) {
  83.         warShip.set(index, warShip.get(index) - damage);
  84.  
  85.     }
  86.  
  87.     private static void defend(List<Integer> pirateShip, int index, int endIndex, int defendDamage) {
  88.         for (int i = index; i <= endIndex; i++) {
  89.             pirateShip.set(i, pirateShip.get(i) - defendDamage);
  90.         }
  91.     }
  92.  
  93.     private static void repair(List<Integer> pirateShip, int index, int health, int maxHealth) {
  94.         pirateShip.set(index, pirateShip.get(index) + health);
  95.  
  96.         if (pirateShip.get(index) > maxHealth) {
  97.             pirateShip.set(index, maxHealth);
  98.         }
  99.     }
  100.  
  101.     private static void status(List<Integer> pirateShip, int sectionCounter, int maxHealth) {
  102.         for (Integer section : pirateShip) {
  103.             double percentage = (section * 1.0 / maxHealth) * 100;
  104.             if (percentage < 20) {
  105.                 sectionCounter++;
  106.             }
  107.         }
  108.         System.out.printf("%d sections need repair.%n", sectionCounter);
  109.     }
  110. }
  111.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×