Skip to content

Commit

Permalink
reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
LaciProg committed May 19, 2024
1 parent fcb04cd commit a8645f8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 80 deletions.
24 changes: 3 additions & 21 deletions src/main/java/Fields/ActiveFields/Cistern.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Controll.Controller;
import Fields.Pipe;
import Players.Player;
import StringResource.StringResourceController;

import java.util.ArrayList;
import java.util.Random;
Expand Down Expand Up @@ -93,29 +94,10 @@ public Pipe pickUpPipe() {
@Override
public String toString() {
ArrayList<Player> 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<Pipe> 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()
Expand Down
25 changes: 3 additions & 22 deletions src/main/java/Fields/ActiveFields/Pump.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Controll.Controller;
import Fields.Pipe;
import Players.Player;
import StringResource.StringResourceController;

import java.util.ArrayList;
import java.util.Random;
Expand Down Expand Up @@ -128,8 +129,6 @@ public String toString() {

String playersNames = getPlayerNames();



String pipesNames = getPipeNames();


Expand Down Expand Up @@ -157,30 +156,12 @@ public String toString() {

private String getPlayerNames() {
ArrayList<Player> 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<Pipe> 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);
}
}
24 changes: 4 additions & 20 deletions src/main/java/Fields/ActiveFields/Spring.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Controll.Controller;
import Fields.Pipe;
import Players.Player;
import StringResource.StringResourceController;

import java.util.ArrayList;

Expand Down Expand Up @@ -60,29 +61,12 @@ public void step() {
@Override
public String toString() {
ArrayList<Player> 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<Pipe> 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()
Expand Down
20 changes: 3 additions & 17 deletions src/main/java/Fields/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -340,25 +341,10 @@ public void step(){
@Override
public String toString() {
ArrayList<Player> 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<ActiveFields> 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()
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/StringResource/StringResourceController.java
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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();
}
}

0 comments on commit a8645f8

Please sign in to comment.