Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.94 KB | None | 0 0
  1. package atmgui.views;
  2.  
  3. import java.awt.EventQueue;
  4. import java.sql.*;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.UIManager;
  11. import javax.swing.GroupLayout;
  12. import javax.swing.GroupLayout.Alignment;
  13. import javax.swing.JMenuBar;
  14. import javax.swing.JMenu;
  15. import javax.swing.JMenuItem;
  16. import javax.swing.JTextField;
  17. import javax.swing.JLabel;
  18. import javax.swing.LayoutStyle.ComponentPlacement;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JButton;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.ActionEvent;
  25. import org.eclipse.wb.swing.FocusTraversalOnArray;
  26. import java.awt.Component;
  27.  
  28. public class Main extends JFrame {
  29.  
  30.     private JPanel contentPane;
  31.     private JTextField ssn;
  32.     private JPasswordField passwordField;
  33.     private JTextField accountNumber;
  34.     private JTextField amount;
  35.     String ssNumber = "";
  36.     int pinCode = 0;
  37.     int accountNr = 0;
  38.     float amountToWithdraw =0;
  39.    
  40.  
  41.     public Connection createConnection() {
  42.        
  43.         Connection conn = null;
  44.        
  45.         try {
  46.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
  47.             String URL = "jdbc:sqlserver://194.47.129.139:1433;databaseName=dv1219_ht12_2;";
  48.             String User = "dv1219_ht12_2";
  49.             String Password = "VÅRAT LÖSENORD";
  50.             conn = DriverManager.getConnection(URL, User, Password);
  51.        
  52.         }
  53.        
  54.         catch (Exception e)
  55.         {
  56.             System.out.println(e.getMessage());
  57.         }
  58.  
  59.         return conn;
  60.     }
  61.    
  62.     private void SQLQuery(String ssNumber, int pinCode, int accountNr, float amountToWithdraw) {
  63.        
  64.         try {
  65.            
  66.             Connection conn = createConnection();
  67.             CallableStatement cs = conn.prepareCall("{? = call checkValidity(?,?,?,?)}");
  68.             cs.registerOutParameter(1, java.sql.Types.INTEGER);
  69.             cs.setString(2, ssNumber);
  70.             cs.setInt(3,accountNr);
  71.             cs.setInt(4, pinCode);
  72.             cs.setFloat(5, amountToWithdraw);
  73.             cs.execute();
  74.             System.out.println("Return Status" +cs.getInt(1));
  75.            
  76.             if(cs.getInt(1) == 1)
  77.                 JOptionPane.showMessageDialog(this, "Wiiiiiieeeeeee!!!!", "Du har gjort rätt", JOptionPane.ERROR_MESSAGE);
  78.             else
  79.                 JOptionPane.showMessageDialog(this, "Ohhh noes!!!!", "Du har gjort fel", JOptionPane.ERROR_MESSAGE);
  80.             cs.close();
  81.            
  82.         }
  83.         catch (Exception e){
  84.             e.printStackTrace();
  85.         }
  86.            
  87.        
  88.         finally {
  89.            
  90.         }
  91.     }
  92.        
  93.     private void SaveContent() {
  94.        
  95.         String ssNumber = ssn.getText();
  96.         int pinCode = Integer.parseInt(passwordField.getText());
  97.         int accountNr = Integer.parseInt(accountNumber.getText());
  98.         String tempAmount = amount.getText();
  99.         float amountToWithdraw = Float.parseFloat(tempAmount);
  100.    
  101.         SQLQuery(ssNumber, pinCode, accountNr, amountToWithdraw);
  102.        
  103.         ssn.setText("");
  104.         passwordField.setText("");
  105.         accountNumber.setText("");
  106.         amount.setText("");
  107.            
  108.        
  109.        
  110.     }
  111.    
  112.     /**
  113.      * Launch the application.
  114.      */
  115.     public static void main(String[] args) {
  116.         try {
  117.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  118.         } catch (Throwable e) {
  119.             e.printStackTrace();
  120.         }
  121.         EventQueue.invokeLater(new Runnable() {
  122.             public void run() {
  123.                 try {
  124.                     Main frame = new Main();
  125.                     frame.setVisible(true);
  126.                 } catch (Exception e) {
  127.                     e.printStackTrace();
  128.                 }
  129.             }
  130.         });
  131.     }
  132.  
  133.    
  134.     /**
  135.      * Create the frame.
  136.      */
  137.     public Main() {
  138.         setTitle("ATM 1.0");
  139.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140.         setBounds(100, 100, 444, 314);
  141.        
  142.         JMenuBar menuBar = new JMenuBar();
  143.         setJMenuBar(menuBar);
  144.        
  145.         JMenu mnFile = new JMenu("File");
  146.         menuBar.add(mnFile);
  147.        
  148.         JMenuItem mntmExit = new JMenuItem("Exit");
  149.         mntmExit.addActionListener(new ActionListener() {
  150.             public void actionPerformed(ActionEvent arg0) {
  151.                 dispose();
  152.             }
  153.         });
  154.        
  155.         mnFile.add(mntmExit);
  156.         contentPane = new JPanel();
  157.         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  158.         setContentPane(contentPane);
  159.        
  160.         ssn = new JTextField();
  161.         ssn.setToolTipText("Personnummer");
  162.         ssn.setColumns(10);
  163.        
  164.         JLabel lblPersonnummer = new JLabel("Personnummer:");
  165.        
  166.         JLabel lblPinkod = new JLabel("Pinkod:");
  167.        
  168.         passwordField = new JPasswordField();
  169.         passwordField.setToolTipText("Pinkod");
  170.        
  171.         JLabel lblKontonummer = new JLabel("Kontonummer:");
  172.        
  173.         accountNumber = new JTextField();
  174.         accountNumber.setToolTipText("Kontonummer");
  175.         accountNumber.setColumns(10);
  176.        
  177.         JLabel lblBelopp = new JLabel("Belopp:");
  178.        
  179.         amount = new JTextField();
  180.         amount.setToolTipText("\u00D6nskat belopp");
  181.         amount.setColumns(10);
  182.        
  183.         JButton btnNewButton = new JButton("Ok");
  184.        
  185.         btnNewButton.addMouseListener(new MouseAdapter() {
  186.             @Override
  187.             public void mouseClicked(MouseEvent arg0) {
  188.                 createConnection();
  189.                 SaveContent();
  190.                
  191.             }
  192.         });
  193.        
  194.         JButton btnNewButton_1 = new JButton("Avbryt");
  195.         btnNewButton_1.addMouseListener(new MouseAdapter() {
  196.             @Override
  197.             public void mouseClicked(MouseEvent arg0) {
  198.  
  199.                 ssn.setText("");
  200.                 passwordField.setText("");
  201.                 accountNumber.setText("");
  202.                 amount.setText("");
  203.                
  204.             }
  205.         });
  206.        
  207.         GroupLayout gl_contentPane = new GroupLayout(contentPane);
  208.         gl_contentPane.setHorizontalGroup(
  209.             gl_contentPane.createParallelGroup(Alignment.LEADING)
  210.                 .addGroup(gl_contentPane.createSequentialGroup()
  211.                     .addContainerGap()
  212.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  213.                         .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  214.                             .addGroup(gl_contentPane.createSequentialGroup()
  215.                                 .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
  216.                                     .addComponent(ssn, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
  217.                                     .addComponent(amount, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
  218.                                     .addComponent(accountNumber, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 342, Short.MAX_VALUE)
  219.                                     .addComponent(passwordField, Alignment.LEADING, 189, 189, Short.MAX_VALUE)
  220.                                     .addComponent(lblPersonnummer, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
  221.                                     .addComponent(lblPinkod, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))
  222.                                 .addGap(33))
  223.                             .addGroup(gl_contentPane.createSequentialGroup()
  224.                                 .addComponent(lblBelopp, GroupLayout.PREFERRED_SIZE, 77, GroupLayout.PREFERRED_SIZE)
  225.                                 .addPreferredGap(ComponentPlacement.RELATED)))
  226.                         .addGroup(gl_contentPane.createSequentialGroup()
  227.                             .addComponent(lblKontonummer, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
  228.                             .addGap(74)))
  229.                     .addPreferredGap(ComponentPlacement.RELATED)
  230.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
  231.                         .addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  232.                         .addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE))
  233.                     .addGap(50))
  234.         );
  235.         gl_contentPane.setVerticalGroup(
  236.             gl_contentPane.createParallelGroup(Alignment.LEADING)
  237.                 .addGroup(gl_contentPane.createSequentialGroup()
  238.                     .addContainerGap()
  239.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
  240.                         .addComponent(btnNewButton, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  241.                         .addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
  242.                             .addComponent(lblPersonnummer)
  243.                             .addPreferredGap(ComponentPlacement.RELATED)
  244.                             .addComponent(ssn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  245.                             .addPreferredGap(ComponentPlacement.RELATED)
  246.                             .addComponent(lblPinkod)
  247.                             .addPreferredGap(ComponentPlacement.RELATED)
  248.                             .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
  249.                     .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
  250.                         .addGroup(gl_contentPane.createSequentialGroup()
  251.                             .addGap(13)
  252.                             .addComponent(lblKontonummer)
  253.                             .addPreferredGap(ComponentPlacement.RELATED)
  254.                             .addComponent(accountNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  255.                             .addGap(13)
  256.                             .addComponent(lblBelopp)
  257.                             .addPreferredGap(ComponentPlacement.RELATED)
  258.                             .addComponent(amount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  259.                         .addGroup(gl_contentPane.createSequentialGroup()
  260.                             .addGap(26)
  261.                             .addComponent(btnNewButton_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  262.                     .addContainerGap(21, Short.MAX_VALUE))
  263.         );
  264.         contentPane.setLayout(gl_contentPane);
  265.         contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{ssn, passwordField, accountNumber, amount, btnNewButton, btnNewButton_1}));
  266.         setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{ssn, passwordField, accountNumber, amount, btnNewButton, btnNewButton_1}));
  267.     }
  268.    
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement