A Passenger class - use defined class encapsulating a passenger. A passenger has
ID: 3680194 • Letter: A
Question
A Passenger class - use defined class encapsulating a passenger. A passenger has two attributes: a name, and a class of service, which will be 1 or 2. Provide an appropriate constructor, get and set methods along with equals( ) and toString( ) methods. A Train class, encapsulating a train of passengers. This class requires only one instance variable: passengerList of ArrayList type You should include the following methods in your Train class: a. Implement a default constructor which initializes passengerList instance variable to a new ArrayList object b. A method that will take a Passenger object as a parameter and add the passenger to the passengerList. c. A method returning the reference to the Passenger Object at a specific index position. The index position with be passed as a parameter to the method. Hint: The method header would be public Passenger getPassenger(int i) { Code goes here } d. A method returning the number of passengers in the train. e. A method returning the percentage of passengers traveling in the first class f. A method taking two parameters representing the price of traveling in first and second class and returning the total revenue for the train. g. A method checking if a certain person is on the train; if he/she is, the method returns true; otherwise, it returns false. h. Implement the toString( ) and equals ( ) method. i. Include JavaDoc comments for both Passenger and Train class. Next you will implement a test class called TrainTest with the main ( ) method. In the main method, • Create a Train object (At this time the train is empty i.e. the passenger List is zero). • Next create at least 10 Passengers using the information as below. James 1 Ben 2 Suri 1 Sarah 1 Jane 2 ….. • Add the Passengers to the Train. • Use for loop to print the names of all the passengers in the train. • Demonstrate the various method of the Train class. • Make sure to keep your output verbose.
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Train
{
private ArrayList<Passenger> list;
public Train()
{
list = new ArrayList<Passenger>();
InputStreamReader stream = new InputStreamReader( Train.class.getResourceAsStream( "Passenger.txt" ) );
BufferedReader read = new BufferedReader(stream);
String line;
try
{
while ((line = read.readLine()) != null){
String [] temp = line.split(" ");
list.add(new Passenger(temp[0], Integer.parseInt(temp[1])));
}
read.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public boolean isPersonOnTrain(String name)
{
for (int i = 0; i < list.size(); i++)
{
if(list.get(i).getName().equals(name... return true;
}
return false;
}
public double percentTravelingFirstClass()
{
double first = 0;
for (int i = 0; i < list.size(); i++){
if(list.get(i).getClassOfService() == 1) first++;
}
return (first/list.size())*100;
}
public double totalRevenue(double firstClass, double secondClass)
{
double total = 0.0;
for (int i = 0; i < list.size(); i++){
total += list.get(i).getClassOfService() == 1 ? firstClass : secondClass;
}
return total;
}
public static void main (String [] args)
{
Train myTrain = new Train();
System.out.println( myTrain.isPersonOnTrain("John") );
System.out.println( myTrain.percentTravelingFirstClass() );
System.out.println( myTrain.totalRevenue(50, 10) );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.