Advertisement
Guest User

Untitled

a guest
Apr 7th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintWriter;
  5. import java.util.*;
  6. import java.util.ArrayList;
  7.  
  8.  
  9. public class Driver {
  10.  
  11.  
  12. static ArrayList<Driver> userInfoArray = new ArrayList<Driver>();
  13. public String username;
  14. public String password;
  15.  
  16. public Driver (){
  17.  
  18. }
  19.  
  20. public Driver (String args_username, String args_password){
  21. username = args_username;
  22. password = args_password;
  23. }
  24.  
  25. // getter methods
  26. public String getUserName(){
  27. return username;
  28. }
  29.  
  30. public String getPassword(){
  31. return password;
  32. }
  33.  
  34. // setter methods
  35. public void setUserName(String args_username){
  36. username = args_username;
  37. }
  38.  
  39. public void setPassword(String args_password){
  40. password = args_password;
  41. }
  42.  
  43. public static void main(String[] args) {
  44.  
  45. //load();
  46.  
  47. Driver d = new Driver();
  48.  
  49. Scanner inputChoice = new Scanner(System.in);
  50. Scanner inputUserName = new Scanner(System.in);
  51. Scanner inputPassword = new Scanner(System.in);
  52.  
  53. System.out.println("*** Welcome to HR Management System. ***" + "\n" + "\n" + "\n");
  54. System.out.println("If you have an existing account 1" + "\n");
  55. System.out.println("If you want to create a new account 2" + "\n");
  56. System.out.println("Exit Program X" + "\n");
  57.  
  58. Scanner menuValue = new Scanner(System.in);
  59. String enteredValue = "";
  60.  
  61.  
  62. do {
  63.  
  64. System.out.println("Enter a value");
  65. enteredValue = menuValue.next();
  66.  
  67. switch (enteredValue) {
  68. case "1":
  69. System.out.println("Existing Account");
  70.  
  71. System.out.println("Enter the username: ");
  72. d.username = inputUserName.nextLine();
  73.  
  74. if (d.username.isEmpty()) {
  75. System.out.println("\nNothing was entered for the UserName, Please try again.");
  76. d.username = inputUserName.nextLine();
  77. }
  78.  
  79. if (!(d.username.isEmpty()) && d.userNameDuplicate(d.username)){
  80. System.out.println("Enter the password: ");
  81. d.password = inputPassword.nextLine();
  82.  
  83. //check if password is correct
  84. System.out.println(d.getUserInfo(d.username).getPassword());
  85. if (d.password.equals(d.getUserInfo(d.username).getPassword())){
  86. // password matches
  87. System.out.println("You are logged in");
  88. } else {
  89. System.out.println("Password does not match ");
  90. }
  91.  
  92. } else {
  93. System.out.println("This username does not exist");
  94. }
  95.  
  96.  
  97. break;
  98. case "2":
  99. System.out.println("Creating Account");
  100.  
  101. System.out.println("Enter the username: ");
  102. d.username = inputUserName.nextLine();
  103.  
  104. if (d.username.isEmpty()) {
  105. System.out.println("\nNothing was entered for the UserName, Please try again.");
  106. d.username = inputUserName.nextLine();
  107. }
  108.  
  109. //check for username duplicity
  110. if (!(d.username.isEmpty()) && !(d.userNameDuplicate(d.username))){
  111. //continue
  112.  
  113. System.out.println("Enter the password: ");
  114. d.password = inputPassword.nextLine();
  115. } else {
  116. System.out.println("UserName already exist, Choose another User Name");
  117. }
  118.  
  119. //adding user details to the array list
  120. Driver d1 = new Driver(d.username,d.password);
  121. d.userInfoArray.add(d1);
  122. break;
  123.  
  124. case "X":
  125. case "x":
  126. /*
  127. try{
  128. FileWriting();
  129. System.out.println("Save and Exit");
  130. } catch (FileNotFoundException fe){
  131. //throw OfferException("File Not Found");
  132. System.out.println("File Not Found" );
  133. }
  134. */ }
  135.  
  136. } while (!(enteredValue.toUpperCase().equals("X")));
  137.  
  138.  
  139. }
  140.  
  141.  
  142. //checks if the username exists in the arraylist
  143.  
  144. public boolean userNameDuplicate(String arg_username) {
  145.  
  146. boolean userNameFound = false;
  147.  
  148. //System.out.println("In if duplicate check Array Size"+userInfoArray.size());
  149.  
  150. for (int i = 0; i < userInfoArray.size(); i++) {
  151.  
  152. String currentUserName = ((Driver) userInfoArray.get(i)).getUserName();
  153.  
  154. if (arg_username.toUpperCase().equals(currentUserName.toUpperCase())) {
  155.  
  156. userNameFound = true;
  157. break;
  158.  
  159. } else {
  160. userNameFound = false;
  161. }
  162. }
  163.  
  164. return userNameFound;
  165. }
  166.  
  167.  
  168. public Driver getUserInfo(String arg_username) {
  169. for (Driver d0 : userInfoArray) {
  170. if (d0.getUserName().toUpperCase().equals(arg_username.toUpperCase())) {
  171. return d0;
  172. }
  173. }
  174.  
  175. return null;
  176. }
  177.  
  178.  
  179. /*Writing arraylist into a file
  180. *
  181. */
  182. public static void FileWriting() throws FileNotFoundException {
  183. // TODO Auto-generated method stub
  184. String filename = "backup.txt"; // path & filename
  185. //C:\Users\Dhiraj\workspace\Sem2\SEF
  186. PrintWriter outputStream = null;
  187.  
  188. try {
  189. // create a new object of the printWriter class & assign to the
  190. // Object variable
  191. outputStream = new PrintWriter(new FileOutputStream(filename));
  192.  
  193. for (Driver d0 : userInfoArray) {
  194. outputStream.print(d0.getUserName()+ ",");
  195. outputStream.print(d0.getPassword());
  196. outputStream.println();
  197. }
  198. System.out.println("Yeah! File was written to without error");
  199.  
  200. } catch (FileNotFoundException e) {
  201. // display the inbuilt error message belonging to e object
  202. System.out.println(e);
  203. } // end try catch
  204. outputStream.close(); // close the stream
  205. }
  206.  
  207.  
  208. //load values from the txt file
  209. private static void load() {
  210. Scanner inputStream = null; // create object variable
  211.  
  212. try { // create Scanner object & assign to variable
  213. inputStream = new Scanner(new File("backup.txt"));
  214. inputStream.useDelimiter(",");
  215. } catch (FileNotFoundException e) {
  216. System.out.println("No User Infomation was Loaded");
  217. //System.exit(0);
  218. return;
  219. }
  220.  
  221. try{
  222. while (inputStream.hasNext()) {
  223. System.out.println(inputStream.toString());
  224. String readUserName = inputStream.next();
  225. String readPassword = inputStream.next();
  226.  
  227. Driver d1 = new Driver(readUserName, readPassword);
  228.  
  229. userInfoArray.add(d1);
  230.  
  231. }
  232. } catch (IllegalStateException e){
  233. System.err.println("Could not read from the file");
  234. } catch (InputMismatchException e){
  235. System.err.println("Something wrong happened while reading the file");
  236. }
  237. inputStream.close();
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement