Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Issue#2976 - Refactoring for the code smells. #2988

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions layered-architecture/src/main/java/dto/CakeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,23 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package dto;

import java.util.List;
import java.util.Optional;

/**
* DTO for cakes.
*/
public class CakeInfo {

public final Optional<Long> id;
public final Long id;
public final CakeToppingInfo cakeToppingInfo;
public final List<CakeLayerInfo> cakeLayerInfos;

/**
* Constructor.
*/
public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
this.id = Optional.of(id);
this.id = id;
this.cakeToppingInfo = cakeToppingInfo;
this.cakeLayerInfos = cakeLayerInfos;
}
Expand All @@ -50,7 +47,7 @@ public CakeInfo(Long id, CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> ca
* Constructor.
*/
public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerInfos) {
this.id = Optional.empty();
this.id = null;
this.cakeToppingInfo = cakeToppingInfo;
this.cakeLayerInfos = cakeLayerInfos;
}
Expand All @@ -59,14 +56,14 @@ public CakeInfo(CakeToppingInfo cakeToppingInfo, List<CakeLayerInfo> cakeLayerIn
* Calculate calories.
*/
public int calculateTotalCalories() {
var total = cakeToppingInfo != null ? cakeToppingInfo.calories : 0;
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.orElse(-1L),
return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id,
cakeToppingInfo, cakeLayerInfos, calculateTotalCalories());
}
}
}
10 changes: 4 additions & 6 deletions layered-architecture/src/main/java/dto/CakeLayerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@

package dto;

import java.util.Optional;

