Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class ShoppingCart {
- // "Attributes"
- private double taxRate;
- private double preTaxTotalPrice;
- private double totalPrice;
- // Constructor
- public ShoppingCart() {
- // Set the variables to their default values
- taxRate = 0D;
- preTaxTotalPrice = 0;
- totalPrice = 0;
- }
- // "Operations"
- public double getTaxRate() {
- // Just a simple getter
- return taxRate;
- }
- public void setTaxRate(double taxRate) {
- // Just a simple setter
- this.taxRate = taxRate;
- }
- public void getInput(Scanner scanner) {
- System.out.println("Enter the name, quantity, and price of the item."); // Prompt the user
- String name = scanner.next(); // Get first the string,
- int quantity = scanner.nextInt(); // And then the integer
- double price = scanner.nextDouble(); // And then the double
- addItem(name, quantity, price); // addItem using those three values
- }
- public void addItem(String item, int quantity, double price) {
- preTaxTotalPrice += quantity * price; // Add the price times the quantity to the pTTP
- totalPrice = preTaxTotalPrice * (1 + taxRate); // Multiple the pTTP times 1.(taxRate) to get tP
- }
- public String getPreTaxTotalPrice() {
- // I like using DecimalFormat over the way mentioned in the hint
- return "$" + new DecimalFormat("#.##").format(preTaxTotalPrice);
- }
- public String getTotalPrice() {
- // Identical to gPTTP outside of the variable being formatted
- return "$" + new DecimalFormat("#.##").format(totalPrice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement