• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class design question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic