Console Based Sales Tax Calculator in Java
The code below can be used to create a sales tax calculator in Java. This is for review exercise 3.1 in the Intro to Java Series.
package salestaxcalc;
package topic03SalesTaxCalcReview;
import java.math.BigDecimal;
import java.util.Scanner;
public class salesTaxCalc {
public static void main(String[] args) {
//Initialize Variables
BigDecimal salesTaxRate = new BigDecimal("0.06");
//Create a Scanner & Ask For User Input
Scanner userInput = new Scanner(System.in);
System.out.println("Enter item price:");
BigDecimal itemPrice = userInput.nextBigDecimal().setScale(2, BigDecimal.ROUND_HALF_UP);
userInput.close();
//Calculations
BigDecimal salesTax = itemPrice.multiply(salesTaxRate).setScale(2, BigDecimal.ROUND_HALF_UP);
BigDecimal totalPrice = itemPrice.add(salesTax).setScale(2, BigDecimal.ROUND_HALF_UP);
//Print Out The Results
System.out.println("Item Price: $" + itemPrice);
System.out.println("Sales Tax: $" + salesTax);
System.out.println("Total Price: $" + totalPrice);
}
}
0 comments:
Post a Comment