Sunday 15 March 2015

Method calculations/arguments in Java -



Method calculations/arguments in Java -

i have create virtual coffee shop user enters order number, how many of order want, calculate subtotal , discount, etc. whole point of process's divided various methods. of methods pretty simple, i'm having problem computesubtotal method. have initialize subtotal in main method create work, when subtotal's calculated in computesubtotal, ends beingness zero. sorry if seems stupid, have no thought i'm doing wrong, help?

import java.util.scanner; public class coffeeshopwithmethods { public static void main(string[] args) { scanner user_input = new scanner (system.in); string user_name; system.out.print("\nplease come in name: "); user_name = user_input.next(); system.out.println("\nwelcome java byte code coffee shop, " + user_name + "!"); int ordernumber = 0; int orderquan = 0; double subtotal = 0.0; //beginning of calls methods displaymenu(); getitemnumber(ordernumber); getquantity(orderquan); computesubtotal(ordernumber, orderquan, subtotal); discountcheck(subtotal); } public static void displaymenu() { system.out.println("\nhere our menu: \n" + "\n 1. coffee $1.50" + "\n 2. latte $3.50" + "\n 3. cappuccino $3.25" + "\n 4. espresso $2.00"); } public static int getitemnumber(int ordernumber) //prompts user item number (1 coffee, 2 latte, etc...) { scanner user_input = new scanner(system.in); system.out.print("\nplease come in item number: "); ordernumber = user_input.nextint(); final double coffee = 1.50; final double latte = 3.50; final double cappuccino = 3.25; final double espresso = 2.00; double cost = 0; if (ordernumber == 1) cost = coffee; if (ordernumber == 2) cost = latte; if (ordernumber == 3) cost = cappuccino; if (ordernumber == 4) cost = espresso; homecoming ordernumber; } public static int getquantity(int orderquan) { scanner user_input = new scanner(system.in); system.out.print("\nplease come in quantity: "); orderquan = user_input.nextint(); homecoming orderquan; } public static double computesubtotal(int ordernumber, int orderquan, double subtotal) { subtotal = (ordernumber * orderquan); system.out.print("your total before discount , taxation is: $" + subtotal); homecoming subtotal; } public static boolean discountcheck(double subtotal) //takes subtotal , returns true if user earned discount (over $10) { if (subtotal >= 10.00) homecoming true; else homecoming false; } }

your methods getitemnumber, getquantity, computesubtotal , discountcheck homecoming value, not storing homecoming value in main method.

in add-on that, getitemnumber() method storing cost locally, discarded when method finished - cost should returned (and method renamed).

you should have this:

//beginning of calls methods displaymenu(); double itemcost = getitemcost(); // getitemnumber() orderquan = getquantity(orderquan); subtotal = computesubtotal(itemcost, orderquan); boolean shoulddiscount = discountcheck(subtotal);

of course, utilize object-oriented approach, variables should members of class, wouldn't need pass or homecoming values - accessible methods in class.

java methods

No comments:

Post a Comment