⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receipt4.java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// strings/Receipt4.java
// TIJ4 Chapter Strings, Exercise 4, page 518
/* Modify Receipt.java so that the widths are all controlled by a single
* set of constant values. The goal is to allow you to easily change a width
* by changing a single value in one place.
*/
import java.util.*;

public class Receipt4 {
	private double total = 0;
	private Formatter f = new Formatter(System.out);
	private static final int W1 = 15;
	private static final int W2 = 5;
	private static final int W3 = 10;
	private String s1 = "%-" + W1 + "s %" + W2 + "s %" + W3 + "s\n";
	private String s2 = "%-" + W1 + ".15s %" + W2 + "d %" + W3 + ".2f\n";
	private String s3 = "%-" + W1 + "s %" + W2 + "s %" + W3 + ".2f\n";
	public void printTitle() {
		f.format(s1, "Item", "Qty", "Price");
		f.format(s1, "----", "---", "-----");
	}
	public void print(String name, int qty, double price) {
		f.format(s2, name, qty, price);
		total += price;
	}
	public void printTotal() {
		f.format(s3, "Tax", "", total * 0.06);
		f.format(s1, "", "", "-----");
		f.format(s3, "Total", "", total * 1.06);
	}
	public static void main(String[] args) {
		Receipt4 receipt = new Receipt4();
		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();		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -