package hu.bme.mit.spaceship;

import java.util.Random;

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

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

  public TorpedoStore(int numberOfTorpedos){
    this.torpedoCount = numberOfTorpedos;
  }

  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 > 0.01) {
      // successful firing
      this.torpedoCount -= numberOfTorpedos;
      success = true;
    } else {
      // failure
      success = false;
    }

    return success;
  }

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

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

@@ -139,16 +139,54 @@
 tore %7B%0A%0A
+  private double FAILURE_RATE = 0.0;%0A%0A
   privat
@@ -209,16 +209,16 @@
 nt = 0;%0A
-
   privat
@@ -339,16 +339,324 @@
 orpedos;
+%0A%0A    // update failure rate if it was specified in an environment variable%0A    String failureEnv = System.getenv(%22IVT_RATE%22);%0A    if (failureEnv != null)%7B%0A      try %7B%0A        FAILURE_RATE = Double.parseDouble(failureEnv);%0A      %7D catch (NumberFormatException nfe) %7B%0A        FAILURE_RATE = 0.0;%0A      %7D%0A    %7D
 %0A  %7D%0A%0A  
@@ -873,16 +873,17 @@
 %0A%0A    //
+ 
 simulate
@@ -999,13 +999,22 @@
 (r %3E
- 0.01
+= FAILURE_RATE
 ) %7B%0A
@@ -1120,24 +1120,24 @@
  else %7B%0A
-
       //
  failure
@@ -1128,16 +1128,26 @@
       //
+ simulated
  failure

Add torpedo failure rate as an environment variable