Queue

ami0714
ami0714 used Ask the Experts™
on
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
?

Author

Commented:

Author

Commented:
Oh my...my question gone
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
CEHJ,

I'm not sure how to modify the code so that this CustomerOrder thing will work out. Perhaps you can give me some clues after seeing my code.

//QueueReference(currently only queue for Customer)
public class QueueReference implements QueueInterface{

     private Customer front;
     private Customer rear;
     
     public QueueReference(){
          front=null;
          rear=null;
     }

     public boolean isEmpty(){
          return front==null;
     }
     
     public void dequeueAll(){
          front=null;
     }

     public void enqueue(Customer newCustomer){
          Customer newCus = newCustomer;
         
          if(isEmpty()){
               front = newCus;
               rear = newCus;
               
          }
          else{
               rear.setNext(newCus);
               rear = newCus;
               
          }
     }

     public Customer dequeue()throws QueueException{
          if(!isEmpty())
          {
               Customer deqCus = front;
               if(front==rear)
               {
                    front=null;
                    rear=null;
               }
               else
               {
                    front=front.getNext();
               }
               return deqCus;
          }
          else{
               throw new QueueException("Dequeue:Queue is empty");
          }
     }

     public Customer peek()throws QueueException{
          if(!isEmpty()){
               return front;
          }
          else{
               throw new QueueException("Peek: Queue is empty");
          }
     }

}

Author

Commented:
//Customer
public class Customer  {

     private String cusName;
     private String cusAddress;
     private int cusID;
     private String cusTel;
     private int deposit;
     private Customer next;

     public Customer(){
          cusName="";
          cusAddress="";
          cusID=0;
          cusTel="";
          deposit=0;
         
     }

      public void setCusName(String name){
          cusName=name;
     }

      public void setCusAddress(String address){
          cusAddress=address;
     }

      public void setCusID(int ID){
          cusID=ID;
     }
      public void setCusTel(String tel){
          cusTel=tel;
     }

      public void setDeposit(int dep){
          deposit=dep;
     }

      public void setNext(Customer Cus){
          next=Cus;
     }
     
      String getCusName(){
          return cusName;
     }

      String getCusAddress(){
          return cusAddress;
     }

      int getCusID(){
          return cusID;
     }

     String getCusTel(){
          return cusTel;
     }

      int getDeposit(){
          return deposit;
     }
     
      Customer getNext(){
          return next;
     }
}

//Book
public class Book{
     
     private String bookName;
     private String author;
     private String publisher;    
     private int bookID;
     private String status;
     private Book next;

     public Book(){
          bookName="";
          author="";
          publisher="";
          bookID=0;
     }

     public void setBookName(String bookname){
          bookName=bookname;
     }
     
     public void setAuthor(String aut){
          author=aut;
     }

     public void setPublisher(String pub){
          publisher=pub;
     }

     public void setBookID(int ID){
          bookID=ID;
     }

     public void setStatus(String stat){
          status=stat;
     }

     public void setNext(Book Bk){
          next=Bk;
     }

     String getBookName(){
          return bookName;
     }

     String getAuthor(){
          return author;
     }

     String getPublisher(){
          return publisher;
     }

      int getBookID(){
          return bookID;
     }
     
     String getStatus(String stat){
          return status;
     }

     Book getNext(){
          return next;
     }
}

Author

Commented:
Sorry for have to post it in 2 part
Top Expert 2016

Commented:
Simply do a find/replace on your code, replacing Customer in the queue class by CustomerOrder. The customer order is created with an instance of Customer and an instance of Book

Author

Commented:
Should I make both Customer & Book extends the CustomerOrder, like this


public class Book extends CustomerOrder{}

public class Customer extends CustomerOrder{}

   
   
Top Expert 2016

Commented:
No. CustomerOrder is just a container for a Book and a Customer.

Author

Commented:
So both the Customer class and Book class are remain the same, what about the main program like the one I post earlier,

try{
            readFile=input.readLine();
            while(readFile!=null) {
                             
          name=  input.readLine();
             address = input.readLine();
             readFile=input.readLine();
              ID = Integer.valueOf(readFile).intValue();
              tel = input.readLine();
          readFile=input.readLine();
             dep = Integer.valueOf(readFile).intValue();

            Customer readCus = new Customer();
            readCus.setCusName(name);
            readCus.setCusAddress(address);
            readCus.setCusID(ID);
            readCus.setCusTel(tel);
           readCus.setDeposit(dep);
           menu.enqueue(readCus);
           readFile=input.readLine();
            }
            inputFile.close();    
}

Should I change this

Customer readCus = new Customer() ==> CustomerOrder readCus = new CustomerOrder()

or keep it in the same way?
Top Expert 2016

Commented:
You should change

menu.enqueue(readCus);


to

menu.enqueue(new CustomerOrder(readCus,book));

but the problem is - where is your Book?

Author

Commented:
That part of codes is only  about Customer, I have other parts concern about both objects.

Now I have problem in peek() & dequeue().

 Customer C = menu.dequeue();
Customer C = menu.peek();

it says incompatible types. Could you help me change it too?
Top Expert 2016

Commented:
CustomerOrder co = menu.dequeue();
CustomerOrder co = menu.peek();

Author

Commented:
This time it says cannot resolve symbol.

here's one of the error.
Menu.java:302: cannot resolve symbol
symbol  : method getCusAddress  ()
location: class CustomerOrder
                                output.println(C.getCusAddress());
                                                ^

Author

Commented:
oops...the error should look like this one

Menu.java:302: cannot resolve symbol
symbol  : method getCusAddress  ()
location: class CustomerOrder
                      output.println(C.getCusAddress());
                                      ^

Author

Commented:
output.println(C.getCusAddress());
                ^

Top Expert 2016
Commented:
output.println(C.getCustomer().getCusAddress());

Don't forget, your Customer is wrapped inside the CustomerOrder.
That is, i hope you wrote getCustomer() and getBook() methods in CustomerOrder...

Incidentally, i think part of your code has been written by another expert, but you only need to have methods called:

getName()
getAddress()

etc. in Customer. The 'getCus' bit is redundant.

Author

Commented:
urgh...

the arrows points to the dot in C.getCusAddress()
               
Top Expert 2016

Commented:
Well i'm taking it that 'C' is variable holding a reference to a CustomerOrder object. Is that right?

Author

Commented:
Great, it works now. you have end my suffer, another one. thanks pal! hohoho...

since the solution comes in many part of the comment, so I'll accept one of the comment as answer.
Top Expert 2016

Commented:
Glad you've got it sorted out. For good coding practices, this cheap little pocket book is valuable:
http://www.amazon.co.uk/exec/obidos/ASIN/0521777682/proteanit-20

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial