linked list - Java Print Everything in Queue Using LinkedList and The Deque Interfaces -
i'm little stuck here, need helpm here's code:
import java.util.*; import java.util.iterator; class customer { public string lastname; public string firstname; public customer() { } public customer(string last, string first) { this.lastname = last; this.firstname = first; } public string tostring() { return firstname + " " + lastname; } } class hourlycustomer extends customer { public double hourlyrate; public hourlycustomer(string last, string first) { super(last, first); } } class genqueue<e> { private linkedlist<e> list = new linkedlist<e>(); public listiterator<e> iterator = list.listiterator(); public void enqueue(e item) { list.addlast(item); } public e dequeue() { return list.poll(); } public e show(){ return list.peek(); } public void printqueueelements(){ } public e isnotend(){ return list.getlast(); } public boolean hasitems() { return !list.isempty(); } public boolean isempty() { return list.isempty(); } public iterator<e> iterator() { return 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(genqueue<? extends e> q) { while (q.hasnext()) list.addlast(q.dequeue()); } } public class jerald { public static void main(string[] args){ scanner sc = new scanner(system.in); string input1; string input2; int choice = 1000; genqueue<customer> emplist; emplist = new genqueue<customer>(); genqueue<hourlycustomer> hlist; hlist = new genqueue<hourlycustomer>(); while(true){ 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("0, quit\n"); system.out.println("enter choice:"); try{ choice = sc.nextint(); switch(choice){ case 1: do{ system.out.println("\nplease enter last name: "); input1 = sc.next(); system.out.println("\nplease enter first name: "); input2 = sc.next(); hlist.enqueue(new hourlycustomer(input1, input2)); emplist.additems(hlist); system.out.println("\n"+(input2 + " " + input1) + " successful queued"); system.out.println("\ndo still want enqueue?<1> or want view in queue?<0> or \nback main menu dequeueing?<menu>: "); choice = sc.nextint(); }while (choice != 0); system.out.println("\nthe customers' names are: \n"); int numberofelements = emplist.size(); (int = 0; < numberofelements; i++) { customer emp = emplist.dequeue(); system.out.println(emp.firstname + " " + emp.lastname + "\n"); emplist.enqueue(emp); } break; case 2: if (emplist.isempty()) { system.out.println("the queue empty!"); } else { system.out.println("\ndequeued customer: " +emplist.getfirst()); emplist.removefirst(); } if (emplist.isempty()) { system.out.println("the queue empty!"); } else { system.out.println("\nnext customer in queue: " +emplist.getfirst()+"\n"); } break; case 0: system.exit(0); default: system.out.println("invalid choice"); } } catch(inputmismatchexception e){ system.out.println("please enter 1-5, 0 quit"); sc.nextline(); } }while(choice != 0); } } }
look @ case 1, in loop im trying print have been saved in queue. dont see errors unfortuantely when run it,after getting users input, ending do-while loop, doesnt print whats inside queue. problem method? in deque methods have tested poll() element() , peek()
bu didnt either. anyway i'm stuck here , cant move forward, don't know problem is, reread , reread code think flowing enough. thoughts on this? how fix it? asked question after trying fix error no luck. please share thoughts , fix.
i think loop in line 155, in case 1, giving wrong output. thoughts?
by way i'm doing in case 2, dequeueing,
the problem when calling additems method:
emplist.additems(hlist);
i don't know why hasnext
method used additems
returning false? not make sense, need modify logic inside it. result of being false, code inside while loop never called.
public void additems(genqueue<? extends e> q) { while (q.hasnext()) list.addlast(q.dequeue()); }
Comments
Post a Comment