Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. /**import codes **/
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.RandomAccessFile;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javax.swing.JOptionPane;
  11.  
  12. /**body codes **/
  13. /**dont forget to change the file loc **/
  14. /**field names are un,pw,em **/
  15. /**generate the data first then erase the first lines :> **/
  16.  
  17. File f = new File("C:\\Users\\Ivan Louise\\Documents\\Accounts");
  18. int ln;
  19. String username,password,email;
  20.  
  21. public Login() {
  22. initComponents();
  23. }
  24. void createFolder(){
  25. if(!f.exists()){
  26. f.mkdirs();
  27. }
  28. }
  29.  
  30. void readFile(){
  31. try {
  32. FileReader fr = new FileReader(f+"\\database.text");
  33. System.err.println("connected to database");
  34. } catch (FileNotFoundException ex) {
  35. try {
  36. FileWriter fw = new FileWriter(f+"\\database.text");
  37. System.out.println("File created");
  38. } catch (IOException ex1) {
  39. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex1);
  40. }
  41. }
  42.  
  43. }
  44.  
  45. void addData(String user,String pass,String email){
  46. RandomAccessFile raf;
  47. try {
  48. raf = new RandomAccessFile(f+"\\database.text","rw");
  49. for(int i=0;i<ln;i++){
  50. raf.readLine();
  51. }
  52. raf.writeBytes("\r\n");
  53. raf.writeBytes("\r\n");
  54. raf.writeBytes("Username:"+user+"\r\n");
  55. raf.writeBytes("Password:"+pass+"\r\n");
  56. raf.writeBytes("Email:"+email);
  57. JOptionPane.showMessageDialog(null, "Registered Successfully");
  58.  
  59. } catch (FileNotFoundException ex) {
  60. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  61. } catch (IOException ex) {
  62. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  63. }
  64.  
  65.  
  66. }
  67.  
  68. void CheckData(String user,String pass){
  69. try {
  70. RandomAccessFile raf = new RandomAccessFile(f+"\\database.text", "rw");
  71. String line = raf.readLine();
  72. username=line.substring(9);
  73. password=raf.readLine().substring(9);
  74. email=raf.readLine().substring(6);
  75. if(user.equals(username)&password.equals(password)){
  76. JOptionPane.showMessageDialog(null, "Pasword matched");
  77. }
  78. else{
  79. JOptionPane.showMessageDialog(null, "invalid username or password");
  80. }
  81.  
  82. } catch (FileNotFoundException ex) {
  83. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  84. } catch (IOException ex) {
  85. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  86. }
  87. }
  88.  
  89. void logic(String user,String pass){
  90. try {
  91. RandomAccessFile raf= new RandomAccessFile(f+"\\database.text", "rw");
  92. for(int i=0;i<ln;i+=4){
  93.  
  94. String forUser = raf.readLine().substring(9);
  95. String forPswd = raf.readLine().substring(9);
  96. if(user.equals(forUser) & pass.equals(forPswd)){
  97. JOptionPane.showMessageDialog(null, "Log in successfully");
  98. break;
  99. }else if(i==(ln-4)){
  100. JOptionPane.showMessageDialog(null, "login failed");
  101. break;
  102. }
  103. for(int k=1;k<=2;k++){
  104. raf.readLine();
  105. }
  106. }
  107. } catch (FileNotFoundException ex) {
  108. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  109. } catch (IOException ex) {
  110. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  111. }
  112.  
  113. }
  114.  
  115. void countLines(){
  116. try {
  117. ln=1;
  118. RandomAccessFile raf = new RandomAccessFile(f+"\\database.text", "rw");
  119. for(int i=0;raf.readLine()!=null;i++){
  120. ln++;
  121. }
  122. System.out.println("number of lines:"+ln);
  123. } catch (FileNotFoundException ex) {
  124. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  125. } catch (IOException ex) {
  126. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  127. }
  128. }
  129.  
  130.  
  131.  
  132. /**button codes **/
  133. private void registerActionPerformed(java.awt.event.ActionEvent evt) {
  134. createFolder();
  135. readFile();
  136. countLines();
  137. addData(un.getText(),pw.getText(),em.getText());
  138. }
  139.  
  140. private void clearActionPerformed(java.awt.event.ActionEvent evt) {
  141. un.setText("");
  142. pw.setText("");
  143. em.setText("");
  144. }
  145.  
  146. private void loginActionPerformed(java.awt.event.ActionEvent evt) {
  147. createFolder();
  148. readFile();
  149. countLines();
  150. logic(un.getText(),pw.getText());
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement