From a92a3fea29911be8c77301605852663085c53812 Mon Sep 17 00:00:00 2001 From: Laci Date: Wed, 22 May 2024 00:13:01 +0200 Subject: [PATCH] Copy files to other folder for merge --- .../src/Controll/Controller.java | 241 ++++++++++-------- .../src/Controll/Menu.java | 11 +- .../src/Controll/ViewGame.java | 10 +- .../src/Drawing/CisternDraw.java | 2 + .../src/Drawing/PipeDraw.java | 7 +- .../src/Drawing/PumpDraw.java | 1 + .../src/Drawing/SaboteurDraw.java | 1 + .../src/Drawing/SpringDraw.java | 1 + .../src/Fields/ActiveFields/ActiveFields.java | 2 +- .../src/Fields/ActiveFields/Cistern.java | 41 +-- .../src/Fields/ActiveFields/Pump.java | 39 +-- .../src/Fields/ActiveFields/Spring.java | 25 +- .../src/Fields/Field.java | 1 + .../src/Fields/Pipe.java | 42 ++- .../src/Players/Saboteur.java | 1 + .../StringResourceController.java | 18 ++ 16 files changed, 212 insertions(+), 231 deletions(-) diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Controller.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Controller.java index 12d6f29..1b09cd0 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Controller.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Controller.java @@ -14,20 +14,13 @@ import Players.Saboteur; import StringResource.StringResourceController; -import javax.swing.text.View; import java.io.*; -import java.lang.reflect.Array; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.HashMap; -import java.util.Map; -import java.util.Iterator; -import java.util.Scanner; -import java.util.regex.Pattern; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.regex.Pattern; @SuppressWarnings("DuplicatedCode") @@ -185,9 +178,8 @@ public static void Run() { /** * Game mode after "create" you cannot create new objects in this mode. - * @throws FileNotFoundException */ - public static void Game() throws FileNotFoundException { + public static void Game(){ while(gameMode) { currentPlayer = activePlayers.get(0); // az első játékos a sor végére rakom, jelenleg ő az aktív activePlayers.remove(0); @@ -202,17 +194,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; @@ -248,7 +240,7 @@ public static void load(String cmd){ fileName = tmp[tmp.length-1]; while (scanner.hasNextLine()){ commandList.add(scanner.nextLine()); - logger.log(Level.INFO, commandList.get(commandList.size()-1)); + logger.log(Level.INFO, ()-> commandList.get(commandList.size()-1)); } if (test) { commandList.add("save " + filePath.replace(".in", ".out")); @@ -274,7 +266,7 @@ public static void loadFileFromSrcToReader(String fileNameToOpen) { } } } catch (IOException e) { - e.printStackTrace(); + logger.log(Level.WARNING, StringResourceController.FILE_NOT_FOUND); } } @@ -288,13 +280,21 @@ public static void pump(String[] cmd){ commands[i-3] = cmd[i].split(":"); } - for(int i=0; i neighbors = p.getStandingField().getNeighborFields(); + List neighbors = p.getStandingField().getNeighborFields(); if(neighbors.contains(f)) { neighborsContains(p,f); } @@ -589,9 +616,10 @@ public static void placepump(String[] cmd){ pumpA = ((Pipe)p.getStandingField()).getFields().get(0); pumpB = ((Pipe)p.getStandingField()).getFields().get(1); } catch(Exception e) { - + logger.log(Level.WARNING, StringResourceController.INVALID_ACTION); } - + + assert p != null; Pump hp = ((Mechanic)p).getHoldingPump(); //new pump Pipe pipe = p.placePump(); //new pipe if(pipe != null ){ @@ -684,11 +712,14 @@ public static void connect(String[] cmd){ try { holdedPipe = ((Mechanic)player).getHoldingPipe(); standing = (Pump)player.getStandingField(); - } catch(Exception e) {} + } catch(Exception e) { + logger.log(Level.WARNING, StringResourceController.INVALID_ACTION); + } if(player.connect()){ lastResult = true; PipeDraw pd = (PipeDraw)ViewGame.objectDrawReverseNames.get(holdedPipe); + assert holdedPipe != null; Drawable toPumpD = ViewGame.objectDrawReverseNames.get(holdedPipe.getFields().get(0)); Drawable fromPumpD = ViewGame.objectDrawReverseNames.get(standing); pd.setCoords(fromPumpD, toPumpD); @@ -735,7 +766,7 @@ public static void getpump(String[] cmd){ public static void pickuppipe(String[] cmd){ Player player = (Player)objectNames.get(cmd[1]); if(player.pickUpPipe()){ - lastResult = lastResult && true; + lastResult = true; //legyen már az új csőnek drawable-je is Pipe newPipe = ((Mechanic)player).getHoldingPipe(); @@ -798,43 +829,42 @@ public static void makeslippery(String[] cmd){ * */ public static void save(String[] cmd) { try (PrintWriter out = new PrintWriter(cmd[1].replace(".in", ".out"))) { - for (int i = 0; i < outResults.size(); i++) { - out.println(outResults.get(i)); + for (String outResult : outResults) { + out.println(outResult); } } catch(FileNotFoundException e) { logger.log(Level.WARNING, StringResourceController.FILE_NOT_FOUND); } - try { - Scanner scannerResult = new Scanner(new File(cmd[1])); - Scanner scannerExpected = new Scanner((new File(cmd[1].replace(".out", ".test")))); - ArrayList result = new ArrayList<>(); - ArrayList expected = new ArrayList<>(); - while (scannerResult.hasNextLine()){ - result.add(scannerResult.nextLine().strip()); - } - while (scannerExpected.hasNextLine()){ - expected.add(scannerExpected.nextLine().strip()); - } - String separator = "\\"; - String[] tmp=cmd[1].replaceAll(Pattern.quote(separator), "\\\\").split("\\\\"); - fileName = tmp[tmp.length-1]; - logger.log(Level.INFO, "Test name: " + fileName.replace(".out", "")); - if (result.size() != expected.size()) { - logger.log(Level.INFO, "Test failed. The 2 files do not have the same amount of lines."); - return; - } - int errors = getErros(result, expected); - if (errors == 0 && !result.isEmpty() && !expected.isEmpty()) { - logger.log(Level.INFO, "Test succeeded.\n"); - } - else { - logger.log(Level.INFO, "Test failed.\n"); + try(Scanner scannerResult = new Scanner(new File(cmd[1]))) { + try(Scanner scannerExpected = new Scanner((new File(cmd[1].replace(".out", ".test"))));) { + ArrayList result = new ArrayList<>(); + ArrayList expected = new ArrayList<>(); + while (scannerResult.hasNextLine()) { + result.add(scannerResult.nextLine().strip()); + } + while (scannerExpected.hasNextLine()) { + expected.add(scannerExpected.nextLine().strip()); + } + String separator = "\\"; + String[] tmp = cmd[1].replaceAll(Pattern.quote(separator), "\\\\").split("\\\\"); + fileName = tmp[tmp.length - 1]; + logger.log(Level.INFO, () -> "Test name: " + fileName.replace(".out", "")); + if (result.size() != expected.size()) { + logger.log(Level.INFO, "Test failed. The 2 files do not have the same amount of lines."); + return; + } + int errors = getErros(result, expected); + if (errors == 0 && !result.isEmpty() && !expected.isEmpty()) { + logger.log(Level.INFO, "Test succeeded.\n"); + } else { + logger.log(Level.INFO, "Test failed.\n"); + } + pipes = pumps = 0; + waterCounter.reset(); + objectNames.clear(); + objectReverseNames.clear(); } - pipes=pumps=0; - waterCounter.reset(); - objectNames.clear(); - objectReverseNames.clear(); } catch(FileNotFoundException e) { logger.log(Level.WARNING, StringResourceController.FILE_NOT_FOUND); @@ -847,7 +877,8 @@ private static int getErros(ArrayList result, ArrayList expected if (!result.isEmpty() && !expected.isEmpty()) { for (int i = 0; i < expected.size(); i++) { if (!result.get(i).equals(expected.get(i))) { - logger.log(Level.INFO, "Error in line " + (i+1) + ".\nExpected: " + expected.get(i) + ", but got: " + result.get(i)); + int finalI = i; + logger.log(Level.INFO, () -> "Error in line " + (finalI +1) + ".\nExpected: " + expected.get(finalI) + ", but got: " + result.get(finalI)); errors++; } } @@ -873,7 +904,7 @@ public static void testAll(String[] cmd) { * */ public static void list(){ for (Object obj : objectNames.values()) { - logger.log(Level.INFO, objectReverseNames.get(obj) + " "); + logger.log(Level.INFO, () -> objectReverseNames.get(obj) + " "); } logger.log(Level.INFO, ""); } /** @@ -909,7 +940,7 @@ public static void endturn(String[] cmd){ //léptetés for (Object obj : objectNames.values()) { if(obj instanceof Steppable) { - Steppable value = (Steppable)obj; + Steppable value = (Steppable) obj; value.step(); } } diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Menu.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Menu.java index bc1e2ae..dae4f42 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Menu.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/Menu.java @@ -1,16 +1,9 @@ package Controll; +import javax.swing.*; +import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.awt.Color; -import javax.swing.WindowConstants; - -import java.io.FileNotFoundException; - -import javax.swing.JFrame; -import javax.swing.JTextField; -import javax.swing.JButton; -import javax.swing.JComboBox; public class Menu extends JFrame implements ActionListener { diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/ViewGame.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/ViewGame.java index e09e536..2f07afc 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/ViewGame.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Controll/ViewGame.java @@ -1,20 +1,18 @@ package Controll; -import Drawing.*; +import Drawing.Drawable; +import Drawing.PipeDraw; import Enums.Fluid; -import Fields.*; - -import Fields.ActiveFields.*; +import Fields.Pipe; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; -import java.util.List; import java.util.HashMap; +import java.util.List; import java.util.Map; -import javax.swing.WindowConstants; public class ViewGame extends JFrame implements ActionListener { diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/CisternDraw.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/CisternDraw.java index c0e5515..477c67a 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/CisternDraw.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/CisternDraw.java @@ -1,6 +1,7 @@ package Drawing; import Controll.ViewGame; + import javax.swing.*; import java.awt.*; @@ -31,6 +32,7 @@ public CisternDraw(int tmpX, int tmpY) { * @param panel * @param g */ + @Override public void Draw(JPanel panel, Graphics2D g) { cistern.setBounds(x, y, 50, 50); cistern.setBorder(BorderFactory.createLineBorder(Color.blue, 5)); diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PipeDraw.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PipeDraw.java index 06ab9cb..8d2b945 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PipeDraw.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PipeDraw.java @@ -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(); diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PumpDraw.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PumpDraw.java index 5f3837d..ea3bfaa 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PumpDraw.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/PumpDraw.java @@ -31,6 +31,7 @@ public PumpDraw(int tmpX, int tmpY) { * @param panel The panel to draw on. * @param g The graphics to draw with. * */ + @Override public void Draw(JPanel panel, Graphics2D g) { pump.setBounds(x, y, 50, 50); Pump p = (Pump)ViewGame.objectDrawNames.get(this); diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SaboteurDraw.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SaboteurDraw.java index 9bcf1ad..40443b3 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SaboteurDraw.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SaboteurDraw.java @@ -9,6 +9,7 @@ import Fields.Pipe; import Players.Player; import Players.Saboteur; + import javax.swing.*; import java.awt.*; import java.util.ArrayList; diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SpringDraw.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SpringDraw.java index c10b40d..5642954 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SpringDraw.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Drawing/SpringDraw.java @@ -1,6 +1,7 @@ package Drawing; import Controll.ViewGame; + import javax.swing.*; import java.awt.*; diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/ActiveFields.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/ActiveFields.java index 507b7be..521183e 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/ActiveFields.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/ActiveFields.java @@ -22,7 +22,7 @@ public abstract class ActiveFields extends Field { * @return The pipes connected to the active field */ public ArrayList getPipes() { - if (pipes.size() == 0) return null; + if (pipes.isEmpty()) return new ArrayList<>(); return pipes; } diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Cistern.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Cistern.java index c7c6dda..148126c 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Cistern.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Cistern.java @@ -3,6 +3,8 @@ import Controll.Controller; import Fields.Pipe; import Players.Player; +import StringResource.StringResourceController; + import java.util.ArrayList; import java.util.Random; @@ -30,7 +32,7 @@ public Cistern() { */ @Override public void step() { - if (getPipes() != null) { + if (!getPipes().isEmpty()) { for (Pipe pipe : getPipes()) { super.setWater(super.getWater() + pipe.getWater()); } @@ -59,16 +61,6 @@ public Pump createNewPump(boolean b) { else return null; } - /** - * Method for getting the water from the field. - * Prints the amount of water taken. - * @return The amount of water in the field. - */ - @Override - public int getWater() { - return super.getWater(); - } - /** * Method for picking up a (new) pipe from the field. * @return The new pipe. @@ -92,35 +84,16 @@ public Pipe pickUpPipe() { @Override public String toString() { ArrayList players = this.getPlayers(); - String playersNames = "null"; - - for (int i = 0; i < players.size(); i++) { - if(i == 0) playersNames = ""; - playersNames += Controller.objectReverseNames.get(players.get(i)); - if (i != players.size() - 1) { - playersNames += ", "; - } - } - - + String playerBuilder = StringResourceController.stingBuilder(players); ArrayList pipes = this.getPipes(); - String pipesNames ="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 != pipes.size() - 1) { - pipesNames += ", "; - } - } - } + String pipeBuilder = StringResourceController.stingBuilder(pipes); return "name: "+ Controller.objectReverseNames.get(this) + "\noccupied: " + this.isOccupied() + "\nwater: " + getWaterNoChange() + "\nbroken: " + this.isBroken() - + "\nplayers: " + playersNames - + "\npipes: " + pipesNames + "\n"; + + "\nplayers: " + playerBuilder + + "\npipes: " + pipeBuilder + "\n"; } } diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Pump.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Pump.java index f629375..69e9327 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Pump.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/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,19 +129,17 @@ public String toString() { String playersNames = getPlayerNames(); - - 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())); } @@ -151,36 +150,18 @@ public String toString() { + "\nplayers: " + playersNames + "\npipes: " + pipesNames + "\ntank: " + this.getTank() - + "\nwaterFrom: " +SWaterFrom - + "\nwaterTo: " +SWaterTo + "\n"; + + "\nwaterFrom: " +sWaterFrom + + "\nwaterTo: " +sWaterTo + "\n"; } private String getPlayerNames() { ArrayList players = this.getPlayers(); - String playersNames = "null"; - for (int i = 0; i < players.size(); i++) { - if(i == 0) playersNames = ""; - playersNames += Controller.objectReverseNames.get(players.get(i)); - if (i != players.size() - 1) { - playersNames += ", "; - } - } - return playersNames; + return StringResourceController.stingBuilder(players); } private String getPipeNames(){ ArrayList pipes = this.getPipes(); - String pipesNames ="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 != pipes.size() - 1) { - pipesNames += ", "; - } - } - } - return pipesNames; + return StringResourceController.stingBuilder(pipes); } } diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Spring.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Spring.java index 44dd54c..b6e842f 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/ActiveFields/Spring.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/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,32 +61,18 @@ public void step() { @Override public String toString() { ArrayList players = this.getPlayers(); + String playerBuilder = StringResourceController.stingBuilder(players); - String playersNames = "null"; - - for (int i = 0; i < players.size(); i++) { - if(i == 0) playersNames = ""; - playersNames += Controller.objectReverseNames.get(players.get(i)); - if (i != players.size() - 1) { - playersNames += ", "; - } - } ArrayList 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 += ", "; - } - } + String pipeBuilder = StringResourceController.stingBuilder(pipes); + 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"; } diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Field.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Field.java index b1eec75..34921db 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Field.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Field.java @@ -4,6 +4,7 @@ import Fields.ActiveFields.Pump; import Interfaces.Steppable; import Players.Player; + import java.util.ArrayList; /** diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Pipe.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Pipe.java index eba8320..bd3bf9d 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Pipe.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Fields/Pipe.java @@ -5,7 +5,10 @@ import Fields.ActiveFields.ActiveFields; import Fields.ActiveFields.Pump; import Players.Player; +import StringResource.StringResourceController; + import java.util.ArrayList; +import java.util.List; import java.util.Random; /** @@ -37,7 +40,7 @@ public class Pipe extends Field { /** * The ends of the pipe. Default is empty. */ - private ArrayList fields = new ArrayList<>(); + private List fields = new ArrayList<>(); /** * Constructor for Pipe @@ -50,7 +53,7 @@ public Pipe(int capacity) { /** * Setter for capacity. Only for initialization. */ - public void setFields(ArrayList fields) { + public void setFields(List fields) { this.fields = fields; } /** @@ -84,7 +87,7 @@ public void setFluid(Fluid fluid) { /** * Getter for fields as ActiveFields. */ - public ArrayList getFields() { return fields; } + public List getFields() { return fields; } /** * Getter for fields as Field. */ @@ -146,7 +149,7 @@ public boolean repair() { @Override public Pipe placePump(Pump newPump) { if(newPump == null) { return null; } - ActiveFields oldPump = (ActiveFields) fields.remove(0); + ActiveFields oldPump = fields.remove(0); disconnect(oldPump); @@ -261,6 +264,7 @@ public Field accept(Player p) { * @param p The player to be removed. * @return True if the player was removed. */ + @Override public boolean removePlayer(Player p){ if(fluid == Fluid.STICKY){ if(leave){ @@ -279,6 +283,7 @@ public boolean removePlayer(Player p){ * Methods for making the pipe slippery. * @return True if the pipe successfully became slippery. */ + @Override public boolean makeSlippery(){ if(fluid == Fluid.STICKY) return false; if(remainingFluidTime == 0){ @@ -297,6 +302,7 @@ public boolean makeSlippery(){ * Methods for making the pipe sticky. * @return True if the pipe successfully became sticky. */ + @Override public boolean makeSticky(){ if(fluid == Fluid.SLIPPERY) return false; if(remainingFluidTime == 0){ @@ -316,6 +322,7 @@ public boolean makeSticky(){ * Reduces the amount of time left until the pipe becomes dry. * If the time is 0 makes the pipe dry. */ + @Override public void step(){ if(breakable > 0){ breakable--; @@ -335,33 +342,16 @@ public void step(){ @Override public String toString() { ArrayList players = this.getPlayers(); + String playerBuilder = StringResourceController.stingBuilder(players); - String playersNames = "null"; - - for (int i = 0; i < players.size(); i++) { - if(i == 0) playersNames = ""; - playersNames += Controller.objectReverseNames.get(players.get(i)); - if (i != players.size() - 1) { - playersNames += ", "; - } - } - - ArrayList 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 += ", "; - } - } - + List localFields = this.getFields(); + String fieldBuilder = StringResourceController.stingBuilder(localFields); 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() diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Players/Saboteur.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Players/Saboteur.java index 3458f84..960ae4f 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Players/Saboteur.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/Players/Saboteur.java @@ -19,6 +19,7 @@ public Saboteur() { * Method for making standingField slippery * @return boolean - returns true if action was successful. */ + @Override public boolean makeSlippery(){ return this.getStandingField().makeSlippery(); diff --git a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/StringResource/StringResourceController.java b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/StringResource/StringResourceController.java index b417d44..2e94262 100644 --- a/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/StringResource/StringResourceController.java +++ b/Sivatagi vizhalozatok uzemeltetese a gyakorlatban 2/src/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"; @@ -9,5 +13,19 @@ private StringResourceController(){} public static final String GOOD_ACTION = "Sikeres művelet"; 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