Please help me with the task: Refill the glass

Hello, I’m working on this task:

We are prototyping a robot that refills glasses during dinner. Every glass holds 200 milliliters. During dinner, people either drink water or juice, and as soon as there is less than 100 ml left in the glass, the robot refills it back to 200 ml.
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 is my code:

using System;

namespace ClassAndObject
{

   class Glass
   {
       public int LiquidLevel;

       public void Drink(int milliliters)
       {

           LiquidLevel -= milliliters;
       }

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

   }


   public class RefillMyGlass
   {
       public static void Main()
       {
           Glass glass = new Glass();
           glass.LiquidLevel = 200;


           bool stopped = false;

           while (!stopped)

           {
               string command = Console.ReadLine();
               string[] divide = command.Split(' ');
               string number = divide[1];
               int milliliters = int.Parse(number);

               switch (command)
               {
                   case "drink":
                       {
                           if (glass.LiquidLevel < 100)
                               glass.Refill();

                           if (glass.LiquidLevel >= 100)
                               glass.Drink(milliliters);

                           break;
                       }

                   case "print":
                       Console.WriteLine($"This glass contains {glass.LiquidLevel} of liquid.");
                       break;

                   case "stop":
                       stopped = true;
                       break;

               }

           }

       }
   }
}

Can anyone please help me to solve it?

  1. your code never goes inside “drink flow”, since in switch the comparison is exact, so you should rather check divide[0] than command in the switch.
  2. you’re getting an error since for all inputs you’re trying to split an array. It’s not working. Put the number and mililiters inside drink flow
  3. you should “drink” anyway, and only then check if you need to refil the glass. So
    if (glass.LiquidLevel >= 100) should be removed
  4. You’ve missed ml in Console.WriteLine($"This glass contains {glass.LiquidLevel} of liquid.");
    should be Console.WriteLine($"This glass contains {glass.LiquidLevel}ml of liquid.");
    all the rest looks good! :slight_smile:
1 Like

Thank you! It works! I thought I lacked understanding of the topic. But there is still a lack of logic and imagination)