誰能幫我重構一下我這個計算器程式。
本人菜鳥,寫了個計算器程式,但是覺得沒什麼物件導向思想,也學了工廠,策略,裝飾,代理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();
}
}
}
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();
}
}
}
相關文章
- 誰能幫我做一下這個簡單的專案啊!我是初學者。
- 誰能幫我看下哪裡有問題?
- 誰能幫我讓liferay執行正常起來?
- 你能幫我把這個檔案重新命名嗎?
- 曾經有個能幫我修電腦的程式設計師,我沒有好好珍惜……程式設計師
- 這個顯示器總算能完整顯示我的Java類了Java
- 這個程式就是執行不料。真著急。。大家幫幫我。。
- 誰說我們程式設計師悶騷,我跟誰急程式設計師
- LINUX下TOMCAT連線池問題???誰能幫我?LinuxTomcat
- 我這個程式設計師 (轉)程式設計師
- 誰再黑程式設計師我就打誰程式設計師
- 誰來關心我們這些程式設計師 (轉)程式設計師
- 請高手幫我看一下這是怎麼回事
- 老鳥能邦我看一下我的程式碼,很小不大
- 請大家幫我分析一下這個畢業設計題目是不是有問題?
- JS求助!!哪位大佬幫幫我,解答一下。JS
- 各位大俠幫我看一下這個小程式怎麼破(內有下載地址) (173字)
- 誰來幫幫我?IP電話BuddyPhone 的破解. (417字)
- 問一個基礎問題,幫我理解一下概念
- 【附原始碼】為了幫助程式設計師談戀愛,我做了這個APP原始碼程式設計師APP
- JBuiler 建立資料庫查詢工程過程中問題,有誰能幫我?UI資料庫
- 誰能幫我破解AutoGraphicsHTML5.5,請看詳細內容>> (1千字)HTML
- 我希望我56歲的時候還能程式設計程式設計
- (斑竹也看一下好嗎?)高手清幫幫我!!!!!
- 誰動了我的程式碼!?
- model 與modelform屬性對應問題,誰幫幫我啊ORM
- 這次我們聊一下CookieCookie
- java從程式設計師,到架構需要什麼?(banq幫我看看)Java程式設計師架構
- HashMap?面試?我是誰?我在哪?HashMap面試
- 用過XMLHTTP的朋友幫幫忙,我這個點選後沒反應XMLHTTP
- 用程式碼構建機器心智,我們離這個目標還有多遠?
- 程式設計師:活到 35 歲,我對不起誰?程式設計師
- 我做這個程式設計師還有意思嗎?程式設計師
- 我用VJ基本完成了一個四國象棋網路程式,因時間關係,不能繼續,誰幫我完成? (轉)
- 我的程式設計之路:勤能補拙程式設計
- 請問誰能告訴我現在用哪個版本的Eclipse好?Eclipse
- 哪位高手幫幫我
- 我為誰工作