For the following problem, I have created the passenger class and have created the ArrayList in the Train Class. I do not know how to read the passengers from a text file into the ArrayList. And I also do not understand how to do the other methods. Help would be greatly appreciated.
Train Class
import java.util.ArrayList; public class Train { private ArrayList<Passenger> train; public Train() { train = new ArrayList<Passenger>(); } }
Passenger Class
public class Passenger { private int serviceClass; private String name; public Passenger(int sC, String n) { setServiceClass(sC); setName(n); } public void setServiceClass(int sC) { if(sC == 1 || sC == 2) serviceClass = sC; else System.out.println("Error"); } public void setName(String n) { name = n; } public String getName() { return name; } public int getServiceClass() { return serviceClass; } }
Design and code a program including the following classes, as well as a client class to test all the methods coded:
A Passenger class, encapsulating a passenger. A passenger has two attributes: a name, and a class of service, which will be 1 or 2.
A Train class, encapsulating a train of passengers. A train of passengers has one attribute: a list of passengers, which must be represented with an ArrayList. Your constructor will build the list of passengers by reading date from a file called passengers.txt (that you would create). You can assume that passengers.txt has the following format:
<name1> <class1>
<name2> <class2>
….
For instance, the file could contain:
James 1
Ben 2
Suri 1
Sarah 1
Jane 2
…..
You should include the following methods in your Train class:
A method returning the percentage of passengers traveling in the first class
A method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train.
A method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false.