diff --git a/src/main/java/Fields/ActiveFields/Cistern.java b/src/main/java/Fields/ActiveFields/Cistern.java index 5505de8..02e099d 100644 --- a/src/main/java/Fields/ActiveFields/Cistern.java +++ b/src/main/java/Fields/ActiveFields/Cistern.java @@ -3,6 +3,7 @@ import Controll.Controller; import Fields.Pipe; import Players.Player; +import StringResource.StringResourceController; import java.util.ArrayList; import java.util.Random; @@ -93,29 +94,10 @@ public Pipe pickUpPipe() { @Override public String toString() { ArrayList players = this.getPlayers(); - StringBuilder playerBuilder = new StringBuilder("null"); - for (int i = 0; i < players.size(); i++) { - if(i == 0) playerBuilder.delete(0,3); - playerBuilder.append(Controller.objectReverseNames.get(players.get(i))); - if (i != players.size() - 1) { - playerBuilder.append(", "); - } - } - - - + String playerBuilder = StringResourceController.stingBuilder(players); ArrayList pipes = this.getPipes(); - StringBuilder pipeBuilder = new StringBuilder("null"); - if(pipes != null) { - for (int i = 0; i < pipes.size(); i++) { - if (i == 0) pipeBuilder.delete(0,3); - pipeBuilder.append(Controller.objectReverseNames.get(pipes.get(i))); - if (i != pipes.size() - 1) { - pipeBuilder.append(", "); - } - } - } + String pipeBuilder = StringResourceController.stingBuilder(pipes); return "name: "+ Controller.objectReverseNames.get(this) + "\noccupied: " + this.isOccupied() diff --git a/src/main/java/Fields/ActiveFields/Pump.java b/src/main/java/Fields/ActiveFields/Pump.java index 59c2d13..69e9327 100644 --- a/src/main/java/Fields/ActiveFields/Pump.java +++ b/src/main/java/Fields/ActiveFields/Pump.java @@ -3,6 +3,7 @@ import Controll.Controller; import Fields.Pipe; import Players.Player; +import StringResource.StringResourceController; import java.util.ArrayList; import java.util.Random; @@ -128,8 +129,6 @@ public String toString() { String playersNames = getPlayerNames(); - - String pipesNames = getPipeNames(); @@ -157,30 +156,12 @@ public String toString() { private String getPlayerNames() { ArrayList players = this.getPlayers(); - StringBuilder playerBuilder = new StringBuilder("null"); - for (int i = 0; i < players.size(); i++) { - if(i == 0) playerBuilder.delete(0,3); - playerBuilder.append(Controller.objectReverseNames.get(players.get(i))); - if (i != players.size() - 1) { - playerBuilder.append(", "); - } - } - return playerBuilder.toString(); + return StringResourceController.stingBuilder(players); } private String getPipeNames(){ ArrayList pipes = this.getPipes(); - StringBuilder pipeBuilder = new StringBuilder("null"); - if(pipes != null) { - for (int i = 0; i < pipes.size(); i++) { - if (i == 0) pipeBuilder.delete(0,3); - pipeBuilder.append(Controller.objectReverseNames.get(pipes.get(i))); - if (i != pipes.size() - 1) { - pipeBuilder.append(", "); - } - } - } - return pipeBuilder.toString(); + return StringResourceController.stingBuilder(pipes); } } diff --git a/src/main/java/Fields/ActiveFields/Spring.java b/src/main/java/Fields/ActiveFields/Spring.java index a47c5d8..b6e842f 100644 --- a/src/main/java/Fields/ActiveFields/Spring.java +++ b/src/main/java/Fields/ActiveFields/Spring.java @@ -3,6 +3,7 @@ import Controll.Controller; import Fields.Pipe; import Players.Player; +import StringResource.StringResourceController; import java.util.ArrayList; @@ -60,29 +61,12 @@ public void step() { @Override public String toString() { ArrayList players = this.getPlayers(); - StringBuilder playerBuilder = new StringBuilder("null"); - for (int i = 0; i < players.size(); i++) { - if(i == 0) playerBuilder.delete(0,3); - playerBuilder.append(Controller.objectReverseNames.get(players.get(i))); - if (i != players.size() - 1) { - playerBuilder.append(", "); - } - } - - + String playerBuilder = StringResourceController.stingBuilder(players); ArrayList pipes = this.getPipes(); - StringBuilder pipeBuilder = new StringBuilder("null"); - if(pipes != null) { - for (int i = 0; i < pipes.size(); i++) { - if (i == 0) pipeBuilder.delete(0,3); - pipeBuilder.append(Controller.objectReverseNames.get(pipes.get(i))); - if (i != pipes.size() - 1) { - pipeBuilder.append(", "); - } - } - } + String pipeBuilder = StringResourceController.stingBuilder(pipes); + return "name: "+ Controller.objectReverseNames.get(this) + "\noccupied: " + this.isOccupied() + "\nwater: " + getWaterNoChange() diff --git a/src/main/java/Fields/Pipe.java b/src/main/java/Fields/Pipe.java index 1801a81..e82f050 100644 --- a/src/main/java/Fields/Pipe.java +++ b/src/main/java/Fields/Pipe.java @@ -5,6 +5,7 @@ import Fields.ActiveFields.ActiveFields; import Fields.ActiveFields.Pump; import Players.Player; +import StringResource.StringResourceController; import java.util.ArrayList; import java.util.Random; @@ -340,25 +341,10 @@ public void step(){ @Override public String toString() { ArrayList players = this.getPlayers(); - StringBuilder playerBuilder = new StringBuilder("null"); - for (int i = 0; i < players.size(); i++) { - if(i == 0) playerBuilder.delete(0,3); - playerBuilder.append(Controller.objectReverseNames.get(players.get(i))); - if (i != players.size() - 1) { - playerBuilder.append(", "); - } - } + String playerBuilder = StringResourceController.stingBuilder(players); ArrayList localFields = this.getFields(); - StringBuilder fieldBuilder = new StringBuilder("null"); - for (int i = 0; i < localFields.size(); i++) { - if(i == 0) fieldBuilder.delete(0,3); - fieldBuilder.append(Controller.objectReverseNames.get(localFields.get(i))); - if (i != localFields.size() - 1) { - fieldBuilder.append(", "); - } - } - + String fieldBuilder = StringResourceController.stingBuilder(localFields); return "name: " + Controller.objectReverseNames.get(this) + "\noccupied: " + this.isOccupied() + "\nwater: " + getWaterNoChange() diff --git a/src/main/java/StringResource/StringResourceController.java b/src/main/java/StringResource/StringResourceController.java index e8414b0..2e94262 100644 --- a/src/main/java/StringResource/StringResourceController.java +++ b/src/main/java/StringResource/StringResourceController.java @@ -1,5 +1,9 @@ package StringResource; +import Controll.Controller; + +import java.util.List; + public final class StringResourceController { private StringResourceController(){} public static final String FALSE = "false"; @@ -10,4 +14,18 @@ private StringResourceController(){} public static final String WATER = "water"; public static final String RANDOM_ON = "A véletlen események be lettek kapcsolva."; public static final String INVALID_ACTION = "A műveletet nem lehet végrehajtani"; + + public static String stingBuilder(List collection){ + StringBuilder builder = new StringBuilder("null"); + if(collection != null) { + for (int i = 0; i < collection.size(); i++) { + if (i == 0) builder.delete(0,3); + builder.append(Controller.objectReverseNames.get(collection.get(i))); + if (i != collection.size() - 1) { + builder.append(", "); + } + } + } + return builder.toString(); + } } \ No newline at end of file