#84 Example improvements. Instead of all toppings and all layers the
service returns available toppings and layers.
This commit is contained in:
parent
89cb234be9
commit
9aa831d688
@ -20,16 +20,26 @@ public class App {
|
|||||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));
|
||||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
||||||
cakeBakingService.getAllLayers().stream().forEach((layer) -> System.out.println(layer));
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("lemon", 950));
|
||||||
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("vanilla", 950));
|
||||||
|
cakeBakingService.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
||||||
|
|
||||||
cakeBakingService.saveNewTopping(new CakeToppingInfo("candies", 350));
|
cakeBakingService.saveNewTopping(new CakeToppingInfo("candies", 350));
|
||||||
cakeBakingService.getAllToppings().stream().forEach((topping) -> System.out.println(topping));
|
cakeBakingService.saveNewTopping(new CakeToppingInfo("cherry", 350));
|
||||||
|
|
||||||
CakeInfo cakeInfo = new CakeInfo(new CakeToppingInfo("candies", 0),
|
CakeInfo cake1 = new CakeInfo(new CakeToppingInfo("candies", 0),
|
||||||
Arrays.asList(new CakeLayerInfo("chocolate", 0), new CakeLayerInfo("banana", 0),
|
Arrays.asList(new CakeLayerInfo("chocolate", 0), new CakeLayerInfo("banana", 0),
|
||||||
new CakeLayerInfo("strawberry", 0)));
|
new CakeLayerInfo("strawberry", 0)));
|
||||||
try {
|
try {
|
||||||
cakeBakingService.bakeNewCake(cakeInfo);
|
cakeBakingService.bakeNewCake(cake1);
|
||||||
|
} catch (CakeBakingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
CakeInfo cake2 = new CakeInfo(new CakeToppingInfo("cherry", 0),
|
||||||
|
Arrays.asList(new CakeLayerInfo("vanilla", 0), new CakeLayerInfo("lemon", 0),
|
||||||
|
new CakeLayerInfo("strawberry", 0)));
|
||||||
|
try {
|
||||||
|
cakeBakingService.bakeNewCake(cake2);
|
||||||
} catch (CakeBakingException e) {
|
} catch (CakeBakingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,9 @@ public interface CakeBakingService {
|
|||||||
|
|
||||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||||
|
|
||||||
List<CakeToppingInfo> getAllToppings();
|
List<CakeToppingInfo> getAvailableToppings();
|
||||||
|
|
||||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||||
|
|
||||||
List<CakeLayerInfo> getAllLayers();
|
List<CakeLayerInfo> getAvailableLayers();
|
||||||
}
|
}
|
||||||
|
@ -25,13 +25,13 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||||
List<CakeToppingInfo> allToppings = getAllToppings();
|
List<CakeToppingInfo> allToppings = getAvailableToppings();
|
||||||
List<CakeToppingInfo> matchingToppings = allToppings.stream()
|
List<CakeToppingInfo> matchingToppings = allToppings.stream()
|
||||||
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
|
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
|
||||||
if (matchingToppings.isEmpty()) {
|
if (matchingToppings.isEmpty()) {
|
||||||
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
|
throw new CakeBakingException(String.format("Topping %s is not available", cakeInfo.cakeToppingInfo.name));
|
||||||
}
|
}
|
||||||
List<CakeLayer> allLayers = getAllLayerEntities();
|
List<CakeLayer> allLayers = getAvailableLayerEntities();
|
||||||
Set<CakeLayer> foundLayers = new HashSet<>();
|
Set<CakeLayer> foundLayers = new HashSet<>();
|
||||||
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
||||||
Optional<CakeLayer> found = allLayers.stream().filter((layer) -> layer.getName().equals(info.name)).findFirst();
|
Optional<CakeLayer> found = allLayers.stream().filter((layer) -> layer.getName().equals(info.name)).findFirst();
|
||||||
@ -69,46 +69,56 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
|||||||
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<CakeTopping> getAllToppingEntities() {
|
private List<CakeTopping> getAvailableToppingEntities() {
|
||||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||||
List<CakeTopping> result = new ArrayList<>();
|
List<CakeTopping> result = new ArrayList<>();
|
||||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
result.add(iterator.next());
|
CakeTopping topping = iterator.next();
|
||||||
|
if (topping.getCake() == null) {
|
||||||
|
result.add(topping);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CakeToppingInfo> getAllToppings() {
|
public List<CakeToppingInfo> getAvailableToppings() {
|
||||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||||
List<CakeToppingInfo> result = new ArrayList<>();
|
List<CakeToppingInfo> result = new ArrayList<>();
|
||||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
CakeTopping next = iterator.next();
|
CakeTopping next = iterator.next();
|
||||||
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
if (next.getCake() == null) {
|
||||||
|
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<CakeLayer> getAllLayerEntities() {
|
private List<CakeLayer> getAvailableLayerEntities() {
|
||||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||||
List<CakeLayer> result = new ArrayList<>();
|
List<CakeLayer> result = new ArrayList<>();
|
||||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
result.add(iterator.next());
|
CakeLayer next = iterator.next();
|
||||||
|
if (next.getCake() == null) {
|
||||||
|
result.add(next);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CakeLayerInfo> getAllLayers() {
|
public List<CakeLayerInfo> getAvailableLayers() {
|
||||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||||
List<CakeLayerInfo> result = new ArrayList<>();
|
List<CakeLayerInfo> result = new ArrayList<>();
|
||||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
CakeLayer next = iterator.next();
|
CakeLayer next = iterator.next();
|
||||||
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
if (next.getCake() == null) {
|
||||||
|
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,15 @@ public class CakeInfo {
|
|||||||
this.cakeLayerInfos = cakeLayerInfos;
|
this.cakeLayerInfos = cakeLayerInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int calculateTotalCalories() {
|
||||||
|
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
|
||||||
|
total += cakeLayerInfos.stream().mapToInt(c -> c.calories).sum();
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("CakeInfo id=%d topping=%s layers=%s", id.get(), cakeToppingInfo, cakeLayerInfos);
|
return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id.get(), cakeToppingInfo,
|
||||||
|
cakeLayerInfos, calculateTotalCalories());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
<map>
|
<map>
|
||||||
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
|
||||||
<entry key="hibernate.hbm2ddl.auto" value="create-drop" />
|
<entry key="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||||
<entry key="hibernate.show_sql" value="true" />
|
<entry key="hibernate.show_sql" value="false" />
|
||||||
</map>
|
</map>
|
||||||
</property>
|
</property>
|
||||||
</bean>
|
</bean>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user