-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.java
More file actions
33 lines (32 loc) · 1.08 KB
/
book.java
File metadata and controls
33 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Stack;
public class Book {
String title;
String author;
double price;
public Book(String title, String author, double price) {
this.title = title;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return title + " by " + author + " (Rs " + price + ")";
}
public static void main(String[] args) {
Stack<Book> S = new Stack<>();
S.push(new Book("The White Tiger", "Aravind Adiga", 499));
S.push(new Book("The God of Small Things", "Arundhati Roy", 350));
S.push(new Book("Train to Pakistan", "Khushwant Singh", 350));
System.out.println("Books in stack:");
for (Book b : S) {
System.out.println(b);
}
System.out.println("\nTop book (peek): " + S.peek());
System.out.println("\nPopping top book: " + S.pop());
System.out.println("\nBooks after pop:");
for (Book b : S) {
System.out.println(b);
}
System.out.println("\nIs stack empty? " + S.isEmpty());
}
}