Shopping Bill Calculator

Java Console Output


Planning Purchases

Calculator

After buying something, do you ever regret having made the purchase? This is called buyer's remorse. Impluse spending can easily cause this. I was once told that if I ever suddenly really wanted to make an unplanned purchase, that I should wait two weeks. After two weeks, if I still felt like I needed the item, then it would probably be a wise purchase.


Writing the Code

Since many people tend to shop extravagantly, I decided it might be useful to learn how to program a shopping bill calculator. This program lets the user enter as many shopping items as they wish. When the user is done entering shopping items, it calculates the total cost plus 10% sales tax. To do this, it uses a while loop with an if statement at the end of it. Because it is an infinite loop, the while loop ends with an if statement. The if statement contains a break to get out of the loop. The only way to get out of the loop is when the user enters anything but “1” when asked if he/she desires to purchase another item.


Running the Code

Here's an example running the code:

Welcome to Shopping Bill Estimator!

Enter the name of the item you wish to purchase: Apples
Enter the cost of Apples in U.S. dollars: $1.99
Enter the number/amount of Apples you wish to buy: 10
Purchasing Apples will cost you $19.9.
Your estimated subtotal before taxes is: $19.9

Do you wish to add another product to your estimate? (Enter 1 for yes, or enter 2 for no): 1
Enter the name of the item you wish to purchase: Oranges
Enter the cost of Oranges in U.S. dollars: $.89
Enter the number/amount of Oranges you wish to buy: 15
Purchasing Oranges will cost you $13.35.
Your estimated subtotal before taxes is: $33.25

Do you wish to add another product to your estimate? (Enter 1 for yes, or enter 2 for no): 1
Enter the name of the item you wish to purchase: Milk
Enter the cost of Milk in U.S. dollars: $3.29
Enter the number/amount of Milk you wish to buy: 2
Purchasing Milk will cost you $6.58.
Your estimated subtotal before taxes is: $39.83

Do you wish to add another product to your estimate? (Enter 1 for yes, or enter 2 for no): 1
Enter the name of the item you wish to purchase: Cereal
Enter the cost of Cereal in U.S. dollars: $3.49
Enter the number/amount of Cereal you wish to buy: 4
Purchasing Cereal will cost you $13.96.
Your estimated subtotal before taxes is: $53.79

Do you wish to add another product to your estimate? (Enter 1 for yes, or enter 2 for no): 1
Enter the name of the item you wish to purchase: Eggs
Enter the cost of Eggs in U.S. dollars: $1.39
Enter the number/amount of Eggs you wish to buy: 3
Purchasing Eggs will cost you $4.17.
Your estimated subtotal before taxes is: $57.96

Do you wish to add another product to your estimate? (Enter 1 for yes, or enter 2 for no): 2
Your total estimate, including taxes is $63.756


Code for the Bill Calculator


For more information on Java, see Oracle's Java Tutorials

© 2019 Authentic Audio, LLC