2013-05-17 3 views
-5
package QADev_AcctReset; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Properties; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.PlainDocument; 

public class AcctReset implements ActionListener { 

    JTextField cardTextField; 
    JButton cardButtonQA, cardButtonDEV, cardButtonResetA, cardButtonResetP; 
    static JLabel frameLabel, frameLabel1, frameLabel2; 

    public JPanel createContentPane() throws ClassNotFoundException, 
      SQLException { 

     JPanel resetGUI = new JPanel(); 
     resetGUI.setLayout(null); 

     frameLabel1 = new JLabel(); 
     frameLabel1.setLocation(40, -40); 
     frameLabel1.setSize(300, 100); 
     frameLabel1.setHorizontalAlignment(0); 
     resetGUI.add(frameLabel1); 
     frameLabel1.setText("Enter Card/Cardless/SmartCheck"); 

     frameLabel2 = new JLabel(); 
     frameLabel2.setLocation(20, -20); 
     frameLabel2.setSize(350, 100); 
     frameLabel2.setHorizontalAlignment(0); 
     frameLabel2.setFont(new Font("Aerial", Font.PLAIN, 9)); 
     resetGUI.add(frameLabel2); 
     frameLabel2 
       .setText("Legend: Card# = 19 digits, Cardless# = 10 digits, Check = 14, 17 or 18 digits"); 
     frameLabel2.setForeground(Color.RED); 

     cardTextField = new JTextField("Enter Card or Cardless Number"); 
     cardTextField.setLocation(45, 38); 
     cardTextField.setSize(300, 30); 
     cardTextField.setHorizontalAlignment(0); 
     cardTextField.setEditable(true); 
     cardTextField.addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent e) { 
       cardTextField.setText(""); 
       frameLabel.setText(""); 
       cardTextField.requestFocusInWindow(); 
       cardButtonResetA.setEnabled(true); 
       cardButtonResetP.setEnabled(true); 
      } 
     }); 
     cardTextField.addActionListener(this); 
     cardTextField.addKeyListener(new KeyAdapter() { 
      public void keyTyped(KeyEvent e) { 
       char c = e.getKeyChar(); 
       if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))) { 
        e.consume(); 
       } 
      } 
     }); 
     cardTextField.setDocument(new LengthRestrictedDocument(25)); 
     resetGUI.add(cardTextField); 

     Image image = Toolkit.getDefaultToolkit().getImage("img/qa.png"); 
     ImageIcon icon = new ImageIcon(image); 
     cardButtonQA = new JButton("", icon); 
     cardButtonQA.setLocation(5, 38); 
     cardButtonQA.setSize(35, 28); 
     cardButtonQA.setVisible(true); 
     cardButtonQA.addActionListener(this); 
     resetGUI.add(cardButtonQA); 

     Image image1 = Toolkit.getDefaultToolkit().getImage("img/dev.png"); 
     ImageIcon icon1 = new ImageIcon(image1); 
     cardButtonDEV = new JButton("", icon1); 
     cardButtonDEV.setLocation(350, 38); 
     cardButtonDEV.setSize(35, 28); 
     cardButtonDEV.setVisible(true); 
     cardButtonDEV.addActionListener(this); 
     resetGUI.add(cardButtonDEV); 


     cardButtonResetA = new JButton("Reset Card To Active"); 
     cardButtonResetA.setLocation(5, 80); 
     cardButtonResetA.setSize(185, 30); 
     cardButtonResetA.setMnemonic('R'); 
     cardButtonResetA.setVisible(true); 
     cardButtonResetA.addActionListener(this); 
     resetGUI.add(cardButtonResetA); 

     cardButtonResetP = new JButton("Reset To Pending (DL Req)"); 
     cardButtonResetP.setLocation(200, 80); 
     cardButtonResetP.setSize(185, 30); 
     cardButtonResetP.setMnemonic('P'); 
     cardButtonResetP.setVisible(true); 
     cardButtonResetP.addActionListener(this); 
     resetGUI.add(cardButtonResetP); 

     frameLabel = new JLabel(); 
     frameLabel.setLocation(20, 80); 
     frameLabel.setSize(350, 100); 
     frameLabel.setHorizontalAlignment(0); 
     frameLabel.setFont(new Font("Aerial", Font.BOLD, 10)); 
     frameLabel.setForeground(Color.BLUE); 
     resetGUI.add(frameLabel); 

     resetGUI.setOpaque(true); 
     return resetGUI; 

    } 


    public void actionPerformed(ActionEvent a) { 

     if (a.getSource() == cardButtonResetA) { 
      try { 
       Connection connection = getConnectionQA(); 
       String cardLeng = cardTextField.getText().trim(); 
       int cardLen = cardLeng.length(); 

       DateFormat dateForm = new SimpleDateFormat("MMddyy-HHmmss"); 
       Date date = new Date(); 
       BufferedWriter reportLog = null; 
       File reportFile = new File("ResetLog.txt"); 

       if (cardLen == 19) { 
        // execute select query to identify the cust_id of the 
        // input 
        // card number. 
        String sql = getThisProperty("sql.select"); 
        PreparedStatement stmt = connection.prepareStatement(sql); 
        stmt.setString(1, cardLeng); 
        ResultSet rs = stmt.executeQuery(); 
        rs.next(); 
        Integer custID = new Integer(rs.getInt("issue_cust_id")); 

        // execute delete in financial_txn table for resp_cd 52, 
        // 82 
        // and 59. 
        String sqldel = getThisProperty("sql.delete"); 
        PreparedStatement stmtdel = connection 
          .prepareStatement(sqldel); 
        stmtdel.setInt(1, custID); 
        stmtdel.executeUpdate(); 

        // execute update to make customer account to be Active. 
        String sqlupdateacct = getThisProperty("sql.updateacctactive"); 
        PreparedStatement stmtacctactive = connection 
          .prepareStatement(sqlupdateacct); 
        stmtacctactive.setInt(1, custID); 
        stmtacctactive.executeUpdate(); 

        // execute update to make the cust_pymt_opt status to be 
        // Active. 
        String sqlupdate = getThisProperty("sql.updateactive"); 
        PreparedStatement stmtupdate = connection 
          .prepareStatement(sqlupdate); 
        stmtupdate.setInt(1, custID); 
        stmtupdate.executeUpdate(); 

        // execute to delete velocity limit of the account. 
        String sqldelvel = getThisProperty("sql.deletevelo"); 
        PreparedStatement stmtdelvel = connection 
          .prepareStatement(sqldelvel); 
        stmtdelvel.setInt(1, custID); 
        stmtdelvel.executeUpdate(); 

        if (!reportFile.exists()) { 
         reportFile.createNewFile(); 
        } 
        reportLog = new BufferedWriter(new FileWriter(reportFile 
          .getName(), true)); 
        reportLog.write("Run Date/Time " + dateForm.format(date) 
          + " : Card # " + cardLeng 
          + " has been reset to be Active.\r\n"); 
        reportLog.flush(); 
        reportLog.close(); 

        frameLabel.setText(cardLeng 
          + " Account Has Been Reset To Active"); 
        cardTextField.setText(null); 
        connection.close(); 
       } else if (cardLen == 10) { 
        String sql = getThisProperty("sql.CLselect"); 
        PreparedStatement stmt = connection.prepareStatement(sql); 
        stmt.setString(1, "%" + cardLeng + "_"); 
        ResultSet rs = stmt.executeQuery(); 
        rs.next(); 
        Integer custID = new Integer(rs.getInt("issue_cust_id")); 

        // execute delete in financial_txn table for resp_cd 52, 
        // 82 
        // and 59. 
        String sqldel = getThisProperty("sql.delete"); 
        PreparedStatement stmtdel = connection 
          .prepareStatement(sqldel); 
        stmtdel.setInt(1, custID); 
        stmtdel.executeUpdate(); 

        // execute update to make customer account to be Active. 
        String sqlupdateacct = getThisProperty("sql.updateacctactive"); 
        PreparedStatement stmtacctactive = connection 
          .prepareStatement(sqlupdateacct); 
        stmtacctactive.setInt(1, custID); 
        stmtacctactive.executeUpdate(); 

        // execute update to make the cust_pymt_opt status to be 
        // Active. 
        String sqlupdate = getThisProperty("sql.updateactive"); 
        PreparedStatement stmtupdate = connection 
          .prepareStatement(sqlupdate); 
        stmtupdate.setInt(1, custID); 
        stmtupdate.executeUpdate(); 

        // execute to delete velocity limit of the account. 
        String sqldelvel = getThisProperty("sql.deletevelo"); 
        PreparedStatement stmtdelvel = connection 
          .prepareStatement(sqldelvel); 
        stmtdelvel.setInt(1, custID); 
        stmtdelvel.executeUpdate(); 

        if (!reportFile.exists()) { 
         reportFile.createNewFile(); 
        } 
        reportLog = new BufferedWriter(new FileWriter(reportFile 
          .getName(), true)); 
        reportLog.write("Run Date/Time " + dateForm.format(date) 
          + " : Cardless # " + cardLeng 
          + " has been reset to be Active.\r\n"); 
        reportLog.flush(); 
        reportLog.close(); 

        frameLabel.setText(cardLeng 
          + " Account Has Been Reset To Active"); 
        cardTextField.setText(null); 
        connection.close(); 
       } else if (cardLen == 14 || cardLen == 17 || cardLen == 18) { 
        String sql = getThisProperty("sql.select"); 
        PreparedStatement stmt = connection.prepareStatement(sql); 
        stmt.setString(1, cardLeng); 
        ResultSet rs = stmt.executeQuery(); 
        rs.next(); 
        Integer custID = new Integer(rs.getInt("issue_cust_id")); 

        // execute delete in financial_txn table for resp_cd 52, 
        // 82 
        // and 59. 
        String sqldel = getThisProperty("sql.delete"); 
        PreparedStatement stmtdel = connection 
          .prepareStatement(sqldel); 
        stmtdel.setInt(1, custID); 
        stmtdel.executeUpdate(); 

        // execute update to make customer account to be Active. 
        String sqlupdateacct = getThisProperty("sql.updateacctactive"); 
        PreparedStatement stmtacctactive = connection 
          .prepareStatement(sqlupdateacct); 
        stmtacctactive.setInt(1, custID); 
        stmtacctactive.executeUpdate(); 

        // execute update to make the cust_pymt_opt status to be 
        // Active. 
        String sqlupdate = getThisProperty("sql.updateactive"); 
        PreparedStatement stmtupdate = connection 
          .prepareStatement(sqlupdate); 
        stmtupdate.setInt(1, custID); 
        stmtupdate.executeUpdate(); 

        // execute to delete velocity limit of the account. 
        String sqldelvel = getThisProperty("sql.deletevelo"); 
        PreparedStatement stmtdelvel = connection 
          .prepareStatement(sqldelvel); 
        stmtdelvel.setInt(1, custID); 
        stmtdelvel.executeUpdate(); 

        if (!reportFile.exists()) { 
         reportFile.createNewFile(); 
        } 
        reportLog = new BufferedWriter(new FileWriter(reportFile 
          .getName(), true)); 
        reportLog.write("Run Date/Time " + dateForm.format(date) 
          + " : Check # " + cardLeng 
          + " has been reset to be Active.\r\n"); 
        reportLog.flush(); 
        reportLog.close(); 

        frameLabel.setText(cardLeng 
          + " SCheck Account Has Been Reset To Active"); 
        cardTextField.setText(null); 
        connection.close(); 
       } else if (cardLen != 10 && cardLen != 19 && cardLen != 14 
         && cardLen != 17 && cardLen != 18) { 
        frameLabel.setText("Enter Valid Input"); 
        cardTextField.requestFocusInWindow(); 
       } 
       connection.close(); 
      } catch (ClassNotFoundException e) { 
       frameLabel.setText("Java Class Error"); 
       // e.printStackTrace(); 
      } catch (SQLException e) { 
       frameLabel.setText("No Result Found, Enter a Valid Account"); 
       // e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } else if (a.getSource() == cardButtonResetP) { 
      try { 
       Connection connection = getConnectionQA(); 
       String cardLeng = cardTextField.getText().trim(); 
       int cardLenP = cardLeng.length(); 

       DateFormat dateForm = new SimpleDateFormat("MMddyy-HHmmss"); 
       Date date = new Date(); 
       BufferedWriter reportLog = null; 
       File reportFile = new File("ResetLog.txt"); 

       if (cardLenP == 19) { 

        // execute select query to identify the cust_id of the 
        // input 
        // card number. 
        String sql = getThisProperty("sql.select"); 
        PreparedStatement stmt = connection.prepareStatement(sql); 
        stmt.setString(1, cardLeng); 
        ResultSet rs = stmt.executeQuery(); 
        rs.next(); 
        Integer custID = new Integer(rs.getInt("issue_cust_id")); 

        // execute delete in financial_txn table for resp_cd 52, 
        // 82 
        // and 59. 
        String sqldel = getThisProperty("sql.delete"); 
        PreparedStatement stmtdel = connection 
          .prepareStatement(sqldel); 
        stmtdel.setInt(1, custID); 
        stmtdel.executeUpdate(); 

        // execute update to make customer account to be Active. 
        String sqlupdateacct = getThisProperty("sql.updateacctactive"); 
        PreparedStatement stmtacctactive = connection 
          .prepareStatement(sqlupdateacct); 
        stmtacctactive.setInt(1, custID); 
        stmtacctactive.executeUpdate(); 

        // execute update to make the cust_pymt_opt status to be 
        // Pending DL Required. 
        String sqlupdate = getThisProperty("sql.updatepending"); 
        PreparedStatement stmtupdate = connection 
          .prepareStatement(sqlupdate); 
        stmtupdate.setInt(1, custID); 
        stmtupdate.executeUpdate(); 

        // execute to delete velocity limit of the account. 
        String sqldelvel = getThisProperty("sql.deletevelo"); 
        PreparedStatement stmtdelvel = connection 
          .prepareStatement(sqldelvel); 
        stmtdelvel.setInt(1, custID); 
        stmtdelvel.executeUpdate(); 

        if (!reportFile.exists()) { 
         reportFile.createNewFile(); 
        } 
        reportLog = new BufferedWriter(new FileWriter(reportFile 
          .getName(), true)); 
        reportLog.write("Run Date/Time " + dateForm.format(date) 
          + " : Card # " + cardLeng 
          + " has been reset to be Pending DL Required.\r\n"); 
        reportLog.flush(); 
        reportLog.close(); 

        frameLabel.setText(cardLeng 
          + " Have Been Reset To Pending DL Required"); 
        cardTextField.setText(null); 
        connection.close(); 
       } else if (cardLenP == 10) { 
        // execute select query to identify the cust_id of the 
        // input 
        // card number. 
        String sql = getThisProperty("sql.CLselect"); 
        PreparedStatement stmt = connection.prepareStatement(sql, 
          ResultSet.TYPE_SCROLL_INSENSITIVE, 
          ResultSet.CONCUR_READ_ONLY); 
        stmt.setString(1, "%" + cardLeng + "_"); 
        ResultSet rs = stmt.executeQuery(); 
        rs.next(); 

        Integer custID = new Integer(rs.getInt("issue_cust_id")); 

        // execute delete in financial_txn table for resp_cd 52, 
        // 82 
        // and 59. 
        String sqldel = getThisProperty("sql.delete"); 
        PreparedStatement stmtdel = connection 
          .prepareStatement(sqldel); 
        stmtdel.setInt(1, custID); 
        stmtdel.executeUpdate(); 

        // execute update to make customer account to be Active. 
        String sqlupdateacct = getThisProperty("sql.updateacctactive"); 
        PreparedStatement stmtacctactive = connection 
          .prepareStatement(sqlupdateacct); 
        stmtacctactive.setInt(1, custID); 
        stmtacctactive.executeUpdate(); 

        // execute update to make the cust_pymt_opt status to be 
        // Pending DL Required. 
        String sqlupdate = getThisProperty("sql.updatepending"); 
        PreparedStatement stmtupdate = connection 
          .prepareStatement(sqlupdate); 
        stmtupdate.setInt(1, custID); 
        stmtupdate.executeUpdate(); 

        // execute to delete velocity limit of the account. 
        String sqldelvel = getThisProperty("sql.deletevelo"); 
        PreparedStatement stmtdelvel = connection 
          .prepareStatement(sqldelvel); 
        stmtdelvel.setInt(1, custID); 
        stmtdelvel.executeUpdate(); 

        if (!reportFile.exists()) { 
         reportFile.createNewFile(); 
        } 
        reportLog = new BufferedWriter(new FileWriter(reportFile 
          .getName(), true)); 
        reportLog.write("Run Date/Time " + dateForm.format(date) 
          + " : Cardless # " + cardLeng 
          + " has been reset to be Pending DL Required.\r\n"); 
        reportLog.flush(); 
        reportLog.close(); 

        frameLabel.setText(cardLeng 
          + " Have Been Reset To Pending DL Required"); 
        cardTextField.setText(null); 
        connection.close(); 
       } else if (cardLenP != 10 && cardLenP != 19) { 
        frameLabel.setText("Enter Valid Input"); 
        cardTextField.requestFocusInWindow(); 
       } 
       connection.close(); 
      } catch (ClassNotFoundException e) { 
       frameLabel.setText("Java Class Error"); 
      } catch (SQLException e) { 
       frameLabel.setText("No Result Found, Enter a Valid Account"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } else if (a.getSource() == cardTextField) { 
      cardTextField.setText(""); 
      frameLabel.setText(""); 
      cardTextField.requestFocusInWindow(); 
     } 
    } 

    // document to restrict cardTextField input. 
    @SuppressWarnings("serial") 
    public final class LengthRestrictedDocument extends PlainDocument { 

     private final int limit; 

     public LengthRestrictedDocument(int limit) { 
      this.limit = limit; 
     } 

     public void insertString(int offs, String str, 
       javax.swing.text.AttributeSet a) throws BadLocationException { 
      if (str == null) 
       return; 

      if ((getLength() + str.length()) <= limit) { 
       super.insertString(offs, str, a); 
      } 
     } 
    } 

    private String getThisProperty(String propKey) { 
     Properties sqlQuery = new Properties(); 
     try { 
      sqlQuery.load(new FileInputStream("sql.properties")); 
     } catch (FileNotFoundException e) { 
      System.out.println("File Not Found Error"); 
     } catch (IOException e) { 
      System.out.println("IO Exception Error"); 
     } 
     return sqlQuery.getProperty(propKey); 
    } 

    private static void launchGUI() throws ClassNotFoundException, SQLException { 

     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("QA/DEV Reset Tool - Beta"); 

     AcctReset window = new AcctReset(); 
     frame.setContentPane(window.createContentPane()); 

     Image icon = Toolkit.getDefaultToolkit().getImage("img/gep.png"); 
     frame.setIconImage(icon); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 180); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setResizable(false); 

    } 

    // Connect To DEV DB 
    @SuppressWarnings("unused") 
    private static Connection getConnectionDEV() throws ClassNotFoundException, 
      SQLException { 
     Class.forName("driver"); 
     Connection connection = DriverManager.getConnection(
       "dev url", "username", 
       "password"); 
     // frameLabel.setText("DB, connection obtained "); 
     return connection; 

    } 

    // Connect To QA DB 
    private static Connection getConnectionQA() throws ClassNotFoundException, 
      SQLException { 
     Class.forName("driver"); 
     Connection connection = DriverManager.getConnection(
       "qa url", "username", 
       "password"); 
     return connection; 

    } 

    @SuppressWarnings("unused") 
    private static ServerSocket SERVER_SOCKET; 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        SERVER_SOCKET = new ServerSocket(1354); 
        launchGUI(); 

       } catch (ClassNotFoundException e) { 
        frameLabel.setText("Java Class Error"); 
       } catch (SQLException e) { 
        frameLabel.setText("Cannot Connect To Server"); 
       } catch (IOException e) { 
        System.exit(1); 
        // JOptionPane.showMessageDialog(null,"DEV Reset Tool is already running."); 
       } 
      } 
     }); 
    } 
} 

Выше мой код и он работает. Моя проблема в том, что мне нужно будет добавить кнопки QA и DEV, которые будут по-прежнему использовать текущие кнопки cardButtonResetA и cardButtonResetP как для сред QA, так и для DEV. Хоу, я это сделаю?JButton, который будет использовать другой JButton

Кроме того, если у вас есть предложение по улучшению оптимизации кода для вышеуказанного кода, очень приветствуется.

Я просто новичок в программировании на Java. Благодарю.

+1

Пожалуйста сжать ваш треску е к соответствующим частям. – moonwave99

+0

Вы хотите вызвать/вызывать два JButtons из другого – mKorbel

+0

Существует '/ *', но где '* /'? – johnchen902

ответ

3

Q:

вы хотите звонить/ссылаться на два JButtons из другого одного

A:

Да, когда я выбрать кнопку ОК или DEV, я буду необходимо использовать существующую две кнопки, у которых есть другой вариант сброса. Благодарю.

  • см JButton.doClick(), то внутри ActionPerformed (QA or DEV button) может вызвать другую JButton (ы) с помощью их имени переменной (ов)

  • но использовать Swing Action (managable its executions by setEnabled), там вы можете вызвать бесконечные действия дерево , где ActionPerformed не выполняется в том случае, когда Swing Action установлен в setEnabled(false)

+1

Для [примера] (http://stackoverflow.com/a/5797965/230513). – trashgod

Смежные вопросы