Railway station simulation
Read the program information; fill in the blanks and draw class diagrams for
the program.
[Program Description]
This program simulates the processing by an automated ticket gate installed at each
station of a railroad line comprising four stations A, B, C, and D, with A serving as
the starting point and D as the terminal point. On this line, the following two types of
fare certificates can be used: one-way ticket (hereinafter, “ticket”); and prepaid card
(hereinafter, “card”).
A fare on the line is decided based on the distance between stations. The fare for
up to 4 kilometers is 120 yen (this fare is called the base fare). An amount of 30 yen
is added for each additional 2 kilometers. Any additional distance less than 2
kilometers is rounded up to 2 kilometers. For example, if the distance is 7 kilometers,
the fare is 180 yen.
The station of embarkation is recorded on a ticket when a passenger passes through
the automated ticket gate in that station to enter the platform area. When he/she
passes through the automated ticket gate to leave the platform area in the station
of disembarkation, the fare is calculated, and if the amount paid for the ticket is
insufficient, the gate is closed to prevent him/her from leaving the platform area.
Once a ticket is used, it becomes invalid. On this line, a passenger can enter the
platform area through the automated ticket gate in any station, thus being able to
embark, regardless of the station that issued the ticket. For instance, with a ticket
issued at Station A, a passenger can enter the platform area through the automated
ticket gate in Station B.
The station of embarkation is recorded on a card when a passenger enters the
platform area through the automated ticket gate in that station. At this time, if the
balance on the card is zero, the gate is closed to prevent him/her from entering the
platform area. When he/she leaves the platform area through the automated ticket
gate in the station of disembarkation, a fare adjustment is processed. Namely, the
fare is calculated and is subtracted from the balance on the card. At this time, if this
balance is less than the amount of the fare, the gate is closed to prevent him/her
from leaving the platform area.
1
Class Line represents the railroad line. Method getFare calculates the fare on
the basis of a given distance, and returns this information.
Class Gate signifies an automated ticket gate. Fields A, B, C and D in class Line
are instances of Gate, and represent the automated ticket gates installed at stations
A, B, C and D, respectively. The constructor and each method perform the following
processing.
1) The constructor generates an instance of Gate. A station name is specified as
the first argument, and the distance from station A, which is the starting point of the
line, is specified as the second argument.
2) Method enter performs processing when a passenger enters the platform area
through an automated ticket gate. If the fare certificate is not valid, the gate is closed.
If entrance processing is carried out normally, information on the station of
embarkation is recorded on this fare certificate.
3) Method exit performs processing when a passenger leaves the platform area
through an automated ticket gate. If the fare certificate is invalid in that, for example,
the amount (balance) on the fare certificate is insufficient, then the gate is closed.
4) Methods open and close output messages for opening and closing a gate,
respectively.
Abstract class Ticket, which represents a fare certificate for this line, is inherited
in defining a ticket and a card. The constructor and each method perform the
following processing.
1) The constructor retains a purchase amount on the fare certificate as the initial
value.
2) Method getValue returns the fare certificate amount (balance) as it is at the
point in time when it is called.
3) Method adjustValue performs fare adjustment processing if necessary.
4) Method deduct deducts the amount specified as an argument from the amount
(balance) of the fare certificate, and updates the amount (balance).
5) Method setOrigin records a specified Gate as the station of embarkation. If
null is specified, the record of the station of embarkation is deleted.
6) Method getOrigin returns the station of embarkation that is recorded. If this
station is not recorded, null is returned.
2
Class OneWayTicket represents a ticket, and class PrepaidCard represents a
card. In the processing of either fare certificate, an abstract method is implemented,
and the method in class Ticket is overridden, as necessary.
[Program 1]
public final class Line {
public static final Gate A = new Gate("A", 0);
public static final Gate B = new Gate("B", 5);
public static final Gate C = new Gate("C", 8);
public static final Gate D = new Gate("D", 14);
public static int getFare(int distance) {
return 120 +
(Math.max(distance - 3, 0) / 2) * 30;
}
}
[Program 2]
public class Gate {
private final String name;
private final int distance;
public Gate(String name, int distance) {
this.name = name;
this.distance = distance;
}
public void enter(Ticket ticket) {
if (ticket.isValid() && ticket.getOrigin() == null){
A ;
open();
} else {
close();
}
3
}
public void exit(Ticket ticket) {
Gate origin = ticket.getOrigin();
if (origin != null) {
int d = Math.abs(origin.distance - distance);
int fare = Line.getFare(d);
if ( B ){
ticket.adjustValue(fare);
ticket.setOrigin(null);
open();
return;
}
}
close();
}
private void open() {
System.out.println(name + ": open");
}
private void close() {
System.out.println(name + ": closed");
}
}
[Program 3]
public abstract class Ticket {
private Gate origin;
private int value;
public Ticket(int value) {
this.value = value;
}
public int getValue() { return value; }
public void deduct(int amount) { value -= amount; }
public void setOrigin(Gate gate) { origin = gate; }
public Gate getOrigin() { return origin; }
public abstract void adjustValue(int amount);
public abstract boolean isValid();
4
}
[Program 4]
public class OneWayTicket extends Ticket {
private boolean valid = true;
public OneWayTicket(int value) {
C ;
}
public void setOrigin(Gate gate) {
super.setOrigin(gate);
if (gate == null)
valid = false;
public void adjustValue(int amount) { }
public boolean isValid() { return valid; } }
[Program 5]
public class PrepaidCard extends Ticket {
public PrepaidCard(int value) {
C ;
}
public void adjustValue(int amount) { deduct(amount); }
public boolean isValid() { return getValue() > 0; }
It was decided that a new type of fare certificate be sold. A fare certificate of this
type permits a passenger to freely embark or disembark at all stations within 24
hours from the time that the fare certificate is issued. Twenty-four hours after such
5
a fare certificate is issued, the passenger can leave the platform area in any station
but cannot enter any such area. For this, it is desired that a new class that inherits
abstract class Ticket be defined to permit the use of this type of certificate without
making any modification to class Gate. Processing by the constructor and methods
of this class are shown in the following table. From the answer group below, select
the correct answers to be inserted into the blanks in the table. Here, value in the
answer group is a field value of class Ticket, and it is assumed that the initial
value is set by the constructor and that the value is obtained by method getValue.
Constructor and methods Processing
Constructor D
Method getValue E
Method setOrigin The same as is defined in the superclass
Method getOrigin The same as is defined in the superclass
Method adjustValue No processing is performed (methods proper do not
contain any statements).
Method isValid F
Answer group:
a) 0 is returned at all times.
b) An amount that is theoretically too large to be spent in 24 hours is set as the
initial value of value.
c) Method deduct is called, with amount as an argument.
d) No processing is performed (methods proper do not contain any statements).
e) The base fare on the line is returned at all times.
f) The highest fare on the line (the maximum value among all fares) is set as the
initial value of value, and the fare certificate issuance time is recorded.
6
g) The same as is defined in the superclass
h) true is returned only when the call time is within 24 hours from the fare
certificate issuance time stored in the instance. In other cases, false is returned.
i) true is returned only when value is not less than the base fare on the line. In
other cases, false is returned.
j) true is returned only when value is not less than the highest fare on the line
(the maximum value among all fares). In other cases, false is returned.