1 回答
TA贡献1826条经验 获得超6个赞
雪,你正处于一段漫长而有趣且富有成果的旅程的开始。祝你好运!对于您的第一个问题,就面向对象设计原则而言,在不同的完善水平上有许多正确的答案。我需要建议你尽早阅读 Java 编码标准、控制反转、单元测试。我将仅提及封装并推荐您以下解决方案。
我将首先创建两个类 Receipt 和 ReceiptItem(可以根据项目的范围创建 Product、ProductPrice 等),假设它不仅是您尝试打印的单个收据(并且可能用于存储收据以供以后检查)还需要一个持久层,这是为您探索的另一个领域),但您将处理许多带有更多收据详细信息行的收据。然后我将创建这两个类的实例,并在将其发送到文本显示/打印设备之前调用 Receipt 类的 toFormattedString 方法。
public class ReceiptItem {
private static final int TITLE_WIDTH = 30;
private static final String DETAIL_FORMAT="%-" + TITLE_WIDTH + "s : %6s%n";
private String title;
private BigDecimal totalPrice;
public ReceiptItem(String title, BigDecimal totalPrice) {
setTitle(title);
setTotalPrice(totalPrice);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public BigDecimal getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(BigDecimal totalPrice) {
this.totalPrice = totalPrice;
}
public String toFormattedString() {
return String.format(DETAIL_FORMAT, getTitle(),
getTotalPrice().toPlainString());
}
}
public class Receipt {
private static final String RECEIPT_HEADER_FORMAT = "Receipt number #%s%n";
private static int LAST_RECEIPT_NUMBER = 0;
private int receiptNumber;
private List<ReceiptItem> items= new ArrayList<>();
public Receipt() {
super();
setReceiptNumber(++LAST_RECEIPT_NUMBER);
}
public Receipt(int receiptNumber) {
this();
setReceiptNumber(LAST_RECEIPT_NUMBER=receiptNumber);
}
public void addItem(ReceiptItem item) {
items.add(item);
}
public int getReceiptNumber() {
return receiptNumber;
}
public void setReceiptNumber(int receiptNumber) {
this.receiptNumber = receiptNumber;
}
public List<ReceiptItem> getItems() {
return items;
}
public void setItems(List<ReceiptItem> items) {
this.items = items;
}
public String toFormattedString() {
StringBuilder builder = new StringBuilder();
builder.append(String.format(RECEIPT_HEADER_FORMAT, getReceiptNumber()));
for (ReceiptItem item:getItems()) {
builder.append(item.toFormattedString());
}
return builder.toString();
}
}
Receipt receipt = new Receipt();
receipt.addItem(new ReceiptItem("Beef Burgers", new BigDecimal("5.00")));
receipt.addItem(new ReceiptItem("Cheese Burgers", new BigDecimal("5.00")));
receipt.addItem(new ReceiptItem("Fish and Chips", new BigDecimal("6.00")));
receipt.addItem(new ReceiptItem("French Fries", new BigDecimal("4.00")));
receipt.addItem(new ReceiptItem("Steak", new BigDecimal("10.00")));
receipt.addItem(new ReceiptItem("Sprite Drinks", new BigDecimal("1.00")));
receipt.addItem(new ReceiptItem("Soda Drinks", new BigDecimal("0.40")));
receipt.addItem(new ReceiptItem("Fuzetea Drinks", new BigDecimal("0.70")));
recieptText.setText(reciept.toFormattedString());
我对你的第二个问题的回答:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//... Your code here
JButton btnExit = new JButton("Exit");
btnExit.setFont(new Font("Tahoma", Font.PLAIN, 19));
btnExit.setBounds(766, 484, 127, 39);
if (btnExit.isSelected()==true) {
System.exit(0);
}
btnExit.addActionListener((e)-> {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
});
frame.getContentPane().add(btnExit);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
frame.setVisible(true);
frame.pack();
添加回答
举报