Java OOP Concepts: The Barista

Java OOP Concepts: The Barista

Java is the 2nd programming language I am learning and although already knowing one language helps to understand the documentation of another, there is still a learning curve!

Already beyond fundamentals, I found myself applying OOP concepts for Cafe Java last week. Let's recap with one of the assignment projects, Barista.

Classes, Constructors, & Getters/Setters

I needed two objects that model specific behaviors and created two classes, MenuItem and Order. Each is its own file and has individual member variables, also referred to as attributes. I kept the attributes private to have control and security of the information as well as more flexibility to change them later without disrupting the interface. Below is the MenuItem class:

To access an instance of the class and to be able to get and set values, I added constructors along with getters and setters as seen below for MenuItems.

Methods

It would have been easy if my Order class was the same, however, I needed an ArrayList to hold possible multiple items in an order. I also practiced using overloaded constructors, which allow different methods of accessing the class (also known as instantiating). In addition, my Order class would also hold methods. I practiced with methods to add items, get order totals, get an order status message, and display orders. Check it out below:

Testing & Running

All that code looks great but, does it work? Well... the only way to see is by creating another class, TestOrders. The TestOrders file will be the one running the project so I added menu items and created some orders to see what would print out in the console and if it worked the way it should.

My project ran as it should, however, the process is not as quick and smooth as it may seem on a blog post. With Java being a strong and statically typed language, it is best practice to test often. I worked on each method one by one before moving on to the next method. You can, however, type code and comment it out if you are wanting to get a method out of the way but have not worked with the test file portion of it. I tend to do that to keep myself on track with methods and have an outline of what I am wanting or needing to do. A few errors I had along the way involved the ArrayList as that is something I struggled a lot with.

What Now?

From class inheritances and interface implementations to full stack Sprint Boot projects ran on a server - there is way more to learn! I am hoping to share my journey as I brew Java daily to not only help me reinforce what I learned but also to share any thoughts, ideas, or perhaps constructive criticism.