0

I have created a User Java class to store the users' information that was provided by the user when the registered. (The information is then stored in Firebase Realtime Database.) The variables username, email, dob, weight, height and gender are all set when the user registers however I want to create the value for the CalorieIntake using the other variables in the class in a calculation.

To calculate the Calorie Intake I need to use the weight and height and this calculation will be based on if the user is a male or female.

The code I have used doesn't seem to calculate the calorieIntake or show up on the profile page, so I am now very confused as to how to calculate it and get it to show up on my profile page.
Any help would be appreciated.

Below is my code in the User.java class:

public class User {
    public String username;
    public String email;
    public String dob;
    public String weight;
    public String height;
    public String gender;
    public String calorieIntake;


    //default constructor
    public User() {
    }

    public User(String username, String email, String dob, String weight, String height, String gender, String calorieIntake ) {
        this.username = username;
        this.email = email;
        this.dob = dob;
        this.weight = weight;
        this.height = height;
        this.gender = gender;
        this.calorieIntake = calorieIntake;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getdob() {
        return dob;
    }

    public void setdob(String dob) {
        this.dob = dob;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCalorieIntake() {
        return calorieIntake;
    }

    public void setCalorieIntake(String calorieIntake) {

        if(this.getGender() == "Male"){
            calorieIntake = String.valueOf(((((Integer.parseInt(this.getWeight()) * 10) + (6.25 * Integer.parseInt(this.getHeight()))) - (5 * 21)) + 5));
       } else if (this.getGender() == "Female"){
            calorieIntake = String.valueOf(((((Integer.parseInt(this.getWeight()) * 10) + (6.25 * Integer.parseInt(this.getHeight()))) - (5 * 21)) - 161));
       }
        this.calorieIntake = calorieIntake;
    }

}
Bashir
  • 2,057
  • 5
  • 19
  • 44
dmcm
  • 1
  • 2
  • Problem: `if(this.getGender() == "Male"){` Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Mar 08 '20 at 21:49
  • Better: `if (gender.equalsIgnoreCase("Male")) {` – Hovercraft Full Of Eels Mar 08 '20 at 21:49
  • Same for the else-if block below this one – Hovercraft Full Of Eels Mar 08 '20 at 21:50
  • 1
    Why does the setter setCalorieIntake have an argument, if the argument is not used? Also: If you like to set the calorieIntake in the constructor depending on the other arguments of the constructor and using a method, then this method should be static: https://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java/15348070#15348070 – Christian Fries Mar 08 '20 at 21:56
  • Hi Christian, am I making the getCalorieIntake or setCalorieIntake method static? Also from the example you led me too am I creating a new constructor with the gender, height and gender in it? – dmcm Mar 08 '20 at 22:09
  • No, your setCalorieIntake should in fact be a non-static calculated getter method, one without a parameter and that returns a value depending on the state of the fields. – Hovercraft Full Of Eels Mar 08 '20 at 22:11
  • So my getter method for the calorieIntake is where my calculation should be? – dmcm Mar 08 '20 at 22:20
  • Yes. There should be no "setter" method for a calculated property – Hovercraft Full Of Eels Mar 08 '20 at 22:22
  • Got it displaying now, thank you so much for the help! – dmcm Mar 08 '20 at 22:44

0 Answers0