From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating an iterable

Creating an iterable - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Creating an iterable

- [Instructor] In this example, I have an app that keeps track of stock for a store that sells stationary. I want to be able to iterate over the items and print out just the ones that are in stock. So first of all, there is a class called Item, which has two fields. One of type string called "name," and the other of type int called "quantity." Both of these get set by variables. That's apassed into the constructor. And then there are get methods for each field. So there's one called "getName" and one cause "getQuantity." Next there is a Main class with a Main method. In here I'm creating three item objects. The first one has the name "pens" under quantity of 175. The second one is "pencils" and the quantity is zero. So it's out of stock. And the third one is "paper," and this has a quantity of 500. So the first thing I want to do is create a collection of items of stock. That's like an iterate saver. So I'm going…

Contents