    /*
       Solution to Problem 5 in Practice Problem Set 1.
        Aditya Mathur. January 27, 2019
    */

import java.util.Scanner;

public class GroceryReceipt {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        double pricePerPound; // In dollars
        double qtyPurchased; // In pounds
        double netPrice; //Net price of items purchased
        final double taxRate = 0.07;// This is 7%
        double priceToPay; //Price to be paid;
        double cashTendered;// Cash given by the custoner
        double changeReturned; //Change returned to mthe customer

        // Get input data;
        System.out.print("Price per pound please: ");
        pricePerPound=s.nextFloat();
        System.out.print("Quantity purchased (in pounds): ");
        qtyPurchased=s.nextFloat();
        //Compute net price
        netPrice=pricePerPound*qtyPurchased;
        //Add tax.
        double tax=netPrice*taxRate;
        priceToPay=netPrice+tax;
        System.out.printf("Amount to be paid: %.2f\n ",netPrice);
        System.out.println();
        System.out.printf("%s", "Amount paid: ");
        cashTendered=s.nextFloat();
        if (cashTendered<priceToPay){ // Check if amount paid is OK.
            System.out.print("Sorry, you have not paid the full amount. ");
        }// End of then-part
        else {
            changeReturned = cashTendered - netPrice;
            // Print receipt
            System.out.printf("%-20s %.2f pounds\n", "Tomatoes purchased", qtyPurchased);
            System.out.printf("%-20s $%.2f per pound\n", "Unit price", pricePerPound);
            System.out.printf("%-20s $%.2f \n", "Net price", netPrice);
            System.out.printf("%-20s %.2f \n", "Tax(7%) ", tax); // Note how % is escaped using %
            System.out.printf("%-20s $%.2f  \n", "Total price", priceToPay);
            System.out.printf("%-20s $%.0f  \n", "Cash tendered", cashTendered);
            System.out.printf("%-20s $%.2f  \n", "Change", changeReturned);
        }// End of else
    }//End of main()
}//End of class GroceryReceipt
