Write a menu driven console application for Maintaining Train Details in Southern Railway using the concept of interfaces.
a. Create an interface called IOperations and declare necessary functions such as addTrain(), editTrain(), etc.
b. Create a class called Railways which implements this interface.
c. Create a Test class which contains the main function.
d. Include functionalities such as
i. Add Train
ii. Edit Train
iii. Display All Train Details
iv. Search Train by Name
v. Search Train by
i. Add Train
ii. Edit Train
iii. Display All Train Details
iv. Search Train by Name
v. Search Train by Source & Destination
vi. Search Train by Train Number
vii. Display all the Express Train
import java.util.ArrayList;
import java.util.Scanner;
class Train {
private String name;
private String trainNumber;
private String source;
private String destination;
private boolean isExpress;
/**
* Constructor
*/
public Train() {
}
/**
* Constructor
*
* @param name
* @param trainNumber
* @param source
* @param destination
* @param isExpress
*/
public Train(String name, String trainNumber, String source, String destination, boolean isExpress) {
this.name = name;
this.trainNumber = trainNumber;
this.source = source;
this.destination = destination;
this.isExpress = isExpress;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the trainNumber
*/
public String getTrainNumber() {
return trainNumber;
}
/**
* @param trainNumber the trainNumber to set
*/
public void setTrainNumber(String trainNumber) {
this.trainNumber = trainNumber;
}
/**
* @return the source
*/
public String getSource() {
return source;
}
/**
* @param source the source to set
*/
public void setSource(String source) {
this.source = source;
}
/**
* @return the destination
*/
public String getDestination() {
return destination;
}
/**
* @param destination the destination to set
*/
public void setDestination(String destination) {
this.destination = destination;
}
/**
* @return the isExpress
*/
public boolean isExpress() {
return isExpress;
}
/**
* @param isExpress the isExpress to set
*/
public void setExpress(boolean isExpress) {
this.isExpress = isExpress;
}
public String toString() {
return "Name: " + this.name + "\n" + "Train number: " + this.trainNumber + "\n" + "Source: " + this.source
+ "\n" + "Destination: " + this.destination + "\n" + "Is express: " + this.isExpress;
}
}
interface IOperations {
public void addTrain(Train train);
public void editTrain(int selectedTrain, Train train);
public void displayAllTrainDetails();
public void searchTrainByName(String name);
public void searchTrainSourceDestination(String sourceDestination);
public void searchTrainByTrainNumber(String trainNumber);
public void displayAllExpressTrain();
}
class Railways implements IOperations {
private ArrayList<Train> trains;
public Railways() {
this.trains = new ArrayList<Train>();
}
@Override
public void addTrain(Train train) {
trains.add(train);
}
@Override
public void editTrain(int selectedTrain, Train train) {
trains.remove(selectedTrain);
trains.add(selectedTrain, train);
}
@Override
public void displayAllTrainDetails() {
for (int i = 0; i < trains.size(); i++) {
System.out.println(trains.get(i).toString() + "\n");
}
}
@Override
public void searchTrainByName(String name) {
for (int i = 0; i < trains.size(); i++) {
if (trains.get(i).getName().compareTo(name) == 0) {
System.out.println(trains.get(i).toString() + "\n");
}
}
}
@Override
public void searchTrainSourceDestination(String sourceDestination) {
for (int i = 0; i < trains.size(); i++) {
if (trains.get(i).getSource().compareTo(sourceDestination) == 0
|| trains.get(i).getDestination().compareTo(sourceDestination) == 0) {
System.out.println(trains.get(i).toString() + "\n");
}
}
}
public int getTrainByTrainNumber(String trainNumber) {
for (int i = 0; i < trains.size(); i++) {
if (trains.get(i).getTrainNumber().compareTo(trainNumber) == 0) {
return i;
}
}
return -1;
}
@Override
public void searchTrainByTrainNumber(String trainNumber) {
for (int i = 0; i < trains.size(); i++) {
if (trains.get(i).getTrainNumber().compareTo(trainNumber) == 0) {
System.out.println(trains.get(i).toString() + "\n");
}
}
}
@Override
public void displayAllExpressTrain() {
for (int i = 0; i < trains.size(); i++) {
if (trains.get(i).isExpress()) {
System.out.println(trains.get(i).toString() + "\n");
}
}
}
}
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int ch = -1;
Railways railways = new Railways();
String name;
String trainNumber;
String source;
String destination;
boolean isExpress;
while (ch != 0) {
System.out.println("1. Add Train");
System.out.println("2. Edit Train");
System.out.println("3. Display All Train Details");
System.out.println("4. Search Train by Name");
System.out.println("5. Search Train by by Source & Destination");
System.out.println("6. Search Train by Train Number");
System.out.println("7. Display all the Express Train");
System.out.println("0. Exit");
System.out.print("Your choice: ");
ch = keyBoard.nextInt();
keyBoard.nextLine();
switch (ch) {
case 0:
// exit
break;
case 1: {
System.out.print("Enter a train name: ");
name = keyBoard.nextLine();
System.out.print("Enter a train number: ");
trainNumber = keyBoard.nextLine();
System.out.print("Enter a train source: ");
source = keyBoard.nextLine();
System.out.print("Enter a train destination: ");
destination = keyBoard.nextLine();
isExpress = false;
System.out.print("Is a train express? (1. yes, 2. no): ");
int intIsExpress = keyBoard.nextInt();
if (intIsExpress == 1) {
isExpress = true;
}
Train train = new Train(name, trainNumber, source, destination, isExpress);
System.out.println("A new Train has been added.\n\n");
railways.addTrain(train);
}
break;
case 2: {
System.out.print("Enter a train number to edit: ");
trainNumber = keyBoard.nextLine();
int selectedTrain = railways.getTrainByTrainNumber(trainNumber);
if (selectedTrain != -1) {
System.out.print("Enter a new train name: ");
name = keyBoard.nextLine();
System.out.print("Enter a new train number: ");
trainNumber = keyBoard.nextLine();
System.out.print("Enter a new train source: ");
source = keyBoard.nextLine();
System.out.print("Enter a new train destination: ");
destination = keyBoard.nextLine();
isExpress = false;
System.out.print("Is a train express? (1. yes, 2. no): ");
int intIsExpress = keyBoard.nextInt();
if (intIsExpress == 1) {
isExpress = true;
}
Train train = new Train(name, trainNumber, source, destination, isExpress);
System.out.println("A new Train has been updated.\n\n");
railways.editTrain(selectedTrain, train);
}
}
break;
case 3:
railways.displayAllTrainDetails();
break;
case 4:
System.out.print("Enter a train name to search: ");
name = keyBoard.nextLine();
railways.searchTrainByName(name);
break;
case 5:
System.out.print("Enter a new train source or destination to search: ");
String sourceDestination = keyBoard.nextLine();
railways.searchTrainSourceDestination(sourceDestination);
break;
case 6:
System.out.print("Enter a new train number to edit: ");
trainNumber = keyBoard.nextLine();
railways.searchTrainByTrainNumber(trainNumber);
break;
case 7:
railways.displayAllExpressTrain();
break;
default:
System.out.println("Wrong menu item.");
break;
}
}
keyBoard.close();
}
}
Comments
Leave a comment