Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package boxcar;
- import java.util.ArrayList;
- public class FreightTrain {
- private ArrayList<Boxcar> train = new ArrayList<Boxcar>();
- public FreightTrain()
- {
- train.add(new Boxcar()) ;
- }
- public FreightTrain(int n)
- {
- if(n <= 0) {
- train.add(new Boxcar()) ;
- }
- else {
- for(int i = 0; i < n; i++) {
- train.add(new Boxcar()) ;
- }
- }
- }
- // This method returns a String representation of the
- // Boxcar objects in the ArrayList, one per line. For example,
- // here is the String returned when toString is called on a
- // FreightTrain with 5 boxcars:
- //
- // 3 gadgets in service
- // 2 wadgets in service
- // 0 gizmos in repair
- // 7 gadgets in service
- // 0 gadgets in repair
- //
- // Note: there is one space between the number of units and
- // the cargo type, and a tab between the cargo type and
- // "in repair"/"in service".
- public String toString()
- {
- String output = "" ;
- for(Boxcar i: train) {
- output = output + i + '\n' ;
- }
- return output;
- }
- public void setCargo(String c)
- {
- for(Boxcar x: train) {
- x.setCargo(c);
- }
- }
- public void setMultiCargo()
- {
- int counter = 0 ;
- for(Boxcar y: train) {
- if(counter == 0) {
- y.setCargo("gizmos");
- }
- if(counter == 1) {
- y.setCargo("gadgets");
- }
- if(counter == 2) {
- y.setCargo("widgets");
- }
- if(counter == 3) {
- y.setCargo("wadgets");
- }
- if(counter == 4) {
- y.setCargo("gizmos");
- }
- if(counter == 5) {
- y.setCargo("gadgets");
- }
- if(counter == 6) {
- y.setCargo("widgets");
- }
- if(counter == 7) {
- y.setCargo("wadgets");
- counter = 0;
- }
- counter ++ ;
- }
- }
- public void fillTrain()
- {
- for(Boxcar y: train) {
- for(int i = 0; i < 11; i++) {
- y.loadCargo();
- }
- }
- }
- public void callForRepair(int i)
- {
- train.get(i).callForRepair();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement