Please help me with the task: Refill the glass

Hello, I’m working on this task:

Create a class Glass with one public int field LiquidLevel and methods public Drink(int milliliters) that takes the amount of liquid that a person drank and public Refill() that refills the glass to be 200 ml full. Both methods should not return any value. Initially set LiquidLevel to 200.
In the Main method create an object of class Glass and read commands from the screen until the user terminates the program (see next). Don’t forget to refill the glass when needed! Commands are:

  1. drink followed by a number. It indicates that the person drank that amount of milliliters from the glass. You can use string. Split(’ ') to separate the word and the number.
  2. print - you need to output to the screen how much liquid is in the glass in the format “This glass contains …ml of liquid.”
  3. stop - program should quit.
    Example 1:
    >drink 37
    >drink 12
    >print
    This glass contains 151ml of liquid.
    >stop

Example 2:
>drink 50
>drink 51
>print
This glass contains 200ml of liquid.
>stop

Here’s my code. It’s very raw and contains many mistakes, but I’ll still publish it here since I’m stuck and need your help. :sob: :sob: :sob: I don’t fully understand how the classes and methods work and how they should be called, I don’t know how to create Drink and Refill methods properly, and how to call the LiquidLevel and milliliters variables :grimacing: :cold_sweat: Please, please, please help me :pray::


namespace ClassAndObject
{

   class Glass
   {
       public int LiquidLevel = 200;  
         

       public Drink (int milliliters)
       {
           string input = Console.Readline();            
           int.TryParse(string.Join("", input.Where(c => char.IsDigit(c))), out milliliters);
       }

       public Refill()
       {
           if (LiquidLevel < 100)
           {
               LiquidLevel = 200;
           }
       }
   }

   public class RefillMyGlass
   {
       public static void Main()
       {
          var someGlass = new Glass();

          bool readCommands = true;

           while(readCommands)
           {
              
               string command = Console.Readline();
               
               switch (command)
               {
                   
                   case "drink":
                       while(someGlass.LiquidLevel >= 100)
                       {
                           someGlass.LiquidLevel = someGlass.LiquidLevel - someGlass.Drink(milliliters);
                       }
                       
                       break;                       
                   case "print":
                       Console.WriteLine($"This glass contains {milliliters} of liquid.");
                       break;
                   case "stop":
                       readCommands = false;
                       break;                  
                   
               }
           }

       }
   }
}

Many thanks to @Maria_CWLm who helped me with this task via PM :star_struck: :hugs:
But any tips and suggestions from mentors concerning general approach to this task would be appreciated :blush:

2 Likes

First, I think that there is a missing requirement related to Refill logic (and when it should be called).

General approach:
Modification of LiquidLevel should be controlled within a class by calling either Drink or Refill. Ideally, LiquidLevel should be a property with public get and private set.

2 Likes