Using  Java Formatting To Create A Receipt
1- Biên nhận đơn giản.
     
 class Rextester{  
       private double total
= 0;
       private Formatter f = new
Formatter(System.out);
       public void printTitle(){
        f.format("%-15s
%10s \n", "ITEM", "QTY");
        f.format("%-15s
%10s \n", "----", "----");}
       public void print(String name, int qty){
       f.format("%-15s %10d \n", name,
qty); total += qty;}   
       public void
printTotal(){
       f.format("%-15s
%10s\n", " ", "----");
       f.format("%-15s %10s\n",
"Total",  total );}
        public static void main(String[] args){
       Rextester receipt = new Rextester();
       receipt.printTitle();
       receipt.print("Xoai Mango", 4);
       receipt.print("Pineaple", 4);
       receipt.print("Sâu Riêng", 4);
       receipt.printTotal();}
       }
SUCCESS. Compilation
time: 0.96 sec, absolute running time: 0.14 sec, 
cpu time: 0.07 sec, memory peak: 16 Mb, absolute service time: 1.1
sec
Output
 
ITEM QTY
  ----                  ---- 
Xoai Mango               4 
Pineaple                 4 
Sâu Riêng                4 
                        ----
TOTAL                   12
2- Biên nhận hơi phức tạp.     
       
class Rextester{      
       private double total = 0; 
       private double total2 = 0;
       private Formatter f = new
Formatter(System.out);
       public void printTitle(){
        f.format("%-15s %10s %15s
\n", "ITEM", "QTY","PRICE");
        f.format("%-15s %10s %15s
\n", "----","----" ,"----");}
       public void print(String
name, int qty, double price){   
        f.format("%-15s %10d %15.2f \n",
name, qty, price);total+=qty;
        total2+=price;
} 
       public void printTotal(){
       f.format("%-15s %10s %15s\n",    " 
 ",  "----", "----");
       f.format("%-15s %10.2s
%15.2f\n", "TOTAL", total, total2);}
       public static void
main(String[] args){
       Rextester receipt = new Rextester();
       receipt.printTitle();
       receipt.print("Xoai",
4, 4.25);
       receipt.print("Pineaple",
4, 3.25);
       receipt.print("Sâu
Riêng",1, 14.25);
       receipt.print("Banana",1,
2.50);
       receipt.printTotal();
}}
SUCCESS . Compilation time: 0.94 sec, absolute running time: 0.14
sec, cpu time: 0.07 sec, 
memory peak: 18 Mb, absolute
service time: 1.09 sec
Output
ITEM                   QTY           PRICE 
----                   ----          ---- 
Xoai                     4            4.25 
Pineaple                 4            3.25 
Sâu Riêng                1           14.25 
Banana                   1            2.50 
                       ----          ----
TOTAL                   10           24.25
 3-  Biên nhận có  TAX - Hàng không phải thực phẩm.   
       
class Rextester{      
       private double total = 0; 
       private
double total2 = 0;
       private Formatter f = new
Formatter(System.out);
       public void printTitle(){
        f.format("%-15s %10s %15s \n",
"ITEM", "QTY","PRICE");
        f.format("%-15s %10s %15s
\n", "----","----" ,"----");}
       public void print(String
name, int qty, double price){   
        f.format("%-15s %10d %15.2f \n",
name, qty, price);total+=qty;
        total2+=price;
} 
       public void printTotal(){
       f.format("%-15s
%10s %15.2f\n", "TAX", " 
 " , total2*0.07);
       f.format("%-15s %10s %15s\n",    " 
 ",  "----", "----"); 
       f.format("%-15s %10.1s
%15.2f\n", "TOTAL", total, total2*1.07);}
public static void main(String[] args){
       Rextester receipt = new Rextester();
receipt.printTitle();
receipt.printTitle();
       receipt.print("T-shirts small
", 4, 12.00);
       receipt.print("Ceramic
spoons", 2, 2.50);
       receipt.print("Plastic
bucket",1, 4.25);
       receipt.print("Aluminium
foil",2, 2.50);
       receipt.print("Saran", 2,
5.00);
       receipt.printTotal();
}}
SUCCESS . Compilation time: 0.73 sec, absolute running time: 0.14
sec, 
cpu time: 0.06 sec, memory peak:
18 Mb,absolute service time: 0.88 sec
Output.
ITEM QTY PRICE---- ---- ----T-shirts small 4 12.00Ceramic spoons 2 2.50Plastic bucket 1 4.25Aluminium foil 2 2.50Saran 2 5.00TAX 2.10---- ----TOTAL 11 28.35
GHI CHÚ- HƯỚNG DẪN
Trong mỗi biên
nhận cần phải có những thành phần kê sau .
* Formatter
f = new Formatter(System.out) của Formatter class chứa trong Java 1.5 có nhiệm vụ tự động sắp đặt vị trí và điều
chỉnh khoảng cách ( auto padding ) của chữ viêt và các con số theo ý
chúng ta.
Vì chúng ta dùng Formatter để ouput printStream bytes nên
Formatter phải có chứa  System.out .
*  public void printTitle(){  }  Để viết đầu đề headers và vẽ
đường ngăn cách
*  public void print(){  }        Để viết tên món hàng, số lượng,
giá cả
* public
void printTotal(){  } Để viết tổng cộng các món
hàng, tổng cộng giá ,tax 
*  public static void main(String[] args){  } Để vận
hành program.
 Dấu
% là
bắt đầu formatting expression.
 Dấu
%10 nghĩa là cần có một khoãng rộng 10 byte field (The value of field
width is 10).
 Dấu %10d. Sau byte field thì phải có một trong những chữ sau.
 s dùng cho string, d dùng cho decimal interger, f dùng cho floating point,
t dùng cho time,date vv…
 %-15s : chọn dấu tr̀ư khi chúng ta lấy bên trái làm chuẩn
cho output (left-justify) .
 %10.2s : chỉ lấy 2
chữ số. %10.1s : chỉ lấy
một chữ số. %10.0s : không lấy
chữ số nào hết nghĩa là bỏ trống.
 
Dấu  \n  gọi là escape sequence hay new line .
Muốn có con
số tổng cộng các món hàng thì
phải viết total
+= qty                                       
Muốn có con
số tổng cộng giá cả của tất cả
các  món hàng thì phải viết total += price                                        
Muốn có Total Tax cho tất cả các món hàng
thì phải viết : final price * (1+TAX_RATE) mới cho kết quả đúng.
Thí dụ.
     double item1price = 10.25,item2price = 5.25,item3price = 2.25;
double finalprice = (item1price+item2price+item3price)*(1+TAX_RATE);
System.out.println("Your total purchase is:"+finalprice+" with tax.");
Output. Your total purchase is:19.17 with tax.
4- Biên nhận thực phẫm có ghi quốc gia sãn xuất.
       class Rextester{  
       private double total = 0; 
       private double total2 = 0;
       private Formatter f = new
Formatter(System.out);
       public void printTitle(){
        f.format("%-15s %10s %15s
%15s\n", "ITEM",
"QTY","PRICE","ORIGIN");
        f.format("%-15s %10s %15s
%15s\n", "----","----"
,"----","----");}
       public void print(String
name, int qty,double price,String origin){  
        f.format("%-15s %10d %15.2f
%15s\n", name, qty, price,origin);total+=qty;
        total2+=price; } 
       public void printTotal(){
       f.format("%-15s %10s %15s
%15s\n",    "   ", 
"----", "----","----");
       f.format("%-15s %10.2s %15.2f
%15s\n", "TOTAL", total, total2,"  ");}
       public static void
main(String[] args){
       Rextester receipt = new Rextester();
       receipt.printTitle();
       receipt.print("Xoai", 4,
4.25,"Mexico");
       receipt.print("Pineaple", 4,
3.25,"USA");
       receipt.print("Sâu Riêng",1,
14.25,"Indonesia");
       receipt.print("Banana",1,
2.50,"Guatemala");
       receipt.printTotal();
}}
SUCCESS.
Compilation
time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.1
sec, memory peak: 23 Mb, absolute service time: 0.98 sec. 
Output.
ITEM                   QTY           PRICE          ORIGIN
----                  ----            ----            ----
Xoai                     4            4.25          Mexico
Pineaple                 4            3.25             USA
Sâu Riêng                1           14.25       Indonesia
Banana                   1            2.50       Guatemala
                      ----            ----            ----
TOTAL 10 24.25