PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Library.java

https://gitlab.com/jakenherman7/sql-project
Java | 725 lines | 497 code | 216 blank | 12 comment | 79 complexity | 4fd8336331fe191f35742d570a565e3d MD5 | raw file
  1. import java.io.FileReader;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.FileWriter;
  6. import java.io.BufferedWriter;
  7. import java.util.*;
  8. public class Library
  9. {
  10. private ArrayList<Object> bookList = new ArrayList<Object>(); // to store the entire library book list
  11. private ArrayList<Object>myList = new ArrayList<Object>(); // to store a user's specific book list
  12. private int location =-1; // used in LookUp() default set to -1
  13. private ArrayList<User>users = new ArrayList<User>();
  14. private ArrayList<Object>tempMyList = new ArrayList<Object>();
  15. private User tempUser;
  16. public Library()
  17. {
  18. ReadBooks();
  19. }
  20. public Library(User temp)
  21. {
  22. tempUser = temp;
  23. ReadBooks();
  24. ReadUsers();
  25. }
  26. public void AddBooks (String newTitle, String newAuthor, char newType)
  27. {
  28. boolean found = LookUp(newTitle, newAuthor);
  29. if(found)
  30. {
  31. System.out.println("The book is already in the list.");
  32. }
  33. else if(newType=='F' && found == false)
  34. {
  35. Fiction ficBook = new Fiction(newTitle,newAuthor,newType);
  36. bookList.add(ficBook);
  37. }
  38. else if(newType=='N' && found == false)
  39. {
  40. NonFiction nonFicBook = new NonFiction(newTitle,newAuthor,newType);
  41. bookList.add(nonFicBook);
  42. }
  43. else if(newType=='B' && found == false)
  44. {
  45. System.out.println("Must have nonfiction or fiction selected.");
  46. }
  47. bookList.trimToSize();
  48. WriteBooks();
  49. ReadBooks();
  50. }
  51. public String RemoveBook(String newTitle, String newAuthor)
  52. {
  53. boolean found = LookUp(newTitle, newAuthor);
  54. String out = "";
  55. if (found)
  56. {
  57. bookList.remove(getLocation());
  58. bookList.trimToSize();
  59. WriteBooks();
  60. ReadBooks();
  61. out = ("Book has been removed.");
  62. }
  63. else
  64. {
  65. out=("Book not found");
  66. }
  67. return out;
  68. }
  69. public boolean LookUp(String newTitle, String newAuthor)
  70. {
  71. Object tbook;
  72. String title1= "", author1="";
  73. boolean found = false;
  74. int booklistsize= bookList.size()-1;
  75. for(int i = 0; i <booklistsize; i++)
  76. {
  77. tbook = bookList.get(i);
  78. System.out.println(tbook);
  79. if(tbook instanceof Fiction)
  80. {
  81. Fiction tbookFiction =(Fiction)tbook;
  82. title1 = tbookFiction.getTitle();
  83. author1 = tbookFiction.getAuthor();
  84. }
  85. else if (tbook instanceof NonFiction)
  86. {
  87. NonFiction tbookNonFiction = (NonFiction)tbook;
  88. title1 = tbookNonFiction.getTitle();
  89. author1 = tbookNonFiction.getAuthor();
  90. }
  91. if(title1.equals(newTitle) && author1.equals(newAuthor))
  92. {
  93. found = true;
  94. setLocation(i);
  95. }
  96. }
  97. return found;
  98. }
  99. public void ReadBooks()
  100. {
  101. try
  102. {
  103. FileReader fr = new FileReader( "books.txt" );
  104. BufferedReader br = new BufferedReader( fr );
  105. bookList.clear();
  106. // declare String variable and prime the read
  107. String stringRead = br.readLine( );
  108. while( stringRead != null ) // end of the file?
  109. {
  110. // process the line read
  111. StringTokenizer st = new
  112. StringTokenizer( stringRead, "," );
  113. String title = st.nextToken();
  114. String author = st.nextToken();
  115. char type = st.nextToken().charAt(0);
  116. if (type=='F')
  117. {
  118. Fiction ficBook = new Fiction(title, author, 'F');
  119. bookList.add(ficBook);
  120. }
  121. else if(type=='N')
  122. {
  123. NonFiction nonFicBook = new NonFiction(title, author,'N');
  124. bookList.add(nonFicBook);
  125. }
  126. else if (type == 'B')
  127. {
  128. NonFiction nonFicBook = new NonFiction(title, author,'N');
  129. Fiction ficBook = new Fiction(title, author, 'F');
  130. bookList.add(nonFicBook);
  131. bookList.add(ficBook);
  132. }
  133. bookList.trimToSize();
  134. // read the next line
  135. stringRead = br.readLine( );
  136. }
  137. // release resources associated with "books.txt"
  138. br.close( );
  139. }
  140. catch( FileNotFoundException fnfe )
  141. {
  142. System.out.println( "Unable to find books.txt" );
  143. }
  144. catch( IOException ioe )
  145. {
  146. ioe.printStackTrace( );
  147. }
  148. }
  149. public int getLocation()
  150. {
  151. return location;
  152. }
  153. public void setLocation(int i)
  154. {
  155. location = i;
  156. }
  157. public void WriteBooks()
  158. {
  159. try
  160. {
  161. FileWriter fw = new FileWriter( "books.txt", false );
  162. // false means we will be writing to output.txt,
  163. // rather than appending to it
  164. BufferedWriter bw = new BufferedWriter( fw );
  165. for ( Object bookRef : bookList )
  166. {
  167. if (bookRef instanceof Fiction) // if object bookRef is of type Fiction
  168. {
  169. Fiction tbookFiction = (Fiction)bookRef; //cast it to fiction and write to text file with commas in between
  170. bw.write(tbookFiction.getTitle()+","+ tbookFiction.getAuthor()+","+ tbookFiction.getType()+","+System.getProperty("line.separator"));
  171. }
  172. else if(bookRef instanceof NonFiction) // if object bookRef is of type nonfiction
  173. {
  174. NonFiction tbookNonFiction = (NonFiction)bookRef; // Cast it to nonfic and write to text file
  175. bw.write(tbookNonFiction.getTitle()+","+ tbookNonFiction.getAuthor()+","+ tbookNonFiction.getType()+ "," + System.getProperty("line.separator"));
  176. }
  177. }
  178. bw.close( );
  179. System.out.println( "File written successfully" );
  180. }
  181. catch( IOException ioe )
  182. {
  183. ioe.printStackTrace( );
  184. }
  185. }
  186. public void SortBooks()
  187. {
  188. Object tbook, sbook;
  189. String title1= "";
  190. String title2= "";
  191. int bookListSize = bookList.size()-1;
  192. for(int i = 0; i < bookListSize; i++)
  193. for(int ct = 0; ct < bookListSize - i; ct++)
  194. {
  195. tbook = bookList.get(ct);
  196. sbook = bookList.get(ct+1);
  197. if (tbook instanceof Fiction)
  198. {
  199. Fiction tbookFiction = (Fiction)tbook;
  200. title1 = tbookFiction.getTitle();
  201. }
  202. else if (tbook instanceof NonFiction)
  203. {
  204. NonFiction tbookNonFiction = (NonFiction)tbook;
  205. title1 = tbookNonFiction.getTitle();
  206. }
  207. if (sbook instanceof Fiction)
  208. {
  209. Fiction sbookFiction = (Fiction)sbook;
  210. title2 = sbookFiction.getTitle();
  211. }
  212. else if (sbook instanceof NonFiction)
  213. {
  214. NonFiction sbookNonFiction = (NonFiction)sbook;
  215. title2 =sbookNonFiction.getTitle();
  216. }
  217. if (title1.compareTo(title2) > 0) // if title1 is after title 2 alphabetically
  218. {
  219. bookList.set( ct, sbook);
  220. bookList.set( ct+1, tbook);
  221. }
  222. }
  223. }
  224. public String getBookList(char type)
  225. {
  226. SortBooks(); // first sort
  227. String outStringList = " "; // placeholder strings for output
  228. String ficStringList = " ";
  229. String nonFicStringList = " ";
  230. for(int ct=0; ct < bookList.size(); ct++)
  231. {
  232. Object obj = bookList.get(ct); //Gets first book in the list to see what genre it is
  233. if( obj instanceof Fiction)
  234. {
  235. Fiction ficBook = (Fiction) obj;
  236. //ADD THE TITLE AND AUTHOR TO STRING
  237. ficStringList += ficBook.getTitle() + " " + ficBook.getAuthor() + "\n";
  238. }
  239. else if(obj instanceof NonFiction)
  240. {
  241. NonFiction nonFicBook = (NonFiction) obj;
  242. nonFicStringList += nonFicBook.getTitle()+ " " + nonFicBook.getAuthor()+ "\n";
  243. }
  244. }
  245. switch(type)
  246. {
  247. case 'F':
  248. {
  249. outStringList = ficStringList; //ADD FICTION LISTING TO THE FINAL LIST
  250. break;
  251. }
  252. case 'N':
  253. {
  254. outStringList = nonFicStringList; //ADD NONFICTION LISTING TO THE FINAL LIST
  255. break;
  256. }
  257. case 'B':
  258. {
  259. outStringList += ficStringList + nonFicStringList;
  260. //ADD BOTH LISTS
  261. break;
  262. }
  263. }
  264. return outStringList;
  265. }
  266. public String checkIn(String newTitle, String newAuthor)
  267. {
  268. boolean found = LookUpUser(newTitle,newAuthor);
  269. String out = "";
  270. if (found)
  271. {
  272. myList.remove(location);
  273. myList.trimToSize();
  274. WriteUsers();
  275. ReadUsers();
  276. out = ("Book has been removed from your list and added to the library's.");
  277. }
  278. else
  279. {
  280. out=("Book not found");
  281. }
  282. return out;
  283. }
  284. public void checkOut (String newTitle, String newAuthor, char newType)
  285. {
  286. if(newType=='F')
  287. {
  288. Fiction ficBook = new Fiction(newTitle,newAuthor,'F');
  289. myList.add(ficBook);
  290. myList.trimToSize();
  291. tempMyList.add(ficBook);
  292. tempMyList.trimToSize();
  293. }
  294. else if(newType=='N')
  295. {
  296. NonFiction nonFicBook = new NonFiction(newTitle,newAuthor,'N');
  297. myList.add(nonFicBook);
  298. myList.trimToSize();
  299. tempMyList.add(nonFicBook);
  300. tempMyList.trimToSize();
  301. }
  302. }
  303. public boolean LookUpUser(String newTitle, String newAuthor) // look up user books
  304. {
  305. Object tbook;
  306. String title1= "", author1="";
  307. boolean found = false;
  308. myList = tempUser.getMyList();
  309. int mylistsize = myList.size()-1;
  310. for(int i = 0; i <mylistsize; i++)
  311. {
  312. tbook = myList.get(i);
  313. if(tbook instanceof Fiction)
  314. {
  315. Fiction tbookFiction =(Fiction)tbook;
  316. title1 = tbookFiction.getTitle();
  317. author1 = tbookFiction.getAuthor();
  318. }
  319. else if (tbook instanceof NonFiction)
  320. {
  321. NonFiction tbookNonFiction = (NonFiction)tbook;
  322. title1 = tbookNonFiction.getTitle();
  323. author1 = tbookNonFiction.getAuthor();
  324. }
  325. if(title1.equals(newTitle) && author1.equals(newAuthor))
  326. {
  327. found = true;
  328. location = i;
  329. }
  330. }
  331. return found;
  332. }
  333. public void ReadUsers()
  334. {
  335. try
  336. {
  337. FileReader fr = new FileReader( "user.txt" );
  338. BufferedReader br = new BufferedReader( fr );
  339. String stringRead = br.readLine( );
  340. while( stringRead != null )
  341. {
  342. StringTokenizer st = new StringTokenizer( stringRead, "," );
  343. String userID = st.nextToken( );
  344. String userPassword = st.nextToken();
  345. String name = st.nextToken();
  346. int noBooks = Integer.parseInt(st.nextToken());
  347. for(int ct = 0; ct<noBooks;ct++)
  348. {
  349. String title = st.nextToken( );
  350. String author = st.nextToken();
  351. char type = st.nextToken().charAt(0);
  352. if (type=='F')
  353. {
  354. Fiction ficBook = new Fiction(title, author, 'F');
  355. tempMyList.add(ficBook);
  356. tempMyList.trimToSize();
  357. }
  358. else if(type=='N')
  359. {
  360. NonFiction nonFicBook = new NonFiction(title, author,'N');
  361. tempMyList.add(nonFicBook);
  362. tempMyList.trimToSize();
  363. }
  364. }
  365. if(userID.equals(tempUser.getUserID()) && userPassword.equals(tempUser.getUserPassword()))
  366. {
  367. users.add(tempUser);
  368. users.trimToSize();
  369. }
  370. else
  371. {
  372. User tUser = new User(userID,userPassword,name,tempMyList);
  373. users.add(tUser);
  374. users.trimToSize();
  375. }
  376. // read the next line
  377. stringRead = br.readLine( );
  378. }
  379. // release resources associated with "books.txt"
  380. br.close( );
  381. }
  382. catch( FileNotFoundException fnfe )
  383. {
  384. System.out.println( "Unable to find user.txt" );
  385. }
  386. catch( IOException ioe )
  387. {
  388. ioe.printStackTrace( );
  389. }
  390. }
  391. public void WriteUsers()
  392. {
  393. try
  394. {
  395. FileWriter fw = new FileWriter( "user.txt", false );
  396. // false means we will be writing to user.txt,
  397. // rather than appending to it
  398. BufferedWriter bw = new BufferedWriter( fw );
  399. for (int ct = 0; ct< users.size()-1;ct++)
  400. { System.out.println(users.size());
  401. User tempUser = users.get(ct);
  402. tempMyList = tempUser.getMyList();
  403. int size = tempMyList.size();
  404. String out =(tempUser.getUserID() + "," + tempUser.getUserPassword() + "," + tempUser.getName()+ "," + size + ",");
  405. bw.write(out);
  406. for( int i=0; i<tempMyList.size(); i++)
  407. { Object tbook = tempMyList.get(i);
  408. if (tbook instanceof Fiction)
  409. {
  410. Fiction tbookFiction = (Fiction)tbook;
  411. String fic = (tbookFiction.getTitle() + "," + tbookFiction.getAuthor() + "," + tbookFiction.getType() + ",");
  412. bw.write(fic);
  413. }
  414. else if (tbook instanceof NonFiction)
  415. {
  416. NonFiction nonficbook = (NonFiction)tbook;
  417. String nonfic = (nonficbook.getTitle() + "," + nonficbook.getAuthor() + "," + nonficbook.getType() + ",");
  418. bw.write(nonfic);
  419. }
  420. }
  421. }
  422. bw.close( );
  423. System.out.println( "File written successfully" );
  424. }
  425. catch( IOException ioe )
  426. {
  427. ioe.printStackTrace( );
  428. }
  429. }
  430. public void SortUserBooks()
  431. {
  432. Object tbook, sbook;
  433. String title1= "";
  434. String title2= "";
  435. int myListSize = myList.size()-1;
  436. for(int i = 0; i < myListSize; i++)
  437. for(int ct = 0; ct < myListSize - i; ct++)
  438. {
  439. tbook = myList.get(ct);
  440. sbook = myList.get(ct+1);
  441. if (tbook instanceof Fiction)
  442. {
  443. Fiction tbookFiction = (Fiction)tbook;
  444. title1 = tbookFiction.getTitle();
  445. }
  446. else if (tbook instanceof NonFiction)
  447. {
  448. NonFiction tbookNonFiction = (NonFiction)tbook;
  449. title1 = tbookNonFiction.getTitle();
  450. }
  451. if (sbook instanceof Fiction)
  452. {
  453. Fiction sbookFiction = (Fiction)sbook;
  454. title2 = sbookFiction.getTitle();
  455. }
  456. else if (sbook instanceof NonFiction)
  457. {
  458. NonFiction sbookNonFiction = (NonFiction)sbook;
  459. title2 =sbookNonFiction.getTitle();
  460. }
  461. if (title1.compareTo(title2) > 0)
  462. {
  463. myList.set( ct, sbook);
  464. myList.set( ct+1, tbook);
  465. }
  466. }
  467. }
  468. public String getMyList(char type)
  469. {
  470. SortUserBooks();
  471. String outStringList = " ";
  472. String ficStringList = " ";
  473. String nonFicStringList = " ";
  474. myList = tempUser.getMyList();
  475. for(int ct=0; ct < myList.size(); ct++)
  476. {
  477. Object obj = myList.get(ct);
  478. if( obj instanceof Fiction)
  479. {
  480. Fiction ficBook = (Fiction) obj;
  481. ficStringList += ficBook.getTitle()+ " " + ficBook.getAuthor() + "\n";
  482. }
  483. else if(obj instanceof NonFiction)
  484. {
  485. NonFiction nonFicBook = (NonFiction) obj;
  486. nonFicStringList += nonFicBook.getTitle()+ " " + nonFicBook.getAuthor()+ "\n";
  487. }
  488. }
  489. switch(type)
  490. {
  491. case 'F':
  492. {
  493. outStringList = ficStringList;
  494. break;
  495. }
  496. case 'N':
  497. {
  498. outStringList = nonFicStringList;
  499. break;
  500. }
  501. case 'B':
  502. {
  503. outStringList += ficStringList + nonFicStringList;
  504. break;
  505. }
  506. }
  507. return outStringList;
  508. }
  509. }