EugeneGermashuk

ATM (Swing Demo)

May 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. import javax.swing.JFrame;
  2.  
  3. public class Terminal extends Thread{
  4.     public static void main(String args[]){
  5.         Terminal terminal = new Terminal();
  6.         terminal.start();
  7.     }
  8.    
  9.     public void run(){
  10.         JFrame newWindow = new TerminalWindow();
  11.         newWindow.setVisible(true);
  12.     }
  13. }
  14.  
  15.  
  16. import javax.swing.JOptionPane;
  17.  
  18. public class Account {
  19.     double balance;
  20.  
  21.     Account(double balance) {
  22.         this.balance = balance;
  23.     }
  24.  
  25.     public double getBalance() {
  26.         return balance;
  27.     }
  28.  
  29.     synchronized public void getDeposin(double money) {
  30.         if (!depositChecks(money)) {
  31.             return;
  32.         }
  33.         balance = balance + money;
  34.     }
  35.  
  36.     synchronized public void getWithdraw(double money) {
  37.         if (!withdrawChecks(money)) {
  38.             return;
  39.         }
  40.         balance = balance - money;
  41.     }
  42.  
  43.     private boolean withdrawChecks(double money) {
  44.         if (balance - money < 0) {
  45.             JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Insufficient Funds At Yout Account.", "Error", 0);
  46.             return false;
  47.         } else if (money < 10) {
  48.             JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw Less Than 10$ - Impossible.", "Error",0);
  49.             return false;
  50.         } else if (money > 500) {
  51.             JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw More Than 500$ - Impossible.", "Error",0);
  52.             return false;
  53.         } else if (money % 10 != 0) {
  54.             JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw,"Amount Of Money Must Divisible By 10 Without Residue.", "Error", 0);
  55.             return false;
  56.         }
  57.         JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw Completed Sucessful.", "Withdraw", 1);
  58.         return true;
  59.     }
  60.  
  61.     private boolean depositChecks(double money) {
  62.         if (money < 10) {
  63.             JOptionPane.showMessageDialog(TerminalWindow.jbtnDeposit, "Deposit Less Than 10$ - Impossible.", "Error",0);
  64.             return false;
  65.         } else if (money > 1000) {
  66.             JOptionPane.showMessageDialog(TerminalWindow.jbtnDeposit, "Deposit More Than 1000$ - Impossible.", "Error",0);
  67.             return false;
  68.         } else if (money % 10 != 0) {
  69.             JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw,"Amount Of Money Must Divisible By 10 Without Residue.", "Error", 0);
  70.             return false;
  71.         }
  72.         JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Your Account Refilled.", "Deposit.", 1);
  73.         return true;
  74.     }
  75. }
  76.  
  77.  
  78. import java.awt.Color;
  79. import java.awt.GridLayout;
  80. import java.awt.event.ActionEvent;
  81. import java.awt.event.ActionListener;
  82.  
  83. import javax.swing.*;
  84. import javax.swing.border.EmptyBorder;
  85.  
  86. public class TerminalWindow extends JFrame implements ActionListener {
  87.  
  88.     private static final long serialVersionUID = 1117798363487311395L;
  89.  
  90.     Account account = new Account(100.00);
  91.     static JButton jbtnDeposit = new JButton("Deposit");
  92.     static JButton jbtnWithdraw = new JButton("Withdraw");
  93.     static JTextField jtfBalance = new JTextField();
  94.     static JTextField jtfMoney = new JTextField(10);
  95.  
  96.     TerminalWindow() {
  97.         super("ATM");
  98.  
  99.         setSize(350, 260);
  100.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  101.         setLocationRelativeTo(null);
  102.         setResizable(false);
  103.  
  104.         JLabel jlblBalance = new JLabel("Balance:");
  105.         JLabel jlblMoney = new JLabel("Money:");
  106.         jlblBalance.setHorizontalAlignment(JLabel.CENTER);
  107.         jlblMoney.setHorizontalAlignment(JLabel.CENTER);
  108.  
  109.         jtfBalance.setBackground(Color.BLACK);
  110.         jtfBalance.setForeground(Color.GREEN);
  111.         jtfBalance.setOpaque(true);
  112.         jtfBalance.setEditable(false);
  113.         jtfBalance.setHorizontalAlignment(JTextField.CENTER);
  114.         jtfMoney.setHorizontalAlignment(JTextField.CENTER);
  115.  
  116.         JPanel componentPanel = new JPanel();
  117.         componentPanel.setLayout(new GridLayout(3, 2));
  118.  
  119.         componentPanel.add(jlblBalance);
  120.         componentPanel.add(jtfBalance);
  121.         componentPanel.add(jlblMoney);
  122.         componentPanel.add(jtfMoney);
  123.         componentPanel.add(jbtnDeposit);
  124.         componentPanel.add(jbtnWithdraw);
  125.         componentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  126.         getContentPane().add(componentPanel);
  127.  
  128.         showBalance();
  129.         jbtnDeposit.addActionListener(this);
  130.         jbtnWithdraw.addActionListener(this);
  131.     }
  132.  
  133.     @Override
  134.     public void actionPerformed(ActionEvent action) {
  135.         if (action.getSource() == jbtnDeposit) {
  136.             Deposit();
  137.         }
  138.  
  139.         if (action.getSource() == jbtnWithdraw) {
  140.             Withdraw();
  141.         }
  142.     }
  143.  
  144.     private void showBalance() {
  145.         String balance = String.format("%.2f", account.getBalance());
  146.         jtfBalance.setText(balance);
  147.     }
  148.  
  149.     private void Deposit() {
  150.         try {
  151.             double money = Double.parseDouble(jtfMoney.getText());
  152.             account.getDeposin(money);
  153.             showBalance();
  154.             jtfMoney.setText("");
  155.         } catch (NumberFormatException ex) {
  156.             JOptionPane.showMessageDialog(jbtnDeposit, "Unvalid Enter Or Incorrect Character.", "Error", 0);
  157.         }
  158.     }
  159.  
  160.     private void Withdraw() {
  161.         try {
  162.             double money = Double.parseDouble(jtfMoney.getText());
  163.             account.getWithdraw(money);
  164.             showBalance();
  165.             jtfMoney.setText("");
  166.         } catch (NumberFormatException ex) {
  167.             JOptionPane.showMessageDialog(jbtnDeposit, "Unvalid Enter Or Incorrect Character.", "Error", 0);
  168.         }
  169.     }
  170. }
Add Comment
Please, Sign In to add comment