Click here to Skip to main content
15,895,792 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this method in my Calroies Class which inhertis the User class.

class Calories : User
    {
        public void CalcuateCaloriesMale()
        {
            var user = new User();
            var equation = (10 * user.Weight) + (6.25 * user.Height) - (5 * user.Age) + 5;
            Console.WriteLine("Based on the Mifflin – St Jeor Formula You need to eat {0} Kalories a day\nTo Gain Weight ", equation);

        }
    }


And i have this code in Program.cs


if (newGoal.GoalStatusGainWeight ==  true)
    {
        var calories = new Calories();
        switch (newUser.Gender)
        {
            case "m":
                calories.CalcuateCaloriesMale();
                break;



Now I dont understand how to make the Calories Class know what the user has typed in at the beginning of the Beginning.


newUser.Name = Question.AskString("Name: ");
    newUser.Age = Question.AskInt("Age: ");
    newUser.Gender = Question.AskString("Gender(m/f): ");
    newUser.Height = Question.AskInt("Height(cm): ");
    newUser.Weight = Question.AskInt("Weight(kg): ");



The Calories Class doesn't take into consideration waht the user has typed in.
I tought i can juse make a new User, but i guess my thinking was wrong.
What am i doing wrong ? How can i imagine this working so i can learn it better?

What I have tried:

I dont understand how to make the code know what the user has typed in initally
Posted
Updated 3-Jun-18 7:41am

1 solution

Make sure that the Calories class gets newUser, for example as argument to CalculateCaloriesMale:
C#
        public void CalcuateCaloriesMale(User user) // add "user" parameter
        {
            // now you don't need this line: var user = new User();
            var equation = (10 * user.Weight) + (6.25 * user.Height) - (5 * user.Age) + 5;
            Console.WriteLine("Based on the Mifflin – St Jeor Formula You need to eat {0} Kalories a day\nTo Gain Weight ", equation);

        }

...

if (newGoal.GoalStatusGainWeight ==  true)
    {
        var calories = new Calories();
        switch (newUser.Gender)
        {
            case "m":
                calories.CalcuateCaloriesMale(newUser); // Pass newUser to the method
                break;
Alternatively, you could create a constructor[^] for Calories and take a User there and store it, then use it in a parameterless CalculateCaloriesMale class.

Side note: it hardly makes sense to make Calories inherit from User. It would appear to a human developer as if Calories is a certain type of User, which is of course not true. And it doesn't look like it has any use.
 
Share this answer
 
Comments
Member 13733578 3-Jun-18 14:22pm    
Hi, trank you very much. Your solution work and i alos agree with the inheritance part.
What im more curious about is the constructor class needed or can i just work in the class, since i need to make one for female aswell.

Thank you
Thomas Daniels 3-Jun-18 14:23pm    
You do not need to take a User in the constructor if you do not want to; you know better than I what further goals you have, so you should decide. I just wanted to mention it, in case you felt it would be better for your application.
Member 13733578 3-Jun-18 14:28pm    
Oh okay, thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900