Advertisement
apez1

Assignment 2: Part 2 - FreightTrain

Jan 18th, 2019
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package boxcar;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class FreightTrain {
  6.    
  7.     private ArrayList<Boxcar> train = new ArrayList<Boxcar>();
  8.  
  9.    
  10.     public FreightTrain()
  11.     {
  12.  
  13.         train.add(new Boxcar()) ;
  14.  
  15.     }
  16.  
  17.    
  18.     public FreightTrain(int n)
  19.     {
  20.        
  21.         if(n <= 0) {
  22.             train.add(new Boxcar()) ;
  23.         }
  24.        
  25.         else {
  26.        
  27.             for(int i = 0; i < n; i++) {
  28.             train.add(new Boxcar()) ;
  29.             }
  30.         }
  31.        
  32.        
  33.     }
  34.  
  35.     // This method returns a String representation of the
  36.     // Boxcar objects in the ArrayList, one per line. For example,
  37.     // here is the String returned when toString is called on a
  38.     // FreightTrain with 5 boxcars:
  39.     //
  40.     // 3 gadgets    in service
  41.     // 2 wadgets    in service
  42.     // 0 gizmos    in repair
  43.     // 7 gadgets    in service
  44.     // 0 gadgets    in repair
  45.     //
  46.     // Note: there is one space between the number of units and
  47.     // the cargo type, and a tab between the cargo type and
  48.     // "in repair"/"in service".
  49.    
  50.    
  51.     public String toString()
  52.     {
  53.  
  54.         String output = "" ;
  55.        
  56.             for(Boxcar i: train) {
  57.                 output =  output + i + '\n' ;
  58.             }
  59.        
  60.        
  61.    
  62.         return output;
  63.     }
  64.  
  65.     public void setCargo(String c)
  66.     {
  67.  
  68.         for(Boxcar x: train) {
  69.            
  70.             x.setCargo(c);
  71.            
  72.            
  73.         }
  74.    
  75.    
  76.    
  77.     }
  78.  
  79.    
  80.     public void setMultiCargo()
  81.     {
  82.         int counter = 0 ;
  83.        
  84.         for(Boxcar y: train) {
  85.            
  86.             if(counter == 0) {
  87.                 y.setCargo("gizmos");
  88.             }
  89.             if(counter == 1) {
  90.                 y.setCargo("gadgets");
  91.  
  92.             }
  93.             if(counter == 2) {
  94.                 y.setCargo("widgets");
  95.  
  96.             }
  97.             if(counter == 3) {
  98.                 y.setCargo("wadgets");
  99.  
  100.             }
  101.             if(counter == 4) {
  102.                 y.setCargo("gizmos");
  103.  
  104.             }
  105.             if(counter == 5) {
  106.                 y.setCargo("gadgets");
  107.  
  108.             }
  109.             if(counter == 6) {
  110.                 y.setCargo("widgets");
  111.  
  112.             }
  113.             if(counter == 7) {
  114.                 y.setCargo("wadgets");     
  115.                 counter = 0;
  116.             }
  117.            
  118.             counter ++ ;
  119.            
  120.    
  121.            
  122.            
  123.         }
  124.    
  125.    
  126.    
  127.     }
  128.  
  129.     public void fillTrain()
  130.     {
  131.  
  132.         for(Boxcar y: train) {
  133.            
  134.             for(int i = 0; i < 11; i++) {
  135.                 y.loadCargo();
  136.             }
  137.            
  138.            
  139.         }
  140.    
  141.    
  142.     }
  143.  
  144.     public void callForRepair(int i)
  145.     {
  146.  
  147.         train.get(i).callForRepair();
  148.    
  149.    
  150.     }
  151.    
  152.    
  153.    
  154.    
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement