Implementation Overview
The subway management system consists of two primary components: a data model class for subway routes and a management class that hendles operations.
Data Model Class
The RailwayLine class serves as the foundational model:
package TransitSystem;
public class RailwayLine {
private String routeName;
private String[] stationsList;
private String routeDetails;
public String getRouteName() {
return routeName;
}
public void setRouteName(String routeName) {
this.routeName = routeName;
}
public String[] getStationsList() {
return stationsList;
}
public RailwayLine() {
super();
this.routeName = routeName;
this.stationsList = stationsList;
this.routeDetails = routeDetails;
}
public void setStationsList(String[] stationsList) {
this.stationsList = stationsList;
}
public String getRouteDetails() {
return routeDetails;
}
public void setRouteDetails(String routeDetails) {
this.routeDetails = routeDetails;
}
}
Management System Class
The RailwayManager class handles all operational logic:
package TransitSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class RailwayManager {
static Scanner inputReader = new Scanner(System.in);
public static void main(String[] args) throws IOException {
RailwayLine lineOne = new RailwayLine();
RailwayLine lineThree = new RailwayLine();
String []firstLineStations = new String[30];
String []thirdLineStations = new String[10];
File dataFile = new File("D://transit-data//shijiazhuang-subway.txt");
InputStream fileStream = new FileInputStream(dataFile);
byte buffer[] = new byte[1024];
int bytesRead = fileStream.read(buffer);
fileStream.read(buffer);
fileStream.close();
String content = new String(buffer, 0, bytesRead);
String[] elements = content.split(" ");
lineOne.setRouteName(elements[0]);
lineOne.setRouteDetails(elements[21]);
int indexTracker = 0;
for(int position = 1; position < 21; position++) {
firstLineStations[indexTracker++] = elements[position];
}
lineThree.setRouteName(elements[22]);
lineThree.setRouteDetails(elements[29]);
indexTracker = 0;
for(int position = 23; position < 29; position++) {
thirdLineStations[indexTracker++] = elements[position];
}
lineThree.setStationsList(thirdLineStations);
System.out.println("***************************************************************");
System.out.println(" Welcome to Shijiazhuang Metro ");
System.out.println("****************************************************************");
System.out.println(" 1、 Display route information; ");
System.out.println(" 2、 Purchase tickets ");
System.out.println("****************************************************************");
int selection;
inputReader = new Scanner(System.in);
selection = inputReader.nextInt();
if(selection == 1) {
System.out.println("Please select the route you want to view:");
System.out.println("0 :All routes ");
System.out.println("1:Line One ");
System.out.println("2:Line Three ");
inputReader = new Scanner(System.in);
int routeChoice = inputReader.nextInt();
System.out.println("***************************************************************");
System.out.println(" Shijiazhuang Metro Route Map ");
System.out.println("***************************************************************");
int firstIndex = 0;
int secondIndex = 0;
if(routeChoice == 1)
{
firstIndex = 0;
System.out.print("Line 1:");
while(firstLineStations[firstIndex] != null) {
System.out.print(firstLineStations[firstIndex] + " ");
firstIndex++;
}
System.out.println();
}
if(routeChoice == 2) {
System.out.print("Line 3:");
secondIndex = 0;
while(thirdLineStations[secondIndex] != null) {
System.out.print(thirdLineStations[secondIndex] + " ");
secondIndex++;
}
System.out.println();
}
if(routeChoice != 1 && routeChoice != 2){
System.out.println("Line 1:");
firstIndex = 0;
while(firstLineStations[firstIndex] != null) {
System.out.print(firstLineStations[firstIndex] + " ");
firstIndex++;
}
System.out.println();
System.out.println("Line 3:");
secondIndex = 0;
while(thirdLineStations[secondIndex] != null) {
System.out.print(thirdLineStations[secondIndex] + " ");
secondIndex++;
}
System.out.println();
}
System.out.println("***************************************************************");
}
else {
System.out.println("****************************************************************");
System.out.println(" Welcome to Shijiazhuang Metro ");
System.out.println("****************************************************************");
System.out.println(" Starting station:");
System.out.println(" Destination station:");
System.out.println(" Number of tickets:");
System.out.println("****************************************************************");
inputReader = new Scanner(System.in);
String startStation = null;
String endStation = null;
int ticketCount = 0;
startStation = inputReader.next();
endStation = inputReader.next();
ticketCount = inputReader.nextInt();
int startPos = 0, endPos = 0;
int thirdStart = 0, thirdEnd = 0;
int firstCounter = 0, secondCounter = 0;
int thirdCounter = 0, fourthCounter = 0;
int ticketPrice = 0;
int routeIdentifier = 0;
int loopIndex = 0;
int firstRouteCheck = 0;
int secondRouteCheck = 0;
while(firstLineStations[loopIndex] != null) {
if(firstLineStations[loopIndex].compareTo(startStation) == 0) {
startPos = loopIndex;
firstRouteCheck++;
}
if(firstLineStations[loopIndex].compareTo(endStation) == 0) {
endPos = loopIndex;
firstRouteCheck++;
}
loopIndex++;
}
loopIndex = 0;
while(thirdLineStations[loopIndex] != null) {
if(thirdLineStations[loopIndex].compareTo(startStation) == 0) {
thirdStart = loopIndex;
}
if(thirdLineStations[loopIndex].compareTo(endStation) == 0) {
thirdEnd = loopIndex;
}
loopIndex++;
}
if(firstRouteCheck == 2) {
secondRouteCheck = Math.abs(endPos - startPos);
if(secondRouteCheck <= 3) {
ticketPrice = 1;
}
else {
int adjustment = 0;
if(secondRouteCheck % 3 == 0) {
adjustment = 0;
} else {
adjustment = 1;
}
ticketPrice = secondRouteCheck / 3 + adjustment;
}
if(startPos > endPos)
{
int temporary = startPos;
startPos = endPos;
endPos = temporary;
}
System.out.println("***************************************************************");
System.out.println(" Welcome to Shijiazhuang Metro ");
System.out.println("****************************************************************");
System.out.println(secondRouteCheck + " stops");
for(int displayIndex = startPos; displayIndex <= endPos; displayIndex++) {
System.out.println(firstLineStations[displayIndex]);
}
System.out.println("Ticket unit price:" + ticketPrice + " yuan");
System.out.println("Total amount:" + ticketPrice * ticketCount + " yuan");
}
String transferHub = "New Plaza";
if(firstRouteCheck == 0 || (firstRouteCheck == 1 && (startStation.compareTo(transferHub) == 0 || endStation.compareTo(transferHub) == 0))) {
secondRouteCheck = Math.abs(thirdStart - thirdEnd);
if(secondRouteCheck <= 3) {
ticketPrice = 1;
} else {
int adjustment = 0;
if(secondRouteCheck % 3 == 0) {
adjustment = 0;
} else {
adjustment = 1;
}
ticketPrice = secondRouteCheck / 3 + adjustment;
}
if(thirdStart > thirdEnd)
{
int temporary = thirdStart;
thirdStart = thirdEnd;
thirdEnd = temporary;
}
System.out.println("***************************************************************");
System.out.println(" Welcome to Shijiazhuang Metro ");
System.out.println("****************************************************************");
System.out.println(secondRouteCheck + " stops");
for(int displayIndex = thirdStart; displayIndex <= thirdEnd; displayIndex++) {
System.out.println(thirdLineStations[displayIndex]);
}
System.out.println("Ticket unit price:" + ticketPrice + " yuan");
System.out.println("Total amount:" + ticketPrice * ticketCount + " yuan");
}
if(firstRouteCheck == 1 && startStation.compareTo(transferHub) != 0 && endStation.compareTo(transferHub) != 0) {
if(endPos != 0) {
startPos = endPos;
}
if(thirdEnd != 0) {
thirdStart = thirdEnd;
}
int segmentOne = 0;
int segmentTwo = 0;
segmentOne = Math.abs(startPos - 14);
segmentTwo = Math.abs(thirdStart - 1);
secondRouteCheck = segmentOne + segmentTwo;
if(secondRouteCheck <= 3) {
ticketPrice = 1;
} else {
int adjustment = 0;
if(secondRouteCheck % 3 == 0) {
adjustment = 0;
} else {
adjustment = 1;
}
ticketPrice = secondRouteCheck / 3 + adjustment;
}
System.out.println("***************************************************************");
System.out.println(" Welcome to Shijiazhuang Metro ");
System.out.println("****************************************************************");
System.out.println(secondRouteCheck + " stops");
if(startPos < 14) {
for(int displayIndex = startPos; displayIndex < 15; displayIndex++)
System.out.println(firstLineStations[displayIndex]);
} else {
for(int displayIndex = startPos; displayIndex > 13; displayIndex--) {
System.out.println(firstLineStations[displayIndex]);
}
}
System.out.println("(Transfer station)");
if(thirdStart < 1) {
for(int displayIndex = thirdStart; displayIndex < 1; displayIndex++) {
System.out.println(thirdLineStations[displayIndex]);
}
} else {
for(int displayIndex = thirdStart; displayIndex > 1; displayIndex--) {
System.out.println(thirdLineStations[displayIndex]);
}
}
System.out.println("Ticket unit price:" + ticketPrice + " yuan");
System.out.println("Total amount:" + ticketPrice * ticketCount + " yuan");
}
}
}
}
System Limitations
The implementation works correctly for single-line journeys on either Line 1 or Line 3. However, there are issues with cross-line transfers where the output sequence becomes disordered when the journey direction doesn't follow a left-to-right pattern. Additionally, data input errors remain unresolved.
Design Approach
The system separates functionality into route inquiry and ticket purchasing operations. These functions operate independently, allowing for distinct hnadling of each scenario. While route display functionality works properly, ticket purchasing with route display and pricing calculation presents challenges. The approach involves treating transfer stations as regular stations rather than special cases, and ensuring transfer points aren't counted multiple times during route calculations. Station counting excludes the starting point and includes the destination stop.