Formatted all files to the same standard
This commit is contained in:
@ -1,41 +1,41 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* Iterator (ItemIterator) adds abstraction layer on top of a collection
|
||||
* (TreasureChest). This way the collection can change its internal
|
||||
* implementation without affecting its clients.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreasureChest chest = new TreasureChest();
|
||||
public static void main(String[] args) {
|
||||
TreasureChest chest = new TreasureChest();
|
||||
|
||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||
while (ringIterator.hasNext()) {
|
||||
System.out.println(ringIterator.next());
|
||||
}
|
||||
ItemIterator ringIterator = chest.Iterator(ItemType.RING);
|
||||
while (ringIterator.hasNext()) {
|
||||
System.out.println(ringIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||
while (potionIterator.hasNext()) {
|
||||
System.out.println(potionIterator.next());
|
||||
}
|
||||
ItemIterator potionIterator = chest.Iterator(ItemType.POTION);
|
||||
while (potionIterator.hasNext()) {
|
||||
System.out.println(potionIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||
while (weaponIterator.hasNext()) {
|
||||
System.out.println(weaponIterator.next());
|
||||
}
|
||||
ItemIterator weaponIterator = chest.Iterator(ItemType.WEAPON);
|
||||
while (weaponIterator.hasNext()) {
|
||||
System.out.println(weaponIterator.next());
|
||||
}
|
||||
|
||||
System.out.println("----------");
|
||||
System.out.println("----------");
|
||||
|
||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
}
|
||||
ItemIterator it = chest.Iterator(ItemType.ANY);
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ public class Item {
|
||||
this.setType(type);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
|
@ -3,11 +3,11 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Iterator interface.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface ItemIterator {
|
||||
|
||||
boolean hasNext();
|
||||
|
||||
|
||||
Item next();
|
||||
}
|
||||
|
@ -2,9 +2,6 @@ package com.iluwatar;
|
||||
|
||||
public enum ItemType {
|
||||
|
||||
ANY,
|
||||
WEAPON,
|
||||
RING,
|
||||
POTION
|
||||
|
||||
ANY, WEAPON, RING, POTION
|
||||
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ import java.util.List;
|
||||
/**
|
||||
*
|
||||
* Collection class.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class TreasureChest {
|
||||
|
||||
private List<Item> items;
|
||||
|
||||
|
||||
public TreasureChest() {
|
||||
items = new ArrayList<>();
|
||||
items.add(new Item(ItemType.POTION, "Potion of courage"));
|
||||
@ -25,7 +25,7 @@ public class TreasureChest {
|
||||
items.add(new Item(ItemType.WEAPON, "Steel halberd"));
|
||||
items.add(new Item(ItemType.WEAPON, "Dagger of poison"));
|
||||
}
|
||||
|
||||
|
||||
ItemIterator Iterator(ItemType type) {
|
||||
return new TreasureChestItemIterator(this, type);
|
||||
}
|
||||
@ -35,5 +35,5 @@ public class TreasureChest {
|
||||
list.addAll(items);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
this.type = type;
|
||||
this.idx = -1;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return findNextIdx() != -1;
|
||||
@ -27,9 +27,9 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private int findNextIdx() {
|
||||
|
||||
|
||||
List<Item> items = chest.getItems();
|
||||
boolean found = false;
|
||||
int tempIdx = idx;
|
||||
@ -39,7 +39,8 @@ public class TreasureChestItemIterator implements ItemIterator {
|
||||
tempIdx = -1;
|
||||
break;
|
||||
}
|
||||
if (type.equals(ItemType.ANY) || items.get(tempIdx).getType().equals(type)) {
|
||||
if (type.equals(ItemType.ANY)
|
||||
|| items.get(tempIdx).getType().equals(type)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user