// File: InputDemo.java // A Purchase class object has the number of items purchased, a one-word // description of the item, and the price of the item. // Test class InputDemo uses the Scanner class to read data for a Purchase. // (Program also shows a defined constant and formatted output using "third // party" class Align) import java.util.Scanner ; // for input /** * A class to represent a purchase - number of items, description, and price * per item */ class Purchase { // instance vars private int quantity ; // number of each item purchased private String item ; // description of the item private double unitPrice ; // price per item private double cost ; // total cost: quantity * unitPrice private static final double TAX_RATE = 0.065 ; // 6.5% (a constant) /** * Creates a purchase object * @param quantity number of items bought * @param item one-word description of item * @param unitPrice price per item */ public Purchase(int quantity, String item, double unitPrice) { this.quantity = quantity ; this.item = item ; this.unitPrice = unitPrice ; // compute cost of purchase cost = quantity * unitPrice ; } /** * Return the cost of a purchase. * @return the cost of a purchase */ public double getCost() { return cost ; } /** * Returns a String containing the input data for a purchase * @return the quantity, description, and price per item as a String * Example: "5 widget(s) @ $1.23 */ public String data() { return quantity + " " + item + "(s) @ $" + unitPrice ; // example: "5 widget(s) @ $1.23 } /** * Compute and return the tax on a purchase. * @return the tax on a purchase */ public double getTax() { double tax = cost * TAX_RATE ; // tax on purchase return tax ; } /** * Compute and return total cost of a purchase, including tax * @return the total cost of a purchase, tax included */ public double getTotal() { // call getTax() to compute the tax, and add to cost return getTax() + cost ; } } // end of Purchase class definition ======================================== /** * Test class for Purchase class */ public class InputDemo3 { public static void main(String[] args) { Scanner input = new Scanner(System.in) ; // prompt user System.out.println("Enter quantity, description, and price of item " + "(separated by spaces):") ; int quantity = input.nextInt() ; // first token is an int String item = input.next() ; // next is a String double price = input.nextDouble() ; // last is a double // construct a Purchase object, using input data Purchase current = new Purchase(quantity, item, price) ; // print purchase, tax, and total cost System.out.println("\nYour purchase:\n") ; System.out.println(Align.right( current.data(),25 ) + " = " + Align.currency( current.getCost(), 8) ) ; System.out.println(Align.right( "Tax",25 ) + " = " + Align.currency( current.getTax(), 8) ) ; System.out.println(Align.right( "Total cost",25 ) + " = " + Align.currency( current.getTotal(), 8) ) ; } } /* sample output: Enter quantity, description, and price of item (separated by spaces): 8 hockeypuck 4.37 Your purchase: 8 hockeypuck(s) @ $4.37 = $34.96 Tax = $ 2.27 Total cost = $37.23 */