package com.maom.design.D_20_Iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class D_20_Iterator {
public static void main(String[] args) {
BookList bookList = new BookList();
Book book1 = new Book("010203", "Java编程思想", 90);
Book book2 = new Book("010204", "Java从入门到精通", 60);
bookList.addBook(book1);
bookList.addBook(book2);
Iterator iter = bookList.Iterator();
while (iter.hasNext()) {
Book book = (Book) iter.next();
book.display();
}
}
}
class Book {
private String ISBN;
private String name;
private double price;
public Book(String isbn, String name, double price) {
ISBN = isbn;
this.name = name;
this.price = price;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String isbn) {
ISBN = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public void display() {
System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
}
}
class BookList {
private List<Book> bookList;
private int index;
private Iterator iterator;
public BookList() {
bookList = new ArrayList<Book>();
}
public void addBook(Book book) {
bookList.add(book);
}
public void deleteBook(Book book) {
int bookIndex = bookList.indexOf(book);
bookList.remove(bookIndex);
}
public Iterator Iterator() {
return new Itr();
}
private class Itr implements Iterator {
public boolean hasNext() {
if (index >= bookList.size()) {
return false;
}
return true;
}
public Object next() {
return bookList.get(index++);
}
public void remove() {
}
}
}