This program is in java Design and code a program including the following classe
ID: 3745986 • Letter: T
Question
This program is in java
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 data from a file called passengers.txt. 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 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
Here are the Javadoc details for your classes:
Passenger:
Train Class
SAMPLE OUTPUT
The trains are not equal
After changing t1, the trains are equal
The train passengers are:
name: James; service: 1
name: Ben; service: 2
name: Suri; service: 1
name: Reyna; service: 1
name: Jane; service: 2
name: Mario; service: 2
name: Lucy; service: 1
name: Sergio; service: 2
name: Mary; service: 2
44.44% of the passengers travel in first class
Enter the ticket price for first class > 52.00
Enter the ticket price for second class > 28.50
Total train revenues: $350.50
Enter the name of a passenger > Mario
Mario is on the train
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Passenger.java
public class Passenger {
private String name;
private int classOfService;
/**
* Constructor:<BR>
* Allows client to set beginning values for name and service This
* constructor takes two parameters<BR>
* Calls mutator method to validate new value
*
* @param newName
* the name of the passenger
* @param newService
* the class of travel
*/
public Passenger(String newName, int newService) {
this.name = newName;
setService(newService);
}
/**
* Mutator method:<BR>
* Allows client to set value of class of travel <B>setService</B> sets the
* value in <B>service</B> to newService if newService is not 1 or 2,
* service is assigned 2
*
* @param service
* the new class of travel
* @return a reference to this object
*/
public void setService(int newService) {
if (newService == 1 || newService == 2) {
classOfService = newService;
} else {
classOfService = 2;
}
}
/**
* equals
*
* @param o
* Passenger object
* @return return true if name and class of tavel in p are equal to
* corresponding elements in this object
*/
public boolean equals(Passenger o) {
if (name.equalsIgnoreCase(o.name) && classOfService == o.classOfService) {
return true;
}
return false;
}
//other required methods, but not mentioned
public String getName() {
return name;
}
public int getService() {
return classOfService;
}
@Override
public String toString() {
return "Name: "+name+", Service: "+classOfService;
}
}
// Train.java
import java.util.ArrayList;
public class Train {
private ArrayList<Passenger> travelers;
/**
* Constructor:<BR>
* Allows client to set beginning list of passengers for travelers This
* constructor takes one parameter<BR>
* Fills ArrayList travelers with the Passenger objects from newTravelers
*
* @param newTravelers
* the list of travelers
*/
public Train(ArrayList<Passenger> newTravelers) {
this.travelers = newTravelers;
}
/**
* Mutator method:<BR>
* Allows client to set values of travelers <B>setTravelers</B> sets the
* values in <B>travelers</B> to the values in newTravelers
*
* @param newTravelers
* the new ArrayList for travelers
*/
public void setTravelers(ArrayList<Passenger> newTravelers) {
this.travelers = newTravelers;
}
/**
* equals
*
* @param o
* Train object
* @return return true if elements of ArrayList in t are equal to
* corresponding elements in this object and ArrayLists have the
* same size
*/
public boolean equals(Train o) {
if (travelers.size() == o.travelers.size()
&& travelers.containsAll(o.travelers)
&& o.travelers.containsAll(travelers)) {
return true;
}
return false;
}
/**
* percentageFirstClassPassengers method Computes the percentage of first
* class passengers on the train
*
* @return a double, the percentage of rist class passengers on the train
*/
public double percentageFirstClassPassengers() {
int countOfFirstClassPassengers = 0;
for (Passenger p : travelers) {
if (p.getService() == 1) {
countOfFirstClassPassengers++;
}
}
int total = travelers.size();
if (total != 0) {
double percentage = (double) countOfFirstClassPassengers / total;
percentage*=100.0;
return percentage;
}
return 0;
}
/**
* trainRevenues method Computes the total revenues for the train based on
* first and second class ticket prices
*
* @param firstClassPrice
* the price of a first class ticket
* @param secondClassPrice
* the price of a second class ticket
* @return a double, the total revenues for the train
*/
public double trainRevenues(double firstClassPrice, double secondClassPrice) {
double totalRevenue = 0;
for (Passenger p : travelers) {
if (p.getService() == 1) {
totalRevenue += firstClassPrice;
} else {
totalRevenue += secondClassPrice;
}
}
return totalRevenue;
}
/**
* isOnTrain method Searches if a person is on the train
*
* @param person
* the name of a passenger
* @return a boolean, true if person is on the train, false otherwise
*/
public boolean isOnTrain(String person) {
for (Passenger p : travelers) {
if (p.getName().equalsIgnoreCase(person)) {
return true;
}
}
return false;
}
//other required method, but not mentioned
@Override
public String toString() {
String data="The train passengers are:";
for(Passenger p:travelers){
data+=" "+p.toString();
}
return data;
}
}
// Test.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
/**
* method to load passenger data from a file and fill an array list
* @@throws FileNotFoundException if file not found
*/
static ArrayList<Passenger> loadData(String filename)
throws FileNotFoundException {
ArrayList<Passenger> travelers = new ArrayList<Passenger>();
Scanner scanner = new Scanner(new File(filename));
while (scanner.hasNext()) {
String name = scanner.next(); // assuming name without any spaces
int service = scanner.nextInt();
Passenger passenger = new Passenger(name, service);
travelers.add(passenger);
}
return travelers;
}
public static void main(String[] args) throws FileNotFoundException {
// you should have a passengers.txt file in your project folder
ArrayList<Passenger> travelers = loadData("passengers.txt");
//creating a Train
Train train = new Train(travelers);
//displaying train
System.out.println(train);
//displaying other stats
System.out.printf("%.2f %% of the passengers travel in first class ",
train.percentageFirstClassPassengers());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the ticket price for first class > ");
double price1 = scanner.nextDouble();
System.out.print("Enter the ticket price for second class > ");
double price2 = scanner.nextDouble();
System.out.printf("Total train revenues: $%.2f ",
train.trainRevenues(price1, price2));
System.out.print("Enter the name of a passenger > ");
String name = scanner.next();
if(train.isOnTrain(name)){
System.out.println(name+" is on the train");
}else{
System.out.println(name+" is not on the train");
}
}
}
//passengers.txt used
James 1
Ben 2
Suri 1
Sarah 1
Jane 2
//output
The train passengers are:
Name: James, Service: 1
Name: Ben, Service: 2
Name: Suri, Service: 1
Name: Sarah, Service: 1
Name: Jane, Service: 2
60.00 % of the passengers travel in first class
Enter the ticket price for first class > 28
Enter the ticket price for second class > 11.66
Total train revenues: $107.32
Enter the name of a passenger > Suri
Suri is on the train
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.