Lets say i have several methods like add, delete, retrieve, and view an array and i want to run each at a specific time, maybe in a certain process. Ive made a program that does this however it requires me to call the method one after the other which seems more procedural programming rather than object oriented. Is there another way to have all these methods created and called at a certain time while also looping the entire process. Essentially my logic is this: User is asked to pick from 3 options, 1 add, 2 view, 3 delete. if they pick 1 they go on to the add method and add to the array, then the program should ask the user again for 1 of the 3 options. right now i have this being done by giving the options and under each option is className.methodName(); How could i make this more object oriented.
That is the old‑fashioned way to display a menu. You end up with something like this:-That menu code looks the same in procedural and object oriented code. It says somewhere in Bertrand Meyer's old book Object‑Oriented Software Construction that object orientation doesn't alter the structure of algorithms nor make them shorter to write. The only way I can think of to make a menu more object‑oriented would be to write a GUI and link each action to a different control or component. There are stylistic improvements you can make to my code. You can also use a utility class for keyboard input, which I think is more reliable than a “naked” Scanner.
Campbell, you didn't define a case 0 clause. Even though it does nothing, without it, the default will be taken and it will print the error message.
A simple menu class could execute its options directly, as shown. A more complex one might allow registering callbacks. And in an object-oriented design, it would be more common to decouple the option input code from the menu dispatcher.
Loudly announcing something is true and finding out you're wrong makes you feel foolish.
Finding out you're wrong and refusing to admit it makes you LOOK foolish.
When I saw Campbell's response I scrolled past the top line with his name on it and directly looked at the code and the exegesis.
When I saw the name of Bertrand Meyer invoked, I knew for sure it was Campbell.
I remembered that quote, but also came to it myself after studying OOPS seriously the first time.
For me, what my brain formulated was "hard algorithms are still hard, the hard parts of programming are still there, and still kinda hard".
The first time I had seen that was in the mid-1980's.
Lord British had been interviewed, after releasing Ultima IV, the first of his games to be designed using such methodologies (~1985?).
His quote in Electronic Games magazine or some such place was "It really is a very nice tool for organizing things, but the hard parts are still hard!"
I lent the magazine to a dear friend who passed away in 1992. We had spent forever discussing programming together. I never got the magazine back, but I remembered (at least a paraphrase of) the Lord British quote.
Right now i have the program running like this, the menu is given and depending on user input, a method is called. After the method is completed, className.menu(); is called to essentially loop the process. Is this the standard process of doing this?
Edit: I got this feedback to do this but im not too sure what to do with "splitting user interaction and data"
Ive made some edits to the original code. ive removed PhoneBook.AddNumber(); and PhoneBook.CallNumber(); as task will call them when necessary. How would i go about removing the PhoneBook.task(); from the other methods and simply have a loop to run the same process. And in general what is the best way to avoid using so many static methods?
Suggest this for getting rid of statics. Replace this:
Then have your PhoneBook class have fields scan and numbers so you don't have to constantly pass them around to your methods. Then make your methods non-static.
P.S. Never, ever close() a scanner made from System.in.
Could you please expand on why i dont close scanner? Last time i posted a question on Stack Exchange Code Review, i was told i MUST close scanners when they are done being used, thats why i added the try resource block in the first place.
Carey wrote close() with (), but you shouldn't call close() on most things. You should use Try‑with‑Resources instead. For the same reasons, you should not use try with resources on System.in, System.err and System.out. If Eclipse complains, pick the @SuppressWarnings("resource") option.