0

I am currently struggling to figure out this problem for my project. I currently have a Food class that stores name, price and description with getters and setters and a toString. And a course class with subclasses (starter, main dessert). I am trying to figure out how to attach a Food to a Course.

public abstract class Course{
    //fields
    //protected only accessible to subclasses
    protected MenuList starter;
    protected MenuList main;
    protected MenuList dessert;
    protected MenuList drinks;

    //Constructor
    public Course(){
    starter = new MenuList();

        main = new MenuList();

        dessert = new MenuList();

        drinks = new MenuList();    
    }
    //getters and setters

    //methods
    public abstract MenuList getList();

    //add item
    public void addItem(String course, String foodName, double price, String description, int calories){
        this.addItem(course, foodName, price, description, calories);
    }   
}

starter subclass its the same with main and dessert subclasses

public class StarterFood extends Course{
        //fields

    //constructor
    public StarterFood(){
        //course, 
        starter.addItem("starter", "chicken wings", 2.30, "very nice", 150, false);

    }

    @Override
    public MenuList getList() {
        return starter;
    }
    //Constructors


    //getters and setters

    //methods
}

so far ive: adding food (with a name, price, description, calories) listing all food items adding courses searching for a course (by course number or name) listing all courses I only need to do this but I'm struggling any help is appreciated attaching food to courses

rdonuk
  • 3,921
  • 21
  • 39

1 Answers1

1

If your trying to add a Food to a Course, you should use a "has a" relationship for example:

public class Course {
    private Food food;

    public Course(Food food) {
        this.food = food;
    }

    public Course() {

    }

    public Food getFood() {
        return this.food;
    }

    public void setFood(Food food) {
        this.food = food;
    }
}

I also wouldn't use StarterFood to extend a Course, because extends is for and "is a" relationship, I would call it StarterCourse and then add a default food for that course in the constructor.

public class StarterCourse extends Course {

    public StarterCourse(Food food) {
        // here call the super classes constructor
        // add items via the Course constructor
        super(food);
    }
}

Then in your main class to test it out try this:

public class Main() {
    public static void main() {
        // First create new Food object
        Food food = new Food(); 
        // Create a new StarterCourse and add the Food object to it
        StarterCourse starterCourse = new StarterCourse(food);
    }
}
J Mora
  • 26
  • 4
  • you're a legend, thanks but I don't understand what adding a default food in the constructor of starter course means. –  Apr 08 '16 at 18:27
  • Try something like this public class StartCourse extends Course { public StarterCourse { Food food = new Food(); /* create new food here */ this.food = food;} } – J Mora Apr 08 '16 at 18:28
  • This url shows an example http://stackoverflow.com/questions/4488716/java-default-constructor – J Mora Apr 08 '16 at 18:31
  • your code for the setFood method has a return statement is that right? –  Apr 08 '16 at 18:36
  • yes sorry there shouldn't be a return statement in the setter. – J Mora Apr 08 '16 at 18:38
  • what if you wanted to ad multiple items in the constructor of starter course? –  Apr 08 '16 at 18:40
  • welp :( i need help with this –  Apr 08 '16 at 18:53
  • You may have to create a collection in your super class and call it via super.addFood(new Food()); something like that. – J Mora Apr 08 '16 at 18:54
  • Does this require a getter method for the collection? –  Apr 08 '16 at 19:35
  • public void addFood(String course, String foodName, double price, String description, int calories, boolean veg){ List.addItem(course, foodName, price, description, calories, veg); } public MenuList getFoods(){ return List; } –  Apr 08 '16 at 19:35
  • got a getFoods method and a addFoods method –  Apr 08 '16 at 19:35
  • yes I would add a getter for the collection, for addFood() I would put the food object in addFood method, addFood(new Food(course, foodName, price, ...) – J Mora Apr 08 '16 at 19:44
  • I wouldn't make it an abstract class, because you could always make a course that is not a starter course. Here is a good explanation for creating abstract classes http://tutorials.jenkov.com/java/abstract-classes.html – J Mora Apr 08 '16 at 21:31
  • i thought course was a base class for starter course, main course and dessert course (these are subclasses)? –  Apr 08 '16 at 21:41
  • yes, but just because its a superclass(base class) doesn't necessarily mean it should be **abstract**. If it makes sense that an object could be created from the class then do not make it abstract. For example: a class of Shape should be abstract because it does not make sense to make an object of Shape class. Shape is abstract, however you could create a Course object. It all depends on how you want to model your classes. If you want to limit your courses to a group of specific set of subclasses then you should make it abstract. Does this make sense ... – J Mora Apr 08 '16 at 22:20
  • it says there add foods via course constructor I don't understand this –  Apr 09 '16 at 10:04
  • i've added my items in the starter constuctor public StarterCourse(){ //course super(); this.addFood("test", "test", 0, "test", 0, false); } –  Apr 09 '16 at 10:27
  • I added the Course constructor to the example. An a main class to test it out. Adding a Food object via a course constructor is one way to instantiate the object, by giving a value to the food object when you create the course object. Another way is to create the Course object then set the food object later with setFood(food); – J Mora Apr 10 '16 at 01:03
  • Thank you my project working very well and efficient now –  Apr 10 '16 at 09:50