Question: Hey, I have working example of a program that takes takes a book(title,author,price) and Instantizes (?) that data into an object (book). I then then
Hey, I have working example of a program that takes takes a book(title,author,price) and Instantizes (?) that data into an object (book). I then then sort then by title, author and price. Now I have to take that program and modify it so it will read in a file. Which I kinda forget how to do(??) I dont think I am actually reading in the file in the code pasted below. Does my file have to be in a certain location (int the programs src, right?) I would like the final program to both print the sorted booklists as well as create three files (1 sorted by author, 1 sorted by title, 1 by price). Thank you very much for helping me with this, if you wouldnt mind explaining how you came about the correct code I would very much appreciate it. Example of CSV.csv file:
The Hunger Games, Suzanne Collins, 17.49 The Icebound Land, John Flanagan, 7.99 The Library at Night, Alberto Manguel, 23.99 Cicada Summer, Andrea Beaty, 17.34
My code:
package book;
import java.util.*; import java.io.*; import java.util.ArrayList;
public class Book implements Comparable { public String author, title; public double price; // public Data releaseDate;
public Book(String author, String title, double price) { this.author = author; this.title = title; this.price = price; // this.releaseDate=releaseDate; }
public int compareTo(Book s) { return this.author.compareTo(s.author); }
public String toString() { return "Book(" + title + "," + author + ", $" + price + ")"; }
public static class TitleComparator implements Comparator { public int compare(Book s1, Book s2) { return s1.title.compareTo(s2.title); } }
public static class PriceComparator implements Comparator { public int compare(Book s1, Book s2) { return (int) (s1.price - s2.price); } }} /* public static void printList(List L) { for (Book book : L) { System.out.println(book); } } */ class Books{ /* List to hold Books */ ArrayList BookList = new ArrayList(); /** Print this List **/ public static void printList(List L){ Iterator it=L.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } /* Read file and populate Student List */ private void getBooks(File finput) throws Exception { FileInputStream fis = new FileInputStream(finput); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String line = null; br.readLine(); while ((line = br.readLine()) != null) { String delims = "[,]"; String[] tokens = line.split(delims); String title = tokens[0]; String aname = tokens[1]; double price = Double.parseDouble(tokens[2]); //parseInt(tokens[3].trim()); BookList.add(new Book(title, aname, price)); } br.close(); }
/** Method to write an ArrayList of Students to a file */ public static void writeFile(File F, ArrayList AL) throws IOException { Iterator it = AL.iterator(); FileWriter fw = new FileWriter(F); while (it.hasNext()) { Book st = (Book) it.next(); fw.write(st.toString() + System.getProperty("line.separator")); } fw.close(); } //Your Code for default comparison, // Alphabetic comparison by author public static void main(String[] args) throws Exception{ Books m=new Books(); File f=new File("CSV1.csv"); m.getBooks(f); File f_author = new File("Books_by_Author.txt"); File f_title = new File("Books_by_Title.txt"); File f_price = new File("Books_by_Price.txt"); Collections.sort(m.BookList); writeFile(f_author,m.BookList);//shouldnt this write a file? System.out.println("Displaying Books by Title"); //printList(m.BookList); Collections.sort(m.BookList, new cmp_Title()); printList(m.BookList); } }
class cmp_Title implements Comparator { public int compare(Book s1, Book s2) { return s1.title.compareTo(s2.title); } }
class cmp_Author implements Comparator { public int compare(Book s1, Book s2) { return s1.author.compareTo(s2.author); } } /* #1 Book b1 = new Book("dad", "lorem", 7.99); Book b2 = new Book("goog", "ipsum", 2.33); Book b3 = new Book("frank", "aalor", 5.99); Book b4 = new Book("steve", "amet", 123.00); ArrayList BL=new ArrayList(); BL.add(b1); BL.add(b2); BL.add(b3); BL.add(b4); */ //Print Statements /* System.out.println("......Sorted according to Author's name......"); Collections.sort(BookList); printList(BookList); System.out.println(); System.out.println("......Sorted according to Title......"); BL.sort(new TitleComparator()); printList(BL); System.out.println(); System.out.println("......Sorted according to Title......"); BL.sort(new PriceComparator()); printList(BL); System.out.println(); */