2017-04-03 108 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刚刚编辑帖子,以帮助完成。 –