public class Main {
public static void main
(String[] args
) { Order o = new Order(new Order.OrderParams("O-1", "C-1", 100.0));
o.applyPercentageDiscount(10);
Order.Printer.printDetails(o);
Customer c = new Customer(new Customer.CustomerParams(
"C-1", "Ana", "ana@example.com", "21 99999-0000"
));
Customer.Printer.printDetails(c);
}
}
class Order {
private double amount;
// Introduce Parameter Object + Encapsulate Field
public Order(OrderParams params) {
setOrderId(params.getOrderId());
setCustomerId(params.getCustomerId());
setAmount(params.getAmount());
}
// compat: construtor antigo delega
public Order
(String orderId,
String customerId,
double amount
) { this(new OrderParams(orderId, customerId, amount));
}
// Encapsulate Field
public String getOrderId
() { return orderId
; } public void setOrderId
(String orderId
) { if (orderId == null || orderId.trim().isEmpty())
this.orderId = orderId;
}
public String getCustomerId
() { return customerId
; } public void setCustomerId
(String customerId
) { if (customerId == null || customerId.trim().isEmpty())
this.customerId = customerId;
}
public double getAmount() { return amount; }
public void setAmount(double amount) {
this.amount = amount;
}
// Rename Method: applyDiscount -> applyPercentageDiscount
public void applyPercentageDiscount(double discountPercentage) {
validateDiscountRange(discountPercentage); // Extract Method
setAmount(calculateDiscountedAmount(discountPercentage)); // Extract Method
}
// Extract Method
private void validateDiscountRange(double discountPercentage) {
if (discountPercentage <= 0 || discountPercentage > 100) {
}
}
// Extract Method
private double calculateDiscountedAmount(double discountPercentage) {
return getAmount() * (1 - discountPercentage / 100.0);
}
// Move Method: impressão em classe interna dedicada
public static final class Printer {
private Printer() {}
public static void printDetails(Order order) {
System.
out.
println("Order ID: " + order.
getOrderId()); System.
out.
println("Customer ID: " + order.
getCustomerId()); System.
out.
printf("Amount: $%.2f%n", order.
getAmount()); }
}
// Introduce Parameter Object (classe interna)
public static final class OrderParams {
private final String customerId
; private final double amount;
public OrderParams
(String orderId,
String customerId,
double amount
) { if (orderId == null || orderId.trim().isEmpty())
if (customerId == null || customerId.trim().isEmpty())
this.orderId = orderId;
this.customerId = customerId;
this.amount = amount;
}
public String getOrderId
() { return orderId
; } public String getCustomerId
() { return customerId
; } public double getAmount() { return amount; }
}
}
class Customer {
// Introduce Parameter Object + Encapsulate Field
public Customer(CustomerParams params) {
setCustomerId(params.getCustomerId());
setName(params.getName());
setEmail(params.getEmail());
setPhoneNumber(params.getPhoneNumber());
}
// compat: construtor antigo delega
this(new CustomerParams(customerId, name, email, phoneNumber));
}
// Encapsulate Field
public String getCustomerId
() { return customerId
; } public void setCustomerId
(String customerId
) { if (customerId == null || customerId.trim().isEmpty())
this.customerId = customerId;
}
public String getName
() { return name
; } public void setName
(String name
) { if (name == null || name.trim().isEmpty())
this.name = name;
}
public String getEmail
() { return email
; } public void setEmail
(String email
) { if (email == null || email.trim().isEmpty())
this.email = email;
}
public String getPhoneNumber
() { return phoneNumber
; } public void setPhoneNumber
(String phoneNumber
) { if (phoneNumber == null || phoneNumber.trim().isEmpty())
this.phoneNumber = phoneNumber;
}
// Move Method: impressão em classe interna dedicada
public static final class Printer {
private Printer() {}
public static void printDetails(Customer customer) {
System.
out.
println("Customer ID: " + customer.
getCustomerId()); System.
out.
println("Name: " + customer.
getName()); System.
out.
println("Email: " + customer.
getEmail()); System.
out.
println("Phone Number: " + customer.
getPhoneNumber()); }
}
// Introduce Parameter Object (classe interna)
public static final class CustomerParams {
private final String customerId
; private final String phoneNumber
;
if (customerId == null || customerId.trim().isEmpty())
if (name == null || name.trim().isEmpty())
if (email == null || email.trim().isEmpty())
if (phoneNumber == null || phoneNumber.trim().isEmpty())
this.customerId = customerId;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
}
public String getCustomerId
() { return customerId
; } public String getName
() { return name
; } public String getEmail
() { return email
; } public String getPhoneNumber
() { return phoneNumber
; } }
}