ivanmitkoff

ManOWar_with_List/MidExamRetake6August2019

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