谁能帮我重构一下我这个计算器程序。

10-09-02 wudikua123
本人菜鸟,写了个计算器程序,但是觉得没什么面向对象思想,也学了工厂,策略,装饰,代理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();
}
}
}

wudikua123
2010-09-02 13:07

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();
} 
}
}

<p class="indent">

banq
2010-09-03 09:42
一个类的方法超过10行就要重写,你自己看着办,怎么进行减肥,减肥只能自己做,看电视上别人减肥成功,轮到你不一定,所以,减肥这事别人帮不了你。