Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- public class Terminal extends Thread{
- public static void main(String args[]){
- Terminal terminal = new Terminal();
- terminal.start();
- }
- public void run(){
- JFrame newWindow = new TerminalWindow();
- newWindow.setVisible(true);
- }
- }
- import javax.swing.JOptionPane;
- public class Account {
- double balance;
- Account(double balance) {
- this.balance = balance;
- }
- public double getBalance() {
- return balance;
- }
- synchronized public void getDeposin(double money) {
- if (!depositChecks(money)) {
- return;
- }
- balance = balance + money;
- }
- synchronized public void getWithdraw(double money) {
- if (!withdrawChecks(money)) {
- return;
- }
- balance = balance - money;
- }
- private boolean withdrawChecks(double money) {
- if (balance - money < 0) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Insufficient Funds At Yout Account.", "Error", 0);
- return false;
- } else if (money < 10) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw Less Than 10$ - Impossible.", "Error",0);
- return false;
- } else if (money > 500) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw More Than 500$ - Impossible.", "Error",0);
- return false;
- } else if (money % 10 != 0) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw,"Amount Of Money Must Divisible By 10 Without Residue.", "Error", 0);
- return false;
- }
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Withdraw Completed Sucessful.", "Withdraw", 1);
- return true;
- }
- private boolean depositChecks(double money) {
- if (money < 10) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnDeposit, "Deposit Less Than 10$ - Impossible.", "Error",0);
- return false;
- } else if (money > 1000) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnDeposit, "Deposit More Than 1000$ - Impossible.", "Error",0);
- return false;
- } else if (money % 10 != 0) {
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw,"Amount Of Money Must Divisible By 10 Without Residue.", "Error", 0);
- return false;
- }
- JOptionPane.showMessageDialog(TerminalWindow.jbtnWithdraw, "Your Account Refilled.", "Deposit.", 1);
- return true;
- }
- }
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- import javax.swing.border.EmptyBorder;
- public class TerminalWindow extends JFrame implements ActionListener {
- private static final long serialVersionUID = 1117798363487311395L;
- Account account = new Account(100.00);
- static JButton jbtnDeposit = new JButton("Deposit");
- static JButton jbtnWithdraw = new JButton("Withdraw");
- static JTextField jtfBalance = new JTextField();
- static JTextField jtfMoney = new JTextField(10);
- TerminalWindow() {
- super("ATM");
- setSize(350, 260);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- setResizable(false);
- JLabel jlblBalance = new JLabel("Balance:");
- JLabel jlblMoney = new JLabel("Money:");
- jlblBalance.setHorizontalAlignment(JLabel.CENTER);
- jlblMoney.setHorizontalAlignment(JLabel.CENTER);
- jtfBalance.setBackground(Color.BLACK);
- jtfBalance.setForeground(Color.GREEN);
- jtfBalance.setOpaque(true);
- jtfBalance.setEditable(false);
- jtfBalance.setHorizontalAlignment(JTextField.CENTER);
- jtfMoney.setHorizontalAlignment(JTextField.CENTER);
- JPanel componentPanel = new JPanel();
- componentPanel.setLayout(new GridLayout(3, 2));
- componentPanel.add(jlblBalance);
- componentPanel.add(jtfBalance);
- componentPanel.add(jlblMoney);
- componentPanel.add(jtfMoney);
- componentPanel.add(jbtnDeposit);
- componentPanel.add(jbtnWithdraw);
- componentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(componentPanel);
- showBalance();
- jbtnDeposit.addActionListener(this);
- jbtnWithdraw.addActionListener(this);
- }
- @Override
- public void actionPerformed(ActionEvent action) {
- if (action.getSource() == jbtnDeposit) {
- Deposit();
- }
- if (action.getSource() == jbtnWithdraw) {
- Withdraw();
- }
- }
- private void showBalance() {
- String balance = String.format("%.2f", account.getBalance());
- jtfBalance.setText(balance);
- }
- private void Deposit() {
- try {
- double money = Double.parseDouble(jtfMoney.getText());
- account.getDeposin(money);
- showBalance();
- jtfMoney.setText("");
- } catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(jbtnDeposit, "Unvalid Enter Or Incorrect Character.", "Error", 0);
- }
- }
- private void Withdraw() {
- try {
- double money = Double.parseDouble(jtfMoney.getText());
- account.getWithdraw(money);
- showBalance();
- jtfMoney.setText("");
- } catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(jbtnDeposit, "Unvalid Enter Or Incorrect Character.", "Error", 0);
- }
- }
- }
Add Comment
Please, Sign In to add comment