Reputation: 143
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));
}
}
Upvotes: 2
Views: 2406
Reputation: 14286
Use the &&
operator. It's called the logical AND operator.
//if item contains both "imported" and "bottle"
if (item.contains("imported") && item.contains("bottle")){
//code
}
Read more about operators here.
Upvotes: 2
Reputation: 3574
Whenever you use any kinds of an if
statement you pass a boolean variable to it. There are two logical operators in Java that take two booleans and output one boolean:
b1 && b2
is true
if b1
and b2
are true
- otherwise it is false
.
b1 || b2
is true
if b1
or b2
or both are true
- otherwise it is false
.
In your case if you want to execute the same block of code when "music"
or "bottle"
are contained in the input text, you would do something like this:
if(items.contains("music") || items.contains("bottle")) {
//modify prices
}
Upvotes: 0