I have edited my code below with the changes I have made. Here is the output I am getting.
Please enter an item.imported bottle of perfume Please enter the price for imported bottle of perfume: 47.50 Would you like to continue to add items? (Type Y) for Yes and (Type N) for No.n Your cart contains the following at items with tax{imported box of chocolate=10.00, imported bottle of perfume=67.50} 67.50
The box of chocolate should be 10.50 and the imported bottle should be 54.65. I use the debug and saw that the total price variable I am using holds 10.50 but it looks like it changes back. Also, instead of reading the loop that has
if (item.contains("imported") && item.contains("bottle"))
it reads
else if (item.contains("imported"))
first. My full code is below.
public class SalesTax
{
public static void main(String[] args)
{
// Input items for shopping cart
HashMap<String, String> cart = new HashMap<String, String>();
HashMap<String, String> shoppingcart = new HashMap<String, String>();
// Create a Scanner
Scanner input = new Scanner(System.in);
// variables
char done;
//boolean goods;
double totalprice = 0.00;
double taxprice;
// Pick items for list.
do
{
System.out.print("Please enter an item.");
String item = input.nextLine();
System.out.print("Please enter the price for "+ 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 will 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 you like to continue to add items? (Type Y) for Yes and (Type N) for No.");
done = input.nextLine().charAt(0);
} while(Character.toUpperCase(done) == 'Y');
System.out.println("Your cart contains the following at items with tax" + shoppingcart); //+ String.format("%.2f", totalprice));
System.out.println(String.format("%.2f",totalprice));
}
}