package hu.bme.mit.spaceship;

/**
* A simple spaceship with two proton torpedos and four lasers
*/
public class GT4500 implements SpaceShip {

  private TorpedoStore primaryTorpedoStore;
  private TorpedoStore secondaryTorpedoStore;

  private boolean wasPrimaryFiredLast = false;

  public GT4500() {
    this.primaryTorpedoStore = new TorpedoStore(10);
    this.secondaryTorpedoStore = new TorpedoStore(10);
  }

  public boolean fireLasers(FiringMode firingMode) {
    // TODO not implemented yet
    return false;
  }

  /**
  * Tries to fire the torpedo stores of the ship.
  *
  * @param firingMode how many torpedo bays to fire
  * 	SINGLE: fires only one of the bays.
  * 			- For the first time the primary store is fired.
  * 			- To give some cooling time to the torpedo stores, torpedo stores are fired alternating.
  * 			- But if the store next in line is empty the ship tries to fire the other store.
  * 			- If the fired store reports a failure, the ship does not try to fire the other one.
  * 	ALL:	tries to fire both of the torpedo stores.
  *
  * @return whether at least one torpedo was fired successfully
  */
  @Override
  public boolean fireTorpedos(FiringMode firingMode) {

    boolean firingSuccess = false;

    switch (firingMode) {
      case SINGLE:
        if (wasPrimaryFiredLast) {
          // try to fire the secondary first
          if (! secondaryTorpedoStore.isEmpty()) {
            firingSuccess = secondaryTorpedoStore.fire(1);
            wasPrimaryFiredLast = false;
          }
          else {
            // although primary was fired last time, but the secondary is empty
            // thus try to fire primary again
            if (! primaryTorpedoStore.isEmpty()) {
              firingSuccess = primaryTorpedoStore.fire(1);
              wasPrimaryFiredLast = true;
            }

            // if both of the stores are empty, nothing can be done, return failure
          }
        }
        else {
          // try to fire the primary first
          if (! primaryTorpedoStore.isEmpty()) {
            firingSuccess = primaryTorpedoStore.fire(1);
            wasPrimaryFiredLast = true;
          }
          else {
            // although secondary was fired last time, but primary is empty
            // thus try to fire secondary again
            if (! secondaryTorpedoStore.isEmpty()) {
              firingSuccess = secondaryTorpedoStore.fire(1);
              wasPrimaryFiredLast = false;
            }

            // if both of the stores are empty, nothing can be done, return failure
          }
        }
        break;

      case ALL:
        // try to fire both of the torpedos
        //TODO implement feature

        break;
    }

    return firingSuccess;
  }

}

@@ -1276,24 +1276,287 @@
 SINGLE:%0A    
+    firingSuccess = singleFire();%0A        break;%0A%0A      case ALL:%0A        // try to fire both of the torpedos%0A        //TODO implement feature%0A%0A        break;%0A    %7D%0A%0A    return firingSuccess;%0A  %7D%0A%0A  public boolean singleFire()%7B%0A    boolean firingSuccess = false;%0A
     if (wasP
@@ -1570,28 +1570,24 @@
 iredLast) %7B%0A
-    
       // try
@@ -1617,28 +1617,24 @@
 first%0A      
- 
-   
 if (! second
@@ -1654,36 +1654,32 @@
 re.isEmpty()) %7B%0A
-    
         firingSu
@@ -1717,36 +1717,32 @@
 ire(1);%0A        
- 
-   
 wasPrimaryFiredL
@@ -1764,35 +1764,23 @@
     
-    %7D%0A  
+%7D%0A
       
-  
 else %7B%0A
-    
     
@@ -1851,36 +1851,32 @@
 s empty%0A        
- 
-   
 // thus try to f
@@ -1885,36 +1885,32 @@
 e primary again%0A
-    
         if (! pr
@@ -1942,36 +1942,32 @@
 ()) %7B%0A          
- 
-   
 firingSuccess = 
@@ -1997,36 +1997,32 @@
 e(1);%0A          
-    
 wasPrimaryFiredL
@@ -2037,35 +2037,27 @@
 ue;%0A        
- 
-   %7D%0A%0A    
+%7D%0A%0A
         // i
@@ -2134,43 +2134,27 @@
     
-    
 %7D%0A    
- 
-   
 %7D%0A    
- 
-   
 else %7B%0A
-    
     
@@ -2190,28 +2190,24 @@
 first%0A      
-  
-  
 if (! primar
@@ -2225,36 +2225,32 @@
 re.isEmpty()) %7B%0A
-    
         firingSu
@@ -2286,36 +2286,32 @@
 ire(1);%0A        
-  
-  
 wasPrimaryFiredL
@@ -2332,35 +2332,23 @@
     
-    %7D%0A  
+%7D%0A
       
-  
 else %7B%0A
-    
     
@@ -2419,28 +2419,24 @@
 pty%0A        
-  
-  
 // thus try 
@@ -2455,28 +2455,24 @@
 ndary again%0A
-    
         if (
@@ -2514,28 +2514,24 @@
 %7B%0A          
-  
-  
 firingSucces
@@ -2571,28 +2571,24 @@
 ;%0A          
-    
 wasPrimaryFi
@@ -2608,35 +2608,27 @@
 se;%0A        
-  
-  
 %7D%0A%0A
-    
         // i
@@ -2705,148 +2705,9 @@
     
-    %7D%0A        %7D%0A        break;%0A%0A      case ALL:%0A        // try to fire both of the torpedos%0A        //TODO implement feature%0A%0A        break;
+%7D
 %0A   

Refactor fireTorpedos method