fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.math.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. PriceService priceService = new PriceService();
  14. ProductPrice productPrice = new ProductPrice();
  15. ProductMarketPrice productMarketPrice = priceService.getPrice(productPrice, 1L);
  16. System.out.println(productMarketPrice.getAdvice());
  17.  
  18. productMarketPrice = priceService.getPrice(null, 1L);
  19. System.out.println(productMarketPrice.getAdvice());
  20. }
  21. }
  22.  
  23. class PriceService {
  24.  
  25. //@EJB
  26. MarketPriceService marketPriceService = new MarketPriceService();
  27.  
  28. public ProductMarketPrice getPrice(ProductPrice productPrice, Long productId) {
  29. BigDecimal price = BigDecimal.ZERO;
  30. Date date = null;
  31. boolean isFromProduct = false;
  32. if (productPrice != null) {
  33. price = productPrice.getPrice();
  34. date = productPrice.getRegisterDate();
  35. isFromProduct = true; // i need to know if the values are from 'one' or 'x'
  36. } else {
  37. MarketPrice marketPrice = this.marketPriceService.findTheMarketPriceForProduct(productId);
  38. price = marketPrice.getPrice();
  39. date = marketPrice.getDate();
  40. }
  41.  
  42. return new ProductMarketPrice(price, date, isFromProduct);
  43. }
  44.  
  45. }
  46.  
  47. class ProductPrice {
  48. public BigDecimal getPrice() {
  49. return new BigDecimal("1.00");
  50. }
  51.  
  52. public Date getRegisterDate() {
  53. return new Date();
  54. }
  55. }
  56.  
  57.  
  58. class ProductMarketPrice {
  59. private BigDecimal price;
  60. private Date date;
  61. private boolean isFromProduct;
  62. public ProductMarketPrice(BigDecimal price, Date date, boolean isFromProduct){
  63. this.price = price;
  64. this.date = date;
  65. this.isFromProduct = isFromProduct;
  66. }
  67. public String getAdvice() {
  68. String answer = this.isFromProduct ? "yes" : "no";
  69. return "Hi, user! This price and date is from our Product? " + answer + ". So, be sure about that when you do your stuff.";
  70. }
  71. }
  72.  
  73. class MarketPriceService {
  74. public MarketPrice findTheMarketPriceForProduct(Long productId) {
  75. return new MarketPrice();
  76. }
  77. }
  78.  
  79. class MarketPrice {
  80. public BigDecimal getPrice() {
  81. return new BigDecimal("2.00");
  82. }
  83.  
  84. public Date getDate() {
  85. return new Date();
  86. }
  87. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
Hi, user! This price and date is from our Product? yes. So, be sure about that when you do your stuff.
Hi, user! This price and date is from our Product? no. So, be sure about that when you do your stuff.