Skip to content

Commit

Permalink
Merge pull request #1 from hajduko/bugfix
Browse files Browse the repository at this point in the history
Solved problems in TorpedoStore.java
  • Loading branch information
hajduko authored Oct 16, 2024
2 parents f086807 + d236902 commit 1737cdb
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/main/java/hu/bme/mit/spaceship/TorpedoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -51,7 +53,7 @@ public boolean fire(int numberOfTorpedos){
return success;
}

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

Expand Down

0 comments on commit 1737cdb

Please sign in to comment.