Guest User

Untitled

a guest
Jul 25th, 2015
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. import java.util.*;
  2. class Entry{
  3. private String first;
  4. private String last;
  5. private String address;
  6. private String email;
  7. Entry(String fir, String las, String adrs, String mail){
  8. first = fir;
  9. last = las;
  10. address = adrs;
  11. email = mail;
  12. }
  13. Entry(){
  14. first = last = address = email = null;
  15. }
  16. public void displayEntries(){
  17. System.out.println("====================================");
  18. System.out.println("First Name: "+first);
  19. System.out.println("Last Name: "+last);
  20. System.out.println("Address: "+address);
  21. System.out.println("Email: "+email);
  22. System.out.println("====================================");
  23. }
  24. public void addEntry(int n, String value){
  25. switch (n){
  26. case 1:
  27. first = value;
  28. break;
  29. case 2:
  30. last = value;
  31. break;
  32. case 3:
  33. address = value;
  34. break;
  35. case 4:
  36. email = value;
  37. break;
  38. }
  39. }
  40. public void removeEntry(int n){
  41. switch (n){
  42. case 1:
  43. first = null;
  44. break;
  45. case 2:
  46. last = null;
  47. break;
  48. case 3:
  49. address = null;
  50. break;
  51. case 4:
  52. email = null;
  53. break;
  54. }
  55. }
  56. public void editEntry(int n, String value){
  57. switch (n){
  58. case 1:
  59. first = value;
  60. break;
  61. case 2:
  62. last = value;
  63. break;
  64. case 3:
  65. address = value;
  66. break;
  67. case 4:
  68. email = value;
  69. break;
  70. }
  71. }
  72. private void sort(int n, ArrayList<Entry> a){
  73. switch (n){
  74. case 1:
  75. for (int i = 0;i<a.size();i++){
  76. for (int j = 0;j<a.size();j++)
  77. if (a.get(j).first.compareTo(a.get(i).first)>0){
  78. Entry temp = a.get(i);
  79. a.set(i, a.get(j));
  80. a.set(j, temp);
  81. }
  82. }
  83. break;
  84. case 2:
  85. for (int i = 0;i<a.size();i++){
  86. for (int j = 0;j<a.size();j++)
  87. if (a.get(j).last.compareTo(a.get(i).last)>0){
  88. Entry temp = a.get(i);
  89. a.set(i, a.get(j));
  90. a.set(j, temp);
  91. }
  92. }
  93. break;
  94. case 3:
  95. for (int i = 0;i<a.size();i++){
  96. for (int j = 0;j<a.size();j++)
  97. if (a.get(j).address.compareTo(a.get(i).address)>0){
  98. Entry temp = a.get(i);
  99. a.set(i, a.get(j));
  100. a.set(j, temp);
  101. }
  102. }
  103. break;
  104. case 4:
  105. for (int i = 0;i<a.size();i++){
  106. for (int j = 0;j<a.size();j++)
  107. if (a.get(j).email.compareTo(a.get(i).email)>0){
  108. Entry temp = a.get(i);
  109. a.set(i, a.get(j));
  110. a.set(j, temp);
  111. }
  112. }
  113. break;
  114. }
  115. }
  116.  
  117. }
  118. public class AddressBook {
  119. public static void main(String[] args){
  120. Scanner s = new Scanner(System.in);
  121. ArrayList<ArrayList<Entry>> library= new ArrayList<ArrayList<Entry>>();
  122. for (int i=0;i<10;i++){
  123. library.add(new ArrayList<Entry>());
  124. for ( int j = 0;j<10;j++){
  125. library.get(i).add(new Entry());
  126. }
  127. }
  128. boolean finished = false;
  129. ArrayList<Entry> currentBook = library.get(0);
  130. Entry currentEntry = currentBook.get(0);
  131. while(finished==false){
  132. System.out.println("Selected Address Book: Book "+(library.indexOf(currentBook)));
  133. System.out.println("Selected Entry: "+currentBook.indexOf(currentEntry));
  134. currentEntry.displayEntries();
  135. System.out.println("1. Add Entry");
  136. System.out.println("2. Remove Entry");
  137. System.out.println("3. Edit Entry");
  138. System.out.print("4. Sort Book\n>");
  139. switch (s.nextInt()){
  140. case 1:
  141. System.out.println("Which field to add to?");
  142. System.out.println("1. First Name");
  143. System.out.println("2. Last Name");
  144. System.out.println("3. Address");
  145. System.out.print("4. Email\n>");
  146. switch(s.nextInt()){
  147. case 1:
  148. System.out.print("Add what? ");
  149. currentEntry.addEntry(1, s.next());
  150. break;
  151. case 2:
  152. System.out.print("Add what? ");
  153. currentEntry.addEntry(2, s.next());
  154. break;
  155. case 3:
  156. System.out.print("Add what? ");
  157. currentEntry.addEntry(3, s.next());
  158. break;
  159. case 4:
  160. System.out.print("Add what? ");
  161. currentEntry.addEntry(4, s.next());
  162. break;
  163. }
  164. currentEntry.displayEntries();
  165. break;
  166. case 2:
  167. System.out.println("Which field to remove?");
  168. System.out.println("1. First Name");
  169. System.out.println("2. Last Name");
  170. System.out.println("3. Address");
  171. System.out.println("4. Email");
  172. currentEntry.removeEntry(s.nextInt());
  173. currentEntry.displayEntries();
  174. break;
  175. case 3:
  176. System.out.println("Which field to edit?");
  177. System.out.println("1. First Name");
  178. System.out.println("2. Last Name");
  179. System.out.println("3. Address");
  180. System.out.println("4. Email");
  181. switch(s.nextInt()){
  182. case 1:
  183. System.out.print("Edit to what? ");
  184. currentEntry.editEntry(1, s.next());
  185. break;
  186. case 2:
  187. System.out.print("Edit to what? ");
  188. currentEntry.editEntry(2, s.next());
  189. break;
  190. case 3:
  191. System.out.print("Edit to what? ");
  192. currentEntry.editEntry(3, s.next());
  193. break;
  194. case 4:
  195. System.out.print("Edit to what? ");
  196. currentEntry.editEntry(4, s.next());
  197. break;
  198. }
  199. case 4:
  200. System.out.println("Which field to sort?");
  201. System.out.println("1. First Name");
  202. System.out.println("2. Last Name");
  203. System.out.println("3. Address");
  204. System.out.println("4. Email");
  205. default:
  206. System.out.print("That's not on the list");
  207. break;
  208. }
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment