Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PastMidExams.MidExamRetake6August2019;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class ManOWarList {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> pirateShipStatus = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
- List<Integer> warShipStatus = Arrays.stream(scanner.nextLine().split(">")).map(Integer::parseInt).collect(Collectors.toList());
- int maxHealth = Integer.parseInt(scanner.nextLine());
- String command = scanner.nextLine();
- while(!command.equals("Retire")) {
- String[] tokens = command.split(" ");
- int damagePoints = 0;
- switch (tokens[0]) {
- case "Fire":
- int indexToDamage = Integer.parseInt(tokens[1]);
- damagePoints = Integer.parseInt(tokens[2]);
- if (indexToDamage >= 0 && indexToDamage < warShipStatus.size()) {
- warShipStatus.set(indexToDamage, (warShipStatus.get(indexToDamage) - damagePoints));
- if (warShipStatus.get(indexToDamage) <= 0) {
- System.out.println("You won! The enemy ship has sunken.");
- return;
- }
- }
- break;
- case "Defend":
- int startIndex = Integer.parseInt(tokens[1]);
- int endIndex = Integer.parseInt(tokens[2]);
- damagePoints = Integer.parseInt(tokens[3]);
- if (startIndex >= 0 && startIndex <= endIndex && endIndex < pirateShipStatus.size()) {
- for (int i = startIndex; i <= endIndex; i++) {
- pirateShipStatus.set(i, (pirateShipStatus.get(i) - damagePoints));
- if (pirateShipStatus.get(i) <= 0) {
- System.out.println("You lost! The pirate ship has sunken.");
- return;
- }
- }
- }
- break;
- case "Repair":
- int indexToRepair = Integer.parseInt(tokens[1]);
- int health = Integer.parseInt(tokens[2]);
- if (indexToRepair >= 0 && indexToRepair < pirateShipStatus.size()) {
- pirateShipStatus.set(indexToRepair, (pirateShipStatus.get(indexToRepair) + health));
- if (pirateShipStatus.get(indexToRepair) > maxHealth) {
- pirateShipStatus.set(indexToRepair, maxHealth);
- }
- }
- break;
- case "Status":
- int sectionsCounter = 0;
- for (int i = 0; i < pirateShipStatus.size(); i++) {
- if (pirateShipStatus.get(i) < (0.2 * maxHealth)) {
- sectionsCounter++;
- }
- }
- System.out.printf("%d sections need repair.%n", sectionsCounter);
- break;
- }
- command = scanner.nextLine();
- }
- int pirateShipSum = pirateShipStatus.stream().mapToInt(Integer::valueOf).sum();
- int warShipSum = warShipStatus.stream().mapToInt(Integer::valueOf).sum();
- System.out.printf("Pirate ship status: %d%n", pirateShipSum);
- System.out.printf("Warship status: %d%n", warShipSum);
- }
- }
Add Comment
Please, Sign In to add comment