3
\$\begingroup\$

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
            {
                types.add(meals[i].type);
            }
        }
    }

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

        double total = 0;
        for (Meal meal : meals) {
            total += meal.caloriesConsumed; //Add calories to total
        }
        return 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");
        }
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Marc-Andre Aug 10 '16 at 17:24
  • \$\begingroup\$ There are no known issues as far as I know. I am reviewing this piece of code and would like second eyes on it. \$\endgroup\$ – Smith winters Aug 10 '16 at 17:42
  • \$\begingroup\$ 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. \$\endgroup\$ – Marc-Andre Aug 10 '16 at 17:56
  • \$\begingroup\$ I just edited it \$\endgroup\$ – Smith winters Aug 10 '16 at 18:18
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Pimgd Aug 11 '16 at 21:35
1
\$\begingroup\$

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...

    public void addMeal(Meal meal) {
        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
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.