Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab4;
- import atm.AutomaticCashTerminal;
- import bankService.BankService;
- import bankService.bankServiceExceptions.AuthException;
- import bankService.bankServiceExceptions.BankServiceException;
- import javax.swing.*;
- import java.awt.*;
- public class ATM {
- public static void main(String[] args) {
- BankService.registerNewAccount("user", "pass");
- var atm = new AutomaticCashTerminal("0001");
- EventQueue.invokeLater(() -> {
- var frame = new JFrame("ATM");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400, 300);
- var atmPanel = new JPanel();
- var loginPanel = new JPanel();
- var workSpacePanel = new JPanel();
- var statusLabel = new JLabel();
- workSpacePanel.setEnabled(false);
- workSpacePanel.setVisible(false);
- statusLabel.setVisible(false);
- atmPanel.setLayout(new BorderLayout());
- atmPanel.add(statusLabel, BorderLayout.SOUTH);
- // loginPanel
- var usernameInput = new JTextField("username", 10);
- var passwordInput = new JPasswordField("password", 10);
- var submitButton = new JButton("submit");
- submitButton.addActionListener(e -> {
- String username = usernameInput.getText();
- String password = new String(passwordInput.getPassword());
- if (atm.loginSuccessful(username, password)) {
- workSpacePanel.setEnabled(true);
- workSpacePanel.setVisible(true);
- showSuccess(statusLabel, "Hello, " + username);
- } else {
- showError(statusLabel, "Wrong username or password");
- }
- });
- loginPanel.add(usernameInput);
- loginPanel.add(passwordInput);
- loginPanel.add(submitButton);
- atmPanel.add(loginPanel, BorderLayout.NORTH);
- // workspacePanel
- var sumInput = new JTextField("0", 10);
- var putMoneyButton = new JButton("put money");
- var getMoneyButton = new JButton("get money");
- putMoneyButton.addActionListener(e -> {
- double sum = Double.parseDouble(sumInput.getText());
- try {
- atm.putMoney(sum);
- var balance = atm.getBalance();
- showSuccess(statusLabel, "Successful! Your balance now: " + balance);
- } catch (AuthException ex) {
- showError(statusLabel, ex.getMessage());
- }
- });
- getMoneyButton.addActionListener(e -> {
- double sum = Double.parseDouble(sumInput.getText());
- try {
- atm.getMoney(sum);
- var balance = atm.getBalance();
- showSuccess(statusLabel, "Successful! Your balance now: " + balance);
- } catch (BankServiceException ex) {
- showError(statusLabel, ex.getMessage());
- }
- });
- workSpacePanel.add(sumInput);
- workSpacePanel.add(putMoneyButton);
- workSpacePanel.add(getMoneyButton);
- atmPanel.add(workSpacePanel, BorderLayout.CENTER);
- frame.add(atmPanel, BorderLayout.CENTER);
- frame.setVisible(true);
- });
- }
- private static void showError(JLabel label, String text) {
- label.setText(text);
- label.setForeground(Color.RED);
- label.setVisible(true);
- }
- private static void showSuccess(JLabel label, String text) {
- label.setText(text);
- label.setForeground(Color.BLACK);
- label.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement