Day 13–14. Makers Academy. OOP Challenge
Over the course of the weekend, for the weekend 2 challenge, I was required to design a program to simulate a takeaway restaurant ordering system. I needed to be able to print a menu, detailing items and prices, allow customers to order from the menu a number of available items, allow the customer to confirm the total number of items ordered and enable them to receive a text with the delivery time of their order.
I found this challenge hard on a number of different levels, and although I completed it in terms of ‘the program worked’ there is a lot to be desired with regards to my over all design, architecture, class dependancies, and TDD. I was impressed with how much I learnt over the weekend and had some interesting challenges involving iterating over arrays and hashes containing objects, but I was disappointed that my RSPec tests were not leading the development and in hindsight, I went about the design in the wrong way.
The biggest faux pas of the weekend was to create a separate class called ‘Dishes’ and initialize each and every dish as its own object. I thought that I could add these en mass into a ‘Menu’ array, the rationale behind it being that a dish could removed or added easily. Unfortunately, each dish had an item name and price passed to it, which meant that each object had a ‘hash’ containing item name and price inside of it. When these dishes were added to a menu array, I ended up with an array of objects, containing hashes with the information I needed.
@menu_object.menu.select { |i| i.name == item_name }[0]
When I added these to an array, I inadvertently created an array object called menu, which contained the menu array of hashes — like this:
When I then came to iterate over the array within the array, I found this difficult and finally found some help on www.stackoverflow.com (without abuse this time I should help!) which allowed me to use code similar to this, to iterate when required to abstract information on price, quantity and name.
@total_cost = @order.inject(0) { |sum, (item, qty)| sum + item.price * qty }
Although I learnt a lot about hashes and iterating over arrays of hashes and objects, I felt disappointed that my RSpec testing fell to the wayside (I had difficulty testing failing methods) and my class dependencies were not ideal.
On a positive side, I learnt that a display port to thunderbolt connection was much easier and more effective to connecting my monitor to my mac than HDMI cables were. For some reason the resolution was better and the text rendered with less blurriness.