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:
- 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 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?