Skip to content

Commit

Permalink
String builder usages
Browse files Browse the repository at this point in the history
  • Loading branch information
LaciProg committed May 19, 2024
1 parent 0b7d58e commit 339276c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 71 deletions.
25 changes: 13 additions & 12 deletions src/main/java/Controll/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ public static void Game() throws FileNotFoundException {
switch(cmd[0]) {
case("show"): show(cmd); break;
case("showobject"): showobject(cmd); break;
case("move"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else move(cmd); break;
case("breakfield"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else breakfield(cmd); break;
case("repair"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else repair(cmd); break;
case("placepump"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else placepump(cmd); break;
case("set"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else set(cmd); break;
case("disconnect"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else disconnect(cmd); break;
case("connect"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else connect(cmd); break;
case("getpump"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else getpump(cmd); break;
case("pickuppipe"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER);else pickuppipe(cmd); break;
case("makesticky"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else makesticky(cmd); break;
case("makeslippery"): moves++; if (!((Player)objectNames.get(cmd[1])).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else makeslippery(cmd); break;
case("move"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else move(cmd); break;
case("breakfield"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else breakfield(cmd); break;
case("repair"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else repair(cmd); break;
case("placepump"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else placepump(cmd); break;
case("set"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else set(cmd); break;
case("disconnect"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else disconnect(cmd); break;
case("connect"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else connect(cmd); break;
case("getpump"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else getpump(cmd); break;
case("pickuppipe"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER);else pickuppipe(cmd); break;
case("makesticky"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else makesticky(cmd); break;
case("makeslippery"): moves++; if (!objectNames.get(cmd[1]).equals(currentPlayer)) logger.log(Level.INFO, StringResourceController.WRONG_PLAYER); else makeslippery(cmd); break;
case("save"): save(cmd); break;
case("testall"): testAll(cmd); break;
case("list"): list(); break;
Expand Down Expand Up @@ -941,7 +941,8 @@ public static void endturn(String[] cmd){
waterCounter.count();
//léptetés
for (Object obj : objectNames.values()) {
if(obj instanceof Steppable value) {
if(obj instanceof Steppable) {
Steppable value = (Steppable) obj;
value.step();
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/Drawing/PipeDraw.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public PipeDraw(int x1, int y1, int x2, int y2) {
pipeB.addActionListener(ViewGame.selectListener);
}
public void setCoords(Drawable pumpFrom, Drawable pumpTo) {
int fromX, toX, fromY, toY;

int fromX;
int toX;
int fromY;
int toY;

fromX = pumpFrom.getX();
fromY = pumpFrom.getY();
toX = pumpTo.getX();
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/Fields/ActiveFields/Cistern.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,26 @@ public Pipe pickUpPipe() {
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();
String playersNames = "null";

StringBuilder playerBuilder = new StringBuilder("null");
for (int i = 0; i < players.size(); i++) {
if(i == 0) playersNames = "";
playersNames += Controller.objectReverseNames.get(players.get(i));
if(i == 0) playerBuilder.delete(0,3);
playerBuilder.append(Controller.objectReverseNames.get(players.get(i)));
if (i != players.size() - 1) {
playersNames += ", ";
playerBuilder.append(", ");
}
}




ArrayList<Pipe> pipes = this.getPipes();
String pipesNames ="null";
StringBuilder pipeBuilder = new StringBuilder("null");
if(pipes != null) {
for (int i = 0; i < pipes.size(); i++) {
if (i == 0) pipesNames = "";
pipesNames += Controller.objectReverseNames.get(pipes.get(i));
if (i == 0) pipeBuilder.delete(0,3);
pipeBuilder.append(Controller.objectReverseNames.get(pipes.get(i)));
if (i != pipes.size() - 1) {
pipesNames += ", ";
pipeBuilder.append(", ");
}
}
}
Expand All @@ -121,7 +121,7 @@ public String toString() {
+ "\noccupied: " + this.isOccupied()
+ "\nwater: " + getWaterNoChange()
+ "\nbroken: " + this.isBroken()
+ "\nplayers: " + playersNames
+ "\npipes: " + pipesNames + "\n";
+ "\nplayers: " + playerBuilder
+ "\npipes: " + pipeBuilder + "\n";
}
}
34 changes: 17 additions & 17 deletions src/main/java/Fields/ActiveFields/Pump.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public String toString() {
String pipesNames = getPipeNames();


String SWaterFrom="";
String SWaterTo="";
String sWaterFrom="";
String sWaterTo="";
if(getWaterFrom() == -1 && getWaterTo() == -1 ){
SWaterFrom = SWaterTo = "null";
sWaterFrom = sWaterTo = "null";
}
else{
SWaterFrom = ""+Controller.objectReverseNames.get(getPipes().get(getWaterFrom()));
SWaterTo = ""+Controller.objectReverseNames.get(getPipes().get(getWaterTo()));
sWaterFrom = ""+Controller.objectReverseNames.get(getPipes().get(getWaterFrom()));
sWaterTo = ""+Controller.objectReverseNames.get(getPipes().get(getWaterTo()));
}


Expand All @@ -151,36 +151,36 @@ public String toString() {
+ "\nplayers: " + playersNames
+ "\npipes: " + pipesNames
+ "\ntank: " + this.getTank()
+ "\nwaterFrom: " +SWaterFrom
+ "\nwaterTo: " +SWaterTo + "\n";
+ "\nwaterFrom: " +sWaterFrom
+ "\nwaterTo: " +sWaterTo + "\n";
}

private String getPlayerNames() {
ArrayList<Player> players = this.getPlayers();
String playersNames = "null";
StringBuilder playerBuilder = new StringBuilder("null");
for (int i = 0; i < players.size(); i++) {
if(i == 0) playersNames = "";
playersNames += Controller.objectReverseNames.get(players.get(i));
if(i == 0) playerBuilder.delete(0,3);
playerBuilder.append(Controller.objectReverseNames.get(players.get(i)));
if (i != players.size() - 1) {
playersNames += ", ";
playerBuilder.append(", ");
}
}
return playersNames;
return playerBuilder.toString();
}


private String getPipeNames(){
ArrayList<Pipe> pipes = this.getPipes();
String pipesNames ="null";
StringBuilder pipeBuilder = new StringBuilder("null");
if(pipes != null) {
for (int i = 0; i < pipes.size(); i++) {
if (i == 0) pipesNames = "";
pipesNames += Controller.objectReverseNames.get(pipes.get(i));
if (i == 0) pipeBuilder.delete(0,3);
pipeBuilder.append(Controller.objectReverseNames.get(pipes.get(i)));
if (i != pipes.size() - 1) {
pipesNames += ", ";
pipeBuilder.append(", ");
}
}
}
return pipesNames;
return pipeBuilder.toString();
}
}
31 changes: 17 additions & 14 deletions src/main/java/Fields/ActiveFields/Spring.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,35 @@ public void step() {
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();

String playersNames = "null";

StringBuilder playerBuilder = new StringBuilder("null");
for (int i = 0; i < players.size(); i++) {
if(i == 0) playersNames = "";
playersNames += Controller.objectReverseNames.get(players.get(i));
if(i == 0) playerBuilder.delete(0,3);
playerBuilder.append(Controller.objectReverseNames.get(players.get(i)));
if (i != players.size() - 1) {
playersNames += ", ";
playerBuilder.append(", ");
}
}




ArrayList<Pipe> pipes = this.getPipes();
String pipesNames ="null";
for (int i = 0; i < pipes.size(); i++) {
if(i == 0) pipesNames = "";
pipesNames += Controller.objectReverseNames.get(pipes.get(i));
if (i != pipes.size() - 1) {
pipesNames += ", ";
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 "name: "+ Controller.objectReverseNames.get(this)
+ "\noccupied: " + this.isOccupied()
+ "\nwater: " + getWaterNoChange()
+ "\nbroken: " + this.isBroken()
+ "\nplayers: " + playersNames
+ "\npipes: " + pipesNames
+ "\nplayers: " + playerBuilder
+ "\npipes: " + pipeBuilder
+ "\nwaterOut: " + this.getWaterOut()
+ "\nmaxOutWater: " + this.getMaxOutWater() + "\n";
}
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/Fields/Pipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,33 +336,31 @@ public void step(){
@Override
public String toString() {
ArrayList<Player> players = this.getPlayers();

String playersNames = "null";

StringBuilder playerBuilder = new StringBuilder("null");
for (int i = 0; i < players.size(); i++) {
if(i == 0) playersNames = "";
playersNames += Controller.objectReverseNames.get(players.get(i));
if(i == 0) playerBuilder.delete(0,3);
playerBuilder.append(Controller.objectReverseNames.get(players.get(i)));
if (i != players.size() - 1) {
playersNames += ", ";
playerBuilder.append(", ");
}
}

ArrayList<ActiveFields> loaclFields = this.getFields();
String fieldsNames ="null";
for (int i = 0; i < loaclFields.size(); i++) {
if(i == 0) fieldsNames = "";
fieldsNames += Controller.objectReverseNames.get(loaclFields.get(i));
if (i != loaclFields.size() - 1) {
fieldsNames += ", ";
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(", ");
}
}

return "name: " + Controller.objectReverseNames.get(this)
+ "\noccupied: " + this.isOccupied()
+ "\nwater: " + getWaterNoChange()
+ "\nbroken: " + this.isBroken()
+ "\nplayers: " + playersNames
+ "\nfields: " + fieldsNames
+ "\nplayers: " + playerBuilder
+ "\nfields: " + fieldBuilder
+ "\ncapacity: " + this.getCapacity()
+ "\nbreakable: " + this.getBreakable()
+ "\nrfluidtime: " + this.getRemainingFluidTime()
Expand Down
File renamed without changes.

0 comments on commit 339276c

Please sign in to comment.