I am trying to take a text file and read it into the ArrayList I created. It should store the passengers name and the passenger's service class.
The text file has the following format:
James 1
Sara 2
Rita 1
....etc
Here is my 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; } }
Here is my Train Class:
import java.io.*; import java.util.Scanner; import java.util.ArrayList; public class Train { private ArrayList<Passenger> train; private double percent; private double revenue; public Train() { train = new ArrayList<Passenger>(); File file = new File("Passengers.txt"); Scanner input = new Scanner(file); String name; int sClass; while(input.hasNext()) { name = input.next(); sClass = input.nextInt(); Passenger pass = new Passenger(sClass, name); train.add(pass); } input.close(); } double percent_first_class() { int count; int total; int serviceClass = pass.getServiceClass(); if(serviceClass == 1) { count++; total++; } else { total++; } percent = count/total*100; return percent; } double train_revenue(double price_fC, double price_sC) { int nFC; int nSC; int serviceClass = pass.getServiceClass(); if(serviceClass == 1) { nFC++; } else { nSC++; } revenue = (price_fC * nFC) + (price_sC * nSC); return revenue; } boolean pass_check() { ArrayList<Passenger> searchforpass = new ArrayList<Passenger>(); for(Passenger currentPassenger : train) { if((currentPass.getName()).indexOf(searchString) != -1) searchforpass.add(currentPass); } searchforpass.trimToSize(); return true; } }
I believe I am inputting the passengers into the ArrayList correctly (but not 100% sure). Then, in my percent_first_class method, I get an error when I try to compile saying that in my line:
It cannot find the variable passint serviceClass = pass.getServiceClass();
The train_revenue method will return the total revenue of the train from given inputs in the parameters. I believe that is correct but once again it cannot find the variable pass.
The pass_check method will allow the user to search for a passenger and will return true if the name is found, and false otherwise. I also believe that is correct.
If you find mistakes, it would be greatly appreciated if you showed me how to fix them. I am very new to Java yet and am still learning. Thank you to all who help with this.