diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/Coach.java b/src/edu/kit/informatik/modelrailwaysimulator/model/Coach.java index 7b37ce9..ae91358 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/Coach.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/Coach.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.regex.Pattern; import static edu.kit.informatik.modelrailwaysimulator.ui.command.CommandFactory.NUMBER; @@ -34,12 +32,12 @@ public abstract class Coach extends RollingStock { * @param length length of coach * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized coach) + * @throws LogicException on invalid user input (e.g. zero-sized coach) */ - public Coach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException { + public Coach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws LogicException { super(length, couplingFront, couplingBack); if (identifier < 1) { - throw new InvalidInputException("coach id has to be positive!"); + throw new LogicException("coach id has to be positive!"); } this.identifier = identifier; } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/DieselEngine.java b/src/edu/kit/informatik/modelrailwaysimulator/model/DieselEngine.java index a154951..5a2b1ed 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/DieselEngine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/DieselEngine.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * Diesel engine. * @@ -29,10 +27,10 @@ public class DieselEngine extends Engine { * @param length length of engine * @param couplingFront whether the engine should have a front coupling * @param couplingBack whether the engine should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized engine) + * @throws LogicException on invalid user input (e.g. zero-sized engine) */ public DieselEngine(String series, String name, int length, - boolean couplingFront, boolean couplingBack) throws InvalidInputException { + boolean couplingFront, boolean couplingBack) throws LogicException { super(series, name, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/ElectricalEngine.java b/src/edu/kit/informatik/modelrailwaysimulator/model/ElectricalEngine.java index b672096..2010389 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/ElectricalEngine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/ElectricalEngine.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * Electrical engine. * @@ -31,10 +29,10 @@ public class ElectricalEngine extends Engine { * @param length length of engine * @param couplingFront whether the engine should have a front coupling * @param couplingBack whether the engine should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized engine) + * @throws LogicException on invalid user input (e.g. zero-sized engine) */ public ElectricalEngine(String series, String name, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(series, name, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/Engine.java b/src/edu/kit/informatik/modelrailwaysimulator/model/Engine.java index 63d058f..f98d653 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/Engine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/Engine.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * Generic engine, is usually either diesel, steam or electric. * @@ -26,10 +24,10 @@ public abstract class Engine extends RollingStock { * @param length length of this engine * @param couplingFront whether this engine should have a front coupling * @param couplingBack whether this engine should have a back coupling - * @throws InvalidInputException on invalid user input (zero-sized coach) + * @throws LogicException on invalid user input (zero-sized coach) */ protected Engine(String series, String name, int length, - boolean couplingFront, boolean couplingBack) throws InvalidInputException { + boolean couplingFront, boolean couplingBack) throws LogicException { super(length, couplingFront, couplingBack); this.series = series; this.name = name; diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/FreightCoach.java b/src/edu/kit/informatik/modelrailwaysimulator/model/FreightCoach.java index 1288e43..07678b3 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/FreightCoach.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/FreightCoach.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * A freight coach. * @@ -27,10 +25,10 @@ public class FreightCoach extends Coach { * @param length length of coach * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized coach) + * @throws LogicException on invalid user input (e.g. zero-sized coach) */ public FreightCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(identifier, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/LogicException.java b/src/edu/kit/informatik/modelrailwaysimulator/model/LogicException.java new file mode 100644 index 0000000..ad8763c --- /dev/null +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/LogicException.java @@ -0,0 +1,18 @@ +package edu.kit.informatik.modelrailwaysimulator.model; + +/** + * Thrown on illogical input or impossible to fulfill input. + * + * @author Arne Keller + * @version 1.0 + */ +public class LogicException extends Exception { + /** + * Construct a new logic exception with a specific message. + * + * @param message the error message + */ + public LogicException(String message) { + super(message); + } +} diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/modelrailwaysimulator/model/ModelRailwaySimulation.java index 7b9e27c..887cf66 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/ModelRailwaySimulation.java @@ -1,7 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.model; import edu.kit.informatik.modelrailwaysimulator.ui.CoachType; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; import java.util.List; import java.util.Map; @@ -55,9 +54,9 @@ public class ModelRailwaySimulation { * @param start start position of the new track * @param end end position of the new track * @return the positive identifier of the new track if successful, -1 if none available - * @throws InvalidInputException if user input is invalid (e.g. zero length track) + * @throws LogicException if user input is invalid (e.g. zero length track) */ - public int addTrack(Vector2D start, Vector2D end) throws InvalidInputException { + public int addTrack(Vector2D start, Vector2D end) throws LogicException { return railNetwork.addTrack(start, end); } @@ -68,9 +67,9 @@ public class ModelRailwaySimulation { * @param end1 end position 1 of the switch * @param end2 end position 2 of the switch * @return the positive identifier of the switch if successful, -1 otherwise - * @throws InvalidInputException if the new switch would be invalid + * @throws LogicException if the new switch would be invalid */ - public int addSwitch(Vector2D start, Vector2D end1, Vector2D end2) throws InvalidInputException { + public int addSwitch(Vector2D start, Vector2D end1, Vector2D end2) throws LogicException { return railNetwork.addSwitch(start, end1, end2); } @@ -115,12 +114,12 @@ public class ModelRailwaySimulation { * Add a new engine to the simulation. * * @param newEngine the engine to add to the simulation - * @throws InvalidInputException when the identifier is already in use + * @throws LogicException when the identifier is already in use */ - public void createEngine(Engine newEngine) throws InvalidInputException { + public void createEngine(Engine newEngine) throws LogicException { final String id = newEngine.getIdentifier(); if (engines.containsKey(id) || trainSets.containsKey(id)) { - throw new InvalidInputException("engine identifier already used"); + throw new LogicException("engine identifier already used"); } engines.put(id, newEngine); } @@ -147,10 +146,10 @@ public class ModelRailwaySimulation { * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling * @return the identifier of the coach if successfully added, -1 if none available - * @throws InvalidInputException if user input is invalid (e.g. zero-sized coach) + * @throws LogicException if user input is invalid (e.g. zero-sized coach) */ public int createCoach(CoachType coachType, int length, - boolean couplingFront, boolean couplingBack) throws InvalidInputException { + boolean couplingFront, boolean couplingBack) throws LogicException { final int id = getNextCoachIdentifier(); if (id < 0) { return -1; @@ -189,12 +188,12 @@ public class ModelRailwaySimulation { * Add a new train set to the simulation. * * @param newTrainSet the train set to add - * @throws InvalidInputException if the identifier is already used + * @throws LogicException if the identifier is already used */ - public void createTrainSet(TrainSet newTrainSet) throws InvalidInputException { + public void createTrainSet(TrainSet newTrainSet) throws LogicException { final String id = newTrainSet.getIdentifier(); if (engines.containsKey(id) || trainSets.containsKey(id)) { - throw new InvalidInputException("train set identifier already used"); + throw new LogicException("train set identifier already used"); } trainSets.put(id, newTrainSet); } @@ -258,13 +257,13 @@ public class ModelRailwaySimulation { * @param trainId identifier of the train * @param rollingStockId identifier of the rolling stock * @return the added rolling stock - * @throws InvalidInputException if train not found and train identifier is not next identifier or rolling stock + * @throws LogicException if train not found and train identifier is not next identifier or rolling stock * is not found */ - public RollingStock addTrain(int trainId, String rollingStockId) throws InvalidInputException { + public RollingStock addTrain(int trainId, String rollingStockId) throws LogicException { final Optional rollingStock = getRollingStock(rollingStockId); if (!rollingStock.isPresent()) { - throw new InvalidInputException("rolling stock not found"); + throw new LogicException("rolling stock not found"); } trainManager.addTrain(trainId, rollingStock.get()); return rollingStock.get(); @@ -304,9 +303,9 @@ public class ModelRailwaySimulation { * * @param id identifier of the train to show * @return rows of ASCII art representing this train - * @throws InvalidInputException if train not found + * @throws LogicException if train not found */ - public List showTrain(int id) throws InvalidInputException { + public List showTrain(int id) throws LogicException { return trainManager.showTrain(id); } @@ -317,10 +316,10 @@ public class ModelRailwaySimulation { * @param position where to place the train * @param direction direction in which the train should initially go * @return whether the train was successfully placed - * @throws InvalidInputException if train could not be found, is already placed, is not valid, direction is invalid + * @throws LogicException if train could not be found, is already placed, is not valid, direction is invalid * or railway network is not ready for trains */ - public boolean putTrain(int trainId, Vector2D position, Vector2D direction) throws InvalidInputException { + public boolean putTrain(int trainId, Vector2D position, Vector2D direction) throws LogicException { return trainManager.putTrain(trainId, position, direction); } @@ -329,9 +328,9 @@ public class ModelRailwaySimulation { * * @param speed amount of steps to move the trains * @return train collisions, no value if no trains are placed - * @throws InvalidInputException if the simulation is not yet ready + * @throws LogicException if the simulation is not yet ready */ - public Optional>> step(short speed) throws InvalidInputException { + public Optional>> step(short speed) throws LogicException { return trainManager.step(speed); } } \ No newline at end of file diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/PassengerCoach.java b/src/edu/kit/informatik/modelrailwaysimulator/model/PassengerCoach.java index c64aceb..37428f2 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/PassengerCoach.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/PassengerCoach.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * A passenger coach. * @@ -28,10 +26,10 @@ public class PassengerCoach extends Coach { * @param length length of coach * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized coach) + * @throws LogicException on invalid user input (e.g. zero-sized coach) */ public PassengerCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(identifier, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/RailwayNetwork.java b/src/edu/kit/informatik/modelrailwaysimulator/model/RailwayNetwork.java index 96d6ca5..cf45eed 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/RailwayNetwork.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -31,13 +29,13 @@ public class RailwayNetwork { * @param start start position of the new track * @param end end position of the new track * @return the positive identifier of the new track if successful, -1 if none available - * @throws InvalidInputException if the user provides incorrect input + * @throws LogicException if the user provides incorrect input */ - public int addTrack(Vector2D start, Vector2D end) throws InvalidInputException { + public int addTrack(Vector2D start, Vector2D end) throws LogicException { final long startPossibleConnections = rails.values().stream().filter(rail -> rail.canConnectTo(start)).count(); final long endPossibleConnections = rails.values().stream().filter(rail -> rail.canConnectTo(end)).count(); if (startPossibleConnections == 2 || endPossibleConnections == 2) { - throw new InvalidInputException("track would connect to two other rails"); + throw new LogicException("track would connect to two other rails"); } if (rails.isEmpty()) { final Track newTrack = new Track(start, end, 1); @@ -53,7 +51,7 @@ public class RailwayNetwork { rails.put(id, newTrack); return newTrack.getIdentifier(); } else { - throw new InvalidInputException("track is not connected to other tracks"); + throw new LogicException("track is not connected to other tracks"); } } } @@ -65,9 +63,9 @@ public class RailwayNetwork { * @param end1 end position 1 of the switch * @param end2 end position 2 of the switch * @return the positive identifier of the switch if successful, -1 otherwise - * @throws InvalidInputException when the new switch would be invalid + * @throws LogicException when the new switch would be invalid */ - public int addSwitch(Vector2D start, Vector2D end1, Vector2D end2) throws InvalidInputException { + public int addSwitch(Vector2D start, Vector2D end1, Vector2D end2) throws LogicException { if (rails.isEmpty()) { final Switch newSwitch = new Switch(start, end1, end2, 1); rails.put(1, newSwitch); @@ -77,7 +75,7 @@ public class RailwayNetwork { final long end1Connections = rails.values().stream().filter(rail -> rail.canConnectTo(end1)).count(); final long end2Connections = rails.values().stream().filter(rail -> rail.canConnectTo(end2)).count(); if (startConnections == 2 || end1Connections == 2 || end2Connections == 2) { - throw new InvalidInputException("switch endpoint would connect to two other rails"); + throw new LogicException("switch endpoint would connect to two other rails"); } if (rails.values().stream() .anyMatch(rail -> rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2))) { @@ -89,7 +87,7 @@ public class RailwayNetwork { rails.put(id, newSwitch); return newSwitch.getIdentifier(); } else { - throw new InvalidInputException("switch not connected to other rails"); + throw new LogicException("switch not connected to other rails"); } } } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/RollingStock.java b/src/edu/kit/informatik/modelrailwaysimulator/model/RollingStock.java index 0a459c8..7b7871e 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/RollingStock.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/RollingStock.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * A rolling stock with a specific integer length and couplings. Is usually an engine, train set or coach. * @@ -28,14 +26,14 @@ public abstract class RollingStock { * @param length length of this rolling stock * @param couplingFront whether this rolling stock should have a front coupling * @param couplingBack whether this rolling stock should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized coach) + * @throws LogicException on invalid user input (e.g. zero-sized coach) */ - protected RollingStock(int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException { + protected RollingStock(int length, boolean couplingFront, boolean couplingBack) throws LogicException { if (length < 1) { - throw new InvalidInputException("rolling stock length has to be positive"); + throw new LogicException("rolling stock length has to be positive"); } if (!couplingFront && !couplingBack) { - throw new InvalidInputException("rolling stocks needs at least one coupling"); + throw new LogicException("rolling stocks needs at least one coupling"); } this.length = length; this.couplingFront = couplingFront; diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/SpecialCoach.java b/src/edu/kit/informatik/modelrailwaysimulator/model/SpecialCoach.java index 615e4cd..a43b75e 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/SpecialCoach.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/SpecialCoach.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * A special coach, used for e.g. firefighting. * @@ -29,10 +27,10 @@ public class SpecialCoach extends Coach { * @param length length of coach * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized coach) + * @throws LogicException on invalid user input (e.g. zero-sized coach) */ public SpecialCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(identifier, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/SteamEngine.java b/src/edu/kit/informatik/modelrailwaysimulator/model/SteamEngine.java index 634758d..ddb2376 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/SteamEngine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/SteamEngine.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * Steam engine. * @@ -29,10 +27,10 @@ public class SteamEngine extends Engine { * @param length length of engine * @param couplingFront whether the engine should have a front coupling * @param couplingBack whether the engine should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized engine) + * @throws LogicException on invalid user input (e.g. zero-sized engine) */ public SteamEngine(String series, String name, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(series, name, length, couplingFront, couplingBack); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/Switch.java b/src/edu/kit/informatik/modelrailwaysimulator/model/Switch.java index 83d1c45..2a84432 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/Switch.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/Switch.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.Objects; /** @@ -31,12 +29,12 @@ public final class Switch extends Rail { * @param end1 first end position * @param end2 second end position * @param id identifier of this switch - * @throws InvalidInputException if the switch is not composed of straight lines, or has zero-length parts + * @throws LogicException if the switch is not composed of straight lines, or has zero-length parts */ - public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws InvalidInputException { + public Switch(Vector2D start, Vector2D end1, Vector2D end2, int id) throws LogicException { super(id); if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) { - throw new InvalidInputException("switch has length zero"); + throw new LogicException("switch has length zero"); } this.positionOne = new Track(start, end1, 1); this.positionTwo = new Track(start, end2, 1); diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/Track.java b/src/edu/kit/informatik/modelrailwaysimulator/model/Track.java index 18ea5a4..6be8ee5 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/Track.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/Track.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.Objects; /** @@ -26,14 +24,14 @@ public final class Track extends Rail { * @param start start position of the track * @param end end position of the track * @param id identifier to use - * @throws InvalidInputException if the positions are not on a straight line + * @throws LogicException if the positions are not on a straight line */ - public Track(Vector2D start, Vector2D end, int id) throws InvalidInputException { + public Track(Vector2D start, Vector2D end, int id) throws LogicException { super(id); if (start.distanceTo(end) == 0) { - throw new InvalidInputException("track has length zero"); + throw new LogicException("track has length zero"); } else if (start.getX() != end.getX() && start.getY() != end.getY()) { - throw new InvalidInputException("invalid track segment: not a straight line"); + throw new LogicException("invalid track segment: not a straight line"); } this.start = start; this.end = end; diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/Train.java b/src/edu/kit/informatik/modelrailwaysimulator/model/Train.java index bc2023f..cbca23f 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/Train.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/Train.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -76,16 +74,16 @@ public final class Train implements Comparable { * Add a rolling stock to this train. * * @param rollingStock the rolling stack to add - * @throws InvalidInputException if the rolling stock could not be added to the train + * @throws LogicException if the rolling stock could not be added to the train */ - public void add(RollingStock rollingStock) throws InvalidInputException { + public void add(RollingStock rollingStock) throws LogicException { final RollingStock last = rollingStocks.get(rollingStocks.size() - 1); // make sure special coupling requirements are honored if (rollingStock.hasCouplingFront() && last.hasCouplingBack() && rollingStock.canCoupleTo(last) && last.canCoupleTo(rollingStock)) { rollingStocks.add(rollingStock); } else { - throw new InvalidInputException("could not add rolling stock to train"); + throw new LogicException("could not add rolling stock to train"); } } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/TrainManager.java b/src/edu/kit/informatik/modelrailwaysimulator/model/TrainManager.java index 2d0232c..74cacad 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/TrainManager.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/TrainManager.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -77,23 +75,23 @@ public final class TrainManager { * * @param trainId train identifier * @param rollingStock rolling stock to add - * @throws InvalidInputException on invalid user input (e.g. rolling stock in use) + * @throws LogicException on invalid user input (e.g. rolling stock in use) */ - public void addTrain(int trainId, RollingStock rollingStock) throws InvalidInputException { + public void addTrain(int trainId, RollingStock rollingStock) throws LogicException { if (getTrainContainingRollingStock(rollingStock).isPresent()) { - throw new InvalidInputException("rolling stock already used"); + throw new LogicException("rolling stock already used"); } final Train train = trains.get(trainId); if (train == null) { // create new train final int correctId = getNextTrainIdentifier(); if (trainId != correctId) { - throw new InvalidInputException("new train identifier must be next free identifier"); + throw new LogicException("new train identifier must be next free identifier"); } final Train newTrain = new Train(trainId, rollingStock); trains.put(trainId, newTrain); } else { if (train.isPlaced()) { - throw new InvalidInputException("can not add rolling stock to placed train"); + throw new LogicException("can not add rolling stock to placed train"); } train.add(rollingStock); } @@ -139,14 +137,14 @@ public final class TrainManager { * * @param trainId identifier of the train to show * @return ASCII art representation of said train - * @throws InvalidInputException if train not found + * @throws LogicException if train not found */ - public List showTrain(int trainId) throws InvalidInputException { + public List showTrain(int trainId) throws LogicException { final Train train = trains.get(trainId); if (train != null) { return train.show(); } else { - throw new InvalidInputException("no such train"); + throw new LogicException("no such train"); } } @@ -158,21 +156,21 @@ public final class TrainManager { * @param position where to place the train * @param direction direction in which the train should initially go * @return whether the train was successfully placed - * @throws InvalidInputException if train could not be found, is already placed, is not valid, direction is invalid + * @throws LogicException if train could not be found, is already placed, is not valid, direction is invalid * or railway network is not ready for trains */ - public boolean putTrain(int trainId, Vector2D position, Vector2D direction) throws InvalidInputException { + public boolean putTrain(int trainId, Vector2D position, Vector2D direction) throws LogicException { final Train train = trains.get(trainId); if (train == null) { - throw new InvalidInputException("train not found"); + throw new LogicException("train not found"); } else if (!train.isProperTrain()) { - throw new InvalidInputException("train is not valid"); + throw new LogicException("train is not valid"); } else if (train.isPlaced()) { - throw new InvalidInputException("train is already placed"); + throw new LogicException("train is already placed"); } else if (!direction.isDirection()) { - throw new InvalidInputException("invalid train direction"); + throw new LogicException("invalid train direction"); } else if (!railNetwork.isReadyForTrains()) { - throw new InvalidInputException("switches not set up"); + throw new LogicException("switches not set up"); } // attempt to place train final boolean placed = train.placeOn(railNetwork, position, direction); @@ -305,11 +303,11 @@ public final class TrainManager { * * @param speed amount of steps to move the trains * @return train collisions, no value if no trains are placed - * @throws InvalidInputException if simulation is not yet ready + * @throws LogicException if simulation is not yet ready */ - public Optional>> step(short speed) throws InvalidInputException { + public Optional>> step(short speed) throws LogicException { if (!railNetwork.isReadyForTrains()) { - throw new InvalidInputException("rail tracks/switches not set up"); + throw new LogicException("rail tracks/switches not set up"); } if (trains.values().stream().noneMatch(Train::isPlaced)) { return Optional.empty(); diff --git a/src/edu/kit/informatik/modelrailwaysimulator/model/TrainSet.java b/src/edu/kit/informatik/modelrailwaysimulator/model/TrainSet.java index c60634b..ccb2673 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/model/TrainSet.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/model/TrainSet.java @@ -1,7 +1,5 @@ package edu.kit.informatik.modelrailwaysimulator.model; -import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; - /** * Train set. * @@ -40,10 +38,10 @@ public class TrainSet extends RollingStock { * @param length length of train set * @param couplingFront whether the train set should have a front coupling * @param couplingBack whether the train set should have a back coupling - * @throws InvalidInputException on invalid user input (e.g. zero-sized train set) + * @throws LogicException on invalid user input (e.g. zero-sized train set) */ public TrainSet(String series, String name, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { super(length, couplingFront, couplingBack); this.name = name; this.series = series; diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/CoachType.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/CoachType.java index ca4bcd6..ee27442 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/CoachType.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/CoachType.java @@ -2,6 +2,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui; import edu.kit.informatik.modelrailwaysimulator.model.Coach; import edu.kit.informatik.modelrailwaysimulator.model.FreightCoach; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.PassengerCoach; import edu.kit.informatik.modelrailwaysimulator.model.SpecialCoach; @@ -38,10 +39,10 @@ public enum CoachType { * @param couplingFront whether the coach should have a front coupling * @param couplingBack whether the coach should have a back coupling * @return new coach - * @throws InvalidInputException on invalid input (e.g. zero-sized coach) + * @throws LogicException on invalid input (e.g. zero-sized coach) */ public Coach createCoach(int id, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { switch (this) { case PASSENGER: return new PassengerCoach(id, length, couplingFront, couplingBack); diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/CommandLine.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/CommandLine.java index 29863ad..ee4823b 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/CommandLine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/CommandLine.java @@ -1,7 +1,9 @@ package edu.kit.informatik.modelrailwaysimulator.ui; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.Terminal; +import edu.kit.informatik.modelrailwaysimulator.ui.command.Command; import edu.kit.informatik.modelrailwaysimulator.ui.command.CommandFactory; /** @@ -46,10 +48,17 @@ public final class CommandLine { } } + final Command command; try { - CommandFactory.getCommand(input).apply(simulation); + command = CommandFactory.getCommand(input); } catch (final NumberFormatException | InvalidInputException e) { - Terminal.printError(e.getMessage()); + Terminal.printError("input error: " + e.getMessage()); + continue; + } + try { + command.apply(simulation); + } catch (final NumberFormatException | LogicException e) { + Terminal.printError("logic error: " + e.getMessage()); } } } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/EngineType.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/EngineType.java index b6027dc..dba29e4 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/EngineType.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/EngineType.java @@ -3,6 +3,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui; import edu.kit.informatik.modelrailwaysimulator.model.DieselEngine; import edu.kit.informatik.modelrailwaysimulator.model.ElectricalEngine; import edu.kit.informatik.modelrailwaysimulator.model.Engine; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.SteamEngine; /** @@ -39,10 +40,10 @@ public enum EngineType { * @param couplingFront whether the engine should have a front coupling * @param couplingBack whether the engine should have a back coupling * @return new engine - * @throws InvalidInputException on invalid input (e.g. zero-sized engine) + * @throws LogicException on invalid input (e.g. zero-sized engine) */ public Engine createEngine(String series, String name, int length, boolean couplingFront, boolean couplingBack) - throws InvalidInputException { + throws LogicException { switch (this) { case ELECTRICAL: return new ElectricalEngine(series, name, length, couplingFront, couplingBack); diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddSwitch.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddSwitch.java index 2149b9d..fa84862 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddSwitch.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddSwitch.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.model.Vector2D; import edu.kit.informatik.Terminal; @@ -38,7 +39,7 @@ public class AddSwitch extends Command { private Vector2D end2; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (start == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrack.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrack.java index 03002df..13d18ba 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrack.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrack.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.model.Vector2D; import edu.kit.informatik.Terminal; @@ -34,7 +35,7 @@ public class AddTrack extends Command { private Vector2D end; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (start == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrain.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrain.java index 344a5b0..a2c8a5f 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrain.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/AddTrain.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.model.RollingStock; import edu.kit.informatik.Terminal; @@ -35,7 +36,7 @@ public class AddTrain extends Command { private String rollingStockId; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (rollingStockId == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Command.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Command.java index 126d26a..9b5753c 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Command.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Command.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; @@ -15,9 +16,9 @@ public abstract class Command { * Apply this command to a model railway simulation. * * @param simulation simulation to use - * @throws InvalidInputException on invalid user input + * @throws LogicException on illogical user input */ - public abstract void apply(ModelRailwaySimulation simulation) throws InvalidInputException; + public abstract void apply(ModelRailwaySimulation simulation) throws LogicException; /** * Parse user input into this command object. diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateCoach.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateCoach.java index 51a82a0..d8fb070 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateCoach.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateCoach.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.ui.CoachType; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.Terminal; @@ -44,7 +45,7 @@ public class CreateCoach extends Command { private boolean couplingBack; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (type == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateEngine.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateEngine.java index 45ac8da..4922659 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateEngine.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateEngine.java @@ -3,6 +3,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; import edu.kit.informatik.Terminal; import edu.kit.informatik.modelrailwaysimulator.model.Coach; import edu.kit.informatik.modelrailwaysimulator.model.Engine; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.ui.EngineType; import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; @@ -56,7 +57,7 @@ public class CreateEngine extends Command { private boolean couplingBack; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (type == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateTrainSet.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateTrainSet.java index 503bc39..12363ce 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateTrainSet.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/CreateTrainSet.java @@ -1,6 +1,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; import edu.kit.informatik.modelrailwaysimulator.model.Coach; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.Terminal; import edu.kit.informatik.modelrailwaysimulator.model.TrainSet; @@ -50,7 +51,7 @@ public class CreateTrainSet extends Command { private boolean couplingBack; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (name == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/PutTrain.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/PutTrain.java index fd52a90..9482b54 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/PutTrain.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/PutTrain.java @@ -1,5 +1,6 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.model.Vector2D; import edu.kit.informatik.Terminal; @@ -40,7 +41,7 @@ public class PutTrain extends Command { private Vector2D direction; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { if (point == null) { throw new IllegalStateException("command not initialized"); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/ShowTrain.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/ShowTrain.java index 32f4561..970a31f 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/ShowTrain.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/ShowTrain.java @@ -1,6 +1,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; import edu.kit.informatik.Terminal; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.ui.InvalidInputException; @@ -24,7 +25,7 @@ public class ShowTrain extends Command { private int id; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { simulation.showTrain(id).forEach(Terminal::printLine); } diff --git a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Step.java b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Step.java index 40a45bb..5f84af0 100644 --- a/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Step.java +++ b/src/edu/kit/informatik/modelrailwaysimulator/ui/command/Step.java @@ -1,6 +1,7 @@ package edu.kit.informatik.modelrailwaysimulator.ui.command; import edu.kit.informatik.Terminal; +import edu.kit.informatik.modelrailwaysimulator.model.LogicException; import edu.kit.informatik.modelrailwaysimulator.model.ModelRailwaySimulation; import edu.kit.informatik.modelrailwaysimulator.model.Vector2D; import edu.kit.informatik.modelrailwaysimulator.ui.CommandLine; @@ -30,7 +31,7 @@ public class Step extends Command { private short speed; @Override - public void apply(ModelRailwaySimulation simulation) throws InvalidInputException { + public void apply(ModelRailwaySimulation simulation) throws LogicException { final Optional>> collisions = simulation.step(speed); if (!collisions.isPresent()) { // print OK if no there are trains placed