RGAlex

O3. Man On War / O6. Mid Exam Retake

Feb 26th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package MidExam.O6;
  2.  
  3.  
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class O3ManOnWar {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         List<Integer> pirateShip = Arrays.stream(scanner.nextLine().split(">")).
  13.                 map(Integer::parseInt).collect(Collectors.toList());
  14.  
  15.         List<Integer> warship = Arrays.stream(scanner.nextLine().split(">")).
  16.                 map(Integer::parseInt).collect(Collectors.toList());
  17.         int maxHealth = Integer.parseInt(scanner.nextLine());
  18.         String command = scanner.nextLine();
  19.         while (!command.equals("Retire")) {
  20.             String type = command.split("\\s+")[0];
  21.  
  22.             switch (type) {
  23.                 case "Fire":
  24.                     int index1 = Integer.parseInt(command.split("\\s+")[1]);
  25.                     int index2 = Integer.parseInt(command.split("\\s+")[2]);
  26.                     if (index1 >= 0 && index1 < warship.size()) {
  27.                         warship.set(index1, warship.get(index1) - index2);
  28.                         if (warship.get(index1) <= 0) {
  29.                             System.out.println("You won! The enemy ship has sunken.");
  30.                             return;
  31.                         }
  32.                     }
  33.                     break;
  34.                 case "Defend":
  35.                     index1 = Integer.parseInt(command.split("\\s+")[1]);
  36.                     index2 = Integer.parseInt(command.split("\\s+")[2]);
  37.                     int index3 = Integer.parseInt(command.split("\\s+")[3]);
  38.                     if (index1 >= 0 && index1 < pirateShip.size()&& index2 >= 0 && index2 < pirateShip.size()) {
  39.                         for (int i = index1; i <= index2; i++) {
  40.                             pirateShip.set(i, pirateShip.get(i)-index3);
  41.  
  42.                             if (pirateShip.get(i) <= 0) {
  43.                                 System.out.println("You lost! The pirate ship has sunken.");
  44.                                 return;
  45.                             }
  46.                         }
  47.                     }
  48.                     break;
  49.                 case "Repair":
  50.                     index1 = Integer.parseInt(command.split("\\s+")[1]);
  51.                     index2 = Integer.parseInt(command.split("\\s+")[2]);
  52.                     if (index1 >= 0 && index1 < pirateShip.size()) {
  53.  
  54.                         pirateShip.set(index1, pirateShip.get(index1) + index2);
  55.                         if (pirateShip.get(index1) > maxHealth) {
  56.                             pirateShip.set(index1,maxHealth);
  57.                         }
  58.                     }
  59.                     break;
  60.                 case "Status":
  61.                     int count = 0;
  62.                     for (Integer integer : pirateShip) {
  63.                         if (integer < 0.2 * maxHealth) {
  64.                             count++;
  65.                         }
  66.                     }
  67.                     System.out.println(count + " sections need repair.");
  68.                     break;
  69.             }
  70.             command = scanner.nextLine();
  71.         }
  72.         int pirateShipStatus = 0;
  73.         for (Integer currentStatus : pirateShip) {
  74.             pirateShipStatus += currentStatus;
  75.         }
  76.         int warShipStatus = 0;
  77.         for (Integer currentStatus : warship) {
  78.             warShipStatus += currentStatus;
  79.         }
  80.         System.out.println("Pirate ship status: " + pirateShipStatus);
  81.         System.out.println("Warship status: " + warShipStatus);
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment