Guest User

Untitled

a guest
Nov 5th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.StringTokenizer;
  8.  
  9. import javax.swing.*;
  10.  
  11. public class Login extends JFrame{
  12. private JButton botao;
  13. private JLabel nome, pass;
  14. private JComboBox box;
  15. private String list[] = {"",""};
  16. private JTextField n;
  17. private JPasswordField camp;
  18. private Container cont;
  19.  
  20. public Login(){
  21. super("Login");
  22. cont = getContentPane();
  23. cont.setLayout(new FlowLayout());
  24.  
  25. nome = new JLabel ("Username: ");
  26. cont.add(nome);
  27. n = new JTextField(15);
  28. cont.add(n);
  29.  
  30. pass = new JLabel ("Password: ");
  31. cont.add(pass);
  32. camp = new JPasswordField(15);
  33. cont.add(camp);
  34.  
  35. botao = new JButton("Entrar");
  36. botao.setHorizontalTextPosition(SwingConstants.CENTER);
  37. cont.add(botao);
  38. TrataEvento tv = new TrataEvento();
  39. botao.addActionListener(tv);
  40.  
  41. getContentPane().setBackground(Color.CYAN);
  42. setSize(300, 150);
  43. setVisible(true);
  44. }
  45.  
  46. public boolean ler(){
  47. String pass, umaLinha = "";
  48. StringTokenizer umaCadeia;
  49.  
  50. try{
  51. FileReader fr = new FileReader("Password.txt");
  52. BufferedReader br = new BufferedReader(fr);
  53.  
  54. umaLinha = br.readLine();
  55. while(umaLinha != null){
  56. umaCadeia = new StringTokenizer(umaLinha);
  57. pass = umaCadeia.nextToken();
  58. }
  59.  
  60. br.readLine();
  61.  
  62. }catch(NumberFormatException e){
  63.  
  64. e.getMessage();
  65.  
  66. }catch(FileNotFoundException f) {
  67.  
  68. f.getMessage();
  69.  
  70. }catch(IOException t){
  71.  
  72. t.getMessage();
  73.  
  74. }
  75.  
  76. return true;
  77. }
  78.  
  79.  
  80. private class TrataEvento implements ActionListener{
  81. public void actionPerformed(ActionEvent ev)
  82. {
  83.  
  84. UserDataFileReader dataFileReader = new UserDataFileReader(UserData.FILES_DATA);
  85.  
  86. UserData successfulUserData = dataFileReader.getUserData();
  87.  
  88. UserData data = new UserData(n.getText(), camp.getText());
  89.  
  90. if(data.equals(successfulUserData)){
  91.  
  92. JOptionPane.showMessageDialog(null, "Username e Password correcta!");
  93.  
  94. }else{
  95.  
  96. JOptionPane.showMessageDialog(null, "Username ou Password errada!\nTente novamente.");
  97.  
  98. }
  99.  
  100. }
  101. }
  102.  
  103. public static void main (String [] args){
  104. Login l = new Login();
  105. l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  106. }
  107. }
  108.  
  109. class UserData{
  110. private String username;
  111. private String password;
  112.  
  113. public static String FILES_DATA = "Password.txt";
  114.  
  115. public UserData(String username, String password){
  116. this.username = username;
  117. this.password = password;
  118. }
  119.  
  120. public String getUserName(){
  121. return username;
  122. }
  123.  
  124. public String getPassword(){
  125. return password;
  126. }
  127.  
  128. public void setUserName(String username){
  129. this.username = username;
  130. }
  131.  
  132. public void setPassword(String password){
  133. this.password = password;
  134. }
  135.  
  136. public static UserData getUserData(){
  137. return new UserData("Nocaida", "003Open");
  138. }
  139.  
  140. public boolean equals(Object obj){
  141. if(!(obj instanceof UserData)){
  142. return false;
  143. }
  144.  
  145. UserData userData = (UserData)obj;
  146. return userData.getUserName().equals(this.getUserName()) && userData.getPassword().equals(this.getPassword());
  147. }
  148.  
  149. //So para mock;
  150. //So para testes, não é necessario ler o ficheiro
  151. public static UserData DEFAULT_USER_DATA = new UserData("Nocaida", "003Open");
  152. }
  153.  
  154. class UserDataFileReader{
  155. private String filename;
  156.  
  157. public UserDataFileReader(String filename){
  158. this.filename = filename;
  159. }
  160.  
  161. public UserData getUserData()
  162. {
  163. try{
  164. String lines = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(filename)));
  165.  
  166. return new UserData(lines.split(";")[0], lines.split(";")[1]);
  167. }catch(Exception ex){
  168.  
  169. ex.printStackTrace();
  170. return UserData.DEFAULT_USER_DATA;
  171.  
  172. }
  173. }
  174. }
Add Comment
Please, Sign In to add comment