Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

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(); }

OpenStudy (microbot):

so what is your question? you could use switch statement also instead of if.

OpenStudy (anonymous):

is there anyway for me to make a 'default" return?

OpenStudy (microbot):

you mean a return outside of the ifs?

OpenStudy (microbot):

could you paste me all of your code so i can test it?

OpenStudy (microbot):

nvm think i can test it anyway

OpenStudy (anonymous):

i dont have a full code yet . this is just part of a big project.

OpenStudy (espex):

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.

OpenStudy (microbot):

yep.

OpenStudy (espex):

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; } }

OpenStudy (anonymous):

whats the default action?

OpenStudy (espex):

Whatever you wanted it to be.

OpenStudy (microbot):

you asked for a default return...that is what @eSpeX means.

OpenStudy (anonymous):

ok.

OpenStudy (anonymous):

thanks

OpenStudy (anonymous):

i will post another question so if you can help, ill be able to give u a medal

OpenStudy (microbot):

and what i wrote to you in the private messages :P

OpenStudy (espex):

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; }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!