Formatted all files to the same standard

This commit is contained in:
matthew
2014-10-08 13:42:12 +01:00
parent 53a2a8b150
commit 3da9ad5469
151 changed files with 952 additions and 870 deletions

View File

@ -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());
}
}
}

View File

@ -9,7 +9,7 @@ public class Item {
this.setType(type);
this.name = name;
}
@Override
public String toString() {
return name;

View File

@ -3,11 +3,11 @@ package com.iluwatar;
/**
*
* Iterator interface.
*
*
*/
public interface ItemIterator {
boolean hasNext();
Item next();
}

View File

@ -2,9 +2,6 @@ package com.iluwatar;
public enum ItemType {
ANY,
WEAPON,
RING,
POTION
ANY, WEAPON, RING, POTION
}

View File

@ -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;
}
}

View File

@ -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;
}
}