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:
- 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.
- print - you need to output to the screen how much liquid is in the glass in the format “This glass contains …ml of liquid.”
- 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. 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 Please, please, please help me :
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;
}
}
}
}
}