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
Kettle.javaCode: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."); } } } }
It compiles and worksCode: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; } }![]()




Reply With Quote