# Tracks the number of calories consumed throughout the day

Tracks the number of calories a person consumes in a given meal it has helper method to get the day's total. The check method Tries to find any duplicate meals within the given list. The assumption is that a day can only contain one of each type of meal.

public class Meal {
public enum Type {
BREAKFAST,
LUNCH,
DINER,
}

public Type type;
double caloriesConsumed;

public Meal(Type type, double caloriesConsumed) {
this.type = type;
this.caloriesConsumed = caloriesConsumed;
}

//Gets calories burned
public double getCaloriesConsumed() {
return caloriesConsumed;
}

//Sets calories burned
public void setCaloriesConsumed(double caloriesConsumed) {
this.caloriesConsumed = caloriesConsumed;
}

//Get type
public Type getType() {
return type;
}

//Set type
public void setType(Type type) {
this.type = type;
}

public boolean equals(Meal meal) {
return meal.getType() == meal.getType() && meal.getCaloriesConsumed() == getCaloriesConsumed();
}

public static void check(Meal... meals) throws Exception {
/**
* Tries to find any duplicate meals within the given list.
* The assumption is that a day can only contain one of each type of meal.
*/
ArrayList<Type> types = new ArrayList<Type>();
for (int i = 0; i < meals.length; i++) {
if (types.contains(meals[i].type))
{
throw new Exception();
}
else
{
}
}
}

public static double getTodaysCalories(Meal... meals) throws Exception
{
check(meals);

double total = 0;
for (Meal meal : meals) {
total += meal.caloriesConsumed; //Add calories to total
}
}

public static void main(String[] args) {
Meal breakfast = new Meal(Type.BREAKFAST, 500.7);
Meal lunch = new Meal(Type.LUNCH, 378.9);
Meal dinner = new Meal(Type.DINER, 620.1);

try {
double caloriesConsumed = Meal.getTodaysCalories(breakfast, lunch, dinner);
System.out.println("Today's Total Calories is " + caloriesConsumed);
} catch (Exception e) {
System.out.println("Unable to calculate today's calories");
}
}
}

• When you say fix issues, do you have issues in your code ? And please edit the title of the question to describe what your code is doing, not what you want as a review. Aug 10, 2016 at 17:24
• There are no known issues as far as I know. I am reviewing this piece of code and would like second eyes on it. Aug 10, 2016 at 17:42
• Please, format your code next time in the question with the button {} or Ctrl + K. I had to edit it again. And please do edit your title to a short description of what your progam do. Aug 10, 2016 at 17:56
• I just edited it Aug 10, 2016 at 18:18
• Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers. Any reason why you wanted to delete all the code from your question? Aug 11, 2016 at 21:35

First, a small note. Your fields are public. There's no need for getters and setters if the fields are public.

Second, the getTodaysCalories() and check() methods seems a bit out of place in this class. I think it would be a lot better to have a 2nd class called DailyMeals. That class would then have a List<Meal> inside of it. That class would be the one to check if a duplicate is being added and what the daily totals are. However the checking for duplicates should be done at the time a meal is added. This would give the code more clarity and put the methods where they should be. As it stands now there isn't really any indication that a list of meals is for a single day and/or that it can't contain duplicates.

In addition, it may be some sort of rule that you can't have 2 of the same meal, but perhaps that would be better done by (instead of throwing an exception) prompting the user asking if they are sure they want to add a second lunch/dinner/breakfast etc. What happens if the person actually ate 2 meals and they consider them both lunch?

The general code structure could look something like this:

public class Meal {
public enum Type {
BREAKFAST,
LUNCH,
DINER,
}

private Type type;
private double caloriesConsumed;

public Meal(Type type, double caloriesConsumed) {
this.type = type;
this.caloriesConsumed = caloriesConsumed;
}

//ommitted getters, setters and equals for brevity...
}

public class DailyMeals {
List<Meal> meals;

//ommitted constructors for brevity...

if (checkForDuplicate(meal))
//throw error (or prompt if they're sure they want to add a duplicate)

//rest of method code...
}

private boolean checkForDuplicates(Meal meal) {
//code to check if multiple meals have the same type...
}

public int getCaloriesForDay() {
//code returning total number of calories for the day
}
}