// CupOfCoffee C = new
cupOfCoffee;
// C.addCup(1)
// C.addMilk(100);
// C,addCup(1) - Error, u got a cup already
// C.addWater(200)
// C.drink error, cause havent stir
// C.Stir() - println stir stir
// C.Drink() - ok
// C.Drink - cannot drink, empty
public class Coffee
{
public static void main(String s[])
{
CupOfCoffee C = new CupOfCoffee();
C.addCup(1);
C.addCoffee(1);
C.addSugar(3);
C.addMilk(30);
C.addMilk(20);
C.addMilk(50);
C.addWater(150);
C.Stir(true);
C.Drink(true);
}
};
class CupOfCoffee
{
private int coffee,
sugar,
milk,
water,
cup;
private boolean stir,
drink;
private static int maxMilk = 100;
void addCup(int cup) // max of 1 (size 200ml)
{
int maxCup;
maxCup = 1;
this.cup = cup;
if (cup > maxCup)
{
System.out.println ("Cannot have more than 1 cup");
}
else
{
System.out.println ("Add " + cup + "
cup");
}
}
void addCoffee(int coffee)
{
this.coffee = coffee;
}
void addMilk(int milk) // max 100ml
{
try
{
Exception e;
if (cup == 0)
{
e = new Exception("You have no cup so there is
a mess on the bench");
throw(e);
}
if ((milk + this.milk) > maxMilk)
{
e = new Exception("Cannot have more 100 ml");
throw(e);
// add what we can and spill the rest
}
else
{
this.milk += milk;
System.out.println ("Now have " + this.milk
+ " ml of milk");
}
}
catch (Exception e)
{
System.out.println (e.toString());
}
}
void addSugar(int sugar) // max 5 spoon
{
int maxSugar = 5;
this.sugar = sugar;
if (sugar > maxSugar)
{
System.out.println ("Cannot have more 5 spoon");
}
}
void addWater(int water) // max 200ml
{
int maxWater = 200;
this.water = water;
if (water > maxWater)
{
System.out.println ("Cannot have more than 200
ml");
}
}
void Stir(boolean stir) // must be stir before drinking
{
this.stir = stir;
if (stir == true)
{
System.out.println ("Stir Stir");
}
}
void Drink(boolean drink) // glug glug
{
this.drink = drink;
if (drink == true)
{
System.out.println ("Drink glug glug");
}
}
};
|