#84 Started work on cake baking
This commit is contained in:
parent
981c5b31e1
commit
2ca8ab09e0
@ -1,17 +1,16 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"applicationContext.xml");
|
||||
|
||||
CakeLayerDao cakeLayerDao = context.getBean(CakeLayerDao.class);
|
||||
cakeLayerDao.save(new CakeLayer("strawberry", 1200));
|
||||
System.out.println("Count CakeLayer records: " + cakeLayerDao.count());
|
||||
|
||||
context.close();
|
||||
CakeBakingService service = new CakeBakingServiceImpl();
|
||||
service.saveNewLayer(new CakeLayerInfo("foo", 1));
|
||||
service.saveNewLayer(new CakeLayerInfo("bar", 2));
|
||||
service.getAllLayers().stream().forEach((layer) -> System.out.println(layer));
|
||||
|
||||
service.saveNewTopping(new CakeToppingInfo("hoi", 11));
|
||||
service.getAllToppings().stream().forEach((topping) -> System.out.println(topping));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -15,14 +15,38 @@ public class Cake {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToMany
|
||||
private List<CakeLayer> layers;
|
||||
|
||||
@OneToOne
|
||||
private CakeTopping topping;
|
||||
|
||||
@OneToMany
|
||||
private List<CakeLayer> layers;
|
||||
|
||||
public Cake() {
|
||||
layers = new ArrayList<>();
|
||||
setLayers(new ArrayList<>());
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public CakeTopping getTopping() {
|
||||
return topping;
|
||||
}
|
||||
|
||||
public void setTopping(CakeTopping topping) {
|
||||
this.topping = topping;
|
||||
}
|
||||
|
||||
public List<CakeLayer> getLayers() {
|
||||
return layers;
|
||||
}
|
||||
|
||||
public void setLayers(List<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
public class CakeBakingException extends Exception {
|
||||
|
||||
public CakeBakingException() {
|
||||
}
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,10 +1,16 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CakeBakingService {
|
||||
|
||||
void bakeNewCake(CakeInfo cakeInfo);
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
List<CakeToppingInfo> getAllToppings();
|
||||
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
List<CakeLayerInfo> getAllLayers();
|
||||
}
|
||||
|
@ -1,16 +1,79 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
|
||||
private AbstractApplicationContext context;
|
||||
|
||||
public CakeBakingServiceImpl() {
|
||||
this.context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bakeNewCake(CakeInfo cakeInfo) {
|
||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||
List<CakeToppingInfo> allToppings = getAllToppings();
|
||||
List<CakeToppingInfo> matchingToppings = allToppings.stream()
|
||||
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
|
||||
if (!matchingToppings.isEmpty()) {
|
||||
// CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
// toppingBean.delete(matchingToppings.iterator().next().id.get());
|
||||
} else {
|
||||
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
|
||||
}
|
||||
List<CakeLayerInfo> allLayers = getAllLayers();
|
||||
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
||||
Optional<CakeLayerInfo> found = allLayers.stream().filter((layer) -> layer.name.equals(info.name)).findFirst();
|
||||
if (found.isPresent()) {
|
||||
// CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
|
||||
// layerBean.delete(found.get().id.get());
|
||||
} else {
|
||||
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
|
||||
}
|
||||
}
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNewTopping(CakeToppingInfo toppingInfo) {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
bean.save(new CakeTopping(toppingInfo.name, toppingInfo.calories));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNewLayer(CakeLayerInfo layerInfo) {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeToppingInfo> getAllToppings() {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
List<CakeToppingInfo> result = new ArrayList<>();
|
||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeTopping next = iterator.next();
|
||||
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeLayerInfo> getAllLayers() {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
List<CakeLayerInfo> result = new ArrayList<>();
|
||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeLayer next = iterator.next();
|
||||
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,22 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final CakeToppingInfo cakeToppingInfo;
|
||||
public final List<CakeLayerInfo> cakeLayerInfos;
|
||||
|
||||
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.of(id);
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
|
||||
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
|
||||
this.id = Optional.empty();
|
||||
this.cakeToppingInfo = cakeToppingInfo;
|
||||
this.cakeLayerInfos = cakeLayerInfos;
|
||||
}
|
||||
|
@ -19,7 +19,36 @@ public class CakeLayer {
|
||||
}
|
||||
|
||||
public CakeLayer(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,27 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeLayerInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
public CakeLayerInfo(String name, int calories) {
|
||||
public CakeLayerInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public CakeLayerInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,36 @@ public class CakeTopping {
|
||||
}
|
||||
|
||||
public CakeTopping(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,27 @@
|
||||
package com.iluwatar.layers;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class CakeToppingInfo {
|
||||
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
|
||||
public CakeToppingInfo(String name, int calories) {
|
||||
public CakeToppingInfo(Long id, String name, int calories) {
|
||||
this.id = Optional.of(id);
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public CakeToppingInfo(String name, int calories) {
|
||||
this.id = Optional.empty();
|
||||
this.name = name;
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("name: %s calories: %d", name, calories);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user