Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. package Lab4;
  2.  
  3. import atm.AutomaticCashTerminal;
  4. import bankService.BankService;
  5. import bankService.bankServiceExceptions.AuthException;
  6. import bankService.bankServiceExceptions.BankServiceException;
  7.  
  8. import javax.swing.*;
  9. import java.awt.*;
  10.  
  11. public class ATM {
  12.     public static void main(String[] args) {
  13.         BankService.registerNewAccount("user", "pass");
  14.         var atm = new AutomaticCashTerminal("0001");
  15.  
  16.         EventQueue.invokeLater(() -> {
  17.             var frame = new JFrame("ATM");
  18.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.             frame.setSize(400, 300);
  20.  
  21.             var atmPanel = new JPanel();
  22.             var loginPanel = new JPanel();
  23.             var workSpacePanel = new JPanel();
  24.             var statusLabel = new JLabel();
  25.             workSpacePanel.setEnabled(false);
  26.             workSpacePanel.setVisible(false);
  27.             statusLabel.setVisible(false);
  28.             atmPanel.setLayout(new BorderLayout());
  29.             atmPanel.add(statusLabel, BorderLayout.SOUTH);
  30.  
  31.             // loginPanel
  32.             var usernameInput = new JTextField("username", 10);
  33.             var passwordInput = new JPasswordField("password", 10);
  34.             var submitButton = new JButton("submit");
  35.             submitButton.addActionListener(e -> {
  36.                 String username = usernameInput.getText();
  37.                 String password = new String(passwordInput.getPassword());
  38.                 if (atm.loginSuccessful(username, password)) {
  39.                     workSpacePanel.setEnabled(true);
  40.                     workSpacePanel.setVisible(true);
  41.                     showSuccess(statusLabel, "Hello, " + username);
  42.                 } else {
  43.                     showError(statusLabel, "Wrong username or password");
  44.                 }
  45.             });
  46.             loginPanel.add(usernameInput);
  47.             loginPanel.add(passwordInput);
  48.             loginPanel.add(submitButton);
  49.             atmPanel.add(loginPanel, BorderLayout.NORTH);
  50.  
  51.             // workspacePanel
  52.             var sumInput = new JTextField("0", 10);
  53.             var putMoneyButton = new JButton("put money");
  54.             var getMoneyButton = new JButton("get money");
  55.             putMoneyButton.addActionListener(e -> {
  56.                 double sum = Double.parseDouble(sumInput.getText());
  57.                 try {
  58.                     atm.putMoney(sum);
  59.                     var balance = atm.getBalance();
  60.                     showSuccess(statusLabel, "Successful! Your balance now: " + balance);
  61.                 } catch (AuthException ex) {
  62.                     showError(statusLabel, ex.getMessage());
  63.                 }
  64.             });
  65.             getMoneyButton.addActionListener(e -> {
  66.                 double sum = Double.parseDouble(sumInput.getText());
  67.                 try {
  68.                     atm.getMoney(sum);
  69.                     var balance = atm.getBalance();
  70.                     showSuccess(statusLabel, "Successful! Your balance now: " + balance);
  71.                 } catch (BankServiceException ex) {
  72.                     showError(statusLabel, ex.getMessage());
  73.                 }
  74.             });
  75.  
  76.             workSpacePanel.add(sumInput);
  77.             workSpacePanel.add(putMoneyButton);
  78.             workSpacePanel.add(getMoneyButton);
  79.             atmPanel.add(workSpacePanel, BorderLayout.CENTER);
  80.  
  81.             frame.add(atmPanel, BorderLayout.CENTER);
  82.             frame.setVisible(true);
  83.         });
  84.     }
  85.  
  86.     private static void showError(JLabel label, String text) {
  87.         label.setText(text);
  88.         label.setForeground(Color.RED);
  89.         label.setVisible(true);
  90.     }
  91.  
  92.     private static void showSuccess(JLabel label, String text) {
  93.         label.setText(text);
  94.         label.setForeground(Color.BLACK);
  95.         label.setVisible(true);
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement