誰能幫我重構一下我這個計算器程式。
本人菜鳥,寫了個計算器程式,但是覺得沒什麼物件導向思想,也學了工廠,策略,裝飾,代理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();
}
}
}
相關文章
- 誰能幫我看下哪裡有問題?
- 曾經有個能幫我修電腦的程式設計師,我沒有好好珍惜……程式設計師
- 這個顯示器總算能完整顯示我的Java類了Java
- 大神們幫我一下!!
- 誰再黑程式設計師我就打誰程式設計師
- JS求助!!哪位大佬幫幫我,解答一下。JS
- zcmu-1184 幫我求算一下斐波那契數吧
- 花3000養個客服,每個月能幫我賺3萬
- 誰動了我的程式碼!?
- 程式設計師:活到 35 歲,我對不起誰?程式設計師
- 用程式碼構建機器心智,我們離這個目標還有多遠?
- ? 那些能幫我愉快Working的Mac軟體Mac
- 這次我們聊一下CookieCookie
- 我的程式設計之路:勤能補拙程式設計
- 我為我是個程式設計師而驕傲程式設計師
- 我的第一個python web開發框架(33)——介面程式碼重構PythonWeb框架
- 我與計算機計算機
- 動手做個 AI 機器人,幫我回訊息!AI機器人
- 簡單的3步,重構我們的程式碼
- 我的Android重構之旅:架構篇Android架構
- 我是程式設計師,我用這種方式銘記歷史程式設計師
- HashMap?面試?我是誰?我在哪?HashMap面試
- 我來試試這個部落格是否能發帖
- 如果能重來,我選擇這樣學習資料科學……資料科學
- 我以為我學懂了資料結構,直到看了這個導圖才發現,我錯了資料結構
- chatGPT幫我最佳化程式碼-2024.06.20ChatGPT
- 我招了個“水貨”程式設計師程式設計師
- 我真是服了!AS Debug還能這麼玩?
- 這 個 鍋 我 不 背
- 劍橋計算機博士推薦,畢業之前,我需要掌握這9個工具計算機
- 如何才能把做好的網站共享到區域網上讓其他訪問(MAC + Homestead),誰能幫我?網站Mac
- 別再問我 WiFi 密碼了:這兩個 GitHub 幫你解決WiFi密碼Github
- 開發好能重構的程式碼,都是這麼幹的
- 我寫了個BoardView,看一下吧。View
- 程式設計師:我想換工作,讓我琢磨琢磨這幾個值得深入思考的面試問答程式設計師面試
- 一個 static 還能難得住我?
- 我不是一個成功的人,但是我想做一個優秀的程式設計師程式設計師
- 我來告訴你程式碼重構有什麼好處
- win10計算器應用商店我們這裡出錯怎麼解決Win10