java - Why isn't my code calculating the correct total? -


i'm having trouble trying find out running total. each time calculate running total miscalculates , gives me incorrect answer. i'm not sure is. cant tell if has method calling did in main, if statement in takeorder or neither.

import java.text.decimalformat;  import javax.swing.joptionpane;  public class mycoffeehouse {     public static void main(string[] args) {         string name = joptionpane.showinputdialog(null, "what name?");         greetcustomer(name);         double totalprice = takeorder(name);         calculatefinalprice(totalprice);     }      public static void greetcustomer(string name) {         // greet customer         joptionpane.showmessagedialog(null, "hello " + name + ", welcome cup of java!");     }      public static double takeorder(string name) { // method returns         string[] food = {"coffee", "bagel", "tea", "muffin"};         double[] price = {3.99, 1.99, 2.99, 4.99};         double totalprice = 0;         decimalformat dec = new decimalformat("#.00");          (int index = 0; index < food.length; index++) {             joptionpane.showmessagedialog(null, "our menu offers: " + food[index] + "(s) " + "$"                 + price[index]);         }          int numitems =             integer.parseint(joptionpane.showinputdialog(name + ", how many items "                 + "you interested in purchasing?"));          // running total         (int index = 0; index < numitems; index++) {             string input =                 joptionpane.showinputdialog(null, "which items interested in purchasing "                     + "our menu: coffee, bagel, tea, or muffin?");              if (input.equalsignorecase(food[index])) {                 totalprice += price[index];             }         }         return totalprice;     }      public static void calculatefinalprice(double totalprice) {         double salestax = (totalprice * 0.07) + totalprice; // 7% salestax         double finalprice;         decimalformat dec = new decimalformat("#.00");          int input =             joptionpane.showconfirmdialog(null, "would dine in?", "confirm",                 joptionpane.yes_no_option, joptionpane.question_message);          if (input == joptionpane.yes_option) {             finalprice = totalprice + (salestax * 0.02);             joptionpane.showmessagedialog(null, "the final price $" + finalprice);         } else {             decimalformat dec = new decimalformat("#.00");             joptionpane.showmessagedialog(null, "the final price $" + dec.format(salestax));         }     } } 

when do

double salestax= totalprice + 0.07; //7% salestax 

this means adding 7 cents in tax, not 7 percent. add 7 % need multiply original price 1.07 or 100% + 7% = 107%

double salestax= totalprice * 1.07; // add 7% salestax 

when do

finalprice=salestax + 0.02; 

you adding 2 cents. note: @ point can add 2% adding 7% , 2% not same @ adding 9% 1.07 * 1.02 > 1.09.


Popular posts from this blog