import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.*;
import com.fazecast.jSerialComm.*;

//import com.fazecast.jSerialComm.*;

 class Gui  {
	public static void main(String[] args)  {    

Scherm scherm = new Scherm();   
scherm.login();
scherm.frameInstellingen();
scherm.paneel1();  
scherm.paneel2();
scherm.paneel3();
scherm.paneel4();
scherm.stortPaneel();
scherm.opneemPaneel();
scherm.toevoegenBijPaneel();



    }
}

class Scherm  extends JFrame implements ActionListener  {//KeyListener

private JFrame f1;
private JPanel p1, p2,p3,p4,pStort,pOpnemen;
private JButton btnNaarP2,btnNaarP1,btnNaarP3,btnLogin,btnSKM,showMoneyButton,btnNaarStortPaneel;
private JButton btnNaarOpnemen,stortButton,stortNaarP4,opneemButton,opnemenNaarP4;
private JPasswordField passwordField;
private JLabel label1,label2,labelWW,labelPas,stortLabel,opneemLabel;
private char getWW;
private int count=0;
private boolean isCorrect = false;
private String correctPass="0000";
private ImageIcon image;
private String empty="";
private Boolean blocked=false;
private double balance=100;
private JTextField stortTextField,opneemTextField;


Scherm() {
}

public void login(){
passwordField = new JPasswordField();
passwordField.setBounds(800,350,140, 30);
passwordField.setDocument(new FixedSizeNumberDocument(passwordField,4));// gaat naar class FixedSizeNumberDocument, zodat alleen integers ingevult kan worden.
passwordField.addActionListener(this);


labelWW = new JLabel();
labelWW.setText("ww: ");
labelWW.setBounds(690,340,100, 40);
labelWW.setFont(new Font("Serif",Font.BOLD,30));

 image= new ImageIcon(getClass().getResource("passcan.jpg"));
labelPas= new JLabel(image);
labelPas.setBounds(300,350,1200,530);


}

public void frameInstellingen(){
f1 = new JFrame("GUI");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setSize(1900,1000);
f1.setVisible(true);

}


public void paneel1(){ 
p1= new JPanel();
p1.setBackground(Color.CYAN);
f1.add(p1);
//button gemaakt met een coordinaat,grootte,style.
btnNaarP2 = new JButton("Verder");
btnNaarP2.setBounds(800,350,140, 40);   
btnNaarP2.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP2.addActionListener(this);


//label gemaakt met een coordinaat,grootte,style.
label1=new JLabel();     
label1.setText("Welkom bij de bank");
label1.setBounds(700, 300, 500, 50);
label1.setFont(new Font("Serif",Font.ITALIC,40));

}


public void paneel2(){
    p2 = new JPanel();
p2.setBackground(Color.CYAN);
f1.add(p2);

//button gemaakt met een coordinaat,grootte,style.
btnNaarP1= new JButton("HOME");   
btnNaarP1.setBounds(100,900,140,40);   
btnNaarP1.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP1.addActionListener(this);


//button gemaakt met een coordinaat,grootte,style.
btnNaarP3= new JButton("Volgende");   
btnNaarP3.setBounds(800,900,180,40);   
btnNaarP3.setFont(new Font("Calibri",Font.PLAIN,30));
btnNaarP3.addActionListener(this);

label2=new JLabel("Scan uw pas");     
label2.setBounds(800, 300, 500, 50);
label2.setFont(new Font("Serif",Font.ITALIC,40));

}


public void paneel3(){  //Paneel 3

    p3 = new JPanel();
p3.setBackground(Color.CYAN);

btnLogin= new JButton("Login");
btnLogin.setBounds(1000,340,140, 40);
btnLogin.setFont(new Font("Serif",Font.BOLD,30));
btnLogin.addActionListener(this);
}



public void paneel4(){
p4= new JPanel();
p4.setBackground(Color.CYAN);

btnSKM= new JButton("Snel 50 euro opnemen");
btnSKM.setBounds(100,840,340, 40);
btnSKM.setFont(new Font("Serif",Font.BOLD,30));
btnSKM.addActionListener(this);

showMoneyButton = new JButton("Check Balance");
showMoneyButton.setBounds(100,740,340, 40);
showMoneyButton.setFont(new Font("Serif",Font.BOLD,30));
showMoneyButton.addActionListener(this);

btnNaarStortPaneel= new JButton("geld storten");
btnNaarStortPaneel.setBounds(1300,840,340, 40);
btnNaarStortPaneel.setFont(new Font("Serif",Font.BOLD,30));
btnNaarStortPaneel.addActionListener(this);

btnNaarOpnemen= new JButton("geld opnemen");
btnNaarOpnemen.setBounds(1300,740,340, 40);
btnNaarOpnemen.setFont(new Font("Serif",Font.BOLD,30));
btnNaarOpnemen.addActionListener(this);
}

public void stortPaneel(){

pStort = new JPanel();
pStort.setBackground(Color.CYAN);

stortLabel = new JLabel(" Hoeveel geld wil je storten?");
stortLabel.setBounds(800, 300, 500, 50);
stortLabel.setFont(new Font("Serif",Font.ITALIC,40));

stortTextField= new JTextField();
stortTextField.setBounds(800,400,500,40);
stortTextField.addActionListener(this);
stortTextField.setDocument(new FixedSizeNumberDocument(stortTextField,5));// gaat naar class FixedSizeNumberDocument, zodat alleen integers ingevult kan worden.

stortButton = new JButton("OK");
stortButton.setBounds(800,450,340, 40);
stortButton.setFont(new Font("Serif",Font.BOLD,30));
stortButton.addActionListener(this);

stortNaarP4 = new JButton("terug");
stortNaarP4.setBounds(700,850,340, 40);
stortNaarP4.setFont(new Font("Serif",Font.BOLD,30));
stortNaarP4.addActionListener(this);

}

public void opneemPaneel(){
pOpnemen = new JPanel();
pOpnemen.setBackground(Color.CYAN);

opneemLabel = new JLabel(" Hoeveel geld wil je opnemen?");
opneemLabel.setBounds(800, 300, 500, 50);
opneemLabel.setFont(new Font("Serif",Font.ITALIC,40));

opneemTextField= new JTextField();
opneemTextField.setBounds(800,400,500,40);
opneemTextField.addActionListener(this);
opneemTextField.setDocument(new FixedSizeNumberDocument(opneemTextField,5));// gaat naar class FixedSizeNumberDocument, zodat alleen integers ingevult kan worden.

opneemButton = new JButton("OK");
opneemButton.setBounds(800,450,340, 40);
opneemButton.setFont(new Font("Serif",Font.BOLD,30));
opneemButton.addActionListener(this);

opnemenNaarP4 = new JButton("terug");
opnemenNaarP4.setBounds(700,850,340, 40);
opnemenNaarP4.setFont(new Font("Serif",Font.BOLD,30));
opnemenNaarP4.addActionListener(this);

}


public void toevoegenBijPaneel(){ //alles wordt hier bij de panelen toegevoegd.
    
p1.setLayout(null);
p2.setLayout(null);
p3.setLayout(null);
p4.setLayout(null);
pStort.setLayout(null);
pOpnemen.setLayout(null);
p1.add(label1);
p1.add(btnNaarP2);
p2.add(btnNaarP3);
p2.add(label2);
p2.add(labelPas);
p3.add(passwordField); 
p3.add(labelWW);
p3.add(btnLogin);
p4.add(btnSKM);
p4.add(showMoneyButton);
p4.add(btnNaarOpnemen);
p4.add(btnNaarStortPaneel);
pStort.add(stortLabel);
pStort.add(stortTextField);
pStort.add(stortButton);
pStort.add(stortNaarP4);
pOpnemen.add(opneemLabel);
pOpnemen.add(opneemButton);
pOpnemen.add(opneemTextField);
pOpnemen.add(opnemenNaarP4);
}


    public void actionPerformed(ActionEvent ae) {// In deze method zorgt je ervoor dat de knoppen iets doen als je erop drukt.
        String action = ae.getActionCommand();



String stortValue1=stortTextField.getText();// lees jouw ingevulde waarde van jouw textfield.
char []passwordLezer=passwordField.getPassword();
String getWW= new String(passwordLezer);// van char array naar string
String opneemValue1=opneemTextField.getText();



if (ae.getSource()== btnNaarP2) {  

p1.setVisible(false);
p3.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p2.add(btnNaarP1);
p2.setVisible(true);
}


if (ae.getSource()== btnNaarP1) {  

p2.setVisible(false);
p3.setVisible(false);
p4.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p1.setVisible(true);
}

if (ae.getSource()== btnNaarP3) {  

p1.setVisible(false);
p2.setVisible(false);
f1.validate();
f1.repaint();
f1.invalidate();
p3.add(btnNaarP1);
f1.getContentPane().add(p3);
p3.setVisible(true);
}


 if(ae.getSource()==btnLogin) {  
passwordField.setText("");
   System.out.print("\n\njouw ingvoerde pincode: ");
     System.out.println(getWW);

        if(getWW.equals(correctPass) && blocked==true ){// dit is na het blokeren van de pas
                 JOptionPane.showMessageDialog(null, "Incorrect password");
            }

          if(getWW.equals(correctPass) && blocked==false){//als de WW goed is en de pas is niet geblokeerd.
        count=0;
        System.out.println("Incorrecte inlogpogingen: "+ count);
    JOptionPane.showMessageDialog(null, "Correct password");
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    p4.add(btnNaarP1);
    f1.getContentPane().add(p4);
    p4.setVisible(true);
                } 

                if(getWW.equals(empty)){
                     JOptionPane.showMessageDialog(null, "ww mag niet leeg zijn","ERROR",JOptionPane.ERROR_MESSAGE);
                }
                if(!getWW.equals(correctPass) && !getWW.equals(empty)) {
                    count+=1;
                    JOptionPane.showMessageDialog(null, "Incorrecte inlogpogingen: "+ count,"ERROR",JOptionPane.WARNING_MESSAGE);
                    System.out.println("Incorrecte inlogpogingen: "+ count);


              if(count==3){
                  JOptionPane.showMessageDialog(null, "PAS GEBLOKEERD");
                  blocked=true;
              }
          
           }
        
}


if(ae.getSource()==btnSKM){
     
     if(balance>=50){
     JOptionPane.showMessageDialog(null, "U krijgt nu 50 euro");
     balance-=50;
     }
     if(balance<50){
     JOptionPane.showMessageDialog(null, "U heeft geen geld meer om 50 euro op te nemen.");
     }
}

if(ae.getSource()==showMoneyButton){
     JOptionPane.showMessageDialog(null,"Jouw balance = " +balance);
}

if(ae.getSource()==btnNaarStortPaneel){
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    p4.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    f1.getContentPane().add(pStort);
    pStort.setVisible(true);
}

if(ae.getSource()==btnNaarOpnemen){
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    p4.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    f1.getContentPane().add(pOpnemen);
    pOpnemen.setVisible(true);
}

if(stortTextField==null){
    JOptionPane.showMessageDialog(null,"Vak is leeg");
}

if(ae.getSource()==stortButton){
      stortTextField.setText("");
Integer stortValue= Integer.valueOf(stortValue1);// zet String om in Integer.
JOptionPane.showMessageDialog(null,"U heeft " + stortValue +" euro gestort");
    balance+=stortValue;

JPanel panelbon= new JPanel();
panelbon.add(new JLabel("Wil je een bon?"));
Object[] options1 = { "JA", 
                "NEE" };

        int result = JOptionPane.showOptionDialog(null, panelbon, "Bon",
                JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
                null, options1, null);
        if (result == JOptionPane.YES_OPTION){
          System.out.println("Ja");
        }
         if (result == JOptionPane.NO_OPTION){
   System.out.println("Nee");
        }


}



if(ae.getSource()==stortNaarP4){
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    pStort.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    f1.getContentPane().add(p4);
    p4.setVisible(true);
}
if(ae.getSource()==opnemenNaarP4){
    p1.setVisible(false);
    p2.setVisible(false);
    p3.setVisible(false);
    pOpnemen.setVisible(false);
    f1.validate();
    f1.repaint();
    f1.invalidate();
    f1.getContentPane().add(p4);
    p4.setVisible(true);
}
if(ae.getSource()==opneemButton){
    opneemTextField.setText("");
    Integer opneemValue= Integer.valueOf(opneemValue1);// zet String om in Integer.

        if(balance>=opneemValue){
    balance-=opneemValue;
    JOptionPane.showMessageDialog(null,"U heeft " + opneemValue +" euro opgenomen");
        }else{
    JOptionPane.showMessageDialog(null,"U heeft niet genoeg geld om optenemen");
    }
}


    }





}
 class FixedSizeNumberDocument extends PlainDocument{// deze class zorgt ervoor dat je alleen Intergers kan invoeren

    private JTextComponent owner;
    private int fixedSize;

    public FixedSizeNumberDocument(JTextComponent owner, int fixedSize)
    {
        this.owner = owner;
        this.fixedSize = fixedSize;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException
    {
        if (getLength() + str.length() > fixedSize) {
            str = str.substring(0, fixedSize - getLength());
            this.owner.getToolkit().beep();
        }

        try {
            Integer.parseInt(str);
        } catch (NumberFormatException e) {
            // inserted text is not a number
            this.owner.getToolkit().beep();
            return;
        }

        super.insertString(offs, str, a);
    }               
}








