I've been doing my homework, but I ran into a problem. My code scans some information about books (author, title etc..) from a txt file then prints it. First I tried to print the author, title, ISBN code, the number of the pages, then the price of the books which is 15*number of pages. It works so far, however I wanted to add a number for each book, so it goes like a list. But when I modified the code to add those numbers, the code didn't want to scan them. it says
int cannot be converted to String
I tried to scan the numbers as Strings out of curiosity, but then the error message said that
String cannot be converted to int
My Book class:
public class Book {
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = number;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
}
#Override
public String toString() {
return author + " - " + title + ", ISBN:" + code + ", " + pages + " pages, Price: " + price() + "Ft";
}
public int price() {
return multiplier * pages;
}
public static int getMultiplier() {
return multiplier;
}
public static void setMultiplier(int multiplier) {
Book.multiplier = multiplier;
}
public int getNumber() {
return number;
}
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getCode() {
return code;
}
public int getPages() {
return pages;
}
}
And my "Controller" class:
public class Controller {
void start() {
scanning();
printing("Avilable books: ");
}
private List<Book> books = new ArrayList<>();
private void scanning() {
try {
Scanner fajlScanner = new Scanner(new File("books.txt"));
String row;
String data[];
while (fajlScanner.hasNextLine()) {
row = fajlScanner.nextLine();
data = row.split(";");
//1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
//this line below gives the error for the data[0]
books.add(new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4])));
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void printing(String title) {
System.out.println(title);
for (Book book : books) {
System.out.println(book);
}
}
}
The content of my txt:
1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
2;J.R.R. Tolkien;The Fellowship of the Ring;1827493762573;431
3;Stephen King;Needful Things;8274653821647;411
4;Eric Knight;Lassie Come-Home;7263845618293;138
5;Molnár Ferenc;A pál utcai fiúk;9283746192846;194
6;Winston Groom;Forrest Gump;0385231342;228
7;Antoine de Saint-Exupéry;The Little Prince;8362748172649;69
8;Stephen King;Cujo;2918467382914;362
I can scan the pages good, but it has some problem with the "number".
Check that you are giving the right parameters in the right order to your Book-constructor.
The constructor:
public Book(String author, String title, String code, int pages, int number)
Your object creation:
new Book(Integer.parseInt(data[0]), data[1], data[2], data[3], Integer.parseInt(data[4]))
Can you spot the error?
your input is: 1;J.K. Rowling;Harry Potter and the Philosopher's Stone;1782637594826;342
your constructor is:
public Book(String author, String title, String code, int pages, int number){
/*.
.
.*/
}
first character of input is "1" which is an integer but in your constructor the first parameter is a String. that's why the error showed up. "1" must be at the end of the input according to the constructor.
for set a number for each book, use a additional static variable, initialize it to 1, every time you wanna add a book, set the current value of the static variable for the book's number, then ++ it.
public class Book {
private static int counter=1;
private int number;
private String author;
private String title;
private String code;
private int pages;
private static int multiplier = 15;
public Book(String author, String title, String code, int pages, int number) {
this.number = counter;
this.author = author;
this.title = title;
this.code = code;
this.pages = pages;
counter++;
}
Good Luck
Related
When I run this code, it gives an error saying cannot involve get__() on the array type Book[].. In the main method, I have a book array with the information from a text file -- title, isbn, price, qty, forsale.
public static int searchByTitleOrISBN(Book[] b)
{
int i = 0;
// print a message "Enter the Title or ISBN of the book: "
System.out.println("Enter the Title or ISBN of the book: ");
String input = scan.next();
// use a while loop to search through the array books using a counter and as long as no match is found
while (i < b.length) {
// if there's a match for the title or a match for the isbn and the book quantity > 0 and the book is for sale
if ((b.getTitle().equals(title) || b.getISBN().equals(isbn)) & b.getQty() > 0 && b.isForSale()) {
Basically what you want is to search the entire list.If you want to keep up your present implementation, then the solution given by #Jens works.
But If you go by searching one by one it would be O(n) but if you leverage inbuilt HashMaps you can do it with O(1) using hashCode and equals and no need of looping through all the objects for each and every search.
the Book class which keeps the data of the book. hashCode and equals is implemented here.
public final class Book{
private final String title;
private final String isbn;
private double price;
private int quantity;
private boolean forSale;
public final Key key;
public Book(String title, String isbn, double price, int quantity,boolean forSale){
this.title = title;
this.isbn = isbn;
this.price = price;
this.quantity =quantity;
this.forSale = forSale;
this.key = makeKey(title,isbn);
}
private static Key makeKey(String title,String isbn) {
return new Key(title,isbn);
}
public Key getKey() {
return this.key;
}
public String getTitle(){
return this.title;
}
public String getISBN(){
return this.isbn;
}
#Override
public final String toString(){
String s = this.title +"; "+ this.isbn+ "; "+this.price+"; "+this.quantity;
return s;
}
public static final class Key{
private final String title;
private final String isbn;
private Key(String title,String isbn) {
this.title = title;
this.isbn = isbn;
}
private String getTitle() {
return this.title;
}
private String getISBN() {
return this.isbn;
}
//calculates the hash of the entry, if we don't override this it will
//use the hash of the reference. so we need to change it for comparision
#Override
public final int hashCode(){
return this.title.hashCode()+this.isbn.hashCode();
}
//if two components have same hash code, then we can use extra criterion
//to check if the two objects are same. in the book case the hash is
//unique.since we are using the title and the isbn;
#Override
public final boolean equals(Object obj){
if(this==obj){
return true;
}
if(obj!=null && this.getClass()==obj.getClass()){
Key tempObj = (Key)obj;
return (this.title == tempObj.getTitle()) && (this.isbn == tempObj.getISBN());
}
return false;
}
}
}
BookMap class which stores all the book objects in form of Map<Book.Key,Book>. This will help with the easy retrival of book data.
public final class BookMap{
private final Map<Book.Key,Book> map;
private final String name;
public BookMap(String name){
this.name = name;
this.map = new HashMap<>();
}
public final String getName(){
return this.name;
}
public final Map<Book.Key,Book> getMap(){
return Collections.unmodifiableMap(this.map);
}
public final void addBook(Book book){
this.map.put(book.getKey(),book);
}
public final Book getBook(Book book){
Book.Key tempKey = book.getKey();
return this.map.get(tempKey);
}
public final boolean findBook(Book book){
if(book != null) {
return this.map.containsKey(book.getKey());
}
return false;
}
#Override
public final String toString() {
String s ="Books list : \n";
for(Map.Entry<Book.Key,Book> b:this.map.entrySet()) {
s= s+b.getValue()+"\n";
}
return s;
}
}
Now a sample Main.js with static main method to retrieve the book list.
public class Main {
public static BookMap list = new BookSet("General list");
public static void main(String[] args) {
Book temp = new Book("Journey to the center of the earth","isbn-001-002-0455",9.99,15,false);
list.addBook(temp);
temp = new Book("The Time Machine","isbn-001-002-0456",9.99,15,false);
list.addBook(temp);
temp = new Book("The War of the Worlds","isbn-001-002-0457",8.99,15,false);
list.addBook(temp);
temp = new Book("Brave New World","isbn-001-002-0458",11.99,15,false);
list.addBook(temp);
temp = new Book("Ninteen Eighty-four","isbn-001-002-0459",19.99,15,false);
list.addBook(temp);
temp = new Book("Ray Bradbury","isbn-001-002-0460",14.99,15,false);
list.addBook(temp);
temp = new Book("I, Robot","isbn-001-002-0461",12.99,15,false);
list.addBook(temp);
temp = new Book("Foundation","isbn-001-002-0462",12.99,15,false);
list.addBook(temp);
temp = new Book("The Martial Chronicles","isbn-001-002-0463",3.99,15,false);
list.addBook(temp);
temp = new Book("Fahrenheit 451","isbn-001-002-0464",6.99,15,false);
list.addBook(temp);
//testing with the book already in the list;
temp = new Book("Fahrenheit 451","isbn-001-002-0464",0,0,false);
//prints book detail
System.out.println(list.getBook(temp));
//testing with the book already in the list;
temp = new Book("I am not in the list","isbn-001-002-0464",0,0,false);
//prints null as the book is not in the list
System.out.println(list.getBook(temp));
System.out.println(list);
}
}
The above is a rough idea, you can develop more on it, i have used final for classes in order to prevent subclassing, as it would become complex with inheritence to understand and debug.i like avoiding complexity as much as i can. If some common pattern is there, then i will try to use abstract classes.
You have to acces an element in your loop not the array itself:
while (i < b.length) {
// if there's a match for the title or a match for the isbn and the book quantity > 0 and the book is for sale
if ((b[i].getTitle().equals(title) || b[i].getISBN().equals(isbn)) & b[i].getQty() > 0 && b[i].isForSale()) {
So I need to create 3 classes "Book"(with fields author, title and body, as string), "Shelf"(with fields id (string) and books (array) and "Library". Then I shall create a method "countAuthor".which counts (and returns as an int) the number of books in the library written by an author whose name (String) is passed as an argument to the method:
class Book {
private String author;
public String getAuthor() {return author;}
private String title;
public String getTitle() {return title;}
private String body;
public String getBody() {return body;}
}
class Shelf {
private String id;
public String getId() {return id;}
private int[] books;
public int[] getBooks() {return books;}
}
class Library {
private int[] shelves;
public int[] getShelves() {return shelves;}
public int countAuthor(String authorName) { // returns the number of books in the library written by author whose name (String)
//is passed as an argument to the method.
int a = ;
return a; // a is the number of books of the author
}
}
After all, in the class (where the main method is stated), I need to add the necessary code and the program overall shall print the number of books an author has written. This is the class of the main method:
public class Exercise {
public static void main(String[] args) {
Shelf shelf1 = new Shelf("Shelf1",
new Book[] {
new Book("Babel", "Odessa Tales", "babelode"),
new Book("Joyce", "Ulisses", "joyceuli")
});
Shelf shelf2 = new Shelf("Shelf2",
new Book[] {
new Book("Mann", "Dr Faustus", "mannfau"),
new Book("Babel", "Red Cavalry", "babelred")
});
Library lib = new Library(
new Shelf[] { shelf1, shelf2 });
System.out.println("# of books by this author: " + lib.countAuthor("Babel"));
}
}
Which should print # of book by this author: 2
Where I am confused with is what to add to the countAuthor() method. And if I need anything in addition to the method. I am pretty new to Java so I still get confused with some structures, especially with the loops.
Here it is. In your code you are missing constructors that you are trying to use so I added them. The logic of counting the books is just go through all the shelves in the lib and count all the books on these shelves with the author name.
public class Exercise {
public static void main(String[] args) {
Shelf shelf1 = new Shelf("Shelf1", new Book[]{
new Book("Babel", "Odessa Tales", "babelode"),
new Book("Joyce", "Ulisses", "joyceuli")});
Shelf shelf2 = new Shelf("Shelf2",
new Book[]{
new Book("Mann", "Dr Faustus", "mannfau"),
new Book("Babel", "Red Cavalry", "babelred")
});
Library lib = new Library(new Shelf[]{shelf1, shelf2});
System.out.println("# of books by this author: " + lib.countAuthor("Babel"));
}
}
class Book {
public Book(String author, String title, String body) {
this.author = author;
this.title = title;
this.body = body;
}
private String author;
public String getAuthor() {
return author;
}
private String title;
public String getTitle() {
return title;
}
private String body;
public String getBody() {
return body;
}
}
class Shelf {
public Shelf(String id, Book[] books) {
this.id = id;
this.books = books;
}
private String id;
public String getId() {
return id;
}
private Book[] books;
public Book[] getBooks() {
return books;
}
}
class Library {
public Library(Shelf[] shelves) {
this.shelves = shelves;
}
private Shelf[] shelves;
public Shelf[] getShelves() {
return shelves;
}
public int countAuthor(
String authorName) { // returns the number of books in the library written by author whose name (String)
//is passed as an argument to the method.
int count = 0;
for (Shelf shelf : shelves) {
for (Book book : shelf.getBooks()) {
if (book.getAuthor().equals(authorName)) {
count++;
}
}
}
return count; // a is the number of books of the author
}
}
Output:
# of books by this author: 2
As I can see from your question, the structure will be like this.
Library can have many Shelf and in every Shelf there are many books.
So to count the no of books for a particular author, first you need all the books.
It will be something like this.
public int countAuthor(String author) {
int result = 0;
for(Shelf shelf : shelves) {
for(Book book : shelf.getBooks()) {
if(author.equals(book.getAuthor()) {
result++;
}
}
}
return result;
}
I'm made a program that creates an invoice but when it comes numbers in the thousands the output isn't neat and ruins everything. How do I fix this so the program's columns are more aligned with numbers like this? Here is the code I used to create the program. If anyone could help, it would be much appericated.
Here's the one with the main method...
public class InvoicePrinter
{
public static void main(String[] args)
{
Address samsAddress=new Address("Sam's Small Appliances", "100 Main
Street", "Anytown", "CA", "98765");
Invoice samsInvoice =new Invoice(samsAddress);
samsInvoice.add(new Product("Toaster", 29.95),3);
samsInvoice.add(new Product("Hair Dryer", 24.95),1);
samsInvoice.add(new Product("Car Vacuum",19.99),2);
samsInvoice.add(new Product("Nano Parts",100000),1);
samsInvoice.addSimple(new Product("Shipping",5.00));
System.out.println(samsInvoice.format());
}
}
These are the other programs needed for the program to work
import java.util.ArrayList;
public class Invoice
{
public Invoice(Address anAddress)
{
items=new ArrayList<LineItem>();
billingAddress=anAddress;
simpleItems= new ArrayList<SimpleLineItem>();
}
public void addSimple(Product aProduct)
{
SimpleLineItem anItem= new SimpleLineItem(aProduct);
simpleItems.add(anItem);
}
public void add(Product aProduct, int quantity)
{
LineItem anItem=new LineItem(aProduct,quantity);
items.add(anItem);
}
public String format()
{
String r=" I N V O I C E\n\n"+billingAddress.format()+String.format("\n\n%-30s%8s%5s%8s\n","Description", "Price","Qty","Total");
for(LineItem i:items)
{
r=r+i.format()+"\n";
}
for(SimpleLineItem j:simpleItems)
{
r=r+j.format() + "\n";
}
r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue());
return r;
}
public double getAmountDue()
{
double amountDue = 0;
for (LineItem i : items)
{
amountDue = amountDue + i.getTotalPrice();
}
for(SimpleLineItem j:simpleItems)
{
amountDue = amountDue + j.getPrice();
}
return amountDue;
}
private Address billingAddress;
private ArrayList<LineItem> items;
private ArrayList<SimpleLineItem> simpleItems;
}
Few more
public class LineItem
{
public LineItem(Product aProduct, int aQuantity)
{
theProduct = aProduct;
quantity = aQuantity;
}
public double getTotalPrice()
{
return theProduct.getPrice() *quantity;
}
public String format()
{
return String.format("%'-30s%'8.2f%'5d%'8.2f", theProduct.getDescription(),theProduct.getPrice(),quantity,getTotalPrice());
}
private int quantity;
private Product theProduct;
}
Another one
public class SimpleLineItem
{
public SimpleLineItem(Product aProduct)
{
theProduct=aProduct;
}
public double getPrice()
{
return theProduct.getPrice();
}
public String format()
{
return String.format("%-30s" +" " + "%8.2f",
theProduct.getDescription(), theProduct.getPrice());
}
private Product theProduct;
}
Two more
public class Product
{
public Product(String aDescription,double aPrice)
{
description = aDescription;
price = aPrice;
}
public String getDescription()
{
return description;
}
public double getPrice()
{
return price;
}
private String description;
private double price;
}
Last one
public class Address
{
public Address(String aName, String aStreet, String aCity, String
aState,String aZip)
{
name = aName;
street = aStreet;
city = aCity;
state = aState;
zip = aZip;
}
public String format()
{
return name + "\n" + street + "\n" + city + ", " + state + " " + zip;
}
private String name;
private String street;
private String city;
private String state;
private String zip;
}
Maybe you can take a look at the javadocs by Oracle on System.out.format and DecimalFormat class
Formatting Numeric Print Output
So basically this happens when you cannot decide the total length of your number column until you print out everything. For this you will need to set the number column's length to the lengthiest number or in your case price length and justify right all the numbers. So you'll need to add all the numbers to an array and loop through them to find the lengthiest number.
I am trying to write a findBook method for my BookCollection class. It finds a book with a specified ISBN. I am having trouble finding a way to compare the different types of elements. We were given this method:
public void changePrice(String isbn, double price){
int index = findBook(isbn); //CREATE FINDBOOK METHOD TO HELP THIS METHOD
if( index == -1){
throw new UnsupportedOperationException("BookNotFound");
}
collection[index].setPrice(price);
}
But I am confused on why the comparison of integer index is made to a findbook method with a string parameter. Basically, we have a collection of type Book[], and with the given parameter isbn, have to search for that isbn in that book collection.
Here is a sloppy rough estimate of what I have so far:
//this method is a helper function
private String findBook(int is){
this.isbn = is;
for(int i = 0; i <= collection.length; i++){
add(isbn);
}
}
I know this method is wrong, but I am having a lot of trouble thinking of a way to write this. How do I search this collection of type Book[] with a string parameter isbn? If you guys want my entire code let me know and I'll post it!
Thank you to whoever helps!
#drorbs Here is my data field and constructor:
private int limit = 200;
//Array of type book
private int Book[];
//actual size of collection, initialized to zero. Must never exceed limit
private Book[] collection; //collection is of book type
private int lastElement;
//Constructor
public BookCollection(int l){
limit = l;
int lastElement = 0;
if(limit <= 200){
Book[] collection = new Book[limit];
} else{
throw new UnsupportedOperationException("CannotExceedLimit");
}
}
A naive implementation of such method would be:
private int findBook(String isbn){
// iterate all the Book elements in the collection array
for(int i = 0; i <= collection.length; i++){
// check if the current book isbn matches the one provided argument
if (collection[i].getIsbn().equals(isbn))
return i;
}
return -1;
}
Notice the method return type should be an int if you are looking for the book index and that the isbn argument should be of type String.
In addition, I think books would be a more suitable name for the array than collection.
Do I understand correctly, you want to search a Book[] for an ISBN, and return the index of the found book, or -1 if you fail to find the book?
I don't know what a Book looks like, but assuming it has a getISBN() method, and assuming collection is a Book[] you want to search, I'd create a method like:
private int findBook( String isbn ) {
int returnIndex = -1;
for ( int index = 0; index < collection.length; index++ ) {
if ( collection[ index ].equals( isbn ) {
returnIndex = index;
break;
}
return returnIndex;
}
import java.util.Arrays;
import java.util.List;
public class Library {
public static void main(String[] args) {
new Library();
}
private List<Book> collection;
public Library() {
collection = Arrays.asList(
new Book("Foo", "000-0-00-000000-1", 0.0d),
new Book("Bar", "000-0-00-000000-2", 0.0d),
new Book("Baz", "000-0-00-000000-3", 0.0d)
);
Book b = collection.get(1);
changePrice(b.getIsbn(), 3.50);
System.out.println(b);
}
public int findBook(String isbn) {
for (Book book : collection) {
if (book.getIsbn().equals(isbn)) {
return collection.indexOf(book);
}
}
return -1;
}
public void changePrice(String isbn, double price) {
int index = findBook(isbn);
if (index < 0) {
throw new UnsupportedOperationException("BookNotFound");
}
collection.get(index).setPrice(price);
}
public class Book implements Comparable<Book> {
private String author;
private String isbn;
private double price;
public Book() {
this.author = "NOT FOUND";
this.isbn = "000-0-00-000000-0";
this.price = 0.0f;
}
public Book(String author, String isbn, double price) {
this.author = author;
this.isbn = isbn;
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
// You can use this to sort books.
#Override
public int compareTo(Book other) {
return this.getIsbn().compareTo(other.getIsbn());
}
#Override
public String toString() {
return String.format("Book [author=%s, isbn=%s, price=$%.2f]",
author, isbn, price);
}
}
}
I'm trying to make a program that creates a library of different books, I have set a number of copies for each item in the library and every time I check out an Item I want it to deduct 1 copy from only the particular object I check out but instead it takes a copy away from all the objects. not sure how to fix the problem.
public abstract class Item{
private int identify;
private String title;
private int copies;
public Item(){
identify=0;
title="N/A";
copies=0;
}
public Item(int id, int copy, String t){
identify=id;
copies=copy;
title=t;
}
public void setIdentificationNumber(int id){
identify = id;
}
public void setTitle(String t){
title=t;
}
public void setNumberCopies(int num){
copies=num;
}
public int getIdentificationNumber(){
return identify;
}
public String getTitle(){
return title;
}
public int getNumberCopies(){
return copies;
}
public void checkOut(){
if(copies>0){
copies-=1;
System.out.println("You have checked out "+title+". Thank You");
}
else{
System.out.println("All copies of "+title+" are checked out!");
}
}
public void checkIn(){
copies+=1;
}
}
The problem may also be in my client method I have posted the code for that as well below.
import java.util.Arrays;
import java.util.Scanner;
public class Library{
static String title;
static String author;
static int id;
static int copies;
static String date;
static Book[] database = new Book[100];
static int count=0;
public static void main(String[] args){
int i;
Scanner s = new Scanner(System.in);
do{
addBook();
System.out.println("would you like to add another book?");
i=s.nextInt();
}while(i == 0);
database[0].viewDetails();
database[1].viewDetails();
checkingOut();
database[0].viewDetails();
database[1].viewDetails();
}
public static void addBook(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the title of the book you want to add to the collection");
title=s.nextLine();
System.out.println("Enter the author of the book you want to add to the collection");
author=s.nextLine();
System.out.println("Enter the publishing date of the book you want to add to the collection");
date=s.nextLine();
System.out.println("Enter the ID number of the book you want to add to the collection");
id=s.nextInt();
System.out.println("Enter the the number of copies that will be added into the collection");
copies=s.nextInt();
Book Book1 = new Book(date, author, copies, id, title);
database[count] = Book1;
count++;
}
public static void checkingOut(){
boolean found=false;
int idSearch;
int i=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch=s.nextInt();
while(i<database.length && found!=true){
if(database[i].getIdentificationNumber() == idSearch){
found = true;
}
i++;
}
if(found==true){
database[i].checkOut();
System.out.println("There are "+database[i].getNumberCopies()+" copies left");
}
else{System.out.println("There is no book with that ID number!");}
}
}
In my addBook method I create a new object called book1 every time I make a new book, so I think that it may be changing all of the book objects every time I add a book. I'm not really sure of a better way to write the method.
here is my method for book also
public class Book extends WrittenItem{
public Book(){
super();
}
public Book(String date, String a, int copy, int id, String t){
super(a, date, copy, id, t);
}
public void viewDetails(){
System.out.println("ID: "+getIdentificationNumber()+"\nTitle: "+getTitle()+"\nAuthor: "+getAuthor()+" Date written: "+getDate()+"\nCopies available: "+getNumberCopies());
}
}
Any help would be greatly appreciated.
I can't 100% tell from your code, but I'm assuming Book extends Item. In that case, try something like this for Book constructor
public class Book extends Item {
String author;
String date;
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title); // Item constructor matches this super() call
// public Item(int id, int copy, String t)
this.author = author;
this.date = date;
}
}
You want Book to have the same copies at Item. So when you checkOut(), the Item number equals you input from the Book constructor. If you don't put the super() in the constructor, your Item copies will remain 0 and you will always get System.out.println("All copies of "+title+" are checked out!"); because copies is never > 0.
If your book is something like this,
public class Book extends Item {
private String date;
private String author;
public Book() {
}
public Book(String date, String author, int copies, int id, String title) {
super(id, copies, title);
this.author = author;
this.date = date;
}
void viewDetails() {
System.out.println("date:" + date + " author:" + author + " copies:" + getNumberCopies() + " id:" + getIdentificationNumber() + " title:" + getTitle());
}
}
Then your code should work fine, as i've tested, if you also add a break in your checkingOut() method,
public static void checkingOut() {
boolean found = false;
int idSearch;
int i = 0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the ID number of the book you want to check out");
idSearch = s.nextInt();
while (i < database.length && found != true) {
if (database[i].getIdentificationNumber() == idSearch) {
found = true;
break; //add this
}
i++;
}
if (found == true) {
database[i].checkOut();
System.out.println("There are " + database[i].getNumberCopies() + " copies left");
} else {
System.out.println("There is no book with that ID number!");
}
}