`
yxx676229549
  • 浏览: 71954 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Formatter类的优点--对其打印

阅读更多

看到编程思想里的formatter,觉得对齐这点做的很好,以前自己写pos机代码时用到这个就好了,记录下来

package star20110512;

import java.util.Formatter;

public class Receipt {

	private double total = 0;
	private Formatter formatter = new Formatter(System.out);
	private Formatter formatter2 = new Formatter(System.err);
	public void printTitle(){
		formatter.format("%-15s %5s %10s\n", "Item" ,"Qty" ,"Price");
		formatter.format("%-15s %5s %10s\n", "----" ,"---" ,"-----");
	}
	public void print(String name, int qty,double price){
		formatter2.format("%-15s %5d %10.2f\n", name,qty,price);
		total += price;
	}
	public void printTotal(){
		formatter.format("%-15s %5s %10.2f\n", "Tax","",total*0.06);
		formatter.format("%-15.15s %5s %10s\n", "","","-----");
		formatter.format("%-15s %5s %10.2f\n", "Total","",total);
	}
	public static void main(String[] args) {
		Receipt receipt = new Receipt();
		receipt.printTitle();
		receipt.print("good check",5, 10.98);
		receipt.print("coco",10, 6.5);
		receipt.print("ice",200, 10);
		receipt.printTotal();
	}
}
公司不能上外网,所以结果就不贴了,想看结果,自己运行下...

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics