Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -4,32 +4,31 @@ import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* Layers is an architectural style where software responsibilities are
|
||||
* divided among the different layers of the application.
|
||||
* Layers is an architectural style where software responsibilities are divided among the different
|
||||
* layers of the application.
|
||||
* <p>
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access
|
||||
* layer, business layer and presentation layer.
|
||||
* This example demonstrates a traditional 3-layer architecture consisting of data access layer,
|
||||
* business layer and presentation layer.
|
||||
* <p>
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>, <code>CakeToppingDao</code> and
|
||||
* <code>CakeLayerDao</code>. The repositories can be used for CRUD operations on cakes, cake toppings
|
||||
* and cake layers respectively.
|
||||
* The data access layer is formed of Spring Data repositories <code>CakeDao</code>,
|
||||
* <code>CakeToppingDao</code> and <code>CakeLayerDao</code>. The repositories can be used for CRUD
|
||||
* operations on cakes, cake toppings and cake layers respectively.
|
||||
* <p>
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code> offers
|
||||
* methods to retrieve available cake toppings and cake layers and baked cakes. Also the
|
||||
* The business layer is built on top of the data access layer. <code>CakeBakingService</code>
|
||||
* offers methods to retrieve available cake toppings and cake layers and baked cakes. Also the
|
||||
* service is used to create new cakes out of cake toppings and cake layers.
|
||||
* <p>
|
||||
* The presentation layer is built on the business layer and in this example it simply lists
|
||||
* the cakes that have been baked.
|
||||
* The presentation layer is built on the business layer and in this example it simply lists the
|
||||
* cakes that have been baked.
|
||||
* <p>
|
||||
* We have applied so called strict layering which means that the layers can only access
|
||||
* the classes directly beneath them. This leads the solution to create an additional set of
|
||||
* DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
|
||||
* to translate data between layers. In other words, <code>CakeBakingService</code> cannot
|
||||
* return entities (<code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>)
|
||||
* directly since these reside on data access layer but instead translates these into business
|
||||
* layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>)
|
||||
* and returns them instead. This way the presentation layer does not have any knowledge of
|
||||
* other layers than the business layer and thus is not affected by changes to them.
|
||||
* We have applied so called strict layering which means that the layers can only access the classes
|
||||
* directly beneath them. This leads the solution to create an additional set of DTOs (
|
||||
* <code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) to translate
|
||||
* data between layers. In other words, <code>CakeBakingService</code> cannot return entities (
|
||||
* <code>Cake</code>, <code>CakeTopping</code>, <code>CakeLayer</code>) directly since these reside
|
||||
* on data access layer but instead translates these into business layer DTOs (<code>CakeInfo</code>, <code>CakeToppingInfo</code>, <code>CakeLayerInfo</code>) and returns them instead. This way
|
||||
* the presentation layer does not have any knowledge of other layers than the business layer and
|
||||
* thus is not affected by changes to them.
|
||||
*
|
||||
* @see Cake
|
||||
* @see CakeTopping
|
||||
@ -45,52 +44,55 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl();
|
||||
|
||||
/**
|
||||
* Application entry point
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// initialize example data
|
||||
initializeData(cakeBakingService);
|
||||
|
||||
// create view and render it
|
||||
CakeViewImpl cakeView = new CakeViewImpl(cakeBakingService);
|
||||
cakeView.render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the example data
|
||||
* @param cakeBakingService
|
||||
*/
|
||||
private static void initializeData(CakeBakingService cakeBakingService) {
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
||||
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("cherry", 350));
|
||||
private static CakeBakingService cakeBakingService = new CakeBakingServiceImpl();
|
||||
|
||||
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(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();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Application entry point
|
||||
*
|
||||
* @param args Command line parameters
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// initialize example data
|
||||
initializeData(cakeBakingService);
|
||||
|
||||
// create view and render it
|
||||
CakeViewImpl cakeView = new CakeViewImpl(cakeBakingService);
|
||||
cakeView.render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the example data
|
||||
*
|
||||
* @param cakeBakingService
|
||||
*/
|
||||
private static void initializeData(CakeBakingService cakeBakingService) {
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("chocolate", 1200));
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("banana", 900));
|
||||
cakeBakingService.saveNewLayer(new CakeLayerInfo("strawberry", 950));
|
||||
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("cherry", 350));
|
||||
|
||||
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(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,50 +19,50 @@ import javax.persistence.OneToOne;
|
||||
@Entity
|
||||
public class Cake {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade=CascadeType.REMOVE)
|
||||
private CakeTopping topping;
|
||||
|
||||
@OneToMany(cascade=CascadeType.REMOVE, fetch=FetchType.EAGER)
|
||||
private Set<CakeLayer> layers;
|
||||
|
||||
public Cake() {
|
||||
setLayers(new HashSet<>());
|
||||
}
|
||||
@OneToOne(cascade = CascadeType.REMOVE)
|
||||
private CakeTopping topping;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER)
|
||||
private Set<CakeLayer> layers;
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public Cake() {
|
||||
setLayers(new HashSet<>());
|
||||
}
|
||||
|
||||
public CakeTopping getTopping() {
|
||||
return topping;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setTopping(CakeTopping topping) {
|
||||
this.topping = topping;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set<CakeLayer> getLayers() {
|
||||
return layers;
|
||||
}
|
||||
public CakeTopping getTopping() {
|
||||
return topping;
|
||||
}
|
||||
|
||||
public void setLayers(Set<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
public void addLayer(CakeLayer layer) {
|
||||
this.layers.add(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("id=%s topping=%s layers=%s", id, topping, layers.toString());
|
||||
}
|
||||
public void setTopping(CakeTopping topping) {
|
||||
this.topping = topping;
|
||||
}
|
||||
|
||||
public Set<CakeLayer> getLayers() {
|
||||
return layers;
|
||||
}
|
||||
|
||||
public void setLayers(Set<CakeLayer> layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
public void addLayer(CakeLayer layer) {
|
||||
this.layers.add(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("id=%s topping=%s layers=%s", id, topping, layers.toString());
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,12 @@ package com.iluwatar.layers;
|
||||
*
|
||||
*/
|
||||
public class CakeBakingException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CakeBakingException() {
|
||||
}
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
public CakeBakingException() {}
|
||||
|
||||
public CakeBakingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
@ -8,41 +8,47 @@ import java.util.List;
|
||||
*
|
||||
*/
|
||||
public interface CakeBakingService {
|
||||
|
||||
/**
|
||||
* Bakes new cake according to parameters
|
||||
* @param cakeInfo
|
||||
* @throws CakeBakingException
|
||||
*/
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
/**
|
||||
* Get all cakes
|
||||
* @return
|
||||
*/
|
||||
List<CakeInfo> getAllCakes();
|
||||
|
||||
/**
|
||||
* Store new cake topping
|
||||
* @param toppingInfo
|
||||
*/
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
/**
|
||||
* Bakes new cake according to parameters
|
||||
*
|
||||
* @param cakeInfo
|
||||
* @throws CakeBakingException
|
||||
*/
|
||||
void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException;
|
||||
|
||||
/**
|
||||
* Get available cake toppings
|
||||
* @return
|
||||
*/
|
||||
List<CakeToppingInfo> getAvailableToppings();
|
||||
|
||||
/**
|
||||
* Add new cake layer
|
||||
* @param layerInfo
|
||||
*/
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
/**
|
||||
* Get available cake layers
|
||||
* @return
|
||||
*/
|
||||
List<CakeLayerInfo> getAvailableLayers();
|
||||
/**
|
||||
* Get all cakes
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeInfo> getAllCakes();
|
||||
|
||||
/**
|
||||
* Store new cake topping
|
||||
*
|
||||
* @param toppingInfo
|
||||
*/
|
||||
void saveNewTopping(CakeToppingInfo toppingInfo);
|
||||
|
||||
/**
|
||||
* Get available cake toppings
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeToppingInfo> getAvailableToppings();
|
||||
|
||||
/**
|
||||
* Add new cake layer
|
||||
*
|
||||
* @param layerInfo
|
||||
*/
|
||||
void saveNewLayer(CakeLayerInfo layerInfo);
|
||||
|
||||
/**
|
||||
* Get available cake layers
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<CakeLayerInfo> getAvailableLayers();
|
||||
}
|
||||
|
@ -22,128 +22,132 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Transactional
|
||||
public class CakeBakingServiceImpl implements CakeBakingService {
|
||||
|
||||
private AbstractApplicationContext context;
|
||||
private AbstractApplicationContext context;
|
||||
|
||||
public CakeBakingServiceImpl() {
|
||||
this.context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||
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 = getAvailableLayerEntities();
|
||||
Set<CakeLayer> foundLayers = new HashSet<>();
|
||||
for (CakeLayerInfo info: cakeInfo.cakeLayerInfos) {
|
||||
Optional<CakeLayer> found = allLayers.stream().filter((layer) -> layer.getName().equals(info.name)).findFirst();
|
||||
if (!found.isPresent()) {
|
||||
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
|
||||
} else {
|
||||
foundLayers.add(found.get());
|
||||
}
|
||||
}
|
||||
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
CakeTopping topping = toppingBean.findOne(matchingToppings.iterator().next().id.get());
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
Cake cake = new Cake();
|
||||
cake.setTopping(topping);
|
||||
cake.setLayers(foundLayers);
|
||||
cakeBean.save(cake);
|
||||
topping.setCake(cake);
|
||||
toppingBean.save(topping);
|
||||
CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
|
||||
for (CakeLayer layer: foundLayers) {
|
||||
layer.setCake(cake);
|
||||
layerBean.save(layer);
|
||||
}
|
||||
}
|
||||
public CakeBakingServiceImpl() {
|
||||
this.context = new ClassPathXmlApplicationContext("applicationContext.xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNewTopping(CakeToppingInfo toppingInfo) {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
bean.save(new CakeTopping(toppingInfo.name, toppingInfo.calories));
|
||||
}
|
||||
@Override
|
||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||
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 = getAvailableLayerEntities();
|
||||
Set<CakeLayer> foundLayers = new HashSet<>();
|
||||
for (CakeLayerInfo info : cakeInfo.cakeLayerInfos) {
|
||||
Optional<CakeLayer> found =
|
||||
allLayers.stream().filter((layer) -> layer.getName().equals(info.name)).findFirst();
|
||||
if (!found.isPresent()) {
|
||||
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
|
||||
} else {
|
||||
foundLayers.add(found.get());
|
||||
}
|
||||
}
|
||||
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||
CakeTopping topping = toppingBean.findOne(matchingToppings.iterator().next().id.get());
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
Cake cake = new Cake();
|
||||
cake.setTopping(topping);
|
||||
cake.setLayers(foundLayers);
|
||||
cakeBean.save(cake);
|
||||
topping.setCake(cake);
|
||||
toppingBean.save(topping);
|
||||
CakeLayerDao layerBean = context.getBean(CakeLayerDao.class);
|
||||
for (CakeLayer layer : foundLayers) {
|
||||
layer.setCake(cake);
|
||||
layerBean.save(layer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveNewLayer(CakeLayerInfo layerInfo) {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
||||
}
|
||||
@Override
|
||||
public void saveNewTopping(CakeToppingInfo toppingInfo) {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
bean.save(new CakeTopping(toppingInfo.name, toppingInfo.calories));
|
||||
}
|
||||
|
||||
private List<CakeTopping> getAvailableToppingEntities() {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
List<CakeTopping> result = new ArrayList<>();
|
||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeTopping topping = iterator.next();
|
||||
if (topping.getCake() == null) {
|
||||
result.add(topping);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
if (next.getCake() == null) {
|
||||
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public void saveNewLayer(CakeLayerInfo layerInfo) {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
bean.save(new CakeLayer(layerInfo.name, layerInfo.calories));
|
||||
}
|
||||
|
||||
private List<CakeLayer> getAvailableLayerEntities() {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
List<CakeLayer> result = new ArrayList<>();
|
||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeLayer next = iterator.next();
|
||||
if (next.getCake() == null) {
|
||||
result.add(next);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
if (next.getCake() == null) {
|
||||
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private List<CakeTopping> getAvailableToppingEntities() {
|
||||
CakeToppingDao bean = context.getBean(CakeToppingDao.class);
|
||||
List<CakeTopping> result = new ArrayList<>();
|
||||
Iterator<CakeTopping> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeTopping topping = iterator.next();
|
||||
if (topping.getCake() == null) {
|
||||
result.add(topping);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeInfo> getAllCakes() {
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
List<CakeInfo> result = new ArrayList<>();
|
||||
Iterator<Cake> iterator = cakeBean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Cake cake = iterator.next();
|
||||
CakeToppingInfo cakeToppingInfo = new CakeToppingInfo(cake.getTopping().getId(),
|
||||
cake.getTopping().getName(), cake.getTopping().getCalories());
|
||||
ArrayList<CakeLayerInfo> cakeLayerInfos = new ArrayList<CakeLayerInfo>();
|
||||
for (CakeLayer layer: cake.getLayers()) {
|
||||
cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories()));
|
||||
}
|
||||
CakeInfo cakeInfo = new CakeInfo(cake.getId(), cakeToppingInfo, cakeLayerInfos);
|
||||
result.add(cakeInfo);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
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();
|
||||
if (next.getCake() == null) {
|
||||
result.add(new CakeToppingInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<CakeLayer> getAvailableLayerEntities() {
|
||||
CakeLayerDao bean = context.getBean(CakeLayerDao.class);
|
||||
List<CakeLayer> result = new ArrayList<>();
|
||||
Iterator<CakeLayer> iterator = bean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
CakeLayer next = iterator.next();
|
||||
if (next.getCake() == null) {
|
||||
result.add(next);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
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();
|
||||
if (next.getCake() == null) {
|
||||
result.add(new CakeLayerInfo(next.getId(), next.getName(), next.getCalories()));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CakeInfo> getAllCakes() {
|
||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||
List<CakeInfo> result = new ArrayList<>();
|
||||
Iterator<Cake> iterator = cakeBean.findAll().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Cake cake = iterator.next();
|
||||
CakeToppingInfo cakeToppingInfo =
|
||||
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(), cake
|
||||
.getTopping().getCalories());
|
||||
ArrayList<CakeLayerInfo> cakeLayerInfos = new ArrayList<CakeLayerInfo>();
|
||||
for (CakeLayer layer : cake.getLayers()) {
|
||||
cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories()));
|
||||
}
|
||||
CakeInfo cakeInfo = new CakeInfo(cake.getId(), cakeToppingInfo, cakeLayerInfos);
|
||||
result.add(cakeInfo);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -10,31 +10,31 @@ import java.util.Optional;
|
||||
*/
|
||||
public class CakeInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final CakeToppingInfo cakeToppingInfo;
|
||||
public final List<CakeLayerInfo> cakeLayerInfos;
|
||||
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;
|
||||
}
|
||||
|
||||
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 totalCalories=%d", id.get(), cakeToppingInfo,
|
||||
cakeLayerInfos, calculateTotalCalories());
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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 totalCalories=%d", id.get(),
|
||||
cakeToppingInfo, cakeLayerInfos, calculateTotalCalories());
|
||||
}
|
||||
}
|
||||
|
@ -14,59 +14,58 @@ import javax.persistence.ManyToOne;
|
||||
@Entity
|
||||
public class CakeLayer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
private int calories;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
private int calories;
|
||||
|
||||
public CakeLayer() {
|
||||
}
|
||||
@ManyToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeLayer(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
public CakeLayer() {}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public CakeLayer(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("id=%s name=%s calories=%d", id, name, calories);
|
||||
}
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("id=%s name=%s calories=%d", id, name, calories);
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
}
|
||||
|
@ -9,24 +9,24 @@ import java.util.Optional;
|
||||
*/
|
||||
public class CakeLayerInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final 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("CakeLayerInfo id=%d name=%s calories=%d", id.get(), name, 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("CakeLayerInfo id=%d name=%s calories=%d", id.get(), name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -14,59 +14,58 @@ import javax.persistence.OneToOne;
|
||||
@Entity
|
||||
public class CakeTopping {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int calories;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public CakeTopping() {
|
||||
}
|
||||
|
||||
public CakeTopping(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
private String name;
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
private int calories;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Cake cake;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public CakeTopping() {}
|
||||
|
||||
public int getCalories() {
|
||||
return calories;
|
||||
}
|
||||
public CakeTopping(String name, int calories) {
|
||||
this.setName(name);
|
||||
this.setCalories(calories);
|
||||
}
|
||||
|
||||
public void setCalories(int calories) {
|
||||
this.calories = calories;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("id=%s name=%s calories=%d", name, calories);
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
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("id=%s name=%s calories=%d", name, calories);
|
||||
}
|
||||
|
||||
public Cake getCake() {
|
||||
return cake;
|
||||
}
|
||||
|
||||
public void setCake(Cake cake) {
|
||||
this.cake = cake;
|
||||
}
|
||||
}
|
||||
|
@ -9,24 +9,24 @@ import java.util.Optional;
|
||||
*/
|
||||
public class CakeToppingInfo {
|
||||
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final int calories;
|
||||
public final Optional<Long> id;
|
||||
public final String name;
|
||||
public final 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("CakeToppingInfo id=%d name=%s calories=%d", id.get(), name, 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("CakeToppingInfo id=%d name=%s calories=%d", id.get(), name, calories);
|
||||
}
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ package com.iluwatar.layers;
|
||||
*/
|
||||
public class CakeViewImpl implements View {
|
||||
|
||||
private CakeBakingService cakeBakingService;
|
||||
private CakeBakingService cakeBakingService;
|
||||
|
||||
public CakeViewImpl(CakeBakingService cakeBakingService) {
|
||||
this.cakeBakingService = cakeBakingService;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
cakeBakingService.getAllCakes().stream().forEach((cake) -> System.out.println(cake));
|
||||
}
|
||||
public CakeViewImpl(CakeBakingService cakeBakingService) {
|
||||
this.cakeBakingService = cakeBakingService;
|
||||
}
|
||||
|
||||
public void render() {
|
||||
cakeBakingService.getAllCakes().stream().forEach((cake) -> System.out.println(cake));
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,6 @@ package com.iluwatar.layers;
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
void render();
|
||||
|
||||
void render();
|
||||
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.layers.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user