Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package Lab46;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import javax.swing.JFrame;
- import javax.swing.JButton;
- import javax.swing.JTextField;
- import javax.swing.JPasswordField;
- import javax.swing.JOptionPane;
- import javax.swing.JLabel;
- public class GUI extends JFrame {
- // GUI - 2,5h x2 (2personer)
- // FIX DB CALLS - 2h x2 (2personer)
- Font font = new Font("Monotype Corsiva", Font.PLAIN, 27);
- DBconnector db = new DBconnector();
- private EventHandler eventHandler;
- // Login-Page
- private JLabel labelLogin;
- private JLabel labelPassword;
- private JTextField fieldLogin;
- private JPasswordField fieldPassword;
- private JButton confirmLogin;
- // Account-Page
- private JLabel textBalance;
- private JLabel textWithdraw;
- private JLabel textDeposit;
- private JTextField fieldWithdraw;
- private JTextField fieldDeposit;
- private JButton withdrawButton;
- private JButton depositButton;
- private JButton logOut;
- private JButton register;
- private JButton back;
- private JButton newuser;
- private String userID;
- private JLabel newUserID;
- private JLabel newUserPass;
- private JTextField newUserText;
- private JTextField newPassText;
- public GUI() {
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- db.closeConnection();
- System.out.println("connection closed");
- System.exit(0);
- }
- });
- setTitle("ATM MACHINE");
- setLayout(new FlowLayout());
- eventHandler = new EventHandler();
- labelLogin = new JLabel("Login: ", 10);
- add(labelLogin);
- fieldLogin = new JTextField(10);
- fieldLogin.setToolTipText("Enter your personal 10digit id here.");
- add(fieldLogin);
- labelPassword = new JLabel("Password: ", 10);
- add(labelPassword);
- fieldPassword = new JPasswordField(10);
- fieldLogin.setToolTipText("Enter your password here.");
- add(fieldPassword);
- confirmLogin = new JButton("Confirm Login");
- fieldLogin.setToolTipText("Click to login.");
- add(confirmLogin);
- confirmLogin.addActionListener(eventHandler);
- register = new JButton("Register new user");
- fieldLogin.setToolTipText("Click to register a new account.");
- add(register);
- register.addActionListener(eventHandler);
- createRegisterPage();
- }
- private void showLoginPage() {
- fieldLogin.setVisible(true);
- fieldPassword.setVisible(true);
- labelLogin.setVisible(true);
- labelPassword.setVisible(true);
- confirmLogin.setVisible(true);
- register.setVisible(true);
- fieldLogin.setText("");
- fieldPassword.setText("");
- }
- private void showRegisterPage() {
- newPassText.setVisible(true);
- newUserID.setVisible(true);
- newUserPass.setVisible(true);
- newUserText.setVisible(true);
- back.setVisible(true);
- newuser.setVisible(true);
- }
- private void hideRegisterPage() {
- newPassText.setVisible(false);
- newUserID.setVisible(false);
- newUserPass.setVisible(false);
- newUserText.setVisible(false);
- back.setVisible(false);
- newuser.setVisible(false);
- }
- private void hideLoginPage() {
- fieldLogin.setVisible(false);
- fieldPassword.setVisible(false);
- labelLogin.setVisible(false);
- labelPassword.setVisible(false);
- confirmLogin.setVisible(false);
- register.setVisible(false);
- }
- private void createAccountPage() {
- textBalance = new JLabel("-1");
- add(textBalance);
- textBalance.setVisible(false);
- logOut = new JButton("Log out from your account");
- add(logOut);
- logOut.setVisible(false);
- depositButton = new JButton("Deposit");
- add(depositButton);
- depositButton.setVisible(false);
- withdrawButton = new JButton("Withdraw");
- add(withdrawButton);
- withdrawButton.setVisible(false);
- logOut.addActionListener(eventHandler);
- depositButton.addActionListener(eventHandler);
- withdrawButton.addActionListener(eventHandler);
- }
- private void createRegisterPage() {
- newUserID = new JLabel("your username:");
- add(newUserID);
- newUserID.setVisible(false);
- newUserText = new JTextField(10);
- newUserText.setToolTipText("Enter username");
- add(newUserText);
- newUserText.setVisible(false);
- newUserPass = new JLabel("your password:");
- add(newUserPass);
- newUserPass.setVisible(false);
- newPassText = new JTextField(10);
- newPassText.setToolTipText("Enter password");
- add(newPassText);
- newPassText.setVisible(false);
- newuser = new JButton("create account");
- add(newuser);
- newuser.setVisible(false);
- back = new JButton("back to login-screen");
- add(back);
- back.setVisible(false);
- newuser.addActionListener(eventHandler);
- back.addActionListener(eventHandler);
- }
- private void showAccountPage() {
- try {
- String balance = Integer.toString(db.getBalance(userID));
- textBalance.setFont(font);
- textBalance.setText("your current amount is " + balance + "kr");
- textBalance.setVisible(true);
- logOut.setVisible(true);
- depositButton.setVisible(true);
- withdrawButton.setVisible(true);
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- }
- private void hideAccountPage() {
- textBalance.setVisible(false);
- logOut.setVisible(false);
- depositButton.setVisible(false);
- withdrawButton.setVisible(false);
- }
- private class EventHandler implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- if (event.getSource() == confirmLogin) {
- userID = (fieldLogin.getText());
- String password = convertPassword(fieldPassword.getPassword());
- if (db.checkLogin(userID, password)) {
- JOptionPane.showMessageDialog(null, "Correct");
- hideLoginPage();
- createAccountPage();
- showAccountPage();
- } else {
- try {
- JOptionPane.showMessageDialog(null, "Incorrect");
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- }
- } else if (event.getSource() == logOut) {
- try {
- hideAccountPage();
- showLoginPage();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- } else if (event.getSource() == depositButton) {
- try {
- makeDeposit();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- } else if (event.getSource() == withdrawButton) {
- try {
- makeWithdraw();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- } else if (event.getSource() == register) {
- try {
- hideLoginPage();
- showRegisterPage();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- } else if (event.getSource() == back) {
- try {
- hideRegisterPage();
- showLoginPage();
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- } else if (event.getSource() == newuser) {
- try {
- String newID = (newUserID.getText());
- String newPass = (newUserPass.getText());
- int balance = 200;
- System.out.println("lol");
- db.registerUser(newID, newPass, balance);
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- showAccountPage();
- }
- }
- }
- private void makeDeposit() {
- try {
- String getInput = JOptionPane.showInputDialog("Enter Amount you want to deposit");
- float temp = Float.parseFloat(getInput); //lägger in värdet i en variabel
- if (temp > 0) // kollar så att värdet är positivt, man vet aldrig, allit förutse att kunderna är idioter
- {
- float amount = db.getBalance(userID) + temp;
- db.updateBalance(userID, amount);
- String balance = Integer.toString(db.getBalance(userID));
- textBalance.setText(balance);
- JOptionPane.showMessageDialog(null, "deposit complete, you have " + balance + " kr on your account");
- } else {
- JOptionPane.showMessageDialog(null, "deposit didn't work"); //flyttar ner den här raden i en else
- }
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- makeDeposit();
- }
- }
- private void makeWithdraw() {
- try {
- String getInput = JOptionPane.showInputDialog("Enter Amount you want to withdraw");
- float amount = db.getBalance(userID) - Float.parseFloat(getInput);
- if (amount > 0) {
- db.updateBalance(userID, amount);
- String balance = Integer.toString(db.getBalance(userID));
- textBalance.setText(balance);
- JOptionPane.showMessageDialog(null, "Withdraw complete, you have " + balance + " kr on your account");
- } else {
- JOptionPane.showMessageDialog(null, "Withdraw didn't work");
- }
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, "something went wrong, pleasy try again");
- makeWithdraw();
- }
- }
- private String convertPassword(char[] oldPassword) {
- String newPassword = "";
- for (int i = 0; i < oldPassword.length; i++) {
- newPassword = newPassword + oldPassword[i];
- }
- return (newPassword);
- }
- }
- }
Add Comment
Please, Sign In to add comment