package za.co.admatech.factory;
/**
* CustomerFactory.java
* CustomerFactory Factory class
*
* Author: Rorisang Makgana (xxxxxxxxx)
*/
import za.co.admatech.domain.*;
import za.co.admatech.util.Helper;
public class CustomerFactory {
public static Customer createCustomer(
String customerID,
String firstName,
String lastName,
String email,
// Cart cartID, // Uncomment when Cart is implemented
Address address) {
// Validate required fields
if (Helper.isNullOrEmpty(customerID)) {
throw new IllegalArgumentException("Customer ID is required.");
}
if (Helper.isNullOrEmpty(firstName)) {
throw new IllegalArgumentException("First name is required.");
}
if (Helper.isNullOrEmpty(lastName)) {
throw new IllegalArgumentException("Last name is required.");
}
if (Helper.isNullOrEmpty(email) || !Helper.isValidEmail(email)) {
throw new IllegalArgumentException("A valid email is required.");
}
if (address == null) {
throw new IllegalArgumentException("Address is required.");
}
// If all validations pass, build and return Customer object
return new Customer.Builder()
.setCustomerID(customerID)
.setFirstName(firstName)
.setLastName(lastName)
.setEmail(email)
//.setCartID(cartID) // Uncomment when applicable
.setAddress(address)
.build();
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... g-an-objec