package hu.bme.mit.spaceship;

import java.util.Random;

/**
* Class storing and managing the torpedoes of a ship
*/
public class TorpedoStore {

  private double FAILURE_RATE = 0.0;

  private int torpedoCount = 0;
  private Random generator = new Random();

  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){
      try {
        FAILURE_RATE = Double.parseDouble(failureEnv);
      } catch (NumberFormatException nfe) {
        FAILURE_RATE = 0.0;
      }
    }
  }

  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
    double r = generator.nextDouble();

    if (r >= FAILURE_RATE) {
      // successful firing
      this.torpedoCount -= numberOfTorpedos;
      success = true;
    } else {
      // simulated failure
      success = false;
    }

    return success;
  }

  public boolean isEmpty(){
    return this.torpedoCount <= 0;
  }

  public int getTorpedoCount() {
    return this.torpedoCount;
  }
}

@@ -112,38 +112,121 @@
 ip%0A*
-/%0Apublic class TorpedoStore %7B%0A
+%0A* (Deliberately contains bugs.)%0A*/%0Apublic class TorpedoStore %7B%0A%0A  // rate of failing to fire torpedos %5B0.0, 1.0%5D
 %0A  p
@@ -258,16 +258,26 @@
 E = 0.0;
+ //NOSONAR
 %0A%0A  priv
@@ -305,51 +305,8 @@
 = 0;
-%0A  private Random generator = new Random();
 %0A%0A  
@@ -828,14 +828,8 @@
     
-throw 
 new 
@@ -984,24 +984,61 @@
 ents firing%0A
+    Random generator = new Random();%0A
     double r
@@ -1149,10 +1149,10 @@
 unt 
--
 =
+-
  num

Add clever refactorings