Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 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.  
  128. public class AddressBook2 {
  129. public static void main(String[] args){
  130. Scanner s = new Scanner(System.in);
  131. System.out.print("How many books do you want to create? ");
  132. int howManyBooks;
  133. int howManyEntries;
  134.  
  135. Book[] library = new Book[0];
  136.  
  137.  
  138. while(true){
  139. howManyBooks = s.nextInt();
  140. if (howManyBooks>0){
  141. library = new Book[howManyBooks]; //This code decides how many books are in the array of books/the library
  142. break;
  143. }
  144. else System.out.print("You must create at least 1 book.");
  145. }
  146.  
  147.  
  148.  
  149. for (int i=0;i<library.length;i++){
  150.  
  151.  
  152. library[i] = new Book(); //Fixed reference to null because each book in the library had not been initialized yet.
  153.  
  154. while(true){
  155. System.out.print("How many entries in book "+i+"? ");
  156. howManyEntries = s.nextInt();
  157. if (howManyEntries>0) {
  158. library[i].initEntries(howManyEntries); //This code decides how many entries are in each book in the library
  159. break;
  160. }
  161. else System.out.println("You must create at least 1 Entry.");
  162. }
  163.  
  164.  
  165. }
  166. boolean done = false;
  167. int selectedBook = 0;
  168. int selection;
  169. while (done==false){
  170. System.out.println("Book "+selectedBook+" is currently selected.");
  171.  
  172. for (int i = 0;i<library[selectedBook].getEntries();i++){
  173. System.out.println("===========Entry "+i+" ===========");
  174. library[selectedBook].contents[i].readEntry(); //Accessing the array of entries INSIDE the array of books/the library
  175. System.out.println("================================");
  176. }
  177.  
  178.  
  179. System.out.println("Select an option!");
  180. System.out.println("1. Add an entry");
  181. System.out.println("2. Remove an entry");
  182. System.out.println("3. Edit an entry");
  183. System.out.println("4. Sort all entries in this book");
  184. System.out.println("5. Select another book");
  185. System.out.println("6. Move entry across books");
  186. System.out.println("7. Exit the menu");
  187. System.out.print("> ");
  188. selection = s.nextInt();
  189. String first, last, address, email;
  190. switch(selection){
  191. case 1:
  192. System.out.print("First name? ");
  193. first = s.next();
  194. System.out.print("Last name? ");
  195. last = s.next();
  196. System.out.print("Address? ");
  197. address = s.next();
  198. System.out.print("Email? ");
  199. email = s.next();
  200. library[selectedBook].add(first, last, address, email);
  201. break;
  202. case 2:
  203. System.out.print("Remove which entry? ");
  204. int entry = s.nextInt();
  205. library[selectedBook].remove(entry);
  206. break;
  207. case 3:
  208. System.out.print("Edit which entry?");
  209. int whichEntry = s.nextInt();
  210. System.out.print("First name? ");
  211. first = s.next();
  212. System.out.print("Last name? ");
  213. last = s.next();
  214. System.out.print("Address? ");
  215. address = s.next();
  216. System.out.print("Email? ");
  217. email = s.next();
  218. library[selectedBook].edit(first, last, address, email, whichEntry);
  219. break;
  220. case 4:
  221. System.out.println("Sort alphabetically by which field?");
  222. System.out.println("1. Sort by first name");
  223. System.out.println("2. Sort by last name");
  224. System.out.println("3. Sort by address");
  225. System.out.println("4. Sort by email");
  226. library[selectedBook].sort(s.nextInt());
  227. break;
  228. case 5:
  229. System.out.print("Select which book?");
  230. selectedBook = s.nextInt();
  231. break;
  232. case 6:
  233. System.out.print("Move which entry? ");
  234. int entryNo = s.nextInt();
  235. Book.Entry entryToMove = library[selectedBook].contents[entryNo];
  236. library[selectedBook].remove(entryNo);
  237. System.out.print("To which book? ");
  238. int whichBook = s.nextInt();
  239. library[whichBook].addFromCopy(entryToMove);
  240. break;
  241. case 7:
  242. done = true;
  243. break;
  244. default:
  245. System.out.print("Please choose a valid menu number");
  246.  
  247.  
  248. }
  249.  
  250. }
  251. }
  252. }
  253.  
  254. Entry() {
  255. this("", "", "", "");
  256. }
  257.  
  258. protected static int entries = 0;
  259.  
  260. List<Entry> to_sort = new ArrayList<Entry>(Arrays.asList(contents));
  261. Collections.sort(to_sort, new Comparator<Entry>() {
  262. public int compare(Entry one, Entry other) {
  263. return one.[whatever is at n].compareTo(other.[whatever is at n]);
  264. }
  265. });
  266.  
  267. ArrayList<Entry> list = new ArrayList<>();
  268. //populate the entries
  269. list.sort((entry1, entry2) -> entry1.last.compareTo(entry2.last));
  270. //list is now sorted by last name
  271.  
  272. book.sort((entry1, entry2) -> entry1.last.compareTo(entry2.last));
  273.  
  274. //or if you create a new class per property it could look like this
  275. class sortEntriesByLastName : Comparator<Entry>{...}
  276. book.sort(new sortEntriesByLastName())
  277.  
  278. public void addFromCopy(Entry e){
  279. if (entries**<**contents.length){
  280.  
  281. public void addFromCopy(Entry e){
  282. if (entries<contents.length>){
Add Comment
Please, Sign In to add comment