Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. public static void add(RandomAccessFile database, String title, String author, String publisher, int pageNum, int pub_date, double price) throws IOException
  2. {
  3. String titlePad = StringUtility.pad(title, 32, '*');
  4. String authorPad = StringUtility.pad(author, 32, '*');
  5. String publisherPad = StringUtility.pad(publisher, 32, '*');
  6.  
  7. Book b = new Book(title, author, publisher, pageNum, pub_date, price);
  8.  
  9. database.seek(pointer);
  10.  
  11. database.writeBoolean(true);
  12. database.writeUTF(titlePad);
  13. database.writeUTF(authorPad);
  14. database.writeUTF(publisherPad);
  15. database.writeInt(pageNum);
  16. database.writeInt(pub_date);
  17. database.writeDouble(price);
  18.  
  19. pointer += Book.REC_SIZE;
  20.  
  21. //create new HashMap
  22. map = new HashMap<String, Long>();
  23.  
  24. map.put(b.getTitle(), pointer);
  25. }
  26.  
  27. public static void close(RandomAccessFile database)
  28. {
  29. try
  30. {
  31. database.close();
  32. }
  33. catch(Exception e)
  34. {
  35. System.out.println("Close failed");
  36. }
  37. }
  38.  
  39.  
  40.  
  41. public static void showAll(RandomAccessFile database) throws IOException
  42. {
  43. for(int i = 0; i<database.length(); i++)
  44. {
  45. try
  46. {
  47. database.seek(i * Book.REC_SIZE);
  48. System.out.println(database.readBoolean());
  49. if(database.readBoolean())
  50. {
  51. String title = StringUtility.unpad(database.readUTF(), '*');
  52. String author = StringUtility.unpad(database.readUTF(), '*');
  53. String publisher = StringUtility.unpad(database.readUTF(), '*');
  54. int pageNum = database.readInt();
  55. int pub_date = database.readInt();
  56. double price = database.readDouble();
  57.  
  58.  
  59. System.out.println(title + " " + author + " " + publisher + " " + pageNum + " " + pub_date+ " " + price);
  60. }
  61. //System.out.println(database.readBoolean());
  62.  
  63. }
  64.  
  65. catch (IOException e)
  66. {
  67. e.getStackTrace();
  68. }
  69. }
  70.  
  71. }
  72.  
  73.  
  74.  
  75. public static void deleteByRecord(RandomAccessFile database, String title) throws IOException
  76. {
  77.  
  78. database.seek(map.get(title)*Book.REC_SIZE);
  79.  
  80. database.writeBoolean(false);
  81. map.remove(title);
  82. count--;
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement