开发者

JAVA Linked List Confused on why I can't check a variable against an node in the Linked List?

开发者 https://www.devze.com 2023-03-11 10:33 出处:网络
public static void check(){ String name; System.out.println(\"Enter Customer Name to CHECK RESERVATION ticket for this Flight: \");
public static void check(){
        String name;
        System.out.println("Enter Customer Name to CHECK RESERVATION ticket for this Flight: ");
        Scanner input = new Scanner(System.in);
        name = input.nextLine();
        if (list.contains(name)) { //WHY IS THIS ASKING FOR SEPARATE METHOD?
            System.out.println(name +" has a Reservation on this FLight!");
            menu();
        }

I am trying to take an input and check to see if that input is in the Linked List. I am having problems though getting this to work right.

If I add the new method in my LinkedList.Java class it says it needs to define a variable for link. Below is what I have in entirety if it helps:

import java.util.Scanner;
class airline {
public static LinkedList list = new LinkedList();

    public static void main(String[] args) {

        list.addAirplane("Allen",501);
        list.addAirplane("James",501);
        list.addAirplane("Andrea",501);
        list.addAirplane("Velvett",501);
        list.addAirplane("Paul",501);

        //Method sort the list after year the car was made
        list.sortList();
        menu();


        //Method to print all objects in List
        System.out.println(list.viewAll());

    }


    public static void menu(){
        int menuOpt;

        System.out.println("Airline Menu:");
        System.out.println("1. Reserve a Ticket");
        System.out.println("2. Cancel Reservations");
        System.out.println("3. Check Reservations");
        System.out.println("4. Display Airplanes on Flights");
        Scanner input = new Scanner(System.in);
        menuOpt=input.nextInt();
        System.out.println(menuOpt);


        switch (menuOpt){
        case 1:
            System.out.println("Reserve a Ticket");
            reserveTick();
            break;

        case 2:
            System.out.println("Cancel Reservations");
            cancel();
            break;

        case 3:
            System.out.println("Check Reservations");
            check();
            break;

        case 4:
            System.out.println("Passengers listed by Flights");
            break;

        default:
            System.out.println("INVALID RESPONSE!");
            menu();
            break;
        }
    }

    public static void reserveTick(){
        String name;
        System.out.println("Enter Customer Name to RESERVE ticket for this Flight: ");
        Scanner input = new Scanner(System.in);
        name = input.nextLine();
        list.addAirplane(name,501);
        System.out.println(name + " has been added to Flight Number 501");
        menu();
    }

    public static void cancel(){
        String name;
        System.out.println("Enter Customer Name to CANCEL ticket for this Flight: ");
        Scanner input = new Scanner(System.in);
        name = input.nextLine();

        list.remove(name, 501);
        System.out.println(name + " has been REMOVED from Flight Number 501");
        menu();
    }

    public static void check(){
        String name;
        System.out.println("Enter Customer Name to CHECK RESERVATION ticket for this Flight: ");
        Scanner input = new Scanner(System.in);
        name = input.nextLine();
        if (list.contains(name)) {
            System.out.println(name +" has a Reservation on this FLight!");
            menu();
        }
        else {
            System.out.println(name + " is not on this Flight!");
            menu();
        }
    }
    public static void listpassengers(){
        list.sortList();
    }

}

--------------------------------------------------开发者_如何学编程----------------



import java.util.*;
public class LinkedList
{

    private AirplaneNode head = null;

    public void addAirplane(String name , int hk)
    {    
        //If head = null then create the first node
        if(head == null)
        {
            head = new AirplaneNode(name,hk,null);
        }
        else
        {
            //If there are more than 1 node
            head = new AirplaneNode(name,hk,head);            
        }

    }


    public void sortList()
    {

        boolean sorted = false;

        while(!sorted)
        {

            sorted = true;

            for(AirplaneNode cursor = head ; cursor.getNext() != null ; cursor = cursor.getNext())
            {
                if(cursor.getHk() < cursor.getNext().getHk())
                {
                    String n = cursor.getName();
                    int hk = cursor.getHk();

                    cursor.setName(cursor.getNext().getName());
                    cursor.setHk(cursor.getNext().getHk());

                    cursor.getNext().setName(n);
                    cursor.getNext().setHk(hk);

                    sorted = false;        
                }


            }


        }



    }


    public String viewAll()
    {

        StringBuffer str = new StringBuffer();    

        for(AirplaneNode cursor = head ; cursor != null ; cursor = cursor.getNext())
        {

            str.append(cursor+"\n");
        }
        return new String(str);

    }    
}
--------------------------------------------------------------
public class AirplaneNode
{
    private String name;
    private int hk;
    private AirplaneNode next;

    public AirplaneNode(String name,int hk,AirplaneNode head)
    {
        this.name = name;
        this.hk = hk;
        this.next = head;

    }



    public AirplaneNode getNext()
    {
        return next;
    }

    public String getName()
    {
        return name;
    }

    public int getHk()
    {
        return hk;
    }

    public void setName(String in)
    {
        name = in;
    }

    public void setHk(int in)
    {
        hk = in;
    }


    public String toString()
    {
        return name + " " + hk ;
    }

}


It seems as if you are creating a own class LinkedList in the top package here:

import java.util.*;
public class LinkedList
{

Since your method check() belongs in the airline class in the same package (and without any import of java.util.LinkedList) it will instead use the class you have created and that class doesn't implement any contains() method.


Declare your linked list this way:

public static LinkedList<String> list = new LinkedList<String>();

EDIT (based on your comment):

It looks like you want a list of flights, where each flight has a list of passenger names.

public class Flight implements Comparable<Flight> {
    private List<String> mPassengers;
    private final int mFlight;
    private static final Collator sCollator = Collator.getInstance();
    public Flight(int flight) {
        mPassengers = new ArrayList<String>();
        mFlight = flight;
    }
    public void sortPassengers() {
        Collections.sort(mPassengers, sCollator);
    }
    public void addPassenger(String name) {
        mPassengers.add(name);
    }
    public boolean removePassenger(String name) {
        return mPassengers.remove(name);
    }
    public boolean hasPassenger(String name) {
        return mPassengers.contains(name);
    }
    public String getFlight() { return mFlight; }
    public int compareTo(Flight other) {
        return mFlight - other.mFlight;
    }
}

public static List<Flight> list = new LinkedList<Flight>();
public static void main(String[] args) {
    Flight flight = new Flight(501);
    flight.addPassenger("Allen");
    // etc. for all flight 501 passengers
    list.add(flight);

    // repeat all the above for each flight number
}

You should be able to fill in the rest.

0

精彩评论

暂无评论...
验证码 换一张
取 消