Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class design question  RSS feed

 
Nikole Six
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Recipe attributes:
Name
vegetable glycerine percent
propylene glycol percent
nicotine percent
variable number of flavors with corresponding percents (or no flavors at all)

Flavor attributes:
name
percent used
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.

Thank you for your help!
Nikole
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikole,

Welcome to CodeRanch!

Nikole Six wrote:So, all flavors will belong to recipes, but not all recipes will have the same number of flavors associated with them

Perfect! You almost figured it out.

When we say flavors belong to recipes, what we say is:
Recipe HAS-A Flavor (of course, it can be 0 or more than 1).

But, if we want to make Flavor as a subclass of Recipe, then it will make sense only when Flavor IS-A Recipe (which is not the case here).

I hope this helps.
 
Nikole Six
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.

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?
 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikole,

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!