Reputation: 1
I'm relatively new to Programming in general and I've been grafting over this piece of work for hours and haven't got any further. I've trawled through the depths of the internet and I cannot find an answer similar to what I am looking for. Here in the Spec:
A) Create two files FictionBook.java and Library.java. Define a FictionBook class that represents a single book. The book will have a title, an author, an availability field (1=available, 0=on loan). You should define accessor methods to borrow and return the book and read the title and author and an accessor method to return the availability. Additionally, define a Library class that contains up to 200 books. The library should model containing books with an ArrayList. The Library class should contain a method to add a book to the library, delete a book from the library and borrow and return books.
B) Create a Librarian.java file and modify the Library.java file. Write code to sort the messages in the Library so that all the books are in alphabetical order by Author’s name. Create a Librarian class with only a main method, so you can simulate the processing of books in the library. Generate 10 new FictionBooks and add them to a Library using the addBook method. Your Library should place the message in the correct place in the library depending on the name of the author.
I think I have completed the first part, although I could be wrong. It is the second part which I am complete stuck on. Here are my three classes
public class FictionBook {
private String title, author;
private int availability;
public FictionBook(String title, String author, int availability){
super();
this.title = title;
this.author = author;
this.availability = availability;
}
public FictionBook() {
this.availability = 1;
}
public void borrowBook1() {
setAvailability(0);
}
public void returnBook1() {
setAvailability(1);
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getAvailability() {
return availability;
}
public void setAvailability(int availability) {
if(availability != 1 || availability != 0) {
System.err.println("Enter 1 for available. Enter 2 for on loan.");
throw new IndexOutOfBoundsException();
} else {
this.availability = availability;
}
}
}
import java.util.ArrayList;
public class Library {
static ArrayList<FictionBook> BookList = new ArrayList<FictionBook>(200);
public static void addBook(FictionBook String){
BookList.add(String);
System.out.println("Book Successfully Added To Library Database.");
System.out.println(BookList);
}
public void deleteBook(){
BookList.remove(index);
}
public void borrowBook(){
BookList.get(index).FictionBook.borrowBook1();
}
public void returnBook(){
BookList.get(index).FictionBook.returnBook1();
}
}
public class Librarian {
public static void main(String args[]){
FictionBook newBook1 = new FictionBook("USB Man", "Bob", 1);
FictionBook newBook2 = new FictionBook("Bin Boys", "Chris", 1);
FictionBook newBook3 = new FictionBook("Dinosaur", "Joe", 1);
FictionBook newBook4 = new FictionBook("Pigasaurus", "Tom", 1);
FictionBook newBook5 = new FictionBook("Cat Attack", "Calvin", 1);
FictionBook newBook6 = new FictionBook("Shark Man", "Alfie", 1);
FictionBook newBook7 = new FictionBook("Burnt Face Man", "Colin", 1);
FictionBook newBook8 = new FictionBook("Egg Life", "Darwin", 1);
FictionBook newBook9 = new FictionBook("Pizza King", "Pringle", 1);
FictionBook newBook10 = new FictionBook("BillyBonka", "Randy", 1);
Library.addBook();
}
}
I am just wondering how I actually use the addBook(); method in my Library class to add the objects defined in the Librarian class to the ArrayList in my Library class? I've been messing around with the code a lot so there may be a lot of mistakes, apologies in advance. Any help would be super. Thank you in advance for your time!
Upvotes: 0
Views: 3064
Reputation: 1469
Look at where you defined the addBook
method:
public static void addBook(FictionBook String)
this means that whenever you want to call addBook
, you MUST include the name of the book (the name of the object of the FictionBook
, not the literal title. And you must do this for each book since you're only doing them one at a time.
so try this
FictionBook newBook1 = new FictionBook("USB Man", "Bob", 1);
Library.addBook(newBook1);
FictionBook newBook2 = new FictionBook("Bin Boys", "Chris", 1);
Library.addBook(newBook2);
etc. etc. for each book you define
also, like Compass said, naming a local variable "String" is not a good idea at all. I would rename it something like book
because it's technically not the name of the book, it's the name of the object.
Upvotes: 1