fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. Order o = new Order(new Order.OrderParams("O-1", "C-1", 100.0));
  4. o.applyPercentageDiscount(10);
  5. Order.Printer.printDetails(o);
  6.  
  7. Customer c = new Customer(new Customer.CustomerParams(
  8. "C-1", "Ana", "ana@example.com", "21 99999-0000"
  9. ));
  10. Customer.Printer.printDetails(c);
  11. }
  12. }
  13.  
  14.  
  15. class Order {
  16. private String orderId;
  17. private String customerId;
  18. private double amount;
  19.  
  20. // Introduce Parameter Object + Encapsulate Field
  21. public Order(OrderParams params) {
  22. setOrderId(params.getOrderId());
  23. setCustomerId(params.getCustomerId());
  24. setAmount(params.getAmount());
  25. }
  26.  
  27. // compat: construtor antigo delega
  28. public Order(String orderId, String customerId, double amount) {
  29. this(new OrderParams(orderId, customerId, amount));
  30. }
  31.  
  32. // Encapsulate Field
  33. public String getOrderId() { return orderId; }
  34. public void setOrderId(String orderId) {
  35. if (orderId == null || orderId.trim().isEmpty())
  36. throw new IllegalArgumentException("orderId inválido");
  37. this.orderId = orderId;
  38. }
  39.  
  40. public String getCustomerId() { return customerId; }
  41. public void setCustomerId(String customerId) {
  42. if (customerId == null || customerId.trim().isEmpty())
  43. throw new IllegalArgumentException("customerId inválido");
  44. this.customerId = customerId;
  45. }
  46.  
  47. public double getAmount() { return amount; }
  48. public void setAmount(double amount) {
  49. if (amount < 0) throw new IllegalArgumentException("amount não pode ser negativo");
  50. this.amount = amount;
  51. }
  52.  
  53. // Rename Method: applyDiscount -> applyPercentageDiscount
  54. public void applyPercentageDiscount(double discountPercentage) {
  55. validateDiscountRange(discountPercentage); // Extract Method
  56. setAmount(calculateDiscountedAmount(discountPercentage)); // Extract Method
  57. }
  58.  
  59. // Extract Method
  60. private void validateDiscountRange(double discountPercentage) {
  61. if (discountPercentage <= 0 || discountPercentage > 100) {
  62. throw new IllegalArgumentException("Desconto deve estar entre (0, 100].");
  63. }
  64. }
  65.  
  66. // Extract Method
  67. private double calculateDiscountedAmount(double discountPercentage) {
  68. return getAmount() * (1 - discountPercentage / 100.0);
  69. }
  70.  
  71. // Move Method: impressão em classe interna dedicada
  72. public static final class Printer {
  73. private Printer() {}
  74. public static void printDetails(Order order) {
  75. System.out.println("Order ID: " + order.getOrderId());
  76. System.out.println("Customer ID: " + order.getCustomerId());
  77. System.out.printf("Amount: $%.2f%n", order.getAmount());
  78. }
  79. }
  80.  
  81. // Introduce Parameter Object (classe interna)
  82. public static final class OrderParams {
  83. private final String orderId;
  84. private final String customerId;
  85. private final double amount;
  86.  
  87. public OrderParams(String orderId, String customerId, double amount) {
  88. if (orderId == null || orderId.trim().isEmpty())
  89. throw new IllegalArgumentException("orderId inválido");
  90. if (customerId == null || customerId.trim().isEmpty())
  91. throw new IllegalArgumentException("customerId inválido");
  92. if (amount < 0) throw new IllegalArgumentException("amount não pode ser negativo");
  93. this.orderId = orderId;
  94. this.customerId = customerId;
  95. this.amount = amount;
  96. }
  97.  
  98. public String getOrderId() { return orderId; }
  99. public String getCustomerId() { return customerId; }
  100. public double getAmount() { return amount; }
  101. }
  102. }
  103.  
  104.  
  105. class Customer {
  106. private String customerId;
  107. private String name;
  108. private String email;
  109. private String phoneNumber;
  110.  
  111. // Introduce Parameter Object + Encapsulate Field
  112. public Customer(CustomerParams params) {
  113. setCustomerId(params.getCustomerId());
  114. setName(params.getName());
  115. setEmail(params.getEmail());
  116. setPhoneNumber(params.getPhoneNumber());
  117. }
  118.  
  119. // compat: construtor antigo delega
  120. public Customer(String customerId, String name, String email, String phoneNumber) {
  121. this(new CustomerParams(customerId, name, email, phoneNumber));
  122. }
  123.  
  124. // Encapsulate Field
  125. public String getCustomerId() { return customerId; }
  126. public void setCustomerId(String customerId) {
  127. if (customerId == null || customerId.trim().isEmpty())
  128. throw new IllegalArgumentException("customerId inválido");
  129. this.customerId = customerId;
  130. }
  131.  
  132. public String getName() { return name; }
  133. public void setName(String name) {
  134. if (name == null || name.trim().isEmpty())
  135. throw new IllegalArgumentException("name inválido");
  136. this.name = name;
  137. }
  138.  
  139. public String getEmail() { return email; }
  140. public void setEmail(String email) {
  141. if (email == null || email.trim().isEmpty())
  142. throw new IllegalArgumentException("email inválido");
  143. this.email = email;
  144. }
  145.  
  146. public String getPhoneNumber() { return phoneNumber; }
  147. public void setPhoneNumber(String phoneNumber) {
  148. if (phoneNumber == null || phoneNumber.trim().isEmpty())
  149. throw new IllegalArgumentException("phone inválido");
  150. this.phoneNumber = phoneNumber;
  151. }
  152.  
  153. // Move Method: impressão em classe interna dedicada
  154. public static final class Printer {
  155. private Printer() {}
  156. public static void printDetails(Customer customer) {
  157. System.out.println("Customer ID: " + customer.getCustomerId());
  158. System.out.println("Name: " + customer.getName());
  159. System.out.println("Email: " + customer.getEmail());
  160. System.out.println("Phone Number: " + customer.getPhoneNumber());
  161. }
  162. }
  163.  
  164. // Introduce Parameter Object (classe interna)
  165. public static final class CustomerParams {
  166. private final String customerId;
  167. private final String name;
  168. private final String email;
  169. private final String phoneNumber;
  170.  
  171. public CustomerParams(String customerId, String name, String email, String phoneNumber) {
  172. if (customerId == null || customerId.trim().isEmpty())
  173. throw new IllegalArgumentException("customerId inválido");
  174. if (name == null || name.trim().isEmpty())
  175. throw new IllegalArgumentException("name inválido");
  176. if (email == null || email.trim().isEmpty())
  177. throw new IllegalArgumentException("email inválido");
  178. if (phoneNumber == null || phoneNumber.trim().isEmpty())
  179. throw new IllegalArgumentException("phone inválido");
  180. this.customerId = customerId;
  181. this.name = name;
  182. this.email = email;
  183. this.phoneNumber = phoneNumber;
  184. }
  185.  
  186. public String getCustomerId() { return customerId; }
  187. public String getName() { return name; }
  188. public String getEmail() { return email; }
  189. public String getPhoneNumber() { return phoneNumber; }
  190. }
  191. }
  192.  
  193.  
Success #stdin #stdout 0.16s 56172KB
stdin
Standard input is empty
stdout
Order ID: O-1
Customer ID: C-1
Amount: $90.00
Customer ID: C-1
Name: Ana
Email: ana@example.com
Phone Number: 21 99999-0000