Skip to main content

Iterator Pattern | Demo Project | Learn Java

PRACTICAL USAGE of ITERATOR PATTERN in JAVA

Requirement

There is a restaurant called "big mama restaurant" which provides
Italian meals and Chinese meals.
They provide Italian and Chinese meals in the separate menu list. Not in the same Menu.
They have only five Chinese meals.
A meal has details such as type (vegetarian or non-vegetarian), name, description, and price.
There is a waiter in the restaurant who reads the Italian and Chinese food menu when required.
The waiter reads only one Italian or Chinese menu at once as required.

Pre-required knowledge of,

Objects, Arrays, and Arraylist.
Java class, interface, and inheritance.
Encapsulation.
Abstract methods and overriding.
Basic loop.

And,

Make Chinese menu as an Array.
Make Italian menu as an Arraylist.
Users can add meals to each menu list.
Give a solution to this waiter to read each menu.

Major steps before that need to think before and while,

1) Create meals and a menu

Create a class called meal which has fields called meal type, meal name, description, price.
Encapsulate these fields. Assign all received fields in the constructor and also getter (get fields) methods.
Create a class called Chinese menu, Create an instance array called Chinese menu.
Create a method called add meal in Chinese menu class that can add meal objects to the Chinese menu array.
Create a class called Italian menu, Create instance ArrayList called Italian menu.
Create a method called add meal in the Italian menu class that can add meal objects to the Italian menu ArrayList.

2) Read the menu

This part is a bit hard to think of for beginners, follow the sample project given and try to understand with the steps below.

Create an interface called an iterator.
Create a public abstract method called HasNext with a boolean return type.
Create a public abstract method called Next with an Object return type.
Create a class called Chinese menu iterator and implement interface iterator.
Override abstract methods.
Create an instance array called the Chinese menu and assign the received array in the constructor.
Create an instance variable (int) for the position.
Create a class called Italian menu iterator and implement interface iterator.
Override abstract methods.
Create an instance ArrayList called the Italian menu and assign the received ArrayList in the constructor.
Create an instance variable (int) for the position.
In the override d HasNext method in both classes,
Return true if the Array/ArrayList has another element from the current element.
Return false if the Array/Arraylist does not has another element from the current element.
At the last element, this must return false.
In the override d Next method in both classes,
Return next element object (meal).
In the Chinese menu class created before (1),
Create a method called get meal that returns iterator parent class object.
Create a new object of the Chinese menu iterator and pass the array Chinese menu.
Return the object Chinese menu iterator.
In the Italian menu class created before (1),
Create a method called to get a meal that returns iterator parent class object.
Create a new object of the Italian menu iterator and pass the ArrayList Italian menu.
Return the object Italian menu iterator.
Create a class called the waiter.
Create a method called read menu with a parameter of the iterator parent class.
read each meal and return details while iterator (Chinese menu array or Italian menu ArrayList) has next.
print details.

3) Run

Create a demo class and create the main method.
Create a new Chinese menu object and add meals.
Create a new Italian menu object and add meals.
Create a new waiter object.
Read (Chinese) menu by passing the meal of Chinese menu.
Read (Italian) menu by passing the meal of Italian menu.
Run the main method and That will print all meals and details.

That's all !!!

Make your own project and practice. Memorize the main steps and keep thinking while developing.
Follow the sample project given. The project is made using an IDE called Netbeans.
Find the src folder and copy files/ java classes if you use another IDE.
This pattern is fast, efficient, and clear. You can apply this pattern for any requirements like this.
Hope you enjoyed it, learned something new, and helped for better work.
Thank you for reading. See you with another practical.

Download demo project here

Comments

Popular

Tkinter Login System | Demo Project | Learn Python

Python practical Requirement Simple user registration and login application. Three interfaces minimum Sign-in: This is the main interface. Enter email and password, then log in. And the option to create accounts for new users. Sign-up: This interface is used to register new users. All user details are saved in a text file. You can build your own structure to save data. Dashboard: This interface will pop up when the user sign-in is successful. This interface has a menu bar with submenu items and a simple text file. You can customize this as your own. All registered users are saved in a text file called “users”. Read the text file when the user is sign-in and write when the new user sign-up. Interfaces This is the main interface of the program also the sign-in interface. Enter email and password and press the sign-in button. The dashboard will be pop up if login is successful else show an error message. This is the interface when the user wants to create a new account. This inter...

Chatting Application | Demo Project | Learn Java

How to make a simple chatting application Java Web? REQUIREMENT Simple chat application. Users can register their names to the application. After login, the user can find any registered friend and start a chat. If a user had previous chat with a selected friend, it should load in chat view. Sent messages and received messages must show without affecting the chat page. ASSUMPTIONS for DEMO PROJECT Usernames do not duplicate. Register different names, login, and chat with others. No need of sending pictures, videos, etc. Just create a text message chat. This demo project is not about user registration, login, file uploading… etc. This demo project is about chatting between two users and how to keep and update chats using a data structure. IMPROVEMENTS Develop better user registration and login system with generating unique user ids to avoid duplicate issues. Develop privilege management for users. Develop chat that can send images, videos too. OPTIONAL Do not use MYSQL or any other DBMS...

How to get a proper Affidavit in Sri Lanka?

What is an Affidavit? An affidavit is a written statement by a person under an oath or affirmation. An affidavit is administered, signed, and sealed by a registered lawyer, the high court, and the ministry. An affidavit includes a related person's personal details, family details, document details, and a comparison of each document detail.  An affidavit includes an oath or affirmative statement to each document's comparison is true and it states about the same person even if there are some differences. An affidavit includes several signs and seals at the bottom of the statement. 1. Related person's signature. 2. The lawyer's name, signature, and seal. 3. The ministry's signature, and seal. The lawyer,  1. Should be registered in the high court. 2. Should have a valid seal. 3. Should able to issue high court receipts. The lawyer should meet all the requirements above to certify a valid affidavit What is the routine of collecting a proper affidavit in Sri Lanka? First...