java - How do I calculate numbers with If statement if more than one condition is met? -


i have edited code below changes have made. here output getting.

please enter item.imported bottle of perfume please enter price imported bottle of perfume: 47.50 continue add items? (type y) yes , (type n) no.n cart contains following @ items tax{imported box of chocolate=10.00, imported bottle of perfume=67.50} 67.50

the box of chocolate should 10.50 , imported bottle should 54.65. use debug , saw total price variable using holds 10.50 looks changes back. also, instead of reading loop has

 if (item.contains("imported") && item.contains("bottle"))  

it reads

 else if (item.contains("imported")) 

first. full code below.

public class salestax  { public static void main(string[] args)  {     // input items shopping cart     hashmap<string, string> cart = new hashmap<string, string>();     hashmap<string, string> shoppingcart = new hashmap<string, string>();      // create scanner     scanner input = new scanner(system.in);      // variables     char done;      //boolean goods;     double totalprice = 0.00;     double taxprice;      // pick items list.          {           system.out.print("please enter item.");         string item = input.nextline();          system.out.print("please enter price "+ item + ": ");         string price = input.nextline();           double price1 = double.parsedouble(price);         totalprice += price1;           //system.out.println(string.format("%.2f",totalprice));         string price2 = string.valueof(price1);         cart.put(item, price2);          //determine if item have additional tax         if (item.contains("music"))         {              price1 = double.parsedouble(price);             taxprice = price1 * .10;              totalprice = (totalprice + taxprice);              //system.out.println(string.format("%.2f",totalprice));             string newprice2 = string.valueof(string.format("%.2f", price1 * 1.10));             shoppingcart.put(item,newprice2);         }         else if (item.contains("imported"))         {              price1 = double.parsedouble(price);             taxprice = price1 * .05;              totalprice = (totalprice + taxprice);              //system.out.println(string.format("%.2f",totalprice));             string newprice2 = string.valueof(string.format("%.2f", totalprice));             shoppingcart.put(item,newprice2);         }         if (item.contains("imported") && item.contains("bottle"))         {              price1 = double.parsedouble(price);             taxprice = price1 * (.05 + .10);              totalprice = (totalprice + taxprice);              //system.out.println(string.format("%.2f",totalprice));             string newprice2 = string.valueof(string.format("%.2f", totalprice));             shoppingcart.put(item,newprice2);         }         else if(item.contains("bottle"))         {              price1 = double.parsedouble(price);             taxprice = price1 * .10;              totalprice = (price1 + taxprice);              //system.out.println(string.format("%.2f",totalprice));             string newprice2 = string.valueof(string.format("%.2f", price1 * 1.10));             shoppingcart.put(item,newprice2);         }         else          {             shoppingcart.put(item, price);         }           system.out.print("would continue add items? (type y) yes , (type n) no.");         done = input.nextline().charat(0);     } while(character.touppercase(done) == 'y');     system.out.println("your cart contains following @ items tax" + shoppingcart); //+ string.format("%.2f", totalprice));    system.out.println(string.format("%.2f",totalprice)); } 

}

use && operator. it's called logical , operator.

//if item contains both "imported" , "bottle" if (item.contains("imported") && item.contains("bottle")){     //code } 

read more operators here.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -