import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class AtmMachine extends JApplet implements ActionListener {
	
	//Set up JButtons
	JButton balance;
	JButton withdraw;
	JButton withdrawReceipt;
	JButton confirm;
	JButton deny;
	JButton clearAll;
	JButton clear;
	JButton [] numButtons;
	
	//Set up JPanels
	JPanel numPanel;
	JPanel buttonPanel;
	
	//Set up JTextAreas
	JTextArea screen;
	
	//Set up JLabels
	JLabel messageTop;
	JLabel messageMiddle;
	JLabel messageBottom;
	JLabel buttonPressed;
	
	public void init() {
		//Initialise JButtons
		numButtons = new JButton[10];
		for(int i=0; i<numButtons.length; i++) {
			numButtons[i] = new JButton("" + i);
			numButtons[i].addActionListener(this);
		}
		balance = new JButton("Balance");
		withdraw = new JButton("Withdraw");
		withdrawReceipt = new JButton();
		JLabel label1 = new JLabel("Withdraw");
		JLabel label2 = new JLabel("w/ Receipt");
		withdrawReceipt.setLayout(new BorderLayout());
		withdrawReceipt.add(BorderLayout.NORTH, label1);
		withdrawReceipt.add(BorderLayout.CENTER, label2);
		confirm = new JButton("Confirm");
		deny = new JButton("Deny");
		clearAll = new JButton("CA");
		clear = new JButton("C");
		numPanel = new JPanel();
		buttonPanel = new JPanel();
		
		//Initialise JTextAreas
		screen = new JTextArea();
		
		//Initialise JLabels
		messageTop = new JLabel("<html><center>The people who hate<br>People Bank</center></html>");
		messageMiddle = new JLabel("<html><center>Enter your pin number<br>Correctly.</center></html>");
		messageBottom = new JLabel("<html><center>Failure to do so will<br>result in your instant<br>termination.</center></html>");
		buttonPressed = new JLabel("");
		
		//Set panel layouts
		numPanel.setLayout(new FlowLayout());
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		
		//Add JButtons to JPanels
		for(int i=1; i<10; i++) {
			numPanel.add(numButtons[i]);
			numButtons[i].setPreferredSize(new Dimension(49,26));
			numButtons[i].addActionListener(this);
		}
		numPanel.add(clearAll);
		numPanel.add(numButtons[0]);
		numPanel.add(clear);
		buttonPanel.add(balance);
		buttonPanel.add(withdraw);
		buttonPanel.add(withdrawReceipt);
		buttonPanel.add(confirm);
		buttonPanel.add(deny);
		
		//Set up ActionListeners
		clear.addActionListener(this);
		clearAll.addActionListener(this);
		balance.addActionListener(this);
		withdraw.addActionListener(this);
		withdrawReceipt.addActionListener(this);
		confirm.addActionListener(this);
		deny.addActionListener(this);
		
		//Set up layouts
		GridLayout buttonLayout = new GridLayout(5, 1);
		buttonPanel.setLayout(buttonLayout);
		
		FlowLayout numLayout = new FlowLayout();
		numPanel.setLayout(numLayout);
		
		//Set sizes of buttons
		numButtons[0].setPreferredSize(new Dimension(48,26));
		clear.setPreferredSize(new Dimension(49,26));
		
		//Set coordinates
		buttonPanel.setBounds(0,0,100,300);
		screen.setBounds(100, 0, 230, 300);
		numPanel.setBounds(350, 130, 170, 170);
		messageTop.setBounds(143, 35, 150, 35);
		messageMiddle.setBounds(140, 100, 150, 35);
		messageBottom.setBounds(143, 165, 150, 50);
		buttonPressed.setBounds(143, 250, 150, 35);
		
		//Set colours
		buttonPanel.setBackground(Color.BLACK);
		screen.setBackground(Color.BLACK);
		numPanel.setBackground(Color.BLACK);
		messageTop.setForeground(Color.WHITE);
		messageMiddle.setForeground(Color.WHITE);
		messageBottom.setForeground(Color.WHITE);
		buttonPressed.setForeground(Color.WHITE);
		
		//Set editable
		screen.setEditable(false);
		
		//Add everything
		add(messageTop);
		add(messageMiddle);
		add(messageBottom);
		add(buttonPressed);
		add(buttonPanel);
		add(numPanel);
		add(screen);
	}
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.WHITE);
		g.fillRect(360, 40, 150, 10);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == numButtons[0]) {
			buttonPressed.setText("<html><center>You pressed 0</center></html>");
		}
		if(e.getSource() == numButtons[1]) {
			buttonPressed.setText("<html><center>You pressed 1</center></html>");
		}
		if(e.getSource() == numButtons[2]) {
			buttonPressed.setText("<html><center>You pressed 2</center></html>");
		}
		if(e.getSource() == numButtons[3]) {
			buttonPressed.setText("<html><center>You pressed 3</center></html>");
		}
		if(e.getSource() == numButtons[4]) {
			buttonPressed.setText("<html><center>You pressed 4</center></html>");
		}
		if(e.getSource() == numButtons[5]) {
			buttonPressed.setText("<html><center>You pressed 5</center></html>");
		}
		if(e.getSource() == numButtons[6]) {
			buttonPressed.setText("<html><center>You pressed 6</center></html>");
		}
		if(e.getSource() == numButtons[7]) {
			buttonPressed.setText("<html><center>You pressed 7</center></html>");
		}
		if(e.getSource() == numButtons[8]) {
			buttonPressed.setText("<html><center>You pressed 8</center></html>");
		}
		if(e.getSource() == numButtons[9]) {
			buttonPressed.setText("<html><center>You pressed 9</center></html>");
		}
		if(e.getSource() == clear) {
			buttonPressed.setText("<html><center>You pressed Clear</center></html>");
		}
		if(e.getSource() == clearAll) {
			buttonPressed.setText("<html><center>You pressed Clear All</center></html>");
		}
		if(e.getSource() == balance) {
			buttonPressed.setText("<html><center>You pressed Balance</center></html>");
		}
		if(e.getSource() == withdraw) {
			buttonPressed.setText("<html><center>You pressed Withdraw</center></html>");
		}
		if(e.getSource() == withdrawReceipt) {
			buttonPressed.setText("<html><center>You pressed Withdraw w/ Receipt</center></html>");
		}
		if(e.getSource() == confirm) {
			buttonPressed.setText("<html><center>You pressed Confirm</center></html>");
		}
		if(e.getSource() == deny) {
			buttonPressed.setText("<html><center>You pressed Deny</center></html>");
		}
		
	}
}
