Code example for Formatter

Methods: format

0
        f.format("%-" + ITEM_WIDTH + ".15s %" + QTY_WIDTH + "d %" + PRICE_WIDTH + ".2f\n", name, qty, price);
        total += price;
    } 
 
    public void printTotal() { 
        f.format("%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + ".2f\n", "Tax", "", total * 0.06);
        f.format("%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + "s\n", "", "", "-----");
        f.format("%-" + ITEM_WIDTH + "s %" + QTY_WIDTH + "s %" + PRICE_WIDTH + ".2f\n", "Total", "", total * 1.06);
    } 
 
    public static void main(String[] args) {
        FormatterReceipt receipt = new FormatterReceipt();
        receipt.printTitle();
        receipt.print("Jack's Magic Beans", 4, 4.25);
        receipt.print("Princess Peas", 3, 5.1);
        receipt.print("Three Bears Porridge", 1, 14.29);
        receipt.printTotal();
    } 
}