Account.java
public class Account {
String customerName;
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
int accountNo;
public Account() {
// TODO Auto-generated constructor stub
}
public Account(String customerName, int accNo){
this.customerName=customerName;
this.accountNo=accNo;
}
public String toString(){
return "[Account Holder Name : "+customerName+", Account No : "+accountNo+"]";
}
}
AccountList.java
import java.util.ArrayList;
public class AccountList {
private ArrayList accountList ;
public AccountList() {
// TODO Auto-generated constructor stub
this.accountList=new ArrayList();
}
public void push(Account account){
this.accountList.add(account);
}
public Account get(int index){
return accountList.get(index);
}
public int getSize(){
return accountList.size();
}
}
AccountListIterator.java
public class AccountListIterator {
private int index;
AccountList accountList;
public AccountListIterator(AccountList accList) {
this.accountList=accList;
index=0;
}
public Account next(){
return accountList.get(index++);
}
public boolean hasNext(){
return index<accountList.getSize();
}
}
IteratorPatternDemo.java
public class IteratorPatternDemo {
public IteratorPatternDemo() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Account acc1 = new Account("Jany Jose", 123);
Account acc2 = new Account("Hana Mose", 223);
Account acc3 = new Account("Tina Bose", 323);
Account acc4 = new Account("Mina Kose", 423);
AccountList accList = new AccountList();
accList.push(acc1);
accList.push(acc2);
accList.push(acc3);
accList.push(acc4);
AccountListIterator itr = new AccountListIterator(accList);
while(itr.hasNext()){
System.out.println("Account : "+itr.next());
}
}
}
Output
