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());
}
}