2017-04-03 316 views
0

你好,我想我真的很難與這部分。我正在製作一個程序,它接受來自用戶的名字和姓氏的值,然後對其進行排隊(CASE1)。然後能夠將其出列(CASE2)並最終顯示列表上的所有內容。在IDE上沒有錯誤,但我不能得到我想要的結果,在CASE 2中它引發異常錯誤,意味着傳遞給它的列表爲空。在CASE 3中沒有顯示任何值。我該如何解決?Java隊列,出列隊列和列隊隊列/出隊列表中的所有值

import java.util.*; 
import java.util.Iterator; 

class Customer2 { 
    public String lastName; 
    public String firstName; 
    public Customer2() { 
    } 
    public Customer2(String last, String first) { 
     this.lastName = last; 
     this.firstName = first; 
    } 
    public String toString() { 
     return firstName + " " + lastName; 
    } 
} 
class HourlyCustomer2 extends Customer2 { 
    public double hourlyRate; 
    public HourlyCustomer2(String last, String first) { 
     super(last, first); 
    } 
} 



class Queue1<E> { 

    private LinkedList<E> list = new LinkedList<E>(); 

    public void enqueue(E item) { 
     list.addLast(item); 
    } 

    public E dequeue() { 

     // return a Customer2 with null values if empty? (up to you) 

      return list.remove(0); 
    } 

    public E isNotEnd(){ 

     return list.getLast(); 
    } 

    public boolean hasItems() { 

     return !list.isEmpty(); 
    } 

    public boolean isEmpty() { 

     return list.isEmpty(); 
    } 

    public Iterator<E> iterator() { 

     return list.iterator(); 
    } 

    public E removeFirst() { 

     return list.removeFirst(); 
    } 

    public E getFirst() { 

     return list.getFirst(); 
    } 

    public int size() { 

     return list.size(); 
    } 

    public boolean hasNext() { 

     return false; 
    } 

    public void addItems(Queue1<? extends E> q) { 

     while (q.hasNext()) list.addLast(q.dequeue()); 
    } 
} 


public class something { 

    public static void main(String[] args) { 

     Scanner sc = new Scanner(System.in); 
     String input1; 
     String input2; 
     int choice = 1000; 

     Queue1<Customer2> empList; 
     empList = new Queue1<Customer2>(); 
     Queue1<HourlyCustomer2> hList; 
     hList = new Queue1<HourlyCustomer2>(); 

     do { 

      System.out.println("================"); 
      System.out.println("Queue Operations Menu"); 
      System.out.println("================"); 
      System.out.println("1,Enquene"); 
      System.out.println("2,Dequeue"); 
      System.out.println("3,View queue"); 
      System.out.println("0, Quit\n"); 
      System.out.println("Enter Choice:"); 

      try { 

       choice = sc.nextInt(); 

       switch(choice) { 

        case 1: 

         System.out.println("\nPlease enter last name: "); 
         input1 = sc.next(); 
         System.out.println("\nPlease enter first name: "); 
         input2 = sc.next(); 
         hList.enqueue(new HourlyCustomer2(input1, input2)); 
         empList.addItems(hList); 

         System.out.println("\n"+(input2 + " " + input1) + " is successful queued"); 

         break; 

        case 2: 

         if (empList.isEmpty()) { 
          System.out.println("The queue is empty!"); 
         } 
         else 
         { 
         System.out.println("\nDequeued customer: " +empList.getFirst()); 
         empList.removeFirst(); 
         } 


         System.out.println("\nNext customer in queue: " +empList.getFirst()+"\n"); 

         break; 

        case 3: 

         System.out.println("\nThe Customer's names are: \n"); 

         Iterator<Customer2> it = empList.iterator(); 

         while (it.hasNext()) { 

           System.out.println("\nThe customers' names are: \n"); 
         } 

         break; 

        case 0: 

         System.exit(0); 

        default: 

         System.out.println("Invalid choice"); 
       } 
      } 
      catch(InputMismatchException e) { 

       System.out.println("Please enter 1-5, 0 to quit"); 
       sc.nextLine(); 
      } 

     } while(choice != 0); 
    } 
} 

回答

0

看看你的自定義隊列;你忘了把邏輯放在你的hasNext方法中。它將始終返回false。因此,您的addItems循環永遠不會將任何項目添加到隊列中。

如果您需要關於如何執行hasNext()的提示,我的建議是查看列表的大小並進行簡單的比較。

public boolean hasNext() { 

    return false; 
} 

public void addItems(Queue1<? extends E> q) { 

    while (q.hasNext()) list.addLast(q.dequeue()); 
} 

PS:我注意到你的代碼也顯示了下一行。您可能需要檢查它是否爲空或hasNext(),如果在出列之後列表爲空,則不會發生崩潰。

+0

你認爲我應該放入我的hasNext()方法嗎? – Rekt

+0

@Rekt剛剛編輯帖子,以幫助完成。 –