Created
November 26, 2012 05:19
-
-
Save ayato-p/4146714 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
public interface Aggregate { | |
public abstract Iterator<Book> iterator(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String name; | |
public Book(String name) { | |
this.name = name; | |
} | |
public String getName(){ | |
return this.name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
public class BookShelf implements Aggregate{ | |
private Book[] books; | |
private int last = 0; | |
public BookShelf(int maxsize) { | |
this.books = new Book[maxsize]; | |
} | |
public Book getBookAt(int index){ | |
return books[index]; | |
} | |
public void appendBook(Book book){ | |
this.books[last] = book; | |
last++; | |
} | |
public int getLength(){ | |
return last; | |
} | |
public Iterator<Book> iterator(){ | |
return new BookShelfIterator(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
public class BookShelfIterator implements Iterator<Book> { | |
private BookShelf bookShelf; | |
private int index; | |
public BookShelfIterator(BookShelf bookShelf) { | |
this.bookShelf = bookShelf; | |
this.index = 0; | |
} | |
@Override | |
public boolean hasNext() { | |
if(index < bookShelf.getLength()){ | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public Book next() { | |
Book book = bookShelf.getBookAt(index); | |
index++; | |
return book; | |
} | |
@Override | |
public void remove() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment