Question
I am tryignt o change these factorymethod codes to Simplefactory method: What changes will require? public abstract class Customer{ protected String customerName; protected String customerAddress;
I am tryignt o change these "factorymethod" codes to "Simplefactory" method:
What changes will require?
public abstract class Customer{ protected String customerName; protected String customerAddress; protected String customerPhoneNumber; public Customer(String customerName, String customerAddress, String customerPhoneNumber) { this.customerName = customerName; this.customerAddress = customerAddress; this.customerPhoneNumber = customerPhoneNumber; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public String getCustomerAddress() { return customerAddress; } public void setCustomerAddress(String customerAddress) { this.customerAddress = customerAddress; } public String getCustomerPhoneNumber() { return customerPhoneNumber; } public void setCustomerPhoneNumber(String customerPhoneNumber) { this.customerPhoneNumber = customerPhoneNumber; } public abstract String getCustomerType();
public class CustomerFactory { public static Customer getCustomer (String customerType, String customerName, String customerAddress, String customerPhoneNumber ) { switch (customerType) { case "Standard": return new StandardCustomer (customerName, customerAddress, customerPhoneNumber); case "Preferred": return new PreferredCustomer (customerName, customerAddress, customerPhoneNumber); case "Business": return new BusinessCustomer (customerName, customerAddress, customerPhoneNumber); default: return null; } }
} public class StandardCustomer extends Customer{ public StandardCustomer(String customerName, String customerAddress, String customerPhoneNumber) { super(customerName, customerAddress, customerPhoneNumber); } @Override public String getCustomerType() { return "Standard"; } } public class PreferredCustomer extends Customer{ public PreferredCustomer(String customerName, String customerAddress, String customerPhoneNumber) { super(customerName, customerAddress, customerPhoneNumber); } @Override public String getCustomerType() { return "Preferred"; } } public class BusinessCustomer extends Customer { public BusinessCustomer(String customerName, String customerAddress, String customerPhoneNumber) { super(customerName, customerAddress, customerPhoneNumber); } @Override public String getCustomerType() { return "Business"; }
} public class CustomerTest {
public static void main(String[] args) { Customer standardCustomer = CustomerFactory.getCustomer("Standard", "Atul A", "123 Main St", "555-555-1212"); System.out.println("Standard Customer:"); System.out.println("Name: " + standardCustomer.getCustomerName()); System.out.println("Address: " + standardCustomer.getCustomerAddress()); System.out.println("Phone Number: " + standardCustomer.getCustomerPhoneNumber()); System.out.println("Customer Type: " + standardCustomer.getCustomerType()); System.out.println(); Customer preferredCustomer = CustomerFactory.getCustomer("Preferred", "Atul B", "456 Main St", "555-555-1212"); System.out.println("Preferred Customer:"); System.out.println("Name: " + preferredCustomer.getCustomerName()); System.out.println("Address: " + preferredCustomer.getCustomerAddress()); System.out.println("Phone Number: " + preferredCustomer.getCustomerPhoneNumber()); System.out.println("Customer Type: " + preferredCustomer.getCustomerType()); System.out.println(); Customer businessCustomer = CustomerFactory.getCustomer("Business", "Atul Corporation", "789 Main St", "555-555-1212"); System.out.println("Business Customer:"); System.out.println("Name: " + businessCustomer.getCustomerName()); System.out.println("Address: " + businessCustomer.getCustomerAddress()); System.out.println("Phone Number: " + businessCustomer.getCustomerPhoneNumber()); System.out.println("Customer Type: " + businessCustomer.getCustomerType()); }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started