Question

This is a very noob basic java question.

I have a superclass.

public class MenuItem {

    private int itemId;

    public int getItemId() {
        return itemId;
    }

    public void setItemId(int itemId) {
        this.itemId = itemId;
    }

}

then subclasses:

public class Beverage extends MenuItem {

    private PriceSML price;

    public PriceSML getPrice() {
        return price;
    }

    public void setPrice(PriceSML price) {
        this.price = price;
    }

}

   public class Cake extends MenuItem {

        private SugarLevel sugarLevel;

        public SugarLevel getSugarLevel() {
            return sugarLevel;
        }

        public void setSugarLevel(SugarLevel price) {
            this.sugarLevel= sugarLevel;
        }

    }

then I created a DAO method:

public boolean createItem(MenuItem menuItem) {
        boolean result = false;
        try {
            Connection conn = createConnection();
            Statement stmt = conn.createStatement();

            String query = "INSERT INTO menu (menu_id, price) 
                    + "VALUES ('"
                    + MenuItem.getItemCode()
                    + "')";
                    //NOTE: HOW TO GET THE PRICE AND PUT IT IN MY QUERY

            stmt.executeUpdate(query);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
}

But how can I get the PriceSML price since it is in the Beverage? Even if I passed it like this

MenuItem menuItem = new Beverage();

So I've decided to do this:

public boolean createBeverageItem(Beverage beverage) {

}

public boolean createCakeItem(Cake Cake) {

}

And so on... (e.g. 100 more methods if I have 100 categories). Take note that I am accessing only the menu table in the db. So how can i do this better?

Thank you.

Was it helpful?

Solution

To do this generically, be generic about it.

Instead of having subclasses of MenuItem just use a single class MenuItem and have a HashMap within it. The HashMap then maps from keys to values for all the properties of that MenuItem. You can then store them in the database using a one-to-many relationship linking the MenuItem table with a MenuItemProperties table.

Trying to do it the way you were doing falls down quickly, because you can't actually usefully do anything with MenuItem without knowing what type it is.

Note also that you are directly inserting values into your query string. While that's safe enough for integer values it's still bad practice and it is very dangerous is you ever have String values.

OTHER TIPS

Create a DAO class for each of the beans that you want to persist. Implement the SQL statement building and JDBC calls in them, each saving the attributes specific to one particular bean. Of course, quite likely all the DAOs will be all subclasses of some common class(es) that will implement common code. The of them inheritance generally depends on your data model and how that maps to your beans. I think you figure that out :)

Now, have all DAOs implement one common interface, and use it to access them (i.e. such interface will declare your createItem(MenuItem menuItem) method and similar methods for update . To create instances of them, implement a factory class, that will provide them. It can do it based on the getClass() call on a bean, and internaly keep the DAOs cached in a map of bean classes to DAOs.

or

Make use of JPA (as someone said in the comments above), or pick some other ORM framework.

Make MenuItem and abstract class which has an abstract method getPrice(). Then override that in your subsclasses.

instead of using

MenuItem menuItem = new Beverage();

you must use

Beverage beverage= new Beverage();

it will allow you to use functions of class MenuItem as well as set and get PriceSML price

why not?

    if(menuItem instanceof Beverage){
        ((Beverage) menuItem).createBeverageItem(myBeverage)
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top