Refactor coach as an abstract class

This commit is contained in:
Arne Keller 2020-02-19 18:44:25 +01:00
parent 8811dcf228
commit 33138162fb
5 changed files with 156 additions and 88 deletions

View File

@ -1,6 +1,5 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.ui.CoachType;
import edu.kit.informatik.ui.InvalidInputException; import edu.kit.informatik.ui.InvalidInputException;
/** /**
@ -9,47 +8,7 @@ import edu.kit.informatik.ui.InvalidInputException;
* @author Arne Keller * @author Arne Keller
* @version 1.0 * @version 1.0
*/ */
public class Coach extends RollingStock { public abstract class Coach extends RollingStock {
/**
* ASCII art representation of a passenger coach.
*/
private static final String[] PASSENGER_TEXT = new String[] {
"____________________",
"| ___ ___ ___ ___ |",
"| |_| |_| |_| |_| |",
"|__________________|",
"|__________________|",
" (O) (O) "
};
/**
* ASCII art representation of a freight coach.
*/
private static final String[] FREIGHT_TEXT = new String[] {
"| |",
"| |",
"| |",
"|__________________|",
" (O) (O) "
};
/**
* ASCII art represantation of a special coach.
*/
private static final String[] SPECIAL_TEXT = new String[] {
" ____",
"/--------------| |",
"\\--------------| |",
" | | | |",
" _|_|__________| |",
"|_________________|",
" (O) (O) "
};
/**
* The type of this coach (passenger, freight or special). TODO: consider using three classes
*/
private final CoachType type;
/** /**
* The unique identifier of this coach. * The unique identifier of this coach.
*/ */
@ -58,17 +17,15 @@ public class Coach extends RollingStock {
/** /**
* Construct a new coach. * Construct a new coach.
* @param identifier identifier to use * @param identifier identifier to use
* @param type type of coach
* @param length length of coach * @param length length of coach
* @param couplingFront whether the coach should have a front coupling * @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling * @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach) * @throws InvalidInputException on invalid user input (zero-sized coach)
*/ */
public Coach(final int identifier, final CoachType type, final int length, public Coach(final int identifier, final int length,
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException { final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack); super(length, couplingFront, couplingBack);
this.identifier = identifier; this.identifier = identifier;
this.type = type;
} }
@Override @Override
@ -89,37 +46,7 @@ public class Coach extends RollingStock {
} }
/** /**
* @return the type of this coach (passenger, freight or special) * @return abbreviation of the type of this coach
*/ */
public CoachType getType() { public abstract String getType();
return type;
}
@Override
public String[] textRepresentation() {
switch (type) {
case PASSENGER:
return PASSENGER_TEXT;
case FREIGHT:
return FREIGHT_TEXT;
case SPECIAL:
return SPECIAL_TEXT;
default:
return null;
}
}
@Override
public String description() {
switch (type) {
case PASSENGER:
return "passenger coach";
case FREIGHT:
return "freight coach";
case SPECIAL:
return "special coach";
default:
return null;
}
}
} }

View File

@ -0,0 +1,43 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
public class FreightCoach extends Coach {
/**
* ASCII art representation of a freight coach.
*/
private static final String[] FREIGHT_TEXT = new String[] {
"| |",
"| |",
"| |",
"|__________________|",
" (O) (O) "
};
/**
* Construct a new freight coach.
* @param identifier identifier to use
* @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 (zero-sized coach)
*/
public FreightCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException {
super(identifier, length, couplingFront, couplingBack);
}
@Override
public String description() {
return "freight coach";
}
@Override
public String[] textRepresentation() {
return FREIGHT_TEXT;
}
@Override
public String getType() {
return "f";
}
}

View File

