Skip to content

Commit

Permalink
Add clever refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
micskeiz committed Apr 2, 2019
1 parent e6dab5c commit eacc265
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/java/hu/bme/mit/spaceship/TorpedoStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

/**
* Class storing and managing the torpedoes of a ship
*
* (Deliberately contains bugs.)
*/
public class TorpedoStore {

private double FAILURE_RATE = 0.0;
// rate of failing to fire torpedos [0.0, 1.0]
private double FAILURE_RATE = 0.0; //NOSONAR

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

public TorpedoStore(int numberOfTorpedos){
this.torpedoCount = numberOfTorpedos;
Expand All @@ -28,17 +30,18 @@ public TorpedoStore(int numberOfTorpedos){

public boolean fire(int numberOfTorpedos){
if(numberOfTorpedos < 1 || numberOfTorpedos > this.torpedoCount){
throw new IllegalArgumentException("numberOfTorpedos");
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 Down

0 comments on commit eacc265

Please sign in to comment.