My First Java Program (Tea Anyone)
This is quite a simple one. I hacked this from what i know in C and PHP so was wondering if I any of this looks like bad coding practice?
TeaTime.java
Code:
import java.io.*;
public class TeaTime
{
private static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[]) throws IOException
{
Kettle kipper = new Kettle();
int fillAmount;
int cupSize;
int noCups;
int spilt;
float actualCups;
System.out.print("Kettle Boiling Program\n\n");
fillAmount = TeaTime.getIntegerFromConsole ("How much water do you want to fill the kettle with? " +
"(MAX " + kipper.getMaxFillLevel() + "ml) ");
spilt = kipper.fill(fillAmount);
if (kipper.isOverFlow())
{
System.out.println("You put too much water in.");
System.out.println(spilt + "ml spilt.");
System.out.println();
}
if (! kipper.boil())
{
if (kipper.isEmpty() && kipper.isBroken())
{
System.out.println("You tried to boil an empty Kettle");
System.out.println("You broke the kettle.");
return;
}
}
System.out.println("Your Kettle Has Boiled\n\n");
cupSize = TeaTime.getIntegerFromConsole("How big are your cups in ml? ");
noCups = TeaTime.getIntegerFromConsole("How many cups of tea would you like? ");
actualCups = 0;
do
{
actualCups += ((float) kipper.pour(cupSize) / (float) cupSize);
if (--noCups == 0)
{
break;
}
}
while (! kipper.isEmpty());
if (kipper.isEmpty())
{
System.out.println("Sorry ... not enough water.");
System.out.println("You got " + actualCups + " cups");
}
System.out.println("Tea is served!!!");
}
private static int getIntegerFromConsole(String msg) throws IOException
{
while(true)
{
System.out.print(msg);
try
{
return new Integer(kb.readLine()).intValue();
}
catch (NumberFormatException e)
{
System.out.println("Enter a proper integer please.");
}
}
}
}
Kettle.java
Code:
public class Kettle
{
private boolean boiled = false;
private boolean broken = false;
private boolean empty = true;
private boolean overFlow = false;
private int maxFillLevel = 2000;
private int fillLevel = 0;
public boolean isBoiled()
{
return this.boiled;
}
public boolean isBroken()
{
return this.broken;
}
public boolean isEmpty()
{
return this.empty;
}
public boolean isOverFlow()
{
return this.overFlow;
}
public int getMaxFillLevel()
{
return this.maxFillLevel;
}
public int getFillLevel()
{
return this.fillLevel;
}
public boolean boil()
{
if (this.broken)
{
return false;
}
else if (this.empty)
{
this.broken = true;
return false;
}
else
{
this.boiled = true;
return true;
}
}
public int fill(int amount)
{
int ret;
if (amount == 0) return 0;
this.empty = false;
if (this.fillLevel + amount > this.maxFillLevel)
{
ret = amount - (this.maxFillLevel - this.fillLevel);
this.overFlow = true;
this.fillLevel = this.maxFillLevel;
}
else
{
this.fillLevel += amount;
ret = 0;
}
return ret;
}
public int pour(int cupSize)
{
int ret;
this.overFlow = false;
if (this.empty)
{
ret = 0;
}
else if (this.fillLevel - cupSize <= 0)
{
ret = this.fillLevel;
this.empty = true;
this.fillLevel = 0;
}
else
{
ret = cupSize;
this.fillLevel -= cupSize;
}
return ret;
}
}
It compiles and works :wave:
Re: My First Java Program (Tea Anyone)
You don't need to use "this" every time (unlike in PHP - and doesn't that confuse me when I'm alternately working on a Java and a PHP project).
Looks good. You could perhaps use exceptions, in boil() in particular.
Re: My First Java Program (Tea Anyone)
I haven't looked at the code much, but did you create those static members and functions on purpose to share between instances of that type of class object?
Re: My First Java Program (Tea Anyone)
As far as I am aware the main function is meant to be static and any methods it invokes in that class must be static too. :ehh:
Re: My First Java Program (Tea Anyone)
True, but I'd just create an instance of that class and use the dot operator to call the methods.
Re: My First Java Program (Tea Anyone)
What for? It's not like TeaTime has any right to be an object instance.
Re: My First Java Program (Tea Anyone)
Why not? It's not like those methods need to be static.
It's obvious that more than one instance of TeaTime would need seperate instances of those methods, especially the bufferedreader.
Re: My First Java Program (Tea Anyone)
Actually, even more than one instance of TeaTime would do very well to share the buffered reader, because those would share the underlying stream too.
That aside, the method should be static. It also should be in a class ConsoleUtils, but that's a different issue. It should be static because the application driver, main(), is static and needs it, and there's no good reason for main() to create a TeaTime object just to use it.
Re: My First Java Program (Tea Anyone)
So what happens if the code is not synchronized?
Re: My First Java Program (Tea Anyone)
Nothing. The streams are thread safe.
Re: My First Java Program (Tea Anyone)
I don't understand how it can be safe though. Will it just wait untill thread 'one' has finished?
Also, what would happen to the getIntegerFromConsole() ?
Re: My First Java Program (Tea Anyone)
Quote:
Will it just wait untill thread 'one' has finished?
It will wait until no thread is accessing it.
What would happen to getIntegerFromConsole()? What do you mean?
Re: My First Java Program (Tea Anyone)
Like, would there be any kind of collision with more than one thread accessing it? Maybe I'm confusing myself, but
Re: My First Java Program (Tea Anyone)
No collision within the stream itself. It's thread-safe. What could happen are collisions like:
T1 writes prompt.
T2 writes prompt.
T1 requests input.
T2 requests input.
Making anything instance-specific won't change that, though. You have to synchronize it.
In general, there should only be one thread that interacts with the user anyway. More than that is bad, because a user can't multithread.