实现一个计算器程序要求:计算和显示分开也就是业务逻辑与界面逻辑分开它们之间的耦合度下降容易维护和扩展实现软件设计的正确性、健壮性、高效性、可复用性、可维护性等目标。用JAVA写
业务逻辑部分:
public class Calculator {
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double subtract(double num1, double num2) {
return num1 - num2;
}
public static double multiply(double num1, double num2) {
return num1 * num2;
}
public static double divide(double num1, double num2) {
if (num2 == 0) {
throw new IllegalArgumentException("Divisor cannot be zero.");
}
return num1 / num2;
}
}
界面逻辑部分:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CalculatorUI {
private JFrame frame;
private JLabel displayLabel;
private double currentNumber = 0.0;
private double result = 0.0;
private String operator = "";
private boolean startNewInput = true;
private boolean decimalAdded = false;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalculatorUI window = new CalculatorUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public CalculatorUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
displayLabel = new JLabel("0.0");
displayLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
displayLabel.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
topPanel.add(displayLabel);
JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5));
buttonPanel.add(createButton("C", new ClearAction()));
buttonPanel.add(createButton("+/-", new NegateAction()));
buttonPanel.add(createButton("%", new PercentageAction()));
buttonPanel.add(createButton("/", new OperatorAction()));
buttonPanel.add(createButton("7", new NumberAction()));
buttonPanel.add(createButton("8", new NumberAction()));
buttonPanel.add(createButton("9", new NumberAction()));
buttonPanel.add(createButton("*", new OperatorAction()));
buttonPanel.add(createButton("4", new NumberAction()));
buttonPanel.add(createButton("5", new NumberAction()));
buttonPanel.add(createButton("6", new NumberAction()));
buttonPanel.add(createButton("-", new OperatorAction()));
buttonPanel.add(createButton("1", new NumberAction()));
buttonPanel.add(createButton("2", new NumberAction()));
buttonPanel.add(createButton("3", new NumberAction()));
buttonPanel.add(createButton("+", new OperatorAction()));
buttonPanel.add(createButton("0", new NumberAction()));
buttonPanel.add(createButton(".", new DecimalAction()));
buttonPanel.add(createButton("=", new EqualsAction()));
JPanel contentPanel = new JPanel(new BorderLayout(5, 5));
contentPanel.add(topPanel, BorderLayout.NORTH);
contentPanel.add(buttonPanel, BorderLayout.CENTER);
frame.setContentPane(contentPanel);
}
private JButton createButton(String text, ActionListener listener) {
JButton button = new JButton(text);
button.setFont(new Font("Tahoma", Font.PLAIN, 16));
button.addActionListener(listener);
return button;
}
private class ClearAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
currentNumber = 0.0;
result = 0.0;
operator = "";
startNewInput = true;
decimalAdded = false;
displayLabel.setText("0.0");
}
}
private class NegateAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (startNewInput) {
return;
}
currentNumber = -currentNumber;
displayLabel.setText(Double.toString(currentNumber));
}
}
private class PercentageAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (startNewInput) {
return;
}
currentNumber = currentNumber / 100.0;
displayLabel.setText(Double.toString(currentNumber));
}
}
private class OperatorAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (startNewInput) {
return;
}
String newOperator = e.getActionCommand();
if (!operator.isEmpty()) {
try {
result = performCalculation();
} catch (IllegalArgumentException ex) {
displayLabel.setText(ex.getMessage());
startNewInput = true;
return;
}
displayLabel.setText(Double.toString(result));
} else {
result = currentNumber;
}
currentNumber = 0.0;
operator = newOperator;
startNewInput = true;
decimalAdded = false;
}
}
private class NumberAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
if (startNewInput) {
currentNumber = 0.0;
startNewInput = false;
}
currentNumber = currentNumber * 10.0 + Double.parseDouble(digit);
displayLabel.setText(Double.toString(currentNumber));
}
}
private class DecimalAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (startNewInput) {
currentNumber = 0.0;
startNewInput = false;
}
if (!decimalAdded) {
displayLabel.setText(displayLabel.getText() + ".");
decimalAdded = true;
}
}
}
private class EqualsAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
result = performCalculation();
} catch (IllegalArgumentException ex) {
displayLabel.setText(ex.getMessage());
startNewInput = true;
return;
}
currentNumber = result;
operator = "";
startNewInput = true;
decimalAdded = false;
displayLabel.setText(Double.toString(result));
}
}
private double performCalculation() {
switch (operator) {
case "+":
return Calculator.add(result, currentNumber);
case "-":
return Calculator.subtract(result, currentNumber);
case "*":
return Calculator.multiply(result, currentNumber);
case "/":
return Calculator.divide(result, currentNumber);
default:
throw new IllegalArgumentException("Unknown operator: " + operator);
}
}
}
这个计算器实现了基本的加、减、乘、除、百分号、正负号等功能,支持小数点输入,可以进行连续计算。业务逻辑部分封装了四则运算的逻辑,界面逻辑部分则负责与用户交互,显示计算结果。两部分分离,提高了代码的可读性和可维护性
原文地址: https://www.cveoy.top/t/topic/eqpt 著作权归作者所有。请勿转载和采集!