/**
* DTO for cake layers.
*/
public class CakeLayerInfo {

public final Optional<Long> id;
public final Long id;
public final String name;
public final int calories;

/**
* Constructor.
*/
public CakeLayerInfo(Long id, String name, int calories) {
this.id = Optional.of(id);
this.id = id;
this.name = name;
this.calories = calories;
}
Expand All @@ -49,13 +47,13 @@ public CakeLayerInfo(Long id, String name, int calories) {
* Constructor.
*/
public CakeLayerInfo(String name, int calories) {
this.id = Optional.empty();
this.id = null;
this.name = name;
this.calories = calories;
}

@Override
public String toString() {
return String.format("CakeLayerInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories);
return String.format("CakeLayerInfo id=%d name=%s calories=%d", id, name, calories);
}
}
11 changes: 4 additions & 7 deletions layered-architecture/src/main/java/dto/CakeToppingInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@

package dto;


import java.util.Optional;

/**
* DTO for cake toppings.
*/
public class CakeToppingInfo {

public final Optional<Long> id;
public final Long id;
public final String name;
public final int calories;

/**
* Constructor.
*/
public CakeToppingInfo(Long id, String name, int calories) {
this.id = Optional.of(id);
this.id = id;
this.name = name;
this.calories = calories;
}
Expand All @@ -50,14 +47,14 @@ public CakeToppingInfo(Long id, String name, int calories) {
* Constructor.
*/
public CakeToppingInfo(String name, int calories) {
this.id = Optional.empty();
this.id = null;
this.name = name;
this.calories = calories;
}

@Override
public String toString() {
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.orElse(-1L), name,
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id, name,
calories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package exception;

import java.io.Serial;
import org.springframework.stereotype.Component;

/**
Expand All @@ -34,7 +33,7 @@
@Component
public class CakeBakingException extends Exception {

@Serial
@java.io.Serial
private static final long serialVersionUID = 1L;

public CakeBakingException() {
Expand All @@ -43,4 +42,4 @@ public CakeBakingException() {
public CakeBakingException(String message) {
super(message);
}
}
}
12 changes: 0 additions & 12 deletions layered-architecture/src/main/java/service/CakeBakingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,13 @@ public interface CakeBakingService {
*/
void saveNewTopping(CakeToppingInfo toppingInfo);

/**
* Get available cake toppings.
*/
List<CakeToppingInfo> getAvailableToppings();

/**
* Add new cake layer.
*/
void saveNewLayer(CakeLayerInfo layerInfo);

/**
* Get available cake layers.
*/
List<CakeLayerInfo> getAvailableLayers();

void deleteAllCakes();

void deleteAllLayers();

void deleteAllToppings();

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
* @param cakeToppingDao the DAO for cake topping-related operations
*/
@Autowired
public CakeBakingServiceImpl(CakeDao cakeDao, CakeLayerDao cakeLayerDao,
CakeToppingDao cakeToppingDao) {
public CakeBakingServiceImpl(CakeDao cakeDao, CakeLayerDao cakeLayerDao, CakeToppingDao cakeToppingDao) {
this.cakeDao = cakeDao;
this.cakeLayerDao = cakeLayerDao;
this.cakeToppingDao = cakeToppingDao;
Expand All @@ -72,18 +71,20 @@ public CakeBakingServiceImpl(CakeDao cakeDao, CakeLayerDao cakeLayerDao,
@Override
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
var allToppings = getAvailableToppingEntities();
var matchingToppings =
allToppings.stream().filter(t -> t.getName().equals(cakeInfo.cakeToppingInfo.name))
.toList();
var matchingToppings = allToppings.stream()
.filter(t -> t.getName().equals(cakeInfo.cakeToppingInfo.name))
.toList();
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));
}
var allLayers = getAvailableLayerEntities();
Set<CakeLayer> foundLayers = new HashSet<>();
for (var info : cakeInfo.cakeLayerInfos) {
var found = allLayers.stream().filter(layer -> layer.getName().equals(info.name)).findFirst();
if (found.isEmpty()) {
var found = allLayers.stream()
.filter(layer -> layer.getName().equals(info.name))
.findFirst();
boolean isFound = found.isPresent();
if (!isFound) {
throw new CakeBakingException(String.format("Layer %s is not available", info.name));
} else {
foundLayers.add(found.get());
Expand Down Expand Up @@ -132,7 +133,11 @@ private List<CakeTopping> getAvailableToppingEntities() {
return result;
}

@Override
/**
* Gets available toppings.
*
* @return a list of available CakeToppingInfo objects.
*/
public List<CakeToppingInfo> getAvailableToppings() {
List<CakeToppingInfo> result = new ArrayList<>();
for (CakeTopping next : cakeToppingDao.findAll()) {
Expand All @@ -153,7 +158,11 @@ private List<CakeLayer> getAvailableLayerEntities() {
return result;
}

@Override
/**
* Gets available layers.
*
* @return a list of available CakeLayerInfo objects.
*/
public List<CakeLayerInfo> getAvailableLayers() {
List<CakeLayerInfo> result = new ArrayList<>();
for (CakeLayer next : cakeLayerDao.findAll()) {
Expand All @@ -164,17 +173,14 @@ public List<CakeLayerInfo> getAvailableLayers() {
return result;
}

@Override
public void deleteAllCakes() {
cakeDao.deleteAll();
}

@Override
public void deleteAllLayers() {
cakeLayerDao.deleteAll();
}

@Override
public void deleteAllToppings() {
cakeToppingDao.deleteAll();
}
Expand All @@ -183,9 +189,11 @@ public void deleteAllToppings() {
public List<CakeInfo> getAllCakes() {
List<CakeInfo> result = new ArrayList<>();
for (Cake cake : cakeDao.findAll()) {
var cakeToppingInfo =
new CakeToppingInfo(cake.getTopping().getId(), cake.getTopping().getName(),
cake.getTopping().getCalories());
var cakeToppingInfo = new CakeToppingInfo(
cake.getTopping().getId(),
cake.getTopping().getName(),
cake.getTopping().getCalories()
);
List<CakeLayerInfo> cakeLayerInfos = new ArrayList<>();
for (var layer : cake.getLayers()) {
cakeLayerInfos.add(new CakeLayerInfo(layer.getId(), layer.getName(), layer.getCalories()));
Expand Down
2 changes: 0 additions & 2 deletions layered-architecture/src/main/java/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@
*/
public interface View {

void render();

}
Loading
Loading