I'm trying to figure out some way to improve the code below. My main problem is the method getPrice
.
Some advices:
- In the
else
clause, I need to query the information in the database (using EJB injection). - The
ProductPrice
andMarketPrice
are very different JPA entities. So, I can't refactor them to use inheritance/composition (per example).
The code is weird, but I can't figure out a way to really improve.
import java.util.*;
import java.math.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
PriceService priceService = new PriceService();
ProductPrice productPrice = new ProductPrice();
ProductMarketPrice productMarketPrice = priceService.getPrice(productPrice, 1L);
System.out.println(productMarketPrice.getAdvice());
productMarketPrice = priceService.getPrice(null, 1L);
System.out.println(productMarketPrice.getAdvice());
}
}
class PriceService {
//@EJB
MarketPriceService marketPriceService = new MarketPriceService();
public ProductMarketPrice getPrice(ProductPrice productPrice, Long productId) {
BigDecimal price = BigDecimal.ZERO;
Date date = null;
boolean isFromProduct = false;
if (productPrice != null) {
price = productPrice.getPrice();
date = productPrice.getRegisterDate();
isFromProduct = true; // i need to know if the values are from 'one' or 'x'
} else {
MarketPrice marketPrice = this.marketPriceService.findTheMarketPriceForProduct(productId);
price = marketPrice.getPrice();
date = marketPrice.getDate();
}
return new ProductMarketPrice(price, date, isFromProduct);
}
}
class ProductPrice {
public BigDecimal getPrice() {
return new BigDecimal("1.00");
}
public Date getRegisterDate() {
return new Date();
}
}
class ProductMarketPrice {
private BigDecimal price;
private Date date;
private boolean isFromProduct;
public ProductMarketPrice(BigDecimal price, Date date, boolean isFromProduct){
this.price = price;
this.date = date;
this.isFromProduct = isFromProduct;
}
public String getAdvice() {
String answer = this.isFromProduct ? "yes" : "no";
return "Hi, user! This price and date is from our Product? " + answer + ". So, be sure about that when you do your stuff.";
}
}
class MarketPriceService {
public MarketPrice findTheMarketPriceForProduct(Long productId) {
return new MarketPrice();
}
}
class MarketPrice {
public BigDecimal getPrice() {
return new BigDecimal("2.00");
}
public Date getDate() {
return new Date();
}
}
You can find the working code here.
Thanks!
std::epsilon
). In 1st place I've learned SE Code Review requires working code to review, so working code should be shown. Just claiming that it's working in a larger context isn't really useful, regarding the unknown parts of it. I know that this restricts SE Code Review in some way, and may be that's one of the culprits for too low traffic and underestimation of this site. \$\endgroup\$SomeClass
,getSomething()
, ...), so it's still off-topic. If you cannot show the true code, then it just doesn't fit with this site. \$\endgroup\$