0

I am writing a basic library app which uses an arraylist of books in its book repository class.

I can add books to the list using a books.add(new book(...)) command as shown below.

However, is there a way I can notify the user if they try adding a book ISBN which already exists?

    // constructor
    public Book(String isbn, String title, String author, String genre, int year, int quantity, int numCheckedOut) {
        super();
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.genre = genre;
        this.year = year;
        this.quantity = quantity;
        this.numCheckedOut = numCheckedOut;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getGenre() {
        return genre;
    }

    public int getYear() {
        return year;
    }
    
    public boolean checkOut() {
        if (numCheckedOut >= quantity) {
            return false;
        }
        numCheckedOut++;
        return true;
    }
    
    public boolean checkIn() {
        if (numCheckedOut <= 0) {
            return false;
        }
        numCheckedOut--;
        return true;
    }

    public int getQuantity() {
        return quantity;
    }

    public int getNumCheckedOut() {
        return numCheckedOut;
    }
}
public class BookRepository {
    
    private ArrayList<Book> books = new ArrayList<>();
    
    public BookRepository() {
        books.add(new Book("1", "To Kill a Mockingbird", "Harper Lee", "Fiction", 1960, 4, 0));
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
nockynock
  • 45
  • 5

0 Answers0