@ -36,7 +36,7 @@ public class ModelRailwaySimulation {
/** /**
* List of coaches used in the simulation. * List of coaches used in the simulation.
*/ */
private final List<Coach> coaches = new ArrayList<>(); private final Map<Integer, Coach> coaches = new HashMap<>();
/** /**
* List of trains simulated. * List of trains simulated.
*/ */
@ -156,8 +156,17 @@ public class ModelRailwaySimulation {
if (id < 0) { if (id < 0) {
return -1; return -1;
} }
Coach coach = new Coach(id, coachType, length, couplingFront, couplingBack); switch (coachType) {
coaches.add(id - 1, coach); case PASSENGER:
coaches.put(id, new PassengerCoach(id, length, couplingFront, couplingBack));
break;
case FREIGHT:
coaches.put(id, new FreightCoach(id, length, couplingFront, couplingBack));
break;
case SPECIAL:
coaches.put(id, new SpecialCoach(id, length, couplingFront, couplingBack));
break;
}
return id; return id;
} }
@ -166,14 +175,13 @@ public class ModelRailwaySimulation {
* @return the next coach identifier, or -1 if none available * @return the next coach identifier, or -1 if none available
*/ */
private int getNextCoachIdentifier() { private int getNextCoachIdentifier() {
int id = 1; for (int id = 1; id > 0; id++) {
for (Coach coach : coaches) { if (!coaches.containsKey(id)) {
if (coach.getNumericalIdentifier() == id) {
id++;
}
}
return id; return id;
} }
}
return -1;
}
/** /**
* Print a list of coaches added to the simulation. * Print a list of coaches added to the simulation.
@ -182,7 +190,8 @@ public class ModelRailwaySimulation {
if (coaches.isEmpty()) { if (coaches.isEmpty()) {
Terminal.printLine("No coach exists"); Terminal.printLine("No coach exists");
} else { } else {
coach: for (Coach coach : coaches) { coach: for (Integer identifier : coaches.keySet().stream().sorted().collect(Collectors.toList())) {
Coach coach = coaches.get(identifier);
for (Train train : trains.values()) { for (Train train : trains.values()) {
if (train.contains(coach)) { if (train.contains(coach)) {
Terminal.printLine(String.format("%d %s %s %d %b %b", Terminal.printLine(String.format("%d %s %s %d %b %b",
@ -250,7 +259,7 @@ public class ModelRailwaySimulation {
} }
engines.remove(rollingStock); engines.remove(rollingStock);
trainSets.remove(rollingStock); trainSets.remove(rollingStock);
coaches.remove(rollingStock); coaches.values().remove(rollingStock);
return true; return true;
} }
@ -262,7 +271,7 @@ public class ModelRailwaySimulation {
public RollingStock getRollingStock(final String id) { public RollingStock getRollingStock(final String id) {
if (id.matches("W\\+?\\d+")) { if (id.matches("W\\+?\\d+")) {
int coachId = Integer.parseInt(id.substring(1)); int coachId = Integer.parseInt(id.substring(1));
for (Coach coach : coaches) { for (Coach coach : coaches.values()) {
if (coach.getNumericalIdentifier() == coachId) { if (coach.getNumericalIdentifier() == coachId) {
return coach; return coach;
} }

View File

@ -0,0 +1,44 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
public class PassengerCoach extends Coach {
/**
* ASCII art representation of a passenger coach.
*/
private static final String[] PASSENGER_TEXT = new String[] {
"____________________",
"| ___ ___ ___ ___ |",
"| |_| |_| |_| |_| |",
"|__________________|",
"|__________________|",
" (O) (O) "
};
/**
* Construct a new passenger coach.
* @param identifier identifier to use
* @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 (zero-sized coach)
*/
public PassengerCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException {
super(identifier, length, couplingFront, couplingBack);
}
@Override
public String description() {
return "passenger coach";
}
@Override
public String[] textRepresentation() {
return PASSENGER_TEXT;
}
@Override
public String getType() {
return "p";
}
}

View File

@ -0,0 +1,45 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
public class SpecialCoach extends Coach {
/**
* ASCII art represantation of a special coach.
*/
private static final String[] SPECIAL_TEXT = new String[] {
" ____",
"/--------------| |",
"\\--------------| |",
" | | | |",
" _|_|__________| |",
"|_________________|",
" (O) (O) "
};
/**
* Construct a new special coach.
* @param identifier identifier to use
* @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 (zero-sized coach)
*/
public SpecialCoach(int identifier, int length, boolean couplingFront, boolean couplingBack) throws InvalidInputException {
super(identifier, length, couplingFront, couplingBack);
}
@Override
public String description() {
return "special coach";
}
@Override
public String[] textRepresentation() {
return SPECIAL_TEXT;
}
@Override
public String getType() {
return "s";
}
}