הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Calculator UI

package model;
 
public class Calculator {
	private float currentValue;
	private float tempValueHolder;
	private Operators currOperation = Operators.None;
	private boolean needsReset = false;
 
	public float getValue() {
		return this.currentValue;
	}
 
	public float getTempValue() {
		return this.tempValueHolder;
	}
 
	public void zero() {
		this.currentValue = 0;
		this.tempValueHolder = 0;
	}
 
	public void userClickedNumber(float num) {
		if ( needsReset ) {
			needsReset = false;
			tempValueHolder = num;
		} else {
			tempValueHolder  *= 10;
			tempValueHolder  += num;			
		}
	}
 
	public void userClickedOperator(Operators op) {
		userWantsResults();
 
		this.currOperation  = op;
		this.tempValueHolder = 0;
		needsReset = true;
	}
 
	public void userWantsResults() {
		if (this.currOperation == Operators.Plus ) {
			this.currentValue += tempValueHolder;
		} else if ( this.currOperation == Operators.None ) {
			this.currentValue = this.tempValueHolder;
		} else if ( this.currOperation == Operators.Divide ) {
			this.currentValue /= tempValueHolder;
		} else if ( this.currOperation == Operators.Multiply ) {
			this.currentValue *= tempValueHolder;
		} else if ( this.currOperation == Operators.Minus ) {
			this.currentValue -= tempValueHolder;
		} else if ( this.currOperation == Operators.Clear ) {
			this.currentValue = 0;
		}
 
		this.tempValueHolder = this.currentValue;
		this.currOperation = Operators.None;
		needsReset = true;
	}
 
	public boolean needsReset() {
		return needsReset;
	}
}
package model;
 
public enum Operators {
	Plus,
	Minus,
	Multiply,
	Divide,
	Clear,
	None
}
 
package controller;
 
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
import javax.swing.JTextField;
 
import model.Calculator;
import model.Operators;
 
public class CleanAction extends AbstractAction {
	private JTextField display;
	private model.Calculator model;
 
	public CleanAction(String text, Calculator model, JTextField display) {
		super(text);
		this.display = display;
		this.model = model;
	}
 
 
	public void actionPerformed(ActionEvent e) {
		this.model.zero();
		this.display.setText("0");
	}
 
}
package controller;
 
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
import javax.swing.JTextField;
 
import model.Calculator;
 
public class NumberClickedAction extends AbstractAction {
	private Calculator calc;
	private JTextField display;
	private int num;
 
	public NumberClickedAction(JTextField display, Calculator calc, int num, String text) {
		super(text);
		this.display = display;
		this.calc    = calc;
		this.num     = num;
	}
 
	public void actionPerformed(ActionEvent arg0) {		
		if ( this.calc.needsReset() ) {
			this.calc.userClickedNumber(this.num);
			this.display.setText(String.valueOf(this.num));
		} else {			
			this.calc.userClickedNumber(this.num);
			this.display.setText(String.valueOf(this.calc.getTempValue()));			
		}
	}
}
 
package controller;
 
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
 
import model.Operators;
 
public class OperationClickedAction extends AbstractAction {
	private model.Calculator model;
	private model.Operators op;
 
	public OperationClickedAction(String text, model.Calculator model, model.Operators op) {
		super(text);
		this.model = model;
		this.op    = op;
	}
 
	public void actionPerformed(ActionEvent e) {
		this.model.userClickedOperator(this.op);
	}
 
}
 


package controller;
 
import java.awt.event.ActionEvent;
 
import javax.swing.AbstractAction;
import javax.swing.JTextField;
 
import model.Calculator;
 
public class EqualsAction extends AbstractAction {
	private Calculator model;
	private JTextField display;
 
	public EqualsAction(String text, Calculator model, JTextField display) {
		super(text);
		this.model = model;
		this.display = display;
	}
 
	public void actionPerformed(ActionEvent e) {
		this.model.userWantsResults();
		this.display.setText(String.valueOf(this.model.getValue()));
	}
 
}
package view;
 
import java.awt.BorderLayout;
import java.awt.GridLayout;
 
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
import model.Operators;
 
import controller.CleanAction;
import controller.EqualsAction;
import controller.NumberClickedAction;
import controller.OperationClickedAction;
 
public class Calculator {
	private JFrame frame;
	private JPanel buttons;
	private JTextField display;
	private model.Calculator model;
	private Action [] operations;
 
	public static final int ROWS = 4;
	public static final int COLS = 4;
	public static final String [] titles = {
		"0", "1", "2", "3", 
		"4", "5", "6", "7", 
		"8", "9", "=", "/", 
		"*", "-", "+", "C"};
 
	public Calculator(model.Calculator model) {
		frame = new JFrame("Best Calculator-In-The-World");
		frame.setSize(350, 200);
		this.model = model;
 
		display = new JTextField("0");
		display.setEditable(false);
		buttons = new JPanel();
		initOperations();
		initButtonsPanel(buttons);	
 
		frame.add(display, BorderLayout.NORTH);
		frame.add(buttons, BorderLayout.CENTER);
 
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 
	private void initOperations() {
		this.operations = new Action[6];
 
		this.operations[0] = new EqualsAction(Calculator.titles[10], this.model, this.display);
		this.operations[1] = new OperationClickedAction(Calculator.titles[11], this.model, Operators.Divide);
		this.operations[2] = new OperationClickedAction(Calculator.titles[12], this.model, Operators.Multiply);
		this.operations[3] = new OperationClickedAction(Calculator.titles[13], this.model, Operators.Minus);
		this.operations[4] = new OperationClickedAction(Calculator.titles[14], this.model, Operators.Plus);
		this.operations[5] = new CleanAction(Calculator.titles[15], this.model, this.display);
	}
 
	private void initButtonsPanel(JPanel buttons) {
		GridLayout layout = new GridLayout(Calculator.ROWS, Calculator.COLS);
		buttons.setLayout(layout);
 
		for ( int i=0; i < Calculator.ROWS * Calculator.COLS; ++i ) {
			String buttonText = Calculator.titles[i];
			JButton b = null;
 
			if ( i < 10 ) {
				b = new JButton(
						new NumberClickedAction(this.display, this.model, i, buttonText));
			} else {
				b = new JButton(this.operations[i - 10]);				
			}
			buttons.add(b);				
		}
	}
 
	public static void main(String [] args) {
		model.Calculator model = new model.Calculator();
		Calculator calc = new Calculator(model);
	}
}
course: