Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class PhoneBookApplication {
  5. public static void main(String [] args){
  6. PhoneBookAdmin admin = new PhoneBookAdmin();
  7. NormalUser user = new NormalUser();
  8.  
  9. //setuservariables method reads from a file and returns a string array
  10. //that has the values for each data field
  11. //Then set these values to the data field for normaluser
  12. String [] uservariables = new String [3];
  13. setuservariables(uservariables);
  14. user.setId(uservariables[0]);
  15. user.setUsername(uservariables[1]);
  16. user.setPassword(uservariables[2]);
  17. //Normal user has four data fields. The first three data fields are set
  18. //Now the data field for directory must be set
  19. //so make a new instance of object phonebookdirectory and set that to the data field in user
  20. PhoneBookDirectory dir = new PhoneBookDirectory();
  21. user.setDirectory(dir);
  22. //this runs the method that fills the directory with default instances of the object
  23. user.getuserDirectory().createdirectory();
  24.  
  25. //same thing as above but for admin
  26. String [] adminuservariables = new String [3];
  27. setadminuservariables(adminuservariables);
  28. admin.setEmailaddress(uservariables[0]);
  29. admin.setUsername(uservariables[1]);
  30. admin.setPassword(uservariables[2]);
  31.  
  32. Scanner input = new Scanner (System.in);
  33. System.out.println("Input Username");
  34. String inputusername = input.next();
  35. System.out.println("Input password");
  36. String inputpassword = input.next();
  37. input.close();
  38.  
  39. if (inputusername.equals(user.getUsername())){
  40. if (inputpassword.equals(user.getPassword())){
  41. System.out.println("Correct username and password for normal user");
  42. System.out.println("Successful login");
  43. usermenu(user);
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. public static void setuservariables(String [] uservariables){
  53. try {
  54. Scanner sc = new Scanner(new File("NormalUser.txt"));
  55. // Separate by commas
  56. sc.useDelimiter(",");
  57.  
  58. while (sc.hasNext()) {
  59. uservariables[0] = sc.next();
  60. uservariables[1] = sc.next();
  61. uservariables[2] = sc.next();
  62. }
  63. sc.close();
  64. }
  65.  
  66. catch (FileNotFoundException e) {
  67. e.printStackTrace();
  68. }
  69.  
  70. }
  71.  
  72. public static void setadminuservariables(String [] uservariables){
  73. try {
  74. Scanner sc = new Scanner(new File("AdminUser.txt"));
  75. // Separate by commas
  76. sc.useDelimiter(",");
  77.  
  78. while (sc.hasNext()) {
  79. uservariables[0] = sc.next();
  80. uservariables[1] = sc.next();
  81. uservariables[2] = sc.next();
  82. }
  83. sc.close();
  84. }
  85.  
  86. catch (FileNotFoundException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. public static void usermenu(NormalUser user){
  92. int loop = 1;
  93. //print menu
  94. Scanner in = new Scanner (System.in);
  95. while (loop == 1){
  96. System.out.println("1. Add Entry");
  97. System.out.println("2. Edit Entry");
  98. System.out.println("3. Sort PhoneBook Directory");
  99. System.out.println("4. Linear Search");
  100. System.out.println("5. Print user info");
  101. System.out.println("6. Logout");
  102. System.out.println("Input selection");
  103. int selection = 0;
  104. selection = in.nextInt();
  105. if (selection == 1){
  106. System.out.println("Input new entry to add");
  107. System.out.println("Input id");
  108. int ID = in.nextInt();
  109. System.out.println("Input first name");
  110. String first_name = in.next();
  111. System.out.println("Input last name");
  112. String last_name = in.next();
  113. System.out.println("Input email");
  114. String email = in.next();
  115. System.out.println("Input zipcode");
  116. int zipcode = in.nextInt();
  117. System.out.println("Input number");
  118. int number = in.nextInt();
  119. PhoneBookEntry Entry = new PhoneBookEntry(ID, first_name, last_name, email, zipcode, number);
  120. System.out.println(user.AddPhoneEntry(Entry));
  121. }
  122. else if (selection == 2){
  123. System.out.println("Input first name of user you want to edit");
  124. String fname = in.next();
  125. System.out.println("Input last name of user you want to edit");
  126. String lname = in.next();
  127. System.out.println(user.EditPhoneEntryFnameLname(fname, lname));
  128. }
  129. else if (selection == 3){
  130. user.SortPhoneBookDirectory();
  131. }
  132. else if (selection == 4){
  133. System.out.println("Input phonenumber you want to serach");
  134. int Phonenumber = in.nextInt();
  135. System.out.println(user.LinearSearch(Phonenumber));
  136. }
  137. else if (selection == 5){
  138. user.PrintUserInfo();
  139. }
  140. else if (selection == 6){
  141. System.out.println("Logout Successful");
  142. break;
  143. }
  144.  
  145. }
  146. in.close();
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement