Skip to content

Commit d891ec5

Browse files
committed
Implements iterator
1 parent c9a014d commit d891ec5

6 files changed

Lines changed: 151 additions & 80 deletions

File tree

README.md

Lines changed: 16 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,26 @@
11
# ☕ Java Design Patterns
22

3-
## About
3+
## Iterator
44

5-
This application is a simple Java Console Application that aims to implement design pattern examples to a Programmin Language Class. A set of design patterns will be developed
6-
each week, and they are divided in different [branches](https://github.com/LBeghini/Java-Design-Patterns/branches).
5+
Iterator pattern is used to access sequentially a list of elements inside an object without having to know the object itself.
76

8-
The main branch is just a template for every other branch.
7+
For example, let's suppose we have a shopping basket and want to add items on it. The cashier just needs to know that there's an item to pass, independent of the quantity.
98

10-
Also, to make it easier to download the source code, [releases](https://github.com/LBeghini/Java-Design-Patterns/releases) are created related to the task of the week, giving a snapshot of the code for that specific implementation.
9+
So, the methot to checkout the shopping items would be like this:
1110

12-
## Implemented design patterns
11+
```java
12+
public static Float checkout(ShoppingBasket shoppingBasket) {
13+
Float total = 0f;
1314

14-
### Behavioural patterns
15+
Screen.promptBanner();
1516

16-
- [ ] Chain of responsability
17-
- [ ] Command
18-
- [ ] Iterator
19-
- [ ] Memento
20-
- [ ] Observer
21-
- [x] [State](https://github.com/LBeghini/Java-Design-Patterns/tree/3-state)
22-
- [ ] Strategy
23-
- [ ] Template method
24-
25-
### Creational patterns
26-
27-
- [ ] Abstract factory
28-
- [x] [Builder](https://github.com/LBeghini/Java-Design-Patterns/tree/1-builder)
29-
- [x] [Factory method](https://github.com/LBeghini/Java-Design-Patterns/tree/2-factory-method)
30-
- [x] [Prototype](https://github.com/LBeghini/Java-Design-Patterns/tree/2-prototype)
31-
- [x] [Singleton](https://github.com/LBeghini/Java-Design-Patterns/tree/1-singleton)
32-
33-
### Structural patterns
34-
35-
- [ ] Adapter
36-
- [ ] Bridge
37-
- [ ] Composite
38-
- [ ] Decorator
39-
- [ ] Facade
40-
- [ ] Flyweight
41-
- [ ] Mediator
42-
- [ ] Proxy
43-
44-
## Technologies
45-
46-
- Java
47-
- JUnit
48-
- Maven
49-
50-
## Requirements
51-
52-
To run and edit the project, be sure to have installed in your computer the following softwares:
53-
- A code editor
54-
55-
After that, you'll need to clone this repo:
56-
57-
```bash
58-
git clone https://github.com/LBeghini/Java-Design-Patterns.git
59-
```
60-
61-
## Change branch
62-
63-
To change to a different branch, run the command:
64-
65-
```bash
66-
git checkout name-of-the-branch
67-
```
68-
69-
The branch names have the pattern:
70-
71-
```bash
72-
{number-of-the-week}-{pattern-name}
73-
```
74-
75-
> `number-of-the-week` corresponds to the week asked to be implemented certain pattern
76-
77-
## Testing
78-
79-
This project has no aim to run any of the implemented classes, as the goal is the code itself. However, the classes will be tested to visualize the behaviour and implementation
80-
of the patterns.
81-
82-
You can run the tests using the maven wrapper:
83-
84-
```bash
85-
./mvnw test
17+
for (Item item : shoppingBasket) {
18+
Screen.promptItem(item);
19+
total += item.getPrice();
20+
}
21+
Screen.promptTotal(total);
22+
return total;
23+
}
8624
```
8725

88-
## :balance_scale: License
89-
90-
[MIT License](https://github.com/LBeghini/Java-Design-Patterns/blob/main/LICENSE)
26+
Notice that we can iterate trought the items with the shopping basket only, without having a declaration of a list of these items.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.language.programming.model;
2+
3+
public class Cashier {
4+
5+
public static Float checkout(ShoppingBasket shoppingBasket) {
6+
Float total = 0f;
7+
8+
Screen.promptBanner();
9+
10+
for (Item item : shoppingBasket) {
11+
Screen.promptItem(item);
12+
total += item.getPrice();
13+
}
14+
Screen.promptTotal(total);
15+
return total;
16+
}
17+
18+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.language.programming.model;
2+
3+
public class Item {
4+
private int code;
5+
private String barcode;
6+
private String name;
7+
private Float price;
8+
private String icon;
9+
10+
public Item(int code, String barcode, String name, Float price, String icon) {
11+
this.code = code;
12+
this.barcode = barcode;
13+
this.name = name;
14+
this.price = price;
15+
this.icon = icon;
16+
}
17+
18+
public int getCode() {
19+
return code;
20+
}
21+
22+
public void setCode(int code) {
23+
this.code = code;
24+
}
25+
26+
public String getBarcode() {
27+
return barcode;
28+
}
29+
30+
public void setBarcode(String barcode) {
31+
this.barcode = barcode;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Float getPrice() {
43+
return price;
44+
}
45+
46+
public void setPrice(Float price) {
47+
this.price = price;
48+
}
49+
50+
public String getIcon() {
51+
return icon;
52+
}
53+
54+
public void setIcon(String icon) {
55+
this.icon = icon;
56+
}
57+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.language.programming.model;
2+
3+
public class Screen {
4+
5+
public static void promptItem(Item item) {
6+
System.out.println(item.getBarcode() + "\ncode: " + item.getCode() + "\n" + item.getIcon() + " "
7+
+ item.getName() + "\n\t $ " + item.getPrice().toString() + "\n-------------------\n");
8+
}
9+
10+
public static void promptBanner() {
11+
System.out.println("╔═╗┌─┐┌─┐┬ ┬┬┌─┐┬─┐\n" + "║ ├─┤└─┐├─┤│├┤ ├┬┘\n" + "╚═╝┴ ┴└─┘┴ ┴┴└─┘┴└─\n");
12+
}
13+
14+
public static void promptTotal(Float total) {
15+
System.out.println("\tTOTAL $" + total.toString());
16+
17+
}
18+
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.language.programming.model;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
public class ShoppingBasket implements Iterable<Item> {
9+
10+
private List<Item> items = new ArrayList<Item>();
11+
12+
public ShoppingBasket(Item... items) {
13+
this.items = Arrays.asList(items);
14+
}
15+
16+
@Override
17+
public Iterator<Item> iterator() {
18+
return items.iterator();
19+
}
20+
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.language.programming.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class CashierTest {
8+
9+
@Test
10+
public void shouldSumItemsPrices() {
11+
ShoppingBasket shoppingBasket = new ShoppingBasket(
12+
new Item(3810392, "│ │█ █│█ █│ ||||", "Chocolate Bar", 2.99f, "🍫"),
13+
new Item(3039483, "████ |||| ██│| █", "Red Apple", 0.99f, "🍎"),
14+
new Item(9389348, "│█ █│█ ||||| ███", "Bread", 3.99f, "🍞"),
15+
new Item(4893023, "||| ████ ██ ||", "Egg", 0.30f, "🥚"),
16+
new Item(5920334, " █│█ |||| ███", "Broccoli", 0.90f, "🥦"));
17+
18+
assertEquals(9.17f, Cashier.checkout(shoppingBasket));
19+
}
20+
}

0 commit comments

Comments
 (0)