基於簡單工廠模式的計算器程序


這個計算器是我學Java時寫的第一個Swing程序,后來我讀《大話設計模式》的第一章簡單工廠模式恰好也是計算器,於是就改進了之前這個。

源代碼下載:https://github.com/myCodingTrip/Calculator

運算類Operation.java

public abstract class Operation {
    protected double numberA = 0;
    protected double numberB = 0;
    protected double result = 0;
    
    public double getNumberA() {
        return numberA;
    }
    public void setNumberA(double numberA) {
        this.numberA = numberA;
    }
    public double getNumberB() {
        return numberB;
    }
    public void setNumberB(double numberB) {
        this.numberB = numberB;
    }
    public abstract double getResult();
}

class OperationAdd extends Operation{
    @Override
    public double getResult() {
        double result = 0;
        result = numberA + numberB;
        return result;
    }
}
class OperationSub extends Operation{
    @Override
    public double getResult() {
        result = numberA - numberB;
        return result;
    }
}
class OperationMul extends Operation{
    @Override
    public double getResult() {
        result = numberA * numberB;
        return result;
    }
}
class OperationDiv extends Operation{
    @Override
    public double getResult(){
        try {
            result = numberA / numberB;
        } catch (Exception e) {
            System.out.println("除數不能為0");
            e.printStackTrace();
        }
        return result;
    }
}
工廠類 OperationFactory.java
//簡單工廠模式
//只需要傳入運算符號,工廠就實例化出合適的對象,用過多態,返回父類的方式實現計算器的結果
public class OperationFactory {
    public static Operation createOperate(char sign) {
        Operation operation = null;
        switch (sign) {
        case '+':
            operation = new OperationAdd();
            break;
        case '-':
            operation = new OperationSub();
            break;
        case '*':
            operation = new OperationMul();
            break;
        case '/':
            operation = new OperationDiv();
            break;
        default:
            break;
        }
        return operation;
    }
}
計算器界面CalculateFrame.java
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class CalculateFrame extends JFrame{
    
    private JTextField textData1, textData2, textResult;
    private JLabel labelNum1, labelNum2, labelResult;
    private JRadioButton radioAdd, radioSub, radioMul, radioDiv;
    private JButton btnCalculate;
    private Operation oper;
    //進行運算的兩個數字和結果
    private String data1, data2, operation;
    private double resultNum;
    public CalculateFrame(){        
        //設置窗口基本屬性
        this.setTitle("簡單計算器");
        this.setSize(300, 200);
        //本機分辨率為1366*768
        this.setLocation(1366/2-300/2, 768/2-200/2);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible (false);
                System.exit(0);
            }
        });
        
        initView();
        initLayout();
        this.setVisible(true);//顯示窗口
        btnCalculate.addActionListener(new ButtonListener());    
    }
    
    //設置一些功能區域
    public void initView() {
        textData1 = new JTextField();
        textData2 = new JTextField();
        textResult = new JTextField();
        textResult.setEnabled(false);
        labelNum1 = new JLabel("第一個數:");
        labelNum2 = new JLabel("第二個數:");
        labelResult = new JLabel("結果:");
        btnCalculate = new JButton("計算");
        radioAdd = new JRadioButton("+");
        radioSub = new JRadioButton("-");
        radioMul = new JRadioButton("*");
        radioDiv = new JRadioButton("/");
    }
    
    //將控件加入到布局中
    public void initLayout() {
        JPanel p1 = new JPanel(new GridLayout(1,2));
        p1.add(labelNum1);
        p1.add(textData1);
        JPanel p2 = new JPanel(new GridLayout(1,4));
        p2.add(radioAdd);
        p2.add(radioSub);
        p2.add(radioMul);
        p2.add(radioDiv);    
        JPanel p3 = new JPanel(new GridLayout(1,2));
        p3.add(labelNum2);
        p3.add(textData2);
        JPanel p4 = new JPanel(new GridLayout(1,2));
        p4.add(labelResult);
        p4.add(textResult);
        JPanel p5 = new JPanel(new GridLayout(1,1));
        p5.add(btnCalculate);    
        this.setLayout(new GridLayout(5,1));        
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        this.add(p5);        
        ButtonGroup group = new ButtonGroup();
        group.add(radioAdd);
        group.add(radioSub);
        group.add(radioMul);
        group.add(radioDiv);
    }
    class ButtonListener implements ActionListener{
        //從程序中讀入數據
        public void actionPerformed(ActionEvent e) {
            data1 = textData1.getText();
            data2 = textData2.getText();
            
            operation = "";
            if(radioAdd.isSelected())        operation = radioAdd.getText();
            else if(radioSub.isSelected())    operation = radioSub.getText();                
            else if(radioMul.isSelected())    operation = radioMul.getText();                
            else if(radioDiv.isSelected())    operation = radioDiv.getText();
            
            //將運算符號傳入工廠中進行實例化
            oper = OperationFactory.createOperate(operation.charAt(0));
            oper.numberA = Double.parseDouble(data1);
            oper.numberB = Double.parseDouble(data2);
            resultNum = oper.getResult();
            textResult.setText(String.valueOf(resultNum));
        }
    }    
}
主程序 Main.java
public class Main {
    public static void main(String[] args) {
        new CalculateFrame();
    }
}

程序界面:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM