Hello guys,
I got a problem in my code which is suposed to work as a linked lists of airplanes and each node in the list(Airplane) has another list beneath it as a nother linked list of passengers, but the problem that the linked list of passengers for each airplane is each time in implemntation is lost
***Airplane class***
***Passengers Calss***public class Airplane { public String name; public int number; public double time; public String caName; public Airplane next; public Passenger refPass; public Airplane(String nm, int num, double tm, String cName) { name=nm; number=num; time=tm; caName=cName; next=null; refPass=null; } public Airplane(String nm, int num, double tm, String cName, Airplane n, Passenger refP) { name=nm; number=num; time=tm; caName=cName; next=n; refPass=refP; } }
***List and implemntation methods Class***public class Passenger { public String frName; public String laName; public int id; public int seatNumber; public int lagNumber; public Passenger next; public Passenger(String fNM, String laNM, int d, int sNum, int lgNum ) { frName=fNM; laName=laNM; id=d; seatNumber=sNum; lagNumber=lgNum; next=null; } public Passenger(String fNM, String laNM, int d, int sNum, int lgNum, Passenger n ) { frName=fNM; laName=laNM; id=d; seatNumber=sNum; lagNumber=lgNum; next=n; } }
***Main Class***import java.util.Scanner; public class List { public Airplane head, tail; public Passenger tmpPass; Scanner kk=new Scanner(System.in); public List() { head=tail=null; tmpPass=null; } public void addToTail(String nm, int num, double tm, String cName)/// Insertion for airplanes and flights. { // A pointer from an Airplane node to point and the first passenger node. if (head==null) { head=tail=new Airplane(nm, num, tm, cName); tmpPass=tail.refPass;// Assigning the pointer to the passenger list pointer. String firstName; String lastName; int iD; int seatNumber; int lagNumber; String op; do{ System.out.println("***Passenger Info***"); System.out.print("First Name:"); firstName=kk.next(); System.out.print("Last Name:"); lastName=kk.next(); System.out.print("ID:"); iD=kk.nextInt(); System.out.print("Seat Number:"); seatNumber=kk.nextInt(); System.out.print("Luggage Number:"); lagNumber=kk.nextInt(); tmpPass=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmpPass);// Creating a passenger node. System.out.println("Do you want to add more passenger[Yes/No]"); op=kk.next(); }while(!op.equalsIgnoreCase("No")); } else { tail.next=new Airplane(nm, num, tm, cName); tail = tail.next; tmpPass=tail.refPass;// Assigning the pointer to the passenger list pointer. String firstName; String lastName; int iD; int seatNumber; int lagNumber; String op; do{ System.out.println("***Passenger Info***"); System.out.print("First Name:"); firstName=kk.next(); System.out.print("Last Name:"); lastName=kk.next(); System.out.print("ID:"); iD=kk.nextInt(); System.out.print("Seat Number:"); seatNumber=kk.nextInt(); System.out.print("Luggage Number:"); lagNumber=kk.nextInt(); tmpPass=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmpPass);// Creating a passenger node. System.out.println("Do you want to add more passenger[Yes/No]"); op=kk.next(); }while(!op.equalsIgnoreCase("No")); } } public boolean searchFlight(int nm) { Airplane tmp; for(tmp=head; tmp!=null &&tmp.number!=nm; tmp=tmp.next); printFlighrSInfo(tmp); return tmp!=null; } public void printFlighrSInfo(Airplane tmp) { System.out.println("Found"); System.out.println("Flight Info"); System.out.println("Airplane Type:"+tmp.name); System.out.println("Flight Number:"+tmp.number); System.out.println("Time:"+tmp.time); System.out.println("Captin Name:"+tmp.caName); int n=1; Passenger tmpPass; tmpPass=tmp.refPass; for(; tmpPass!=null; tmpPass=tmpPass.next) { System.out.println("Passenger"+n+" Info"); System.out.println("First Name:"+tmpPass.frName); System.out.println("Last Name:"+tmpPass.laName); System.out.println("ID:"+tmpPass.id); System.out.println("Seat Number:"+tmpPass.seatNumber); System.out.println("Luggage Number:"+tmpPass.lagNumber);} } public void printAll() { Airplane tmp; Passenger tmp2; for(tmp=head; tmp!=null; tmp=tmp.next) { for(tmp2=tmp.refPass; tmp2!=null; tmp2=tmp2.next) { System.out.println("Name"+tmp2.frName); } } } }
import java.util.Scanner; public class Airport { public static void main(String[] args) { Scanner k=new Scanner(System.in); List listo=new List(); int option; String name; int number; double time; String caName; do{ System.out.print("option:"); option=k.nextInt(); switch(option) { case 1: System.out.println("***Flight Info***"); System.out.print("Airplane Type:"); name=k.next(); System.out.print("Flight Number:"); number=k.nextInt(); System.out.print("Time:"); time=k.nextDouble(); System.out.print("Captin Name:"); caName=k.next(); listo.addToTail(name, number, time, caName); break; case 2: System.out.print("Search Flight No:"); number=k.nextInt(); boolean b=listo.searchFlight(number); if(b==true) { listo.searchFlight(number); } else System.out.println("Not found"); break; case 3: listo.printAll(); break; } }while(option!=10); } }
Please help urgent
Tahnks