From b43f8cf3a401e134bb5455db80e3f9be32115827 Mon Sep 17 00:00:00 2001 From: Hajdu Kolos Date: Wed, 16 Oct 2024 08:38:16 +0200 Subject: [PATCH 1/2] Solved problems in TorpedoStore.java --- .../hu/bme/mit/spaceship/TorpedoStore.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/hu/bme/mit/spaceship/TorpedoStore.java b/src/main/java/hu/bme/mit/spaceship/TorpedoStore.java index 76a3cea..092b39c 100644 --- a/src/main/java/hu/bme/mit/spaceship/TorpedoStore.java +++ b/src/main/java/hu/bme/mit/spaceship/TorpedoStore.java @@ -3,23 +3,23 @@ import java.util.Random; /** -* Class storing and managing the torpedoes of a ship -* -* (Deliberately contains bugs.) -*/ + * Class storing and managing the torpedoes of a ship + * + * (Deliberately contains bugs.) + */ public class TorpedoStore { // rate of failing to fire torpedos [0.0, 1.0] - private double FAILURE_RATE = 0.0; //NOSONAR + private double FAILURE_RATE = 0.0; // NOSONAR private int torpedoCount = 0; - public TorpedoStore(int numberOfTorpedos){ + public TorpedoStore(int numberOfTorpedos) { this.torpedoCount = numberOfTorpedos; // update failure rate if it was specified in an environment variable String failureEnv = System.getenv("IVT_RATE"); - if (failureEnv != null){ + if (failureEnv != null) { try { FAILURE_RATE = Double.parseDouble(failureEnv); } catch (NumberFormatException nfe) { @@ -28,20 +28,22 @@ public TorpedoStore(int numberOfTorpedos){ } } - public boolean fire(int numberOfTorpedos){ - if(numberOfTorpedos < 1 || numberOfTorpedos > this.torpedoCount){ - new IllegalArgumentException("numberOfTorpedos"); + Random generator = new Random(); + + public boolean fire(int numberOfTorpedos) { + if (numberOfTorpedos < 1 || numberOfTorpedos > this.torpedoCount) { + throw new IllegalArgumentException("numberOfTorpedos"); } boolean success = false; // simulate random overheating of the launcher bay which prevents firing - Random generator = new Random(); + double r = generator.nextDouble(); if (r >= FAILURE_RATE) { // successful firing - this.torpedoCount =- numberOfTorpedos; + this.torpedoCount -= numberOfTorpedos; success = true; } else { // simulated failure @@ -51,7 +53,7 @@ public boolean fire(int numberOfTorpedos){ return success; } - public boolean isEmpty(){ + public boolean isEmpty() { return this.torpedoCount <= 0; } From d236902bbfdb6ebb41a623a80056856c6c136e61 Mon Sep 17 00:00:00 2001 From: Hajdu Kolos Date: Wed, 16 Oct 2024 09:10:29 +0200 Subject: [PATCH 2/2] Solved review problems