2015-04-20 15:36:01 -07:00
|
|
|
//: strings/Receipt.java
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class Receipt {
|
|
|
|
private double total = 0;
|
|
|
|
private Formatter f = new Formatter(System.out);
|
|
|
|
public void printTitle() {
|
|
|
|
f.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
|
|
|
|
f.format("%-15s %5s %10s\n", "----", "---", "-----");
|
|
|
|
}
|
|
|
|
public void print(String name, int qty, double price) {
|
|
|
|
f.format("%-15.15s %5d %10.2f\n", name, qty, price);
|
2015-04-29 13:56:17 -07:00
|
|
|
total += price * qty;
|
2015-04-20 15:36:01 -07:00
|
|
|
}
|
|
|
|
public void printTotal() {
|
|
|
|
f.format("%-15s %5s %10.2f\n", "Tax", "", total*0.06);
|
|
|
|
f.format("%-15s %5s %10s\n", "", "", "-----");
|
|
|
|
f.format("%-15s %5s %10.2f\n", "Total", "",
|
|
|
|
total * 1.06);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Receipt receipt = new Receipt();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
} /* Output:
|
|
|
|
Item Qty Price
|
|
|
|
---- --- -----
|
|
|
|
Jack's Magic Be 4 4.25
|
|
|
|
Princess Peas 3 5.10
|
|
|
|
Three Bears Por 1 14.29
|
2015-04-29 13:56:17 -07:00
|
|
|
Tax 2.80
|
2015-04-20 15:36:01 -07:00
|
|
|
-----
|
2015-04-29 13:56:17 -07:00
|
|
|
Total 49.39
|
|
|
|
*///:~
|