Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void add(RandomAccessFile database, String title, String author, String publisher, int pageNum, int pub_date, double price) throws IOException
- {
- String titlePad = StringUtility.pad(title, 32, '*');
- String authorPad = StringUtility.pad(author, 32, '*');
- String publisherPad = StringUtility.pad(publisher, 32, '*');
- Book b = new Book(title, author, publisher, pageNum, pub_date, price);
- database.seek(pointer);
- database.writeBoolean(true);
- database.writeUTF(titlePad);
- database.writeUTF(authorPad);
- database.writeUTF(publisherPad);
- database.writeInt(pageNum);
- database.writeInt(pub_date);
- database.writeDouble(price);
- pointer += Book.REC_SIZE;
- //create new HashMap
- map = new HashMap<String, Long>();
- map.put(b.getTitle(), pointer);
- }
- public static void close(RandomAccessFile database)
- {
- try
- {
- database.close();
- }
- catch(Exception e)
- {
- System.out.println("Close failed");
- }
- }
- public static void showAll(RandomAccessFile database) throws IOException
- {
- for(int i = 0; i<database.length(); i++)
- {
- try
- {
- database.seek(i * Book.REC_SIZE);
- System.out.println(database.readBoolean());
- if(database.readBoolean())
- {
- String title = StringUtility.unpad(database.readUTF(), '*');
- String author = StringUtility.unpad(database.readUTF(), '*');
- String publisher = StringUtility.unpad(database.readUTF(), '*');
- int pageNum = database.readInt();
- int pub_date = database.readInt();
- double price = database.readDouble();
- System.out.println(title + " " + author + " " + publisher + " " + pageNum + " " + pub_date+ " " + price);
- }
- //System.out.println(database.readBoolean());
- }
- catch (IOException e)
- {
- e.getStackTrace();
- }
- }
- }
- public static void deleteByRecord(RandomAccessFile database, String title) throws IOException
- {
- database.seek(map.get(title)*Book.REC_SIZE);
- database.writeBoolean(false);
- map.remove(title);
- count--;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement