// do some exercises on procedures: add static methods that work // on an array of books that are read from a file import java.io.*; public class SearchLib2 { // lib[] is the array of Books stored in our library // being an attribute of SearchLib, it is visible to all methods private static Book lib[]; // the main method reads the books from file book.txt, stores // them in lib[] and tests some of our methods here public static void main(String a[]) { int i; // STEP 1: first pass: just count the number of lines in the file int count = 0; try { BufferedReader file = new BufferedReader(new FileReader("book.txt")); while(file.readLine() != null) { count++; } file.close(); } catch (Exception e) { System.err.println("there's an error reading the file"); System.exit(1); } if (count == 0) { System.err.println("can't find any books"); System.exit(1); } if (count % 3 != 0) { System.err.println("oops: got " + count + " lines -> not divisible by 3"); System.exit(1); } // STEP 2: create an array of Books with enough space for all the books in my file // there's three lines for each book: title, author, year lib = new Book[count / 3]; // STEP 3: read all the books into lib[] try { // open the text file book.txt // (create a BufferedReader object) BufferedReader file = new BufferedReader(new FileReader("book.txt")); // read three Book records (each having three lines) for (i = 0; i < lib.length; i++) { String title = file.readLine(); // first line -> title String author = file.readLine(); // second line -> author String year = file.readLine(); // third line -> year // watch out: you need to transform the String year // into an int, so the constructor is happy lib[i] = new Book(title, author, Integer.parseInt(year)); } file.close(); // close the file } catch (Exception e) { System.out.println("something is wrong"); System.exit(1); } // ---------------------------------------------------------------- // just to check, I want to print out the three titles System.out.println(""); System.out.println("here come all the titles:"); for (i = 0; i < lib.length; i++) { System.out.println(lib[i].getTitle()); } // look for books by Shakespeare System.out.println(""); System.out.println("here come all of Shakespear's books:"); String titles[] = search("W. Shakespear"); for (i = 0; i < titles.length; i++) { System.out.println(titles[i]); } // find the oldest book System.out.println(""); System.out.println("the oldest book is: " + findOldest()); // lend out / give back a few books lib[0].lendTo("Lecha"); lib[5].lendTo("Foruzan"); lib[3].lendTo("Bersam"); lib[3].giveBack(); lib[3].lendTo("Khuseyn"); // now see a list of all the books that are lent out and by whom System.out.println(""); System.out.println("Current list of lent out books:"); printUsers(); } // add a method that prints to the terminal the titles // of all books that are lend out including the names // of the people that lend them public static void printUsers() { int i; for (i = 0; i < lib.length; i++) { if (lib[i].isAvailable() == false) { System.out.println(lib[i].getTitle() + " " + lib[i].getUser()); } } } // add a method that returns the title of the oldest book in lib[] // method name: findOldest // arguments: none // return type: you find out public static String findOldest() { int oldest_so_far = 0; int i; for (i = 0; i < lib.length; i++) { if (lib[i].getYear() < lib[oldest_so_far].getYear()) { oldest_so_far = i; } } return lib[oldest_so_far].getTitle(); } // write a method that given two books returns the title of the older Book, // or the first Book if both have the same age // the method should be called getOlder public static String getOlder(Book a, Book b) { if (a.getYear() < b.getYear()){ return a.getTitle(); } else if (a.getYear() == b.getYear()){ return a.getTitle(); } else { return b.getTitle(); } } // a method that returns the number of books in my library public static int getNumber() { return lib.length; } // NEW: add a method that is given an author and return all title of the books by the // given author as an array of Strings public static String[] search(String aut) { int i; // we need to store all results before returning them, but we don't know in advance // how many results we will get. So the (cheap and dirty) trick is to look for that // author twice: first we just count the results, so we know how large the result // array should be; then we make a second pass where we store the results. // in real life of course you would use dynamic arrays (see java.utils) int count = 0; for (i = 0; i < lib.length; i++) { if (aut.equals(lib[i].getAuthor())) { count++; } } String results[] = new String[count]; int r = 0; for (i = 0; i < lib.length; i++) { if (aut.equals(lib[i].getAuthor())) { // we have found that book number i is by author aut String title = lib[i].getTitle(); results[r] = title; r++; } } return results; } }