3

I want to display a list of orders of type ArrayQueue <Order> The class Order has an ArrayStack<String> as one of its attributes. I overrode the toString() method in the class Order, but how do I override it in the ArrayStack class? Because this is the output I get when I display:

OrderNumber Name Date ArrayStack@481adc30

What would I have to do to display the Strings in ArrayStack correctly? Do I make changes to class ArrayStack or change something in my Display method?

This is my Display method:

 public void display(){
    if (!isEmpty())
    for (int i = 0; i < numberOfEntries; i++) {
        System.out.println(queue[(frontIndex + i) % queue.length]);
    }
    else System.out.println("You don't have any orders");
    }

ArrayStack Class:

 public class ArrayStack < T > implements StackInterface < T >
{
    private T [] stack; // array of stack entries

    private int topIndex; // index of top entry

    private static final int DEFAULT_INITIAL_CAPACITY = 50;

    public ArrayStack ()
    {
        this (DEFAULT_INITIAL_CAPACITY);
    } // end default constructor


    public ArrayStack (int initialCapacity)
    {
        // the cast is safe because the new array contains null entries
        @ SuppressWarnings ("unchecked")
            T [] tempStack = (T []) new Object [initialCapacity];
        stack = tempStack;
        topIndex = -1;
    } // end constructor

    /*  Implementations of the stack operations */

Order Class:

   import java.util.Date;


public class Order {

    int orderNumber;
    String customerName;
    Date date;
    StackInterface <String> items;

Order( int number, String name, Date datum, StackInterface<String> item){
    orderNumber = number;
    customerName= name;
    date= datum;
    items = item;   
}

/Overriding toString() to Display a list of Orders as one String line. 
public String toString(){
    return orderNumber + " " + customerName + " " + date + " " + items;
}
helloworld
  • 173
  • 1
  • 2
  • 8

3 Answers3

2

You can override toString() method in ArrayStack as shown here. This will solve your problem.

public String toString() {
    String result = "";

    for (int scan = 0; scan < top; scan++)
        result = result + stack[scan].toString() + "\n";

    return result;
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • Yaay! It kinda worked! but it displayed only the first item in the stack. Why? could it be a mistake in the way I added items to the stack? `int no = input.nextInt(); StackInterface items = new ArrayStack(no); for(int j= 1; j<= no; j++){ System.out.println("Enter item no: " + j ); String theItem = input.next(); items.push(theItem); } Order order1 = new Order (orderno, customername, real_date, items); orders.enqueue(order1);` – helloworld Apr 17 '15 at 10:01
  • Please put it into your question, it is difficult to read code in comments. – Naman Gala Apr 17 '15 at 10:02
  • Show the `push` method as well as `enqueue` method. – Naman Gala Apr 17 '15 at 10:04
  • Nevermind. It worked now! It wasn't printing the last item but I changed `scan < top` to `scan<=top` and it works perfect now. Thank you so much! :) – helloworld Apr 17 '15 at 10:06
0

May be you should do this:

System.out.println(Arrays.toString(queue.toArray()));
Master Mind
  • 3,014
  • 4
  • 32
  • 63
  • where would I add this? – helloworld Apr 17 '15 at 09:31
  • you would add it to your in place of `for (int i = 0; i < numberOfEntries; i++) { System.out.println(queue[(frontIndex + i) % queue.length]); }` – Master Mind Apr 17 '15 at 09:35
  • That would still print the ArrayStack attribute inside the ArrayQueue as ArrayStack@481adc30 and it will also display nulls if I have only one object in my Queue – helloworld Apr 17 '15 at 09:43
0

Use this:

System.out.println(Arrays.toString(queue));
mel3kings
  • 8,857
  • 3
  • 60
  • 68