I'm building a program to store recipes for e-cigarette juice and I'm having a hard time getting my head around how I should design my classes. Here's how recipes work in reality:
vegetable glycerine percent
propylene glycol percent
variable number of flavors with corresponding percents (or no flavors at all)
vegetable glycerine or propylene glycol based (boolean value)
So, all flavors will belong to recipes, but not all recipes will have the same number of flavors associated with them. I had in my head that flavors should be a sub class of recipes, but flavors do not contain the same attributes as recipes, so that won't work. I really can't get my head around how to make this work.
I understand that this is a one-to-many relationship and my database is designed to reflect this (a foreign key in the flavors table references the recipes table). I know a little bit about composition, but I've only ever used it in instances where the number of variables is known.
My point of confusion is how to reference the flavors within my recipe class when the number of flavors is not static. Should I use an arraylist in my recipes class to hold the flavor objects and simply add them to it as necessary?`Am I overthinking this?
What you are trying to achieve is called tight coupling. The two classes depend on each other. Why don't you start by creating a UML for the two classes. Then write a pseudocode for each the main class which will execute the operating. Flavour can be the super class and recipe can be the sub class that inherit from Flavour or the reverse. which ever one makes sense of what you want to achieve. So write getter and setter methods to your classes and you are on your way.
It will give me the powers of the gods. Not bad for a tiny ad: