mirror of
https://gitlab.com/arnekeller/kit-programmieren-ws1920-final1.git
synced 2024-11-24 01:15:05 +00:00
Refactor coach as an abstract class
This commit is contained in:
parent
8811dcf228
commit
33138162fb
@ -1,6 +1,5 @@
|
||||
package edu.kit.informatik.model;
|
||||
|
||||
import edu.kit.informatik.ui.CoachType;
|
||||
import edu.kit.informatik.ui.InvalidInputException;
|
||||
|
||||
/**
|
||||
@ -9,47 +8,7 @@ import edu.kit.informatik.ui.InvalidInputException;
|
||||
* @author Arne Keller
|
||||
* @version 1.0
|
||||
*/
|
||||
public 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;
|
||||
public abstract class Coach extends RollingStock {
|
||||
/**
|
||||
* The unique identifier of this coach.
|
||||
*/
|
||||
@ -58,17 +17,15 @@ public class Coach extends RollingStock {
|
||||
/**
|
||||
* Construct a new coach.
|
||||
* @param identifier identifier to use
|
||||
* @param type type of 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 (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 {
|
||||
super(length, couplingFront, couplingBack);
|
||||
this.identifier = identifier;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
public abstract String getType();
|
||||
}
|
||||
|
43
src/edu/kit/informatik/model/FreightCoach.java
Normal file
43
src/edu/kit/informatik/model/FreightCoach.java
Normal 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";
|
||||
}
|
||||
}
|
@ -36,7 +36,7 @@ public class ModelRailwaySimulation {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@ -156,8 +156,17 @@ public class ModelRailwaySimulation {
|
||||
if (id < 0) {
|
||||
return -1;
|
||||
}
|
||||
Coach coach = new Coach(id, coachType, length, couplingFront, couplingBack);
|
||||
coaches.add(id - 1, coach);
|
||||
switch (coachType) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -166,13 +175,12 @@ public class ModelRailwaySimulation {
|
||||
* @return the next coach identifier, or -1 if none available
|
||||
*/
|
||||
private int getNextCoachIdentifier() {
|
||||
int id = 1;
|
||||
for (Coach coach : coaches) {
|
||||
if (coach.getNumericalIdentifier() == id) {
|
||||
id++;
|
||||
for (int id = 1; id > 0; id++) {
|
||||
if (!coaches.containsKey(id)) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
return id;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -182,7 +190,8 @@ public class ModelRailwaySimulation {
|
||||
if (coaches.isEmpty()) {
|
||||
Terminal.printLine("No coach exists");
|
||||
} 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()) {
|
||||
if (train.contains(coach)) {
|
||||
Terminal.printLine(String.format("%d %s %s %d %b %b",
|
||||
@ -250,7 +259,7 @@ public class ModelRailwaySimulation {
|
||||
}
|
||||
engines.remove(rollingStock);
|
||||
trainSets.remove(rollingStock);
|
||||
coaches.remove(rollingStock);
|
||||
coaches.values().remove(rollingStock);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -262,7 +271,7 @@ public class ModelRailwaySimulation {
|
||||
public RollingStock getRollingStock(final String id) {
|
||||
if (id.matches("W\\+?\\d+")) {
|
||||
int coachId = Integer.parseInt(id.substring(1));
|
||||
for (Coach coach : coaches) {
|
||||
for (Coach coach : coaches.values()) {
|
||||
if (coach.getNumericalIdentifier() == coachId) {
|
||||
return coach;
|
||||
}
|
||||
|
44
src/edu/kit/informatik/model/PassengerCoach.java
Normal file
44
src/edu/kit/informatik/model/PassengerCoach.java
Normal 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";
|
||||
}
|
||||
}
|
45
src/edu/kit/informatik/model/SpecialCoach.java
Normal file
45
src/edu/kit/informatik/model/SpecialCoach.java
Normal 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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user