0% found this document useful (0 votes)
40 views

Note Java

The document defines a Book class with fields for title, ISBN, author, publisher and a linked list node. It also defines a bookLinkedList class to manage a linked list of Book objects. The bookLinkedList class implements methods to add, delete, find and print books in the list. The Main class provides a menu driven program that allows users to add, delete, find and print books by interacting with the bookLinkedList class.

Uploaded by

Ina apdişalam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Note Java

The document defines a Book class with fields for title, ISBN, author, publisher and a linked list node. It also defines a bookLinkedList class to manage a linked list of Book objects. The bookLinkedList class implements methods to add, delete, find and print books in the list. The Main class provides a menu driven program that allows users to add, delete, find and print books by interacting with the bookLinkedList class.

Uploaded by

Ina apdişalam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;
class Book {

private final String title;


private final String ISBN;
private final String author;
private final String publisher;
private Book next;

public Book(String title, String ISBN, String author, String publisher) {


this.title = title;
this.ISBN = ISBN;
this.author = author;
this.publisher = publisher;
this.next = null;
}

public String getTitle() {


return title;
}

public String getISBN() {


return ISBN;
}

public String getAuthor() {


return author;
}

public String getPublisher() {


return publisher;
}

public Book getNext() {


return next;
}

public void setNext(Book next) {


this.next = next;
}
}

class bookLinkedList {
private Book head;

public bookLinkedList() {
this.head = null;
}

public boolean isEmpty() {


return head == null;
}

public void addBook(Book newBook) {


if (isEmpty()) {
head = newBook;
} else if (newBook.getTitle().compareTo(head.getTitle()) < 0) {
newBook.setNext(head);
head = newBook;
} else {
Book current = head;
while (current.getNext() != null &&
newBook.getTitle().compareTo(current.getNext().getTitle()) > 0) {
current = current.getNext();
}
newBook.setNext(current.getNext());
current.setNext(newBook);
}
System.out.println("Book added successfully.");
}

public void deleteBook(String ISBN) {


if (isEmpty()) {
System.out.println("No books found.");
return;
}

if (head.getISBN().equals(ISBN)) {
head = head.getNext();
System.out.println("Book deleted successfully.");
return;
}

Book current = head;


Book previous = null;
while (current != null && !current.getISBN().equals(ISBN)) {
previous = current;
current = current.getNext();
}

if (current == null) {
System.out.println("Book not found.");
} else {
previous.setNext(current.getNext());
System.out.println("Book deleted successfully.");
}
}

public void findBookByISBN(String ISBN) {


if (isEmpty()) {
System.out.println("No books found.");
return;
}

Book current = head;


while (current != null && !current.getISBN().equals(ISBN)) {
current = current.getNext();
}

if (current == null) {
System.out.println("Book not found.");
} else {
System.out.println("Book details:");
System.out.println("Title: " + current.getTitle());
System.out.println("ISBN: " + current.getISBN());
System.out.println("Author: " + current.getAuthor());
System.out.println("Publisher: " + current.getPublisher());
}
}

public void printBooks() {


if (isEmpty()) {
System.out.println("No books found.");
return;
}

System.out.println("Books' titles in alphabetical order:");


Book current = head;
while (current != null) {
System.out.println(current.getTitle());
current = current.getNext();
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
bookList bookList = new BookList();

while (true) {

System.out.println("Choose one option:");


System.out.println("1. Add a book");
System.out.println("2. Delete a book");
System.out.println("3. Find a book");
System.out.println("4. Print books");
System.out.println("5. Exit");
int choice = scanner.nextInt();
scanner.nextLine();

switch (choice) {
case 1:
System.out.println("Enter title:");
String title = scanner.nextLine();
System.out.println("Enter ISBN:");
String isbn = scanner.nextLine();
System.out.println("Enter author:");
String author =

scanner.nextLine();
System.out.println("Enter publisher:");
String publisher = scanner.nextLine();
bookList.addBook(title, isbn, author, publisher);
System.out.println("Book added");
break;
case 2:
System.out.println("Enter ISBN of book to delete:");
isbn = scanner.nextLine();
bookList.deleteBook(isbn);
break;
case 3:
System.out.println("Enter ISBN of book to find:");
isbn = scanner.nextLine();
bookList.findBook(isbn);
break;
case 4:
bookList.printBooks();
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid choice, please try again");
}
}
}
}

You might also like