#84 Example improvements. Instead of all toppings and all layers the

service returns available toppings and layers.
This commit is contained in:
Ilkka Seppala 2015-08-15 18:35:16 +03:00
parent 89cb234be9
commit 9aa831d688
5 changed files with 45 additions and 18 deletions

View File

@ -20,16 +20,26 @@ public class App {
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));
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.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),
new CakeLayerInfo("strawberry", 0)));
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) {
e.printStackTrace();
}

View File

@ -10,9 +10,9 @@ public interface CakeBakingService {
void saveNewTopping(CakeToppingInfo toppingInfo);
List<CakeToppingInfo> getAllToppings();
List<CakeToppingInfo> getAvailableToppings();
void saveNewLayer(CakeLayerInfo layerInfo);
List<CakeLayerInfo> getAllLayers();
List<CakeLayerInfo> getAvailableLayers();
}

View File

@ -25,13 +25,13 @@ public class CakeBakingServiceImpl implements CakeBakingService {
@Override
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
List<CakeToppingInfo> allToppings = getAllToppings();
List<CakeToppingInfo> allToppings = getAvailableToppings();
List<CakeToppingInfo> matchingToppings = allToppings.stream()
.filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name)).collect(Collectors.toList());
if (matchingToppings.isEmpty()) {
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<>();
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
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));
}
private List<CakeTopping> getAllToppingEntities() {
private List<CakeTopping> getAvailableToppingEntities() {
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
List<CakeTopping> result = new ArrayList<>();
Iterator<CakeTopping> iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
result.add(iterator.next());
CakeTopping topping = iterator.next();
if (topping.getCake() == null) {
result.add(topping);
}
}
return result;
}
@Override
public List<CakeToppingInfo> getAllToppings() {
public List<CakeToppingInfo> getAvailableToppings() {
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()));
if (next.getCake() == null) {
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
}
}
return result;
}
private List<CakeLayer> getAllLayerEntities() {
private List<CakeLayer> getAvailableLayerEntities() {
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
List<CakeLayer> result = new ArrayList<>();
Iterator<CakeLayer> iterator = bean.findAll().iterator();
while (iterator.hasNext()) {
result.add(iterator.next());
CakeLayer next = iterator.next();
if (next.getCake() == null) {
result.add(next);
}
}
return result;
}
@Override
public List<CakeLayerInfo> getAllLayers() {
public List<CakeLayerInfo> getAvailableLayers() {
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()));
if (next.getCake() == null) {
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
}
}
return result;
}

View File

@ -21,8 +21,15 @@ public class CakeInfo {
this.cakeLayerInfos = cakeLayerInfos;
}
public int calculateTotalCalories() {
int total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
total += cakeLayerInfos.stream().mapToInt(c -> c.calories).sum();
return total;
}
@Override
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());
}
}

View File

@ -35,7 +35,7 @@
<map>
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<entry key="hibernate.hbm2ddl.auto" value="create-drop" />
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.show_sql" value="false" />
</map>
</property>
</bean>