Reputation: 5
For this project, I can remove only the top one from the pile, and cannot remove a book from under another one. Likewise, I cannot add a book beneath another one. I can add another book to the pile only by placing it on the top of the pile. In my code, I removed Book E from the pile, but now I want to add a different book. How to add a new book (Book: F) to myBooks and print the list? There is a link to the screenshot of my current output.
public class Driver
{
public static void main(String[] args)
{
Book[] myBooks = { new Book("A"), new Book("B"), new Book("C"), new Book("D"), new Book("E")};
PileOfBooksInterface<Book> bookPiles = new PileOfBooks<>();
System.out.println("Are there any books in the pile? " + bookPiles.isEmpty());
for (int index = 0; index < myBooks.length; index++)
{
Book nextItem = myBooks[index];
bookPiles.add(nextItem);
} // end for
System.out.println("\nTotal books in the pile:");
for (int index = 0; index < myBooks.length; index++)
{
System.out.println(myBooks[index]);
} // end for
System.out.println("\nRemoving the last book:");
bookPiles.remove();
Object[] arr = (bookPiles.toArray());
for (int index = 0; index < arr.length; index++)
{
System.out.println(arr[index]);
} // end for
System.out.println("\nAdding new book on top of the pile:");
// ???
}
}
Upvotes: 0
Views: 376
Reputation: 16
It's simple. You can use ArrayList to store all your books and java have pretty nice inbuilt methods for ArrayList. You can do something as below:
public class Driver
{
public static void main(String[] args)
{
Book[] myBooks = { new Book("A"), new Book("B"), new Book("C"), new Book("D"), new Book("E")};
ArrayList<Book> bookPiles = new ArrayList<Book>();
System.out.println("Are there any books in the pile? " + bookPiles.isEmpty());
for (int index = 0; index < myBooks.size(); index++)
{
Book nextItem = myBooks[index];
bookPiles.add(nextItem);//populating ArrayList
} // end for
System.out.println("\nTotal books in the pile:");
for (int index = 0; index < myBooks.size(); index++)
{
System.out.println(bookPiles.get(index));
} // end for
System.out.println("\nRemoving the last book:");
bookPiles.remove(bookPiles.size()-1); //removing the last element from the bookPile ArrayList
//Object[] arr = (bookPiles.toArray()); //No need for this line
for (int index = 0; index < bookPiles.size(); index++)
{
System.out.println(bookPiles.get(index));
} // end for
System.out.println("\nAdding new book on top of the pile:");
// you can write the code for adding at the top of the pile as follows:
bookPiles.add(0,new Book("E"));//0 is the first position in the ArrayList where we want to add data and all the elements will be shifted automatically.
//the above line will add the new book at the top of the pile.
for (int index = 0; index < bookPiles.size(); index++)
{
System.out.println(bookPiles.get(index)); //print all the books along with the new one
}
}
}
Hope this helps you.
Upvotes: 0
Reputation: 428
With a stack you no longer need a pile of books class and will work the way you expect it to.
public class Driver {
public static void main(String[] args) {
Stack<Book> bookPiles = Arrays.asList(new Book("A"), new Book("B"), new Book("C"), new Book("D"), new Book("E")).stream()
.collect(Collectors.toCollection(Stack::new));
System.out.println("Are there any books in the pile? " + !bookPiles.isEmpty());
System.out.println("\nTotal books in the pile:");
bookPiles.stream().forEach(System.out::println);
System.out.println("\nRemoving the last book:");
bookPiles.pop();
bookPiles.stream().forEach(System.out::println);
System.out.println("\nAdding new book on top of the pile:");
bookPiles.push(new Book("F"));
bookPiles.stream().forEach(System.out::println);
}
}
If PileOfBooks is already written and is known to work as you describe, then you would only need to add a book to the PileOfBooks like you did when adding the books to begin with.
bookPiles.add(new Book("F"));
Upvotes: 1