誰能幫我重構一下我這個計算器程式。

wudikua123發表於2010-09-02
本人菜鳥,寫了個計算器程式,但是覺得沒什麼物件導向思想,也學了工廠,策略,裝飾,代理4種設計模式,但是應用起來還是不會,誰能幫我用物件導向思維重構一下我的程式。閱讀我的程式可能會比較混亂。。。。希望大家見諒。
import java.awt.*;
import java.awt.event.*;
public class Moniter implements ActionListener{
MyCalculator mc;
Moniter(MyCalculator mc){
this.mc = mc;
}

@Override
public void actionPerformed(ActionEvent e) {
Button b = (Button)e.getSource();
String str = b.getLabel();
if(str=="+/-"){
mc.tf.setText("-" + mc.tf.getText());
}
else {
mc.tf.setText(mc.tf.getText() + str);
}
}

}


import java.awt.*;
import java.util.*;
import java.util.List;
import java.awt.event.*;

public class MyCalculator {
TextField tf;
double result = 0,i;
int op;
ArrayList<Double> al_temp = new ArrayList<Double>();
ArrayList<Integer> al_op = new ArrayList<Integer>();
public static void main(String[] args) {
new MyCalculator().lunchFrame();
}

public void lunchFrame(){
Frame f = new Frame("Calculator");
f.setBounds(200, 300, 320, 460);

Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button btnPoint = new Button(".");
Button btnCancel = new Button("C");
Button btnZhengFu = new Button("+/-");
Button btnAdd = new Button("+");
Button btnSub = new Button("-");
Button btnCheng = new Button("*");
Button btnChu = new Button("/");
Button btnEqual = new Button("=");

Panel p = new Panel(new GridLayout(4,4));
p.add(b7);
p.add(b8);
p.add(b9);
p.add(btnChu);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(btnCheng);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(btnSub);
p.add(b0);
p.add(btnZhengFu);
p.add(btnPoint);
p.add(btnAdd);

Panel p2 = new Panel(new GridLayout(2,1));
p2.add(btnCancel);
p2.add(btnEqual);

tf = new TextField();
tf.setEditable(false);

GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(tf, c);
f.add(tf);
c.gridx = 1;
c.gridy = 0;
c.gridheight = 2;
c.gridwidth = 2;
gridbag.setConstraints(p, c);
f.add(p);
c.gridx = 1;
c.gridy = 2;
c.gridheight = 2;
c.gridwidth = 1;
gridbag.setConstraints(p2, c);
f.add(p2);

f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

Moniter al = new Moniter(this);
b0.addActionListener(al);
b1.addActionListener(al);
b2.addActionListener(al);
b3.addActionListener(al);
b4.addActionListener(al);
b5.addActionListener(al);
b6.addActionListener(al);
b7.addActionListener(al);
b8.addActionListener(al);
b9.addActionListener(al);
btnZhengFu.addActionListener(al);
btnPoint.addActionListener(al);

MoniterOperate mo = new MoniterOperate();
btnAdd.addActionListener(mo);
btnSub.addActionListener(mo);
btnCheng.addActionListener(mo);
btnChu.addActionListener(mo);

btnCancel.addActionListener(new MoniterCancel());

btnEqual.addActionListener(new MoniterEqual());

}

class MoniterOperate implements ActionListener{


@Override
public void actionPerformed(ActionEvent e) {
i = Double.parseDouble(tf.getText());
Button b = (Button)e.getSource();
if(b.getLabel()=="+"){
op = 1;
}
if(b.getLabel()=="-"){
op = 2;
}
if(b.getLabel()=="*"){
op = 3;
}
if(b.getLabel()=="/"){
op = 4;
}
al_temp.add(i);
al_op.add(op);
tf.setText(null);
}

}

public void reset(){
tf.setText(null);
op = 0;
al_temp.clear();
al_op.clear();
}

class MoniterCancel implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
reset();
}
}

class MoniterEqual implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
i = Double.parseDouble(tf.getText());
al_temp.add(i);
result = al_temp.get(0);
for(int j = 1; j<al_temp.size(); j++){
op = al_op.get(j-1);
switch (op){
case 1:
result += al_temp.get(j);

break;
case 2:
result -= al_temp.get(j);break;
case 3:
result *= al_temp.get(j);break;
case 4:
result /= al_temp.get(j);break;
}
}

String str = String.valueOf(result);
tf.setText(str);
al_temp.clear();
al_op.clear();
}
}
}

相關文章