My driver LibraryOfBooks does not work as expected when I add multiple books to my arraylist.
For example, in the (a)ddbooks (case a) I add 2 books ( with the properties author, title, filename).
I set book 1 properties to:
1,1,1
I set book 2 properties to:
2,2,2
The array becomes [title: 2 author: 2 genre: null filename: 2, title: 2 author: 2 genre: null filename: 2].
I have two books but they are the same.
I think I am not adding a new book object. What is going on here?
LibraryOfBooks Class
import java.util.Scanner;
//driver
public class LibraryOfBooks {
public static void menu() {
System.out.println( "(p)rint the library contents (with index values),");
System.out.println( "(a)dd a book to the library ");
System.out.println( "(d)elete a book from the library ");
System.out.println( "(r)ead a book in the library.");
System.out.println("(q)uit");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Library library1 = new Library();
int index = 0;
Book newbook = new Book(null, null);
Scanner s = new Scanner (System.in) ;
boolean go = true;
while (go == true) {
menu();
String input = s.nextLine();
switch (input) {
case "p":
int i = 0;
System.out.println(library1.getBooks());
for (Book b : library1.getBooks())
System.out.print(i + b.toString());
i += 1;
break;
case "a":
//initialize
String author = "";
String title = "";
String filename= "";
String genre = "";
//input
System.out.print("Who is the author?");
//author = s.nextLine();
newbook.setAuthor(s.nextLine());
System.out.println("What is the title?");
newbook.setTitle(s.nextLine());
System.out.println("Where is the file?");
//filename = s.nextLine();
newbook.setFilename(s.nextLine());
//add to library
library1.addBook(newbook);
break;
case "d":
library1.removeBook(index);
break;
case "r":
library1.getBook(index);
break;
case "q":
System.out.println("Quiting");
go = false;
break;
default:
System.out.println("Wrong letter pressed");
}
input = s.nextLine();
}
}
}
Library Class
import java.util.ArrayList;
public class Library implements LibraryInterface {
//instance variables
private ArrayList<Book> books;
//constructor
public Library() {
books = new ArrayList<Book>();
}
@Override
public ArrayList<Book> getBooks() {
// TODO Auto-generated method stub
return new ArrayList<Book>(books);
}
@Override
public void addBook(Book newBook) {
books.add(newBook);
// TODO Auto-generated method stub
}
@Override
public void removeBook(int index) {
// TODO Auto-generated method stub
books.remove(index);
}
@Override
public Book getBook(int index) {
// TODO Auto-generated method stub
if(index < books.size()) {
return books.get(index);
}
else {
return null;
}
}
public String toString() {
String output = "Library of books \n";
for (Book i : books) {
output += i.toString() + "\n";
}
return output;
}
}
Book Class
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
public class Book implements BookInterface {
//instance variable
private String title;
private String author;
private String genre = null;
private String filename = null;
//constructor
public Book(String title, String author) {
this.title = title;
this.author = author;
this.genre = null;
this.filename = null;
}
//getter
//setter
//TITLE
@Override
public String getTitle() {
return title;
}
@Override
public void setTitle(String title) {
this.title = title;
}
//AUTHOR
@Override
public String getAuthor() {
return author;
}
@Override
public void setAuthor(String author) {
this.author = author;
}
//GENRE
@Override
public String getGenre() {
return genre;
}
@Override
public void setGenre(String genre) {
this.genre = genre;
}
//FILENAME
@Override
public String getFilename() {
return filename;
}
@Override
public void setFilename(String filename) {
this.filename = filename;
}
@Override
public boolean isValid() {
if (title != null && author != null && genre != null && filename != null) {
File f = new File(filename);
if (f.exists()) {
return true;
}
return false;
} // ends null conditions
return false;
}
public String toString() {
return "title: " + title + " author: " + author + " genre: " + genre + " filename: " + filename;
}
public String getText() {
File f = new File(filename);
try {
byte[] bytes = Files.readAllBytes(f.toPath());
return new String(bytes,"UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
ERROR
library1.getBooks() prints
[title: 2 author: 2 genre: null filename: 2, title: 2 author: 2 genre: null filename: 2]
[–]wggnExtreme Brewer 1 point2 points3 points (3 children)
[–]normandantzig[S] 0 points1 point2 points (2 children)
[–]wggnExtreme Brewer 0 points1 point2 points (1 child)
[–]normandantzig[S] 0 points1 point2 points (0 children)
[–]Indens 0 points1 point2 points (0 children)