C:\Users\Man\Desktop\IA_FinaL_Submission_000307-0092\Product\IA_Netbeans_Project\src\ia\man\Register.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package ia.man;
  7 
  8 import java.awt.Dimension;
  9 import java.awt.Toolkit;
 10 import java.io.BufferedReader;
 11 import java.io.File;
 12 import java.io.FileNotFoundException;
 13 import java.io.FileReader;
 14 import java.io.FileWriter;
 15 import java.io.IOException;
 16 import java.util.logging.Level;
 17 import java.util.logging.Logger;
 18 import javax.swing.JOptionPane;
 19 
 20 /**
 21  *
 22  * @author 14868
 23  */
 24 public class Register extends javax.swing.JFrame {
 25 
 26     /**
 27      * Creates new form Register
 28      */
 29     public Register() {
 30         initComponents();
 31         Toolkit toolkit = getToolkit();
 32         Dimension size = toolkit.getScreenSize();
 33         setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
 34         setDefaultCloseOperation(Register.DISPOSE_ON_CLOSE); //Close only this form
 35     }
 36 
 37     /**
 38      * This method is called from within the constructor to initialize the form.
 39      * WARNING: Do NOT modify this code. The content of this method is always
 40      * regenerated by the Form Editor.
 41      */
 42     @SuppressWarnings("unchecked")
 43     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
 44     private void initComponents() {
 45 
 46         jPopupMenu1 = new javax.swing.JPopupMenu();
 47         nameLabel = new javax.swing.JLabel();
 48         nameTF = new javax.swing.JTextField();
 49         emailLabel = new javax.swing.JLabel();
 50         emailTF = new javax.swing.JTextField();
 51         passwordLabel = new javax.swing.JLabel();
 52         confirmPasswordLabel = new javax.swing.JLabel();
 53         registerButton = new javax.swing.JButton();
 54         passwordField = new javax.swing.JPasswordField();
 55         confirmPasswordField = new javax.swing.JPasswordField();
 56         jLabel1 = new javax.swing.JLabel();
 57 
 58         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 59 
 60         nameLabel.setText("Name");
 61 
 62         nameTF.addActionListener(new java.awt.event.ActionListener() {
 63             public void actionPerformed(java.awt.event.ActionEvent evt) {
 64                 nameTFActionPerformed(evt);
 65             }
 66         });
 67 
 68         emailLabel.setText("Email");
 69 
 70         emailTF.addActionListener(new java.awt.event.ActionListener() {
 71             public void actionPerformed(java.awt.event.ActionEvent evt) {
 72                 emailTFActionPerformed(evt);
 73             }
 74         });
 75 
 76         passwordLabel.setText("Password");
 77 
 78         confirmPasswordLabel.setText("Confirm Password");
 79 
 80         registerButton.setText("Register");
 81         registerButton.addMouseListener(new java.awt.event.MouseAdapter() {
 82             public void mouseReleased(java.awt.event.MouseEvent evt) {
 83                 registerButtonMouseReleased(evt);
 84             }
 85             public void mouseClicked(java.awt.event.MouseEvent evt) {
 86                 registerButtonMouseClicked(evt);
 87             }
 88         });
 89 
 90         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
 91         jLabel1.setText("Register");
 92 
 93         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 94         getContentPane().setLayout(layout);
 95         layout.setHorizontalGroup(
 96             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 97             .addGroup(layout.createSequentialGroup()
 98                 .addGap(63, 63, 63)
 99                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
100                     .addComponent(confirmPasswordLabel)
101                     .addComponent(emailLabel)
102                     .addComponent(nameLabel)
103                     .addComponent(passwordLabel))
104                 .addGap(18, 18, 18)
105                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
106                     .addGroup(layout.createSequentialGroup()
107                         .addComponent(jLabel1)
108                         .addGap(0, 0, Short.MAX_VALUE))
109                     .addGroup(layout.createSequentialGroup()
110                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
111                             .addComponent(confirmPasswordField)
112                             .addComponent(emailTF, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
113                             .addComponent(nameTF)
114                             .addComponent(passwordField))
115                         .addGap(118, 118, 118))
116                     .addGroup(layout.createSequentialGroup()
117                         .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
118                         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
119         );
120         layout.setVerticalGroup(
121             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
122             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
123                 .addGap(38, 38, 38)
124                 .addComponent(jLabel1)
125                 .addGap(18, 18, 18)
126                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
127                     .addComponent(nameTF, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
128                     .addComponent(nameLabel))
129                 .addGap(11, 11, 11)
130                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
131                     .addComponent(emailTF, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
132                     .addComponent(emailLabel))
133                 .addGap(10, 10, 10)
134                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
135                     .addComponent(passwordLabel)
136                     .addComponent(confirmPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE))
137                 .addGap(11, 11, 11)
138                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
139                     .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
140                     .addComponent(confirmPasswordLabel))
141                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
142                 .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
143                 .addGap(59, 59, 59))
144         );
145 
146         pack();
147     }// </editor-fold>                        
148 
149     private void nameTFActionPerformed(java.awt.event.ActionEvent evt) {                                       
150         // TODO add your handling code here:
151     }                                      
152 
153     private void emailTFActionPerformed(java.awt.event.ActionEvent evt) {                                        
154         // TODO add your handling code here:
155     }                                       
156 
157     private void registerButtonMouseReleased(java.awt.event.MouseEvent evt) {                                             
158         try {
159             // TODO add your handling code here:
160             storeData();
161         } catch (IOException ex) {
162             Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
163         }
164     }                                            
165 
166     private void registerButtonMouseClicked(java.awt.event.MouseEvent evt) {                                            
167         // TODO add your handling code here:
168         
169     }                                           
170 
171     /**
172      * @param args the command line arguments
173      */
174     
175     public static void main(String args[]) {
176         /* Set the Nimbus look and feel */
177         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
178         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
179          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
180          */
181         try {
182             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
183                 if ("Nimbus".equals(info.getName())) {
184                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
185                     break;
186                 }
187             }
188         } catch (ClassNotFoundException ex) {
189             java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
190         } catch (InstantiationException ex) {
191             java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
192         } catch (IllegalAccessException ex) {
193             java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
194         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
195             java.util.logging.Logger.getLogger(Register.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
196         }
197         //</editor-fold>
198         //</editor-fold>
199         
200         
201 
202         /* Create and display the form */
203         java.awt.EventQueue.invokeLater(new Runnable() {
204             public void run() {
205                 new Register().setVisible(true);
206             }
207         });
208     }
209  
210     public void storeData() throws FileNotFoundException, IOException
211     {
212         String path = "User.txt";
213         
214         String nameRegister = nameTF.getText();
215         String emailRegister = emailTF.getText();
216         String passwordRegister = passwordField.getText();
217         String confirmPasswordRegister = confirmPasswordField.getText();
218         
219         BufferedReader reader = new BufferedReader(new FileReader(path));
220         int id = 0;
221         while (reader.readLine() != null) id++;
222         id += 1;
223         reader.close();
224         
225         if(passwordRegister.equals(confirmPasswordRegister)){
226             int n = JOptionPane.showConfirmDialog(
227             null,
228             "Would you like to register?",
229             "",
230             JOptionPane.YES_NO_OPTION);
231         //System.out.print(n); // Use n for response
232             if(n==0)
233         {
234             File file = new File(path);
235             FileWriter writer;
236             try {               
237                     writer = new FileWriter(file, true);  //True = Append to file, false = Overwrite
238                     writer.write(String.valueOf(id));
239                     writer.write("|"); 
240                     writer.write(nameRegister);
241                     writer.write("|"); 
242                     writer.write(emailRegister);
243                     writer.write("|");
244                     writer.write(passwordRegister);
245                     writer.write("\n");
246                     writer.close();
247                     System.out.println("Write success!");       
248                 } catch (IOException e) {
249                     // TODO Auto-generated catch block
250                     e.printStackTrace();
251                 }
252         }
253         } else {
254             int m = JOptionPane.showConfirmDialog(
255             null,
256             "Your passwords do not match",
257             "Please re-enter your password and confirm it correctly",
258             JOptionPane.YES_OPTION);
259         }
260         
261         
262     }
263     
264     // Variables declaration - do not modify                     
265     private javax.swing.JPasswordField confirmPasswordField;
266     private javax.swing.JLabel confirmPasswordLabel;
267     private javax.swing.JLabel emailLabel;
268     private javax.swing.JTextField emailTF;
269     private javax.swing.JLabel jLabel1;
270     private javax.swing.JPopupMenu jPopupMenu1;
271     private javax.swing.JLabel nameLabel;
272     private javax.swing.JTextField nameTF;
273     private javax.swing.JPasswordField passwordField;
274     private javax.swing.JLabel passwordLabel;
275     private javax.swing.JButton registerButton;
276     // End of variables declaration                   
277 }
278