i need to make a FACTORY METHOD (java) that returns diff types of customers, based on the number given: public Customer createCustomer(int customerCode ){ if(customerCode==0) return new Teenager(); if(customerCode==1) return new Dealer(); if(customerCode==2) return new CustomerLicenser(); }
so what is your question? you could use switch statement also instead of if.
is there anyway for me to make a 'default" return?
you mean a return outside of the ifs?
could you paste me all of your code so i can test it?
nvm think i can test it anyway
i dont have a full code yet . this is just part of a big project.
If you are using 'if' statements you can set an 'else' as your default response, or if you want to use a switch statement you can create a 'default' that will execute if no other case is met.
yep.
public Customer createCustomer(int customerCode ){ if(customerCode==0) return new Teenager(); else if(customerCode==1) return new Dealer(); else if(customerCode==2) return new CustomerLicenser(); else { return default action; } }
whats the default action?
Whatever you wanted it to be.
you asked for a default return...that is what @eSpeX means.
ok.
thanks
i will post another question so if you can help, ill be able to give u a medal
and what i wrote to you in the private messages :P
And if you wanted to use a switch-case: public Customer createCustomer(int customerCode ){ Customer holder = new Customer(); switch (customerCode) { case 1: holder = new Teenager(); break; case 2: holder = new Dealer(); break; case 3: holder = new CustomerLicenser(); break; default: holder = new Customer(); break; } return holder; }
Join our real-time social learning platform and learn together with your friends!