Guest User

Untitled

a guest
Jul 25th, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Book{
  3. Scanner s = new Scanner(System.in);
  4. //Nested class for each entry
  5. class Entry{
  6. private String first;
  7. private String last;
  8. private String address;
  9. private String email;
  10. Entry(String first, String last, String address, String email){
  11. this.first = first;
  12. this.last = last;
  13. this.address = address;
  14. this.email = email;
  15. }
  16. Entry(){
  17. first = "";
  18. last = "";
  19. address = "";
  20. email = "";
  21. }
  22. public void readEntry(){
  23. System.out.println("First Name:"+first );
  24. System.out.println("Last Name:"+last );
  25. System.out.println("Address:"+address );
  26. System.out.println("Email:"+email );
  27. }
  28. }
  29.  
  30.  
  31. //Keeps track of how many entries are in the book
  32. private int entries = 0;
  33. Entry[] contents;
  34. public void initEntries(int e){
  35. contents = new Entry[e];
  36. for (int i = 0;i<contents.length;i++){ //Initializes an array of entries, then loops through to initialize each individual entry
  37. contents[i] = new Entry();
  38. }
  39. }
  40. public int getEntries(){
  41. return contents.length;
  42. }
  43. //Adds an entry to the book
  44. public void add(String first, String last, String address, String email){
  45. if (entries<contents.length){
  46. contents[entries] = new Entry(first, last, address, email);
  47. entries++;
  48. }
  49. else System.out.println("Error: book is full");
  50. }
  51.  
  52. //Removes an entry from the book
  53. public void remove(int entry){
  54. if (entries>0){
  55. contents[entry] = new Entry();
  56. for (int i = 0;i<entries-entry;i++){
  57. if (entry+1==entries) contents[entry] = new Entry();
  58. else{
  59. Entry temp = contents[entry+i];
  60. contents[entry+i] = contents[entry+i+1]; //Removes an entry end moves each entry after it one backwards.
  61. contents[entry+i+1] = temp;
  62. }
  63. }
  64. entries--;
  65. }
  66. else System.out.println("Error: book is empty.");
  67. }
  68.  
  69. //Changes the values of an entry
  70. public void edit(String first, String last, String address, String email, int selection){
  71. contents[selection].first = first;
  72. contents[selection].last = last;
  73. contents[selection].address = address;
  74. contents[selection].email = email;
  75. }
  76.  
  77. //Sorts the book based on a part of the entry
  78. //int n is used to tell which part of the entries to base the sort on
  79. public void sort(int n){
  80. for(int i = 0;i<contents.length;i++){
  81. for (int j = 0;j<contents.length;j++){
  82. switch(n){
  83. case 1:
  84. if (contents[i].first.compareTo(contents[j].first)<0){
  85. Entry temp = contents[i];
  86. contents[i] = contents[j];
  87. contents[j] = temp;
  88. }
  89. break;
  90. case 2:
  91. if (contents[i].last.compareTo(contents[j].last)<0){
  92. Entry temp = contents[i];
  93. contents[i] = contents[j];
  94. contents[j] = temp;
  95. }
  96. break;
  97. case 3:
  98. if (contents[i].address.compareTo(contents[j].address)<0){
  99. Entry temp = contents[i];
  100. contents[i] = contents[j];
  101. contents[j] = temp;
  102. }
  103. break;
  104. case 4:
  105. if (contents[i].email.compareTo(contents[j].email)<0){
  106. Entry temp = contents[i];
  107. contents[i] = contents[j];
  108. contents[j] = temp;
  109. }
  110. break;
  111. default:
  112. System.out.println("Error: invalid field");
  113. break;
  114. }
  115. }
  116. }
  117. }
  118. public void addFromCopy(Entry e){
  119. if (entries<contents.length){
  120. contents[entries] = e;
  121. entries++;
  122. }
  123. else System.out.println("Error: book is full");
  124. }
  125.  
  126. }
  127. public class AddressBook2 {
  128. public static void main(String[] args){
  129. Scanner s = new Scanner(System.in);
  130. System.out.print("How many books do you want to create? ");
  131. int howManyBooks;
  132. int howManyEntries;
  133.  
  134. Book[] library = new Book[0];
  135.  
  136.  
  137. while(true){
  138. howManyBooks = s.nextInt();
  139. if (howManyBooks>0){
  140. library = new Book[howManyBooks]; //This code decides how many books are in the array of books/the library
  141. break;
  142. }
  143. else System.out.print("You must create at least 1 book.");
  144. }
  145.  
  146.  
  147.  
  148. for (int i=0;i<library.length;i++){
  149.  
  150.  
  151. library[i] = new Book(); //Fixed reference to null because each book in the library had not been initialized yet.
  152.  
  153. while(true){
  154. System.out.print("How many entries in book "+i+"? ");
  155. howManyEntries = s.nextInt();
  156. if (howManyEntries>0) {
  157. library[i].initEntries(howManyEntries); //This code decides how many entries are in each book in the library
  158. break;
  159. }
  160. else System.out.println("You must create at least 1 Entry.");
  161. }
  162.  
  163.  
  164. }
  165. boolean done = false;
  166. int selectedBook = 0;
  167. int selection;
  168. while (done==false){
  169. System.out.println("Book "+selectedBook+" is currently selected.");
  170.  
  171. for (int i = 0;i<library[selectedBook].getEntries();i++){
  172. System.out.println("===========Entry "+i+" ===========");
  173. library[selectedBook].contents[i].readEntry(); //Accessing the array of entries INSIDE the array of books/the library
  174. System.out.println("================================");
  175. }
  176.  
  177.  
  178. System.out.println("Select an option!");
  179. System.out.println("1. Add an entry");
  180. System.out.println("2. Remove an entry");
  181. System.out.println("3. Edit an entry");
  182. System.out.println("4. Sort all entries in this book");
  183. System.out.println("5. Select another book");
  184. System.out.println("6. Move entry across books");
  185. System.out.println("7. Exit the menu");
  186. System.out.print("> ");
  187. selection = s.nextInt();
  188. String first, last, address, email;
  189. switch(selection){
  190. case 1:
  191. System.out.print("First name? ");
  192. first = s.next();
  193. System.out.print("Last name? ");
  194. last = s.next();
  195. System.out.print("Address? ");
  196. address = s.next();
  197. System.out.print("Email? ");
  198. email = s.next();
  199. library[selectedBook].add(first, last, address, email);
  200. break;
  201. case 2:
  202. System.out.print("Remove which entry? ");
  203. int entry = s.nextInt();
  204. library[selectedBook].remove(entry);
  205. break;
  206. case 3:
  207. System.out.print("Edit which entry?");
  208. int whichEntry = s.nextInt();
  209. System.out.print("First name? ");
  210. first = s.next();
  211. System.out.print("Last name? ");
  212. last = s.next();
  213. System.out.print("Address? ");
  214. address = s.next();
  215. System.out.print("Email? ");
  216. email = s.next();
  217. library[selectedBook].edit(first, last, address, email, whichEntry);
  218. break;
  219. case 4:
  220. System.out.println("Sort alphabetically by which field?");
  221. System.out.println("1. Sort by first name");
  222. System.out.println("2. Sort by last name");
  223. System.out.println("3. Sort by address");
  224. System.out.println("4. Sort by email");
  225. library[selectedBook].sort(s.nextInt());
  226. break;
  227. case 5:
  228. System.out.print("Select which book?");
  229. selectedBook = s.nextInt();
  230. break;
  231. case 6:
  232. System.out.print("Move which entry? ");
  233. int entryNo = s.nextInt();
  234. Book.Entry entryToMove = library[selectedBook].contents[entryNo];
  235. library[selectedBook].remove(entryNo);
  236. System.out.print("To which book? ");
  237. int whichBook = s.nextInt();
  238. library[whichBook].addFromCopy(entryToMove);
  239. break;
  240. case 7:
  241. done = true;
  242. break;
  243. default:
  244. System.out.print("Please choose a valid menu number");
  245.  
  246.  
  247. }
  248.  
  249. }
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment