看到编程思想里的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();
}
}
公司不能上外网,所以结果就不贴了,想看结果,自己运行下...
分享到:
相关推荐
maven-java-formatter-plugin-0.4.jar
maven-java-formatter-plugin-0.4-sources.jar
maven-java-formatter-plugin-0.3.jar
maven-java-formatter-plugin-0.3-sources.jar
maven-java-formatter-plugin-0.3.1-sources.jar
maven-java-formatter-plugin-0.2.0-sources.jar
资源分类:Python库 所属语言:Python 资源全名:bandit_sarif_formatter-1.0.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
maven-java-formatter-plugin-0.3.1.jar
matlab-formatter-vscode-master.zip
maven-java-formatter-plugin-0.2.0.jar
资源分类:Python库 所属语言:Python 资源全名:date_formatter-0.1.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python库。 资源全名:gdf_formatter-0.0.5-py3-none-any.whl
资源来自pypi官网。 资源全名:aa_discord_ping_formatter-1.1.4-py3-none-any.whl
formatter-maven-plugin 该项目提供了一种机制,可以在Maven构建过程中自动(重新)格式化项目,或验证其格式,以便您的项目可以收敛于一致的代码样式,而与用户首选项,IDE设置等无关。 它开始关注该主题的帖子...
资源分类:Python库 所属语言:Python 资源全名:jupyterlab_code_formatter-1.4.5-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
python库。 资源全名:pytest_collect_formatter-0.3.0-py2.py3-none-any.whl