-3

I have written a java program which randomly chooses from a set of meals to decide what is to be eaten for breakfast, lunch and dinner. Each meal has nutritional values(calories, carbs e.t.c).

After my program has randomly chosen a meal plan for the day I want it to print out what breakfast option it has chosen, what lunch option it has chosen, and what dinner option it has chosen. It will then display the nutritional totals for the day. Here is where I am getting an error, Cannot make a static reference to the non-static field fp. I understand why I am getting this error but do not know how to fix it.

My meals are not yet created so for now I have just simulated it, inputting random values for the nutrients.

I have 2 classes, my Meal class:

public class Meal {

    int calories, carbs, sugar, protein, fat;

    public Meal (int calories, int carbs, int sugar, int protein, int fat) {
        this.calories = calories;
        this.carbs = carbs;
        this.sugar = sugar;
        this.protein = protein;
    }

    public int getCalories() {
        return calories;
    }

    public int getCarbs() {
        return calories;
    }

    public int getSugar() {
        return calories;
    }

    public int getProtein() {
        return calories;
    }

    public int getFat() {
        return calories;
    }

}

and my FoodPlan class:

import java.util.Random;

public class FoodPlan {

    public FoodPlan(Meal breakfast, Meal lunch, Meal dinner) {
        this.breakfast = breakfast;
        this.lunch = lunch;
        this.dinner = dinner;
    }

    int b = 3;
    int l = 3;
    int d = 3;

    Meal breakfast;
    Meal lunch;
    Meal dinner;

    static int calories;
    static int carbs;
    static int sugar;
    static int protein;
    static int fat;

    Random rand = new Random();

    Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal lunch1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal dinner1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);


    public void setBreakfast(Meal Meal) {
        this.breakfast = Meal;
    }

    public void setLunch(Meal Meal) {
        this.lunch = Meal;
    }

    public void setDinner(Meal Meal) {
        this.dinner = Meal;
    }


    public Meal getBreakfast() {
        return breakfast;
    }

    public Meal getLunch() {
        return lunch;
    }

    public Meal getDinner() {
        return dinner;
    }


    public void breakfast() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setBreakfast(breakfast1);
        }

        if (r == 2) {
            fp.setBreakfast(breakfast2);
        }

        if (r == 3) {
            fp.setBreakfast(breakfast3);
        }
    }


    public void lunch() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setLunch(lunch1);
        }

        if (r == 2) {
            fp.setLunch(lunch2);
        }

        if (r == 3) {
            fp.setLunch(lunch3);
        }
    }


    public void dinner() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setDinner(dinner1);
        }

        if (r == 2) {
            fp.setDinner(dinner2);
        }

        if (r == 3) {
            fp.setDinner(dinner3);
        }
    }


    public int getCalorieTotal() {
        calories = breakfast.getCalories() + lunch.getCalories() + dinner.getCalories();
        return calories;
    }

    public int getCarbsTotal() {
        carbs = breakfast.getCarbs() + lunch.getCarbs() + dinner.getCarbs();
        return carbs;
    }

    public int getSugarTotal() {
        sugar = breakfast.getSugar() + lunch.getSugar() + dinner.getSugar();
        return sugar;
    }

    public int getProteinTotal() {
        protein = breakfast.getProtein() + lunch.getProtein() + dinner.getProtein();
        return protein;
    }

    public int getFatTotal() {
        fat = breakfast.getFat() + lunch.getFat() + dinner.getFat();
        return fat;
    }


    public static void main(String[] args) {    
        System.out.println("Menu Today:");
        System.out.print("Breakfast: " + fp.getBreakfast());
        System.out.print("Lunch: " + fp.getLunch());
        System.out.print("Dinner: " + fp.getDinner());
        System.out.println("Total Calories: " + fp.getCalorieTotal());
        System.out.println("Total Carbs: " + fp.getCarbsTotal());
        System.out.println("Total Sugar: " + fp.getSugarTotal());
        System.out.println("Total Protein: " + fp.getProteinTotal());
        System.out.println("Total Fat: " + fp.getFatTotal());
    }

}

Updated FoodPlan class:

import java.util.Random;

public class FoodPlan {

    public FoodPlan(Meal breakfast, Meal lunch, Meal dinner) {
        this.breakfast = breakfast;
        this.lunch = lunch;
        this.dinner = dinner;
    }

    int b = 3;
    int l = 3;
    int d = 3;

    Meal breakfast;
    Meal lunch;
    Meal dinner;

    int calories;
    int carbs;
    int sugar;
    int protein;
    int fat;

    Random rand = new Random();

    Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal lunch1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal lunch3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    Meal dinner1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
    Meal dinner3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

    FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);


    public void setBreakfast(Meal Meal) {
        this.breakfast = Meal;
    }

    public void setLunch(Meal Meal) {
        this.lunch = Meal;
    }

    public void setDinner(Meal Meal) {
        this.dinner = Meal;
    }


    public Meal getBreakfast() {
        return breakfast;
    }

    public Meal getLunch() {
        return lunch;
    }

    public Meal getDinner() {
        return dinner;
    }


    public void breakfast() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setBreakfast(breakfast1);
        }

        if (r == 2) {
            fp.setBreakfast(breakfast2);
        }

        if (r == 3) {
            fp.setBreakfast(breakfast3);
        }
    }


    public void lunch() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setLunch(lunch1);
        }

        if (r == 2) {
            fp.setLunch(lunch2);
        }

        if (r == 3) {
            fp.setLunch(lunch3);
        }
    }


    public void dinner() {

        int r = rand.nextInt(b) + 1;

        if (r == 1) {
            fp.setDinner(dinner1);
        }

        if (r == 2) {
            fp.setDinner(dinner2);
        }

        if (r == 3) {
            fp.setDinner(dinner3);
        }
    }


    public int getCalorieTotal() {
        calories = breakfast.getCalories() + lunch.getCalories() + dinner.getCalories();
        return calories;
    }

    public int getCarbsTotal() {
        carbs = breakfast.getCarbs() + lunch.getCarbs() + dinner.getCarbs();
        return carbs;
    }

    public int getSugarTotal() {
        sugar = breakfast.getSugar() + lunch.getSugar() + dinner.getSugar();
        return sugar;
    }

    public int getProteinTotal() {
        protein = breakfast.getProtein() + lunch.getProtein() + dinner.getProtein();
        return protein;
    }

    public int getFatTotal() {
        fat = breakfast.getFat() + lunch.getFat() + dinner.getFat();
        return fat;
    }


    public static void main(String[] args) {

        FoodPlan fp = new FoodPlan(breakfast, lunch , dinner);

        fp.breakfast();
        fp.lunch();
        fp.dinner();

        System.out.println("Menu Today:");
        System.out.print("Breakfast: " + fp.getBreakfast());
        System.out.print("Lunch: " + fp.getLunch());
        System.out.print("Dinner: " + fp.getDinner());
        System.out.println("Total Calories: " + fp.getCalorieTotal());
        System.out.println("Total Carbs: " + fp.getCarbsTotal());
        System.out.println("Total Sugar: " + fp.getSugarTotal());
        System.out.println("Total Protein: " + fp.getProteinTotal());
        System.out.println("Total Fat: " + fp.getFatTotal());
    }

}
0

1 Answer 1

1

A variable should be static if and only if it's universal for the class, not associated with a particular instance of that class.

static int calories;
static int carbs;
static int sugar;
static int protein;
static int fat;

None of these should be static, because it's not like there's a specific number of calories for all FoodPlans, there's a specific number of calories for each specific FoodPlan.

Meal breakfast1 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
Meal breakfast2 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));
Meal breakfast3 = new Meal(rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200), rand.nextInt(200));

These breakfasts aren't part of every FoodPlan, they're particular breakfasts to be associated with particular FoodPlans. It looks like these should actually be local variables inside your main method, actually.

FoodPlan fp = new FoodPlan(breakfast, lunch, dinner);

Here you're creating a FoodPlan inside a FoodPlan. This is completely unnecessary.

public static void main(String[] args) {    
    System.out.println("Menu Today:");
    System.out.print("Breakfast: " + fp.getBreakfast());
    ...
}

Which FoodPlan is main supposed to be using here? You should probably be creating a FoodPlan in your main method and using it there.

Sign up to request clarification or add additional context in comments.

7 Comments

I have made all my variables non-static and altered my main to: create a new foodplan instance and then call my breakfast, lunch and dinner method on this instance but now it is asking me to make my meal variables breakfast, lunch and dinner static
@JohnSmith, update your question with your modified code?
I have posted my updated class
@JohnSmith where do you expect the breakfast in your new main method to come from? You're calling new FoodPlan(breakfast, lunch, dinner); what do you expect to be passed in here? Which breakfast?
@JohnSmith, nope. You can either create a breakfast and pass it into the constructor, or you can stop passing the meals into the constructor -- change your constructor to MealPlan(), and then get the breakfast out of the MealPlan after you set it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.