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();
}
Я пытаюсь взять вход и проверить, находится ли этот вход в связанном списке.У меня проблемы, хотя заставить его работать правильно.
Если я добавляю новый метод в мой класс LinkedList.Java, он говорит, что ему нужно определить переменную для ссылки.Ниже приводится то, что я имею в целом, если это помогает:
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 ;
}
}