All pastes #254088 Raw Edit

Stuff

public text v1 · immutable
#254088 ·published 2006-11-21 13:42 UTC
rendered paste body
import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class Rekenmachine extends Applet {
	public static TextArea   displayRekenmachine;
	private       Controller controller;
	
	public void init() {
		setLayout(new BorderLayout());
		
		displayRekenmachine       = new TextArea("", 1, 2, TextArea.SCROLLBARS_NONE);
		controller                = new Controller();
		
		add(displayRekenmachine,    BorderLayout.CENTER);
		add(controller,             BorderLayout.SOUTH);
	}
}

class Controller extends Panel {
	private Button[] cijferRij;
	private Button[] functieRij;
	private String   functieSymbolen[] = { "+", "-",
			                               "/", "*",
			                               "%", "C",
			                               "="
			                           };
	
	public static StringBuffer displayBuffer = new StringBuffer();
	Functies functie;
	public Controller() {
		setLayout(new GridLayout(5, 4));
		cijferRij  = new Button[10];
		functieRij = new Button[6];
		functie    = new Functies();
		
		for(int i = 0; i < cijferRij.length; i++) {
			cijferRij[i] = new Button(""+i);
			cijferRij[i].addActionListener(new setDisplay());
			add(cijferRij[i]);
		}
		
		for(int i = 0; i < functieRij.length;i++) {
			functieRij[i] = new Button(functieSymbolen[i]);
			functieRij[i].addActionListener(new setFunctie());
			add(functieRij[i]);
		}
	}
	
	class setDisplay implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int getal = 0;
			for(int i=0;i<cijferRij.length;i++) {
				if ( e.getSource() == cijferRij[i]) {
					Rekenmachine.displayRekenmachine.setText(""+i);
					getal = Integer.parseInt(Rekenmachine.displayRekenmachine.getText());
				}
			} 
			
			displayBuffer.append(""+getal);
			String s = displayBuffer.toString();
			Rekenmachine.displayRekenmachine.setText(s);
		}
	}
	
	class setFunctie implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			for(int i=0;i<functieRij.length;i++) {
				if ( e.getSource() == functieRij[i]) {
					functie.keuze(i);
				}
			} 
		}
	}
}

class Functies {
	private int invoer01               = 0;
	private int invoer02               = 0;
	private int somVanInvoer           = 0;
	private int eersteFunctieKeuze     = 0;
	private int tweedeFunctieKeuze     = 0;
	private static int aantalAanroepen = 0;
	
	protected void keuze(int keuze) {
		// Invoer eerste operandi, gebeurd een keer na aanroep van eerste functie keuze!
		if (aantalAanroepen == 0) {
			invoer01 = Integer.parseInt(Rekenmachine.displayRekenmachine.getText());
			// Buffer legen, anders gaat het fout!
			Controller.displayBuffer.delete(0, Controller.displayBuffer.length());
			// Onthouden wat de eerste functie is gekozen door luser
			eersteFunctieKeuze = keuze;
		}
		
		// Nu is er dus al minimaal twee keer een functie gekozen
		if (aantalAanroepen > 0) {
			// Je moet nu weten wat de nieuwe gekozen functie is
			tweedeFunctieKeuze = keuze;
			// Invoer van tweede operandi, wordt telkens herhaalt na functie keuze
			invoer02 = Integer.parseInt(Rekenmachine.displayRekenmachine.getText());
			
			// Eerst moet je kijken wat de vorige functie was en deze berekenen,
			// vervolgens de output op het scherm zetten en invoer01 deze waarde geven.
			if ( eersteFunctieKeuze == 0) {
				somVanInvoer       = invoer01 + invoer02;
				invoer01           = somVanInvoer;
				setDisplay(somVanInvoer);
			}
			
			// Aftrekken is ook wel belangrijk
			if (eersteFunctieKeuze == 1) {
				somVanInvoer       = invoer01 - invoer02;
				invoer01           = somVanInvoer;
				setDisplay(somVanInvoer);
			}
			// Delen is lame!
			if (eersteFunctieKeuze == 2) {
				somVanInvoer       = invoer01 / invoer02;
				invoer01           = somVanInvoer;
				setDisplay(somVanInvoer);
			}
			// Vermenigvuldigen maakt veroorzaakt overpopulatie
			if (eersteFunctieKeuze == 3) {
				somVanInvoer       = invoer01 * invoer02;
				invoer01           = somVanInvoer;
				setDisplay(somVanInvoer);
			}
			if (eersteFunctieKeuze == 4) {
				somVanInvoer       = invoer01 % invoer02;
				invoer01           = somVanInvoer;
				setDisplay(somVanInvoer);
			}
			// Eerste functie keuze moet nu dus de tweede worden:
			eersteFunctieKeuze = tweedeFunctieKeuze;
		}
		aantalAanroepen++;
	}
	
	protected void optellen() {
	}
	
	protected void aftrekken() {
	}
	
	protected void delen() {
	}
	
	protected void vermenigvuldigen() {
	}
	
	protected void uitkomst() {
	}
	
	protected void setDisplay(int getal) {
		Controller.displayBuffer.delete(0, Controller.displayBuffer.length());
		Rekenmachine.displayRekenmachine.setText(""+getal);
	}
	
	protected void displaySchoonmaken() {
		invoer01 = invoer02 = somVanInvoer = 0;
		Controller.displayBuffer.delete(0, Controller.displayBuffer.length());
		Rekenmachine.displayRekenmachine.setText("0");
	}
}