X Tutup
Skip to content

Commit 2ca8ab0

Browse files
committed
iluwatar#84 Started work on cake baking
1 parent 981c5b3 commit 2ca8ab0

File tree

10 files changed

+215
-15
lines changed

10 files changed

+215
-15
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.iluwatar.layers;
22

3-
import org.springframework.context.support.ClassPathXmlApplicationContext;
4-
53
public class App {
64

75
public static void main(String[] args) {
8-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
9-
"applicationContext.xml");
6+
7+
CakeBakingService service = new CakeBakingServiceImpl();
8+
service.saveNewLayer(new CakeLayerInfo("foo", 1));
9+
service.saveNewLayer(new CakeLayerInfo("bar", 2));
10+
service.getAllLayers().stream().forEach((layer) -> System.out.println(layer));
1011

11-
CakeLayerDao cakeLayerDao = context.getBean(CakeLayerDao.class);
12-
cakeLayerDao.save(new CakeLayer("strawberry", 1200));
13-
System.out.println("Count CakeLayer records: " + cakeLayerDao.count());
12+
service.saveNewTopping(new CakeToppingInfo("hoi", 11));
13+
service.getAllToppings().stream().forEach((topping) -> System.out.println(topping));
1414

15-
context.close();
1615
}
1716
}

layers/src/main/java/com/iluwatar/layers/Cake.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,38 @@ public class Cake {
1515
@Id
1616
@GeneratedValue
1717
private Long id;
18-
19-
@OneToMany
20-
private List<CakeLayer> layers;
2118

2219
@OneToOne
2320
private CakeTopping topping;
2421

22+
@OneToMany
23+
private List<CakeLayer> layers;
24+
2525
public Cake() {
26-
layers = new ArrayList<>();
26+
setLayers(new ArrayList<>());
27+
}
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public CakeTopping getTopping() {
38+
return topping;
39+
}
40+
41+
public void setTopping(CakeTopping topping) {
42+
this.topping = topping;
43+
}
44+
45+
public List<CakeLayer> getLayers() {
46+
return layers;
47+
}
48+
49+
public void setLayers(List<CakeLayer> layers) {
50+
this.layers = layers;
2751
}
2852
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar.layers;
2+
3+
public class CakeBakingException extends Exception {
4+
5+
public CakeBakingException() {
6+
}
7+
8+
public CakeBakingException(String message) {
9+
super(message);
10+
}
11+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.iluwatar.layers;
22

3+
import java.util.List;
4+
35
public interface CakeBakingService {
46

5-
void bakeNewCake(CakeInfo cakeInfo);
7+
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
68

79
void saveNewTopping(CakeToppingInfo toppingInfo);
10+
11+
List<CakeToppingInfo> getAllToppings();
812

913
void saveNewLayer(CakeLayerInfo layerInfo);
14+
15+
List<CakeLayerInfo> getAllLayers();
1016
}
Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,79 @@
11
package com.iluwatar.layers;
22

3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
import java.util.Optional;
7+
import java.util.stream.Collectors;
8+
9+
import org.springframework.context.support.AbstractApplicationContext;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
312
public class CakeBakingServiceImpl implements CakeBakingService {
413

14+
private AbstractApplicationContext context;
15+
16+
public CakeBakingServiceImpl() {
17+
this.context = new ClassPathXmlApplicationContext("applicationContext.xml");
18+
}
19+
520
@Override
6-
public void bakeNewCake(CakeInfo cakeInfo) {
21+
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
22+
List<CakeToppingInfo> allToppings = getAllToppings();
23+
List<CakeToppingInfo> matchingToppings = allToppings.stream()
24+
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
25+
if (!matchingToppings.isEmpty()) {
26+
// CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
27+
// toppingBean.delete(matchingToppings.iterator().next().id.get());
28+
} else {
29+
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
30+
}
31+
List<CakeLayerInfo> allLayers = getAllLayers();
32+
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
33+
Optional<CakeLayerInfo> found = allLayers.stream().filter((layer) -> layer.name.equals(info.name)).findFirst();
34+
if (found.isPresent()) {
35+
// CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
36+
// layerBean.delete(found.get().id.get());
37+
} else {
38+
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
39+
}
40+
}
41+
CakeDao cakeBean = context.getBean(CakeDao.class);
742
}
843

944
@Override
1045
public void saveNewTopping(CakeToppingInfo toppingInfo) {
46+
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
47+
bean.save(new CakeTopping(toppingInfo.name, toppingInfo.calories));
1148
}
1249

1350
@Override
1451
public void saveNewLayer(CakeLayerInfo layerInfo) {
52+
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
53+
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
54+
}
55+
56+
@Override
57+
public List<CakeToppingInfo> getAllToppings() {
58+
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
59+
List<CakeToppingInfo> result = new ArrayList<>();
60+
Iterator<CakeTopping> iterator = bean.findAll().iterator();
61+
while (iterator.hasNext()) {
62+
CakeTopping next = iterator.next();
63+
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
64+
}
65+
return result;
66+
}
67+
68+
@Override
69+
public List<CakeLayerInfo> getAllLayers() {
70+
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
71+
List<CakeLayerInfo> result = new ArrayList<>();
72+
Iterator<CakeLayer> iterator = bean.findAll().iterator();
73+
while (iterator.hasNext()) {
74+
CakeLayer next = iterator.next();
75+
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
76+
}
77+
return result;
1578
}
1679
}

layers/src/main/java/com/iluwatar/layers/CakeInfo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.iluwatar.layers;
22

33
import java.util.List;
4+
import java.util.Optional;
45

56
public class CakeInfo {
67

8+
public final Optional<Long> id;
79
public final CakeToppingInfo cakeToppingInfo;
810
public final List<CakeLayerInfo> cakeLayerInfos;
11+
12+
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
13+
this.id = Optional.of(id);
14+
this.cakeToppingInfo = cakeToppingInfo;
15+
this.cakeLayerInfos = cakeLayerInfos;
16+
}
917

1018
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
19+
this.id = Optional.empty();
1120
this.cakeToppingInfo = cakeToppingInfo;
1221
this.cakeLayerInfos = cakeLayerInfos;
1322
}

layers/src/main/java/com/iluwatar/layers/CakeLayer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,36 @@ public CakeLayer() {
1919
}
2020

2121
public CakeLayer(String name, int calories) {
22+
this.setName(name);
23+
this.setCalories(calories);
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
2239
this.name = name;
40+
}
41+
42+
public int getCalories() {
43+
return calories;
44+
}
45+
46+
public void setCalories(int calories) {
2347
this.calories = calories;
2448
}
49+
50+
@Override
51+
public String toString() {
52+
return String.format("name: %s calories: %d", name, calories);
53+
}
2554
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.iluwatar.layers;
22

3+
import java.util.Optional;
4+
35
public class CakeLayerInfo {
46

7+
public final Optional<Long> id;
58
public final String name;
69
public final int calories;
710

11+
public CakeLayerInfo(Long id, String name, int calories) {
12+
this.id = Optional.of(id);
13+
this.name = name;
14+
this.calories = calories;
15+
}
16+
817
public CakeLayerInfo(String name, int calories) {
18+
this.id = Optional.empty();
919
this.name = name;
1020
this.calories = calories;
1121
}
22+
23+
@Override
24+
public String toString() {
25+
return String.format("name: %s calories: %d", name, calories);
26+
}
1227
}

layers/src/main/java/com/iluwatar/layers/CakeTopping.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,36 @@ public CakeTopping() {
1919
}
2020

2121
public CakeTopping(String name, int calories) {
22+
this.setName(name);
23+
this.setCalories(calories);
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
2239
this.name = name;
40+
}
41+
42+
public int getCalories() {
43+
return calories;
44+
}
45+
46+
public void setCalories(int calories) {
2347
this.calories = calories;
2448
}
49+
50+
@Override
51+
public String toString() {
52+
return String.format("name: %s calories: %d", name, calories);
53+
}
2554
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package com.iluwatar.layers;
22

3+
import java.util.Optional;
4+
35
public class CakeToppingInfo {
4-
6+
7+
public final Optional<Long> id;
58
public final String name;
69
public final int calories;
710

11+
public CakeToppingInfo(Long id, String name, int calories) {
12+
this.id = Optional.of(id);
13+
this.name = name;
14+
this.calories = calories;
15+
}
16+
817
public CakeToppingInfo(String name, int calories) {
18+
this.id = Optional.empty();
919
this.name = name;
1020
this.calories = calories;
1121
}
22+
23+
@Override
24+
public String toString() {
25+
return String.format("name: %s calories: %d", name, calories);
26+
}
1227
}

0 commit comments

Comments
 (0)
X Tutup