/src/menuView/menuLogic/LoginMenuPanelLogic.java

https://gitlab.com/kirththiga/Bomberman · Java · 255 lines · 152 code · 37 blank · 66 comment · 26 complexity · 170021255cbada47aac91f7483e36126 MD5 · raw file

  1. package menuView.menuLogic;
  2. import gameplay.statemanagers.GameState;
  3. import gameplay.statemanagers.GameStateManager;
  4. import gameplay.world.Level;
  5. import gameplay.world.World;
  6. import java.awt.HeadlessException;
  7. import java.io.IOException;
  8. import javax.swing.JFrame;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.JPasswordField;
  11. import javax.swing.JTextField;
  12. import loginSystem.Account;
  13. import loginSystem.AccountManager;
  14. import loginSystem.CSVreader;
  15. import loginSystem.CSVwriter;
  16. import menuView.PanelTransitionManager;
  17. /**
  18. * @author Philip Hoddinott<philip.hoddinott@mail.mcgill.ca>
  19. * @version 1.7 (current version number of program)
  20. * @since 2014-12-1 (the version of the package this class was first added to)
  21. */
  22. /**
  23. * @author HP
  24. *
  25. */
  26. /**
  27. * @author HP
  28. *
  29. */
  30. public class LoginMenuPanelLogic {
  31. private JFrame controllingFrame;
  32. private final String csvAccountsFile = "UserPass.csv";
  33. private PanelTransitionManager manager = PanelTransitionManager.getInstance();
  34. public LoginMenuPanelLogic() {
  35. }
  36. /**
  37. * This will check that the username and password match valid accounts/ If
  38. * they match, it will return true, else it will return false and make a
  39. * frame pop up to tell the User.
  40. *
  41. * @param userNameField
  42. * @param passwordField
  43. * @return
  44. */
  45. public Boolean loginCheck(JTextField userNameField, JPasswordField passwordField) {
  46. String userName = userNameField.getText();
  47. AccountManager accountManager = new AccountManager();
  48. String csvAccountsFile = "UserPass.csv";
  49. CSVreader reader = new CSVreader();
  50. try {
  51. accountManager.setAccounts(reader.CSVreaderAccounts(csvAccountsFile));
  52. } catch (IOException e1) {
  53. e1.printStackTrace();
  54. }
  55. if (accountManager.isUser(userNameField.getText(), passwordField.getText())) {
  56. Account acc = accountManager.getAccount(userName);
  57. GameStateManager manager = GameStateManager.getInstance();
  58. GameState gameState = new GameState(acc.getUserName(), acc.getName());
  59. gameState.setWorld(new World(31, 13, Level.L1));
  60. manager.setCurrentGameState(gameState);
  61. return true;
  62. }
  63. else {
  64. /*
  65. * If the wrong username/password was entered the input feilds will
  66. * be cleared and a message will be displayed to the user informing
  67. * them of their error
  68. */
  69. JOptionPane.showMessageDialog(controllingFrame, "Wrong username or password");
  70. userNameField.setText(null);
  71. passwordField.setText(null);
  72. return false;
  73. }
  74. }
  75. /**
  76. * This checks if the username and password match the requirements.
  77. *
  78. * This sends the username and password through a string of if/else
  79. * statements. They check that all the requirements are met. If a
  80. * requirement is not met it will return false and display a controlling
  81. * frame with a message about what is wrong.
  82. *
  83. * And even more explanations to follow in consecutive paragraphs separated
  84. * by HTML paragraph breaks.
  85. *
  86. * @param This
  87. * is sent the username and password for an account the user
  88. * wants to create
  89. * @return This returns true or false, depending on the username and
  90. * password. It will also summon a controlling frame that tells the
  91. * user what is wrong
  92. *
  93. */
  94. public boolean valid(String userName, String password) throws HeadlessException, IOException {
  95. int passLength = password.length();
  96. int userLength = userName.length();
  97. boolean userValid = userName.matches("\\w+");
  98. boolean passValid = password.matches("\\w+");
  99. boolean upperLowerCase = false, passUpperCase = false, passLowerCase = false;
  100. boolean areThereNumbers = password.matches(".*\\d+.*");
  101. int i = 0;
  102. AccountManager accountManager = new AccountManager();
  103. if (userName.equals("utest")) {
  104. // THIS IS FOR DEBUGGING PURPOSES,
  105. userValid = true;
  106. passValid = true;
  107. passLength = 9;
  108. userLength = 9;
  109. upperLowerCase = true;
  110. passUpperCase = true;
  111. passLowerCase = false;
  112. return true;
  113. }
  114. else if (accountManager.isUsername(userName)) {
  115. JOptionPane.showMessageDialog(controllingFrame, "Username already exists ");
  116. return false;
  117. }
  118. else if (userLength < 6) {// checks for appropriate size
  119. JOptionPane.showMessageDialog(controllingFrame, "Username should be longer than 6 charicters, you have only " + userLength);
  120. return false;
  121. }
  122. else if (passLength < 8) {
  123. JOptionPane.showMessageDialog(controllingFrame, "Password should be longer than 8 charicters, you have only " + passLength);
  124. return false;
  125. }
  126. else if (!(userValid)) {
  127. JOptionPane.showMessageDialog(controllingFrame, "Username has non acceptable charicters");
  128. return false;
  129. }// Checks for non-appropriate characters in user
  130. for (i = 0; i < passLength - 1; i++) {
  131. upperLowerCase = Character.isUpperCase(password.charAt(i));
  132. if (upperLowerCase) {
  133. passUpperCase = true;
  134. }// end else
  135. }
  136. if (!passUpperCase) {
  137. JOptionPane.showMessageDialog(controllingFrame, "Password needs one uppercase letter");
  138. return false;
  139. }
  140. for (i = 0; i < passLength - 1; i++) {
  141. upperLowerCase = Character.isLowerCase(password.charAt(i));
  142. if (upperLowerCase) {
  143. passLowerCase = true;
  144. }// end else
  145. }
  146. if (!passLowerCase) {
  147. JOptionPane.showMessageDialog(controllingFrame, "Password needs one lowercase letter");
  148. return false;
  149. }
  150. // gets an upper or lower case
  151. else if (!areThereNumbers) {
  152. JOptionPane.showMessageDialog(controllingFrame, "Password needs one number");
  153. return false;
  154. }
  155. else if (passValid) {
  156. JOptionPane.showMessageDialog(controllingFrame, "Password must have one speical charicetr");
  157. return false;
  158. }// password needs one speical case
  159. // password must have one of each the following:upper case, lower case,
  160. // number special
  161. else {
  162. return true;
  163. }
  164. }
  165. /**
  166. * Added logic for new account and integrated the validation.
  167. *
  168. * @author Kirththiga Murugupillai
  169. * @param name
  170. * @param username
  171. * @param password
  172. * @param confirmPassword
  173. * @param score
  174. * @return
  175. */
  176. public boolean newAccountLogic(String name, String username, String password, String confirmPassword, int score) {
  177. boolean check = false;
  178. boolean validate = false;
  179. if (password.equals(confirmPassword)) {
  180. AccountManager accountManager = new AccountManager();
  181. CSVreader reader = new CSVreader();
  182. try {
  183. accountManager.setAccounts(reader.CSVreaderAccounts(csvAccountsFile));
  184. } catch (IOException e1) {
  185. e1.printStackTrace();
  186. }
  187. // This checks that all parameters have been met
  188. try {
  189. // These write to the Database, making a new account for the
  190. // user
  191. CSVwriter writer = new CSVwriter();
  192. writer.CSVwriterAccounts(csvAccountsFile, name, username, password, 0);
  193. } catch (IOException e) {
  194. e.printStackTrace();
  195. }
  196. System.out.println("Entered user is " + username + " and pass is " + password);
  197. // This makes a new gamestate for the new account It then disposes
  198. // the frame and goes to the playgameMenu
  199. GameState gameState = new GameState(username, name);
  200. GameStateManager gManager = GameStateManager.getInstance();
  201. gManager.setCurrentGameState(gameState);
  202. try {
  203. validate = valid(username, password);
  204. if (validate == true) {
  205. check = password.equals(confirmPassword);
  206. }
  207. } catch (HeadlessException e) {
  208. e.printStackTrace();
  209. } catch (IOException e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. else {
  214. JOptionPane.showMessageDialog(controllingFrame, "Password and confirm Password do not match");
  215. }
  216. return check;
  217. }
  218. }