Design & code a program including the following classes, and also a client class
ID: 3562929 • Letter: D
Question
Design & code a program including the following classes, and also a client class to test all of the methods coded:
A Passenger class, encapsulating a passenger. 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>
Explanation / Answer
Passenger.java
public class Passenger {
private String name;
private int service_class;
public Passenger(){
}
public Passenger(String name, int cls){
if(cls == 1 || cls == 2){
this.service_class = cls;
this.name = name;
}
else
throw new IllegalArgumentException();
}
public void setName(String name){
this.name = name;
}
public void setService_class(int cls){
this.service_class = cls;
}
public String getName(){
return name;
}
public int getService_class(){
return service_class;
}
}
Train.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Train {
private String[] tempString;
private List<Passenger> pList = new ArrayList<Passenger>();
private Iterator it;
public Train(){
String curr;
try {
BufferedReader br = new BufferedReader(new FileReader("C:\Users\GOD\Downloads\untitled\src\passenger.txt"));
while(( curr = br.readLine()) != null){
tempString = curr.split(" ");
pList.add(new Passenger(tempString[0], Integer.parseInt(tempString[1])));
}
}
catch(Exception e){
e.printStackTrace();
}
}
public double getFirstPer(){
double per =0;
int count = 0;
int total = 0;
it = pList.iterator();
while(it.hasNext()){
if( (((Passenger)it.next()).getService_class()) == 1 ){
count+=1;
}
}
total = pList.size();
if(total != 0) {
per = (double)count/(double)total;
}
return (per);
}
public float getPrice(float first, float second){
float cost =0;
Passenger temp;
it = pList.iterator();
while(it.hasNext()){
temp = ((Passenger)it.next());
if( (temp.getService_class()) == 1 ){
cost +=first;
}
if( (temp.getService_class()) == 2 ){
cost +=second;
}
}
return cost;
}
public boolean isPresent(String name){
it = pList.iterator();
Passenger temp;
while(it.hasNext()){
temp = ((Passenger)it.next());
if(temp.getName().equals(name)){
return true;
}
}
return false;
}
}
Tester.java
public class Tester {
public static void main(String... args){
Train tr = new Train();
System.out.println(tr.getFirstPer());
System.out.println(tr.getPrice(10,20));
System.out.print(tr.isPresent("yosh"));
}
}
For the above program to work, make necessary changes in the file path of passenger.txt in the constructor of Train.java. Also provide the full path of the file