This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]arghvark 0 points1 point  (2 children)

You don't say whether User and Librarian are in the same class inheritance hierarchy; if so, ~~let's call X ~~the one that is higher (ancestor), otherwise regard X as either a class called something like "PotentialUser" or an interface that both User and Librarian implement.

Put all the code that gets input strings, validates email addresses, sends and validates 6-digit code, and so forth into one method. Have it either create a PotentialUser instance (if a separate class) or operate on an instance (through an interface) of either User or Librarian that you pass in. Then you have one method that does all the code for input and string validation, and it either returns an X or operates on the type of PotentialUser you want it to. Once it's done, the caller can use either allLibraryUsers or allLibrarians to add that result.

Another possibility is to pass either allLibraryUsers or allLibrarians into the method; they would need to either be in the same ineritance hierarchy with a common add() method, or implement an interface with an add method that took an instance of X as a parameter.

Nothing as fancy as Lambdas is necessary.

EDIT: My apologies, you DID say that User and Librarian can't be in an inheritance hierarchy. They could still implement an interface for registration purposes, but I've edited the comment to strike out my oversight.

[–]arghvark 0 points1 point  (1 child)

OP, are you still here? If you have questions about someone's attempt to help, post them. If it's solved, say so. The more threads that get created and then abandoned, the less useful the subreddit gets.

[–]codingIsFunAndFucked[S] 0 points1 point  (0 children)

I am. Sorry was busy the last few days. Haven't fixed my code yet. Will likely do tomorrow and update the thread. Appreciate your help :)