Skip to content

Commit 2f1c2cd

Browse files
Implementing Iterator Design Pattern
1 parent 7014637 commit 2f1c2cd

File tree

10 files changed

+194
-0
lines changed

10 files changed

+194
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package iterator;
2+
3+
4+
public class AppleMobilesCollection implements Collection {
5+
private int index;
6+
Mobile appleMobiles[];
7+
8+
9+
public AppleMobilesCollection(int size) {
10+
this.appleMobiles = new Mobile[size];
11+
index = 0;
12+
}
13+
14+
@Override
15+
public Iterator createIterator() {
16+
return new AppleMobilesIterator(this.appleMobiles);
17+
}
18+
19+
@Override
20+
public void add(Item item) {
21+
this.appleMobiles[index++] = (Mobile) item;
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package iterator;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.List;
5+
6+
public class AppleMobilesIterator implements Iterator {
7+
private int pos;
8+
private Mobile mobiles[];
9+
10+
public AppleMobilesIterator(Mobile[] mobiles) {
11+
pos = 0;
12+
this.mobiles = mobiles;
13+
}
14+
15+
@Override
16+
public boolean hasNext() {
17+
if (pos >= this.mobiles.length || this.mobiles[pos] == null)
18+
return false;
19+
return true;
20+
}
21+
22+
@Override
23+
public Mobile next() {
24+
return mobiles[this.pos++];
25+
}
26+
27+
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package iterator;
2+
3+
public interface Collection {
4+
Iterator createIterator();
5+
6+
void add(Item item);
7+
}

src/main/java/iterator/Item.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package iterator;
2+
3+
public interface Item {
4+
}

src/main/java/iterator/Iterator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package iterator;
2+
3+
import java.util.Objects;
4+
5+
public interface Iterator {
6+
boolean hasNext();
7+
Object next();
8+
}

src/main/java/iterator/Mobile.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package iterator;
2+
3+
public class Mobile implements Item {
4+
private String id;
5+
private String name;
6+
private int quantity;
7+
private double price;
8+
9+
public Mobile(String id, String name, int quantity, double price) {
10+
this.id = id;
11+
this.name = name;
12+
this.quantity = quantity;
13+
this.price = price;
14+
}
15+
16+
public double totalCost() {
17+
return this.price * this.quantity;
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package iterator;
2+
3+
public class MobileStore {
4+
5+
public double getTotalInventoryCost(Collection mobilesCollection) {
6+
Iterator mobilesIterator = mobilesCollection.createIterator();
7+
double totalCost = 0;
8+
while (mobilesIterator.hasNext()) {
9+
var mobile = (Mobile) (mobilesIterator.next());
10+
totalCost += mobile.totalCost();
11+
}
12+
return totalCost;
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package iterator;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public class SamsungMobileCollection implements Collection {
7+
8+
private final List<Mobile> mobiles;
9+
10+
public SamsungMobileCollection() {
11+
mobiles = new LinkedList<>();
12+
}
13+
14+
@Override
15+
public Iterator createIterator() {
16+
return new SamsungMobileIterator(mobiles);
17+
}
18+
19+
@Override
20+
public void add(Item item) {
21+
this.mobiles.add((Mobile) item);
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package iterator;
2+
3+
import java.util.List;
4+
5+
public class SamsungMobileIterator implements Iterator {
6+
private List<Mobile> mobiles;
7+
private int index;
8+
9+
public SamsungMobileIterator(List<Mobile> mobiles) {
10+
this.index = 0;
11+
this.mobiles = mobiles;
12+
}
13+
14+
@Override
15+
public boolean hasNext() {
16+
if(this.mobiles.isEmpty() || this.index >= this.mobiles.size())
17+
return false;
18+
return true;
19+
}
20+
21+
@Override
22+
public Object next() {
23+
return this.mobiles.get(this.index++);
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package iterator;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class MobileStoreTest {
12+
13+
private MobileStore mobileStore;
14+
private Collection applMobilesCollection;
15+
16+
@BeforeEach
17+
void setUp() {
18+
mobileStore = new MobileStore();
19+
applMobilesCollection = new AppleMobilesCollection(5);
20+
applMobilesCollection.add(new Mobile("AM1", "Iphone X", 15, 30000));
21+
applMobilesCollection.add(new Mobile("AM2", "Iphone XI", 25, 40000));
22+
applMobilesCollection.add(new Mobile("AM3", "Iphone XII", 30, 55000));
23+
}
24+
25+
@Test
26+
void shouldGetTotalCostOfAppleMobilesInStore() {
27+
double actualCost = mobileStore.getTotalInventoryCost(applMobilesCollection);
28+
int expectedCost = 3100000;// 15 * 30000 + 25 * 40000 + 30 * 55000;
29+
Assertions.assertEquals(expectedCost, actualCost);
30+
}
31+
@Test
32+
void shouldGetTotalCostOfSamsungMobilesInStore() {
33+
var samsungMobileCollection = new SamsungMobileCollection();
34+
samsungMobileCollection.add(new Mobile("SAM1", "Galaxy X", 15, 20000));
35+
samsungMobileCollection.add(new Mobile("SAM2", "Galaxy XI", 25, 30000));
36+
samsungMobileCollection.add(new Mobile("SAM3", "Galaxy XII", 30, 45000));
37+
38+
double actualCost = mobileStore.getTotalInventoryCost(samsungMobileCollection);
39+
int expectedCost = 2400000; // 15 * 20000 + 25 * 30000 + 30 * 45000;
40+
Assertions.assertEquals(expectedCost, actualCost);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)