Your solutions to this assignment MUST be implemented in functional style (using lambdas and streams), whenever possible.
Needs help with part 3 only:
1. Complete the Train constructor
(verify that the data is correct)
a. carrier must be one of the following:
"Canadian National Railway", "Canadian Pacific Railway", "Hudson
Bay Railway Co", "Quebec North Shore and Labrador Railway",
"RailLink Canada", "Tshiuetin Rail Transportation", "Via
Rail";
b. trainNumber must be between 1 and
999;
c. platform must be between 1 and
26;
d. departureTime and arrivalTime must be
in a 24 hours format, with leading zeros and ":" separating the
hours and minutes (e.g. 00:25, 05:45, 12:01, 17:34, 23:58);
e. destination must be one of the
following: "Toronto", "Labrador City", "Montreal", "Edmonton",
"Vancouver", "Winnipeg", "Halifax", "Ottawa";
f. verification code must be written in
the methods checkCarrier, checkTrainNumber, checkPlatform,
checkTime and checkDestination. These methods will be called from
within the constructor.
2. In the class Train, implement the
methods printInfo and tripDuration
3. In the class TrainStation, implement
the constructor and the following methods (headers are
provided)
a. addTrain
b. printAllTrains
c. printTrainsTo
d. printTrainsBefore
e. printTrainsToBefore
f. printTrainsAfter
g. platformDepartures
h. numberOfTrainsTo
4. In the class TrainStation implement
the methods (headers are provided)
a. cancelTrain
b. cancelTrainsTo
5. In the class TrainStation, implement the method createTrains, which creates at least 5 train trips and replaces the existing timetable. This will allow you to test your code.
trainStation Code:
import java.util.ArrayList;
/**
* Write a description of class TrainStation here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TrainStation
{
// timetable
private ArrayList<Train> xx;
/**
* Constructor for objects of class UniversityRegistrar
*/
public TrainStation()
{
// initialise instance variables
}
/**
* Add the trains recorded in the given filename to the timetable
(replacing previous flights).
* @param filename A CSV file of Train records.
*/
public void addTrainsFromFile(String filename)
{
TrainReader reader = new TrainReader();
xx = new ArrayList<>();
xx.addAll(reader.getTrains(filename));
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addTrain(String depTime,String arrTime, String
destination, String carrierName, int trainNumber, int platform
)
{
// adds a single train to the timetable
}
public void printAllTrains()
{
// prints for all trains the corresponding info
// trains are separated by an empty line
}
public void printList()
{
for(Train record : xx) {
System.out.println(record.getDetails());
}
}
public void printTrainsTo(String destination)
{
// prints all trains to the given destination
// trains are separated by an empty line
}
public void printTrainsBefore(String time)
{
// prints all trains which depart before the given time
// one train per line
}
public void printTrainsToBefore(String destination,String
time)
{
// prints all trains to the given destination which depart before
the given time
// one train per line
}
public void printTrainsAfter(String tt)
{
// prints all trains which depart after the given time
// one train per line
}
public void platformDepartures(int a)
{
// prints all trains that depart from the given platform
// one train per line
}
public int numberOfTrainsTo(String z)
{
// returns number of trains to a given destination
return 0;
}
public void cancelTrain(int trainNumber, String carrierName)
{
// removes given train from the timetable
}
public void cancelTrainsTo(String y)
{
// removes all trains to the given destination from the the
timetable
}
}
Short Summary:
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
TrainStation.java
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* Write a description of class TrainStation here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TrainStation
{
// timetable
private ArrayList<Train> timeTable;
/**
* Constructor for objects of class UniversityRegistrar
*/
public TrainStation()
{
// initialise instance variables
timeTable=new ArrayList<>();
}
/**
* Add the trains recorded in the given filename to the timetable
(replacing previous flights).
* @param filename A CSV file of Train records.
* @throws FileNotFoundException
*/
public void addTrainsFromFile(String filename) throws
FileNotFoundException
{
Scanner file=new Scanner(new
File("C:\\Users\\asus\\"+filename));
while(file.hasNext())
{
String[]
trainArray=file.nextLine().split(",");
timeTable.add(new
Train(trainArray[0],trainArray[1],trainArray[2],trainArray[3],Integer.parseInt(trainArray[4]),Integer.parseInt(trainArray[4])));
}
file.close();
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addTrain(String depTime,String arrTime, String
destination, String carrierName, int trainNumber, int platform
)
{
// adds a single train to the timetable
timeTable.add(new
Train(depTime,arrTime,destination,carrierName,trainNumber,platform));
}
public void printAllTrains()
{
// prints for all trains the corresponding info
// trains are separated by an empty line
timeTable.forEach(s ->
System.out.println(s.toString()+"\n"));
}
public void printList()
{
timeTable.forEach(s ->
System.out.println(s.toString()));
}
public void printTrainsTo(String destination)
{
// prints all trains to the given destination
// trains are separated by an empty line
List<Train> trainList = timeTable.stream()
.filter(n ->
n.getDestination().equalsIgnoreCase(destination))
.collect(Collectors.toList());
trainList.forEach(s ->
System.out.println(s.toString()+"\n"));
}
public void printTrainsBefore(String time)
{
// prints all trains which depart before the given time
// one train per line
String [] timeArr=time.split(":");
LocalTime
givenTime=LocalTime.of(Integer.parseInt(timeArr[0]),Integer.parseInt(
timeArr[1]));
for(Train train:timeTable)
{
String []
depTimeArr=train.getDepTime().split(":");
LocalTime
departTime=LocalTime.of(Integer.parseInt(depTimeArr[0]),Integer.parseInt(
depTimeArr[1]));
if(departTime.isBefore(givenTime))
System.out.println(train.toString());
}
}
public void printTrainsToBefore(String destination,String
time)
{
// prints all trains to the given destination which depart before
the given time
// one train per line
String [] timeArr=time.split(":");
LocalTime
givenTime=LocalTime.of(Integer.parseInt(timeArr[0]),Integer.parseInt(
timeArr[1]));
for(Train train:timeTable)
{
String []
depTimeArr=train.getDepTime().split(":");
LocalTime
departTime=LocalTime.of(Integer.parseInt(depTimeArr[0]),Integer.parseInt(
depTimeArr[1]));
if(departTime.isBefore(givenTime) &&
train.getDestination().equalsIgnoreCase(destination))
System.out.println(train.toString());
}
}
public void printTrainsAfter(String tt)
{
// prints all trains which depart after the given time
// one train per line
String [] timeArr=tt.split(":");
LocalTime
givenTime=LocalTime.of(Integer.parseInt(timeArr[0]),Integer.parseInt(
timeArr[1]));
for(Train train:timeTable)
{
String []
depTimeArr=train.getDepTime().split(":");
LocalTime
departTime=LocalTime.of(Integer.parseInt(depTimeArr[0]),Integer.parseInt(
depTimeArr[1]));
if(departTime.isAfter(givenTime))
System.out.println(train.toString());
}
}
public void platformDepartures(int a)
{
// prints all trains that depart from the given platform
// one train per line
List<Train> trainList = timeTable.stream()
.filter(n ->
n.getPlatform()==a)
.collect(Collectors.toList());
trainList.forEach(s ->
System.out.println(s.toString()+"\n"));
}
public int numberOfTrainsTo(String z)
{
// returns number of trains to a given destination
List<Train> trainList = timeTable.stream()
.filter(n ->
n.getDestination().equalsIgnoreCase(z))
.collect(Collectors.toList());
trainList.forEach(s ->
System.out.println(s.toString()+"\n"));
return trainList.size();
}
public void cancelTrain(int trainNumber, String carrierName)
{
// removes given train from the timetable
Iterator <Train>
iter=timeTable.iterator();
while(iter.hasNext())
{
Train trainObj=iter.next();
if(trainObj.getTrainNumber()==trainNumber &&
trainObj.getCarrierName().equalsIgnoreCase(carrierName))
iter.remove();
}
}
public void cancelTrainsTo(String y)
{
// removes all trains to the given destination from the the
timetable
Iterator <Train>
iter=timeTable.iterator();
while(iter.hasNext())
{
Train trainObj=iter.next();
if(trainObj.getDestination().equalsIgnoreCase(y))
iter.remove();
}
}
}
Train.java
This class is not provided in the question. Have added this to code Trainstation class. Please rearrange the instance variables(in constructor/method calls of Trainstation.java) if it's not in required order
//This class stores the train information
public class Train {
String depTime;
String arrTime; String destination; String
carrierName; int trainNumber; int platform;
public Train(String depTime, String arrTime, String
destination, String carrierName, int trainNumber,
int platform)
{
this.depTime = depTime;
this.arrTime = arrTime;
this.destination =
destination;
this.carrierName =
carrierName;
this.trainNumber =
trainNumber;
this.platform = platform;
}
public String getDepTime() {
return depTime;
}
public String getArrTime() {
return arrTime;
}
public String getDestination() {
return destination;
}
public String getCarrierName() {
return carrierName;
}
public int getTrainNumber() {
return trainNumber;
}
public int getPlatform() {
return platform;
}
}
**************Please do upvote to appreciate our time. Thank you!******************
Get Answers For Free
Most questions answered within 1 hours.