My problem is my code does not recognize the file path.
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
My problem is my code does not recognize the file path.
CODE:
Author.java
package libraryDatabase; import java.util.Objects; public class Author { String firstName; public Author() { } String LastName; public Author(String firstName, String lastName) { this.firstName = firstName; LastName = lastName; } @java.lang.Override public java.lang.String toString() { return "Author{" + "firstName='" + firstName + '\'' + ", LastName='" + LastName + '\'' + '}'; } //this will be used for comparing two authors @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Author author = (Author) o; return Objects.equals(firstName, author.firstName) && Objects.equals(LastName, author.LastName); } //this will be used when we compare two authors in search method @Override public int hashCode() { return Objects.hash(firstName, LastName); } }
Book.java
package libraryDatabase; public class Book { Author author; String title; int year; String publisher; String genre; double rating; double price; public Book() { } public Book(Author author, String title, int year, String publisher, String genre, double rating, double price) { this.author = author; this.title = title; this.year = year; this.publisher = publisher; this.genre = genre; this.rating = rating; this.price = price; } @Override public String toString() { return "Book{" + "author=" + author + ", title='" + title + '\'' + ", year=" + year + ", publisher='" + publisher + '\'' + ", genre='" + genre + '\'' + ", rating=" + rating + ", price=" + price + '}' + '\n'; } }
BookDatabase.java
package libraryDatabase; import java.util.ArrayList; public class BookDatabase { private final ArrayList<Book> books; //if no books provided adding empty list public BookDatabase() { this.books = new ArrayList<>(); } //intialising with given books public BookDatabase(ArrayList<Book> books) { this.books = books; } public void addBook(Book book) { this.books.add(book); } public void removeBook(Book book) { this.books.remove(book); } public ArrayList<Book> search(Author author) { ArrayList<Book> result = new ArrayList<>(); //for every book we compare with author provided // if equal store it in result and return for (Book book : books) { //this equals method is written in author class if (book.author.equals(author)) { result.add(book); } } return result; } public ArrayList<Book> search(int startYear, int endYear) { ArrayList<Book> result = new ArrayList<>(); for (Book book : books) { if (book.year >= startYear && book.year <= endYear) { result.add(book); } } return result; } public ArrayList<Book> search(String genre) { ArrayList<Book> result = new ArrayList<>(); for (Book book : books) { //it's a string equals method if (book.genre.equals(genre)) { result.add(book); } } return result; } @Override public String toString() { return "BookDatabase{" + "books=" + books.toString() + '}'; } }
Client.java
package libraryDatabase; import java.io.*; import java.util.ArrayList; public class Client { private static final String delimiter = ","; public static void main(String[] args) { try { //Declaring and initialising file Readers ArrayList<Book> books = new ArrayList<>(); File file = new File("dataset.csv"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; String[] columns; int i = 0; //reading everyLine in csv file while ((line = br.readLine()) != null) { //splitting the row and storing values in array; columns = line.split(delimiter); if (i == 0) { //skipping header in csv i = 1; continue; } //for every row adding reading its column values and creating book.author //objects and adding it to books //reading column values String firstName = columns[0]; String lastName = columns[1]; String title = columns[2]; int year = Integer.parseInt(columns[3]); String publisher = columns[4]; String genre = columns[5]; double rating = Double.parseDouble(columns[6]); double price = Double.parseDouble(columns[7]); //creating author and book objects Author author = new Author(firstName, lastName); Book book = new Book(author, title, year, publisher, genre, rating, price); //adding book to books books.add(book); } //creating bookDatabase BookDatabase bookDatabase = new BookDatabase(books); //searching with genre System.out.println("genre books Data"); ArrayList<Book> booksWithGenre = bookDatabase.search("Anthologies "); System.out.println(booksWithGenre.toString()); //searching with years System.out.println("Books in Range"); ArrayList<Book> booksInGivenRange = bookDatabase.search(1999, 2010); System.out.println(booksInGivenRange.toString()); //searching with author System.out.println("Books with Author"); ArrayList<Book> booksWithAuthor = bookDatabase.search(new Author("Yvonne", "Vera")); System.out.println(booksWithAuthor); br.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education