Created
November 5, 2018 04:13
-
-
Save hermesespinola/9259300fd7782ac4ab8f46c84344470a to your computer and use it in GitHub Desktop.
Just homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lpalomin; | |
import java.awt.Color; | |
import java.awt.EventQueue; | |
import java.awt.Font; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JFrame; | |
import javax.swing.AbstractButton; | |
import javax.swing.JButton; | |
import javax.swing.JTextArea; | |
// Context class | |
public class CalculadoraGUI extends JFrame { | |
public interface IState { | |
public IState transition(String sourceButton, CalculadoraGUI ctx); | |
} | |
public class SecondOpState implements IState { | |
public IState transition(String sourceButton, CalculadoraGUI ctx) { | |
if (Character.isDigit(sourceButton.charAt(0))) { | |
ctx.operand2 = ctx.operand2 + sourceButton.charAt(0); | |
ctx.setDisplay(ctx.operand1 + ctx.operator + ctx.operand2); | |
ctx.operand2 = "0"; | |
return this; | |
} else if (sourceButton.equals("=")) { | |
ctx.setDisplay(ctx.operand1 + ctx.operator + ctx.operand2 + "=" + ctx.doOperation()); | |
ctx.resetCalculator(); | |
return ctx.initState; | |
} else if (sourceButton.equals("AC")) { | |
ctx.setDisplay(""); | |
ctx.resetCalculator(); | |
return ctx.initState; | |
} | |
return this; | |
} | |
} | |
public class InitState implements IState { | |
public IState transition(String sourceButton, CalculadoraGUI ctx) { | |
if (Character.isDigit(sourceButton.charAt(0))) { | |
ctx.operand1 = ctx.operand1 + sourceButton.charAt(0); | |
ctx.setDisplay(ctx.operand1); | |
return ctx.firstState; | |
} else { | |
ctx.setDisplay(""); | |
ctx.resetCalculator(); | |
return this; | |
} | |
} | |
} | |
public class FirstOpState implements IState { | |
public IState transition(String sourceButton, CalculadoraGUI ctx) { | |
if (Character.isDigit(sourceButton.charAt(0))) { | |
ctx.operand1 = ctx.operand1 + sourceButton.charAt(0); | |
ctx.setDisplay(ctx.operand1); | |
return this; | |
} else if (sourceButton.equals("AC")) { | |
ctx.setDisplay(""); | |
ctx.resetCalculator(); | |
return ctx.initState; | |
} else if (sourceButton.equals("=")) { | |
return this; | |
} else { | |
ctx.operator = sourceButton; | |
ctx.setDisplay(ctx.operand1 + ctx.operator); | |
return ctx.secondState; | |
} | |
} | |
} | |
private JButton[] btnNumbers, btnOperands; | |
private JButton erase, solve; | |
private JTextArea txtDisplay; | |
// Model | |
String operand1, operand2, operator; | |
IState currentState; | |
InitState initState; | |
FirstOpState firstState; | |
SecondOpState secondState; | |
public CalculadoraGUI() { | |
super(); | |
this.resetCalculator(); | |
this.initState = new InitState(); | |
this.firstState = new FirstOpState(); | |
this.secondState = new SecondOpState(); | |
this.currentState = this.initState; | |
Controller controller = new Controller(this); | |
this.setTitle("Simple Binary Calculator"); | |
this.resetCalculator(); | |
this.setBounds(100, 100, 500, 457); | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
this.getContentPane().setLayout(null); | |
this.getContentPane().setBackground(Color.decode("#5e5d5d")); | |
// Number Buttons | |
this.btnNumbers = new JButton[11]; | |
int cont = 0; | |
for(int i = 0; i < 4; i++){ | |
for(int j = 0; j < 3; j++){ | |
if(cont >= 10){ | |
break; | |
} | |
this.btnNumbers[cont] = new JButton((cont == 9)? "0":cont+1+""); | |
this.btnNumbers[cont].setBounds(40 +j*62, 130+i*62, 60, 60); | |
this.btnNumbers[cont].setBackground(Color.decode("#424141")); | |
this.btnNumbers[cont].setForeground(Color.white); | |
this.btnNumbers[cont].setFocusPainted(false); | |
this.btnNumbers[cont].addActionListener(controller); | |
this.getContentPane().add(this.btnNumbers[cont++]); | |
} | |
} | |
// Operator Buttons | |
this.btnOperands = new JButton[5]; | |
this.btnOperands[0] = new JButton("*"); | |
this.btnOperands[1] = new JButton("/"); | |
this.btnOperands[2] = new JButton("+"); | |
this.btnOperands[3] = new JButton("-"); | |
this.btnOperands[4] = new JButton("^"); | |
cont = 0; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 2; j++){ | |
if(cont >= this.btnOperands.length){ | |
break; | |
} | |
this.btnOperands[cont].setBounds(300 +j*62, 192+i*62, 60, 60); | |
this.btnOperands[cont].setBackground(Color.decode("#424141")); | |
this.btnOperands[cont].setForeground(Color.white); | |
this.btnOperands[cont].setFocusPainted(false); | |
this.btnOperands[cont].addActionListener(controller); | |
this.getContentPane().add(this.btnOperands[cont++]); | |
} | |
} | |
// AC and Equal Buttons | |
this.erase = new JButton("AC"); | |
this.erase.setBounds(362, 130, 60, 60); | |
this.erase.setBackground(Color.decode("#424141")); | |
this.erase.setForeground(Color.white); | |
this.erase.setFocusPainted(false); | |
this.erase.addActionListener(controller); | |
this.getContentPane().add(this.erase); | |
this.solve = new JButton("="); | |
this.solve.setBounds(362, 316, 60, 60); | |
this.solve.setBackground(Color.decode("#424141")); | |
this.solve.setForeground(Color.white); | |
this.solve.setFocusPainted(false); | |
this.solve.addActionListener(controller); | |
this.getContentPane().add(this.solve); | |
// TextField Display | |
this.txtDisplay = new JTextArea(); | |
this.txtDisplay.setBounds(43, 25, 400, 85); | |
this.txtDisplay.setBackground(Color.decode("#ffffff")); | |
this.txtDisplay.setFont(new Font("normal", Font.BOLD, 28)); | |
getContentPane().add(this.txtDisplay); | |
this.txtDisplay.setEditable(false); | |
this.setVisible(true); | |
} | |
public void setDisplay(String text) | |
{ | |
this.txtDisplay.setText(text); | |
} | |
protected void resetCalculator() | |
{ | |
this.operand1 = ""; | |
this.operand2 = ""; | |
} | |
protected String doOperation() | |
{ | |
int oper1 = Integer.valueOf(this.operand1); | |
int oper2 = Integer.valueOf(this.operand2); | |
switch(this.operator) { | |
case "+": | |
return Integer.toString(oper1 + oper2); | |
case "-": | |
return Integer.toString(oper1 - oper2); | |
case "*": | |
return Integer.toString(oper1 * oper2); | |
case "/": | |
if(oper2 == 0) { | |
return "INFINITY"; | |
} else { | |
return Integer.toString(oper1 / oper2); | |
} | |
case "^": | |
return Integer.toString(oper1 ^ oper2); | |
} | |
return "???"; | |
} | |
static class Controller implements ActionListener { | |
CalculadoraGUI view; | |
public Controller(CalculadoraGUI view) { | |
this.view = view; | |
} | |
public void actionPerformed(ActionEvent e) | |
{ | |
String sourceButton = ((JButton) e.getSource()).getText(); | |
this.view.currentState = this.view.currentState.transition(sourceButton, this.view); | |
} | |
} | |
public static void main(String[] args) { | |
new CalculadoraGUI(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment