Guest User

Untitled

a guest
Apr 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Project2
  6. {
  7.  
  8.  
  9. public static void main(String [] args) throws IOException
  10. {
  11. String userName,password;
  12. String account = "";
  13. String optionA = "y";
  14. String optionB = "n";
  15. File accountInfo = new File ("Users.txt");
  16. FileWriter usersInfo = new FileWriter(accountInfo,true);
  17. PrintWriter edit = new PrintWriter(usersInfo);
  18. String filePath = ("Users.txt");
  19. String errorMessage1 = usersInfo + " not found";
  20. String errorMessage2 = "No users registered";
  21.  
  22. if (!(accountInfo.exists()))
  23. JOptionPane.showMessageDialog(null,errorMessage1);
  24. else if (accountInfo.length() == 0)
  25. JOptionPane.showMessageDialog(null,errorMessage2);
  26. else
  27. {
  28. createUser();
  29. }
  30. edit.close();
  31. }
  32.  
  33. public static void createUser() throws IOException
  34. {
  35. String userName,password;
  36. String account = "";
  37. String optionA = "y";
  38. String optionB = "n";
  39. File accountInfo = new File ("Users.txt");
  40. FileWriter usersInfo = new FileWriter(accountInfo,true);
  41. PrintWriter edit = new PrintWriter(usersInfo);
  42. String filePath = ("Users.txt");
  43.  
  44. while(!account.equals(optionA)&& !account.equals(optionB))
  45. {
  46. account = JOptionPane.showInputDialog(null,"Do you have an existing account? Y/N");
  47. account = account.toLowerCase();
  48. if (account.equals(optionA))
  49. {
  50. userName = JOptionPane.showInputDialog(null,"Enter username");
  51. password = JOptionPane.showInputDialog(null,"Enter password");
  52. verifyLogin(userName,password,filePath);
  53. }
  54. else if (account.equals(optionB))
  55. {
  56. userName = JOptionPane.showInputDialog(null,"Please enter your desired username");
  57. password = JOptionPane.showInputDialog(null,"Please enter your desired password");
  58. String freshUser = userName + "," + password;
  59. edit.println(freshUser);
  60. }
  61. else
  62. JOptionPane.showMessageDialog(null,"Please enter either Y or N");
  63. }
  64. edit.close();
  65. }
  66.  
  67. public static void verifyLogin(String username, String password, String usersInfo)
  68. {
  69. String line;
  70. String [] parts;
  71. try(BufferedReader br = new BufferedReader(new FileReader(usersInfo)))
  72. {
  73. line = br.readLine();
  74. parts = line.split(",");
  75. if(username.equals(parts[0]) && password.equals(parts[1])) //first line in the Users.txt is reserved for admin only
  76. {
  77. JOptionPane.showMessageDialog(null,"Welcome administrator " + username);
  78. administrator();
  79. }
  80. else
  81. {
  82. while((line = br.readLine()) != null)
  83. {
  84. parts = line.split(",");
  85. if(username.equals(parts[0]) && password.equals(parts[1]))
  86. {
  87. JOptionPane.showMessageDialog(null,"Welcome user " + username);
  88. user();
  89. }
  90. }
  91. JOptionPane.showMessageDialog(null,"User not found");
  92. createUser();
  93. }
  94. }
  95. catch (Exception a)
  96. {
  97. JOptionPane.showMessageDialog(null,"Unknown error occured");
  98. }
  99. }
  100.  
  101. public static void administrator() throws IOException
  102. {
  103. String A = "Register a new user";
  104. String B = "Add a new facility";
  105. String C = "View availability of a facility";
  106. String D = "View bookinngs";
  107. String E = "Remove an existing facility";
  108. String F = "Make bookings";
  109. String G = "Record payments";
  110. String H = "View account statements";
  111. String[] choices = { A, B, C, D, E, F, G, H };
  112.  
  113. String input = (String) JOptionPane.showInputDialog(null, "Choose action",
  114. "Actions available", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
  115.  
  116. if (input.equals(A))
  117. registerANewUser();
  118. else if (input.equals(B))
  119. System.out.print("it works"); //addFacility();
  120. else if (input.equals(C))
  121. System.out.print("it works"); //viewFacility();
  122. else if (input.equals(D))
  123. System.out.print("it works"); //viewBookings();
  124. else if (input.equals(E))
  125. System.out.print("it works"); //remove();
  126. else if (input.equals(F))
  127. System.out.print("it works"); //makeBookings();
  128. else if (input.equals(G))
  129. System.out.print("it works"); //recordPayments();
  130. else if (input.equals(H))
  131. System.out.print("it works"); //viewAccountStatements();
  132. }
  133.  
  134. public static void user()
  135. {
  136. String A = "View bookinngs";
  137. String B = "View account statements";
  138. String[] choices = { A, B };
  139.  
  140. String input = (String) JOptionPane.showInputDialog(null, "Choose action",
  141. "Actions available", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
  142.  
  143. if (input.equals(A))
  144. System.out.print("it works"); //viewBookings();
  145. else if (input.equals(B))
  146. System.out.print("it works"); //viewAccountStatements();
  147. }
  148.  
  149. public static void registerANewUser() throws IOException
  150. {
  151. String userName,password;
  152. File accountInfo = new File ("Users.txt");
  153. FileWriter usersInfo = new FileWriter(accountInfo,true);
  154. PrintWriter edit = new PrintWriter(usersInfo);
  155.  
  156. userName = JOptionPane.showInputDialog(null,"Please enter your desired username");
  157. password = JOptionPane.showInputDialog(null,"Please enter your desired password");
  158. String freshUser = userName + "," + password;
  159. edit.println(freshUser);
  160. edit.close();
  161. administrator();
  162. }
  163.  
  164.  
  165. }
Add Comment
Please, Sign In to add comment