Hi folks,
Could anyone please suggest ways to rewrite the addToppings method, in an even simpler way if possible. I'm new to java would like to see other ways to write the StringTokenizer used in this class.
Thanks.
[ArchAngel added CODE tags]Code:public class Pizza{ String str = "prices.txt"; BufferedReader br = null; StringTokenizer st = null; String strPizzaName; int arrayNum = 0;; int amount; float pizzaPrice; Toppings[] toppings; Base base; DecimalFormat df = new DecimalFormat("0.00"); public Pizza(){ pizzaPrice = 0; strPizzaName = "Please Select"; base = new Base(); amount = 0; toppings = new Toppings[20]; addToppings(); } public void addToppings(){ //Search through "prices.txt" for price of Pizza Price & name try{ br = new BufferedReader(new FileReader(str)); }catch (FileNotFoundException nf) {} try { String temp = " "; String name = " "; String cost = " "; int tokNum = 0; while (temp != null) { //Change words into small strings called Tokens st = new StringTokenizer(temp); while (st.hasMoreTokens()) { tokNum++; String sTok = st.nextToken(); if (tokNum == 1) { name = sTok; } if (tokNum == 2) { cost = sTok; } } temp = br.readLine(); if (tokNum == 2) { float price = Float.parseFloat(cost); toppings[arrayNum] = new Toppings(name, price); arrayNum++; } tokNum = 0; } br.close(); }catch (IOException ie){ }catch (NoSuchElementException nse){} } float getPrice() { //Return the Price per Pizza & amount pizzaPrice = 0; for (int i = 0; i < arrayNum; i++){ pizzaPrice = pizzaPrice + toppings.getToppCost(); } pizzaPrice = pizzaPrice + base.getPrice(); pizzaPrice = pizzaPrice * amount; pizzaPrice = Float.parseFloat(df.format(pizzaPrice)); return pizzaPrice; } int getArrayAmount(){ return arrayNum; } public void setPizzaName(String name) { strPizzaName = name; } public String getPizzaName() { return strPizzaName; } public void setAmount(int pizzaAmount){ amount = pizzaAmount; } public int getAmount(){ return amount; } }


Reply With Quote
But i can tell you an alternative way to parse out strings into tokens, and that's the new split method for strings. It's new in version 1.4 so if you have it, use it!!!


Bookmarks