Text Practice Mode
Java practice
created Thursday August 07, 18:59 by Zaphod Beeblebrox
0
596 words
5 completed
0
Rating visible after 3 or more votes
saving score / loading statistics ...
00:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
// Interface defining book management operations
interface LibraryOperations<T> {
void addItem(T item) throws IllegalArgumentException;
boolean removeItem(String id);
T findItem(String id);
List<T> getAllItems();
}
// Abstract class for library items
abstract class LibraryItem {
protected String id;
protected String title;
protected boolean isCheckedOut;
public LibraryItem(String id, String title) {
this.id = id;
this.title = title;
this.isCheckedOut = false;
}
public String getId() { return id; }
public String getTitle() { return title; }
public boolean isCheckedOut() { return isCheckedOut; }
public void setCheckedOut(boolean status) { this.isCheckedOut = status; }
@Override
public String toString() {
return "ID: " + id + ", Title: " + title + ", Checked Out: " + isCheckedOut;
}
}
// Concrete class for books
class Book extends LibraryItem {
private String author;
private int publicationYear;
public Book(String id, String title, String author, int publicationYear) {
super(id, title);
this.author = author;
this.publicationYear = publicationYear;
}
public String getAuthor() { return author; }
public int getPublicationYear() { return publicationYear; }
@Override
public String toString() {
return super.toString() + ", Author: " + author + ", Year: " + publicationYear;
}
}
// Library management class implementing the interface
class LibraryManager implements LibraryOperations<Book> {
private Map<String, Book> bookInventory;
private static final int MAX_BOOKS = 100;
public LibraryManager() {
bookInventory = new HashMap<>();
}
@Override
public void addItem(Book book) throws IllegalArgumentException {
if (book == null || book.getId() == null) {
throw new IllegalArgumentException("Book or ID cannot be null");
}
if (bookInventory.size() >= MAX_BOOKS) {
throw new IllegalStateException("Library is at full capacity");
}
bookInventory.put(book.getId(), book);
}
@Override
public boolean removeItem(String id) {
if (id != null && bookInventory.containsKey(id)) {
bookInventory.remove(id);
return true;
}
return false;
}
@Override
public Book findItem(String id) {
return bookInventory.getOrDefault(id, null);
}
@Override
public List<Book> getAllItems() {
return new ArrayList<>(bookInventory.values());
}
public int getInventorySize() {
return bookInventory.size();
}
}
// Main class to run the library system
public class LibrarySystem {
public static void main(String[] args) {
LibraryManager library = new LibraryManager();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. Find a book");
System.out.println("4. List all books");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
int choice;
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
continue;
}
switch (choice) {
case 1:
System.out.print("Enter book ID: ");
String id = scanner.nextLine();
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author name: ");
String author = scanner.nextLine();
System.out.print("Enter publication year: ");
int year;
try {
year = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid year. Book not added.");
break;
}
Book newBook = new Book(id, title, author, year);
try {
library.addItem(newBook);
System.out.println("Book added successfully: " + newBook);
} catch (IllegalArgumentException | IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
}
break;
case 2:
System.out.print("Enter book ID to remove: ");
String removeId = scanner.nextLine();
if (library.removeItem(removeId)) {
System.out.println("Book removed successfully.");
} else {
System.out.println("Book not found or invalid ID.");
}
break;
case 3:
System.out.print("Enter book ID to find: ");
String findId = scanner.nextLine();
Book foundBook = library.findItem(findId);
if (foundBook != null) {
System.out.println("Book found: " + foundBook);
} else {
System.out.println("Book not found.");
}
break;
case 4:
List<Book> books = library.getAllItems();
if (books.isEmpty()) {
System.out.println("No books in the library.");
} else {
System.out.println("Library Inventory (" + library.getInventorySize() + " books):");
for (Book book : books) {
System.out.println(book);
}
}
break;
case 5:
running = false;
System.out.println("Exiting Library System. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please select 1-5.");
break;
}
}
scanner.close();
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
// Interface defining book management operations
interface LibraryOperations<T> {
void addItem(T item) throws IllegalArgumentException;
boolean removeItem(String id);
T findItem(String id);
List<T> getAllItems();
}
// Abstract class for library items
abstract class LibraryItem {
protected String id;
protected String title;
protected boolean isCheckedOut;
public LibraryItem(String id, String title) {
this.id = id;
this.title = title;
this.isCheckedOut = false;
}
public String getId() { return id; }
public String getTitle() { return title; }
public boolean isCheckedOut() { return isCheckedOut; }
public void setCheckedOut(boolean status) { this.isCheckedOut = status; }
@Override
public String toString() {
return "ID: " + id + ", Title: " + title + ", Checked Out: " + isCheckedOut;
}
}
// Concrete class for books
class Book extends LibraryItem {
private String author;
private int publicationYear;
public Book(String id, String title, String author, int publicationYear) {
super(id, title);
this.author = author;
this.publicationYear = publicationYear;
}
public String getAuthor() { return author; }
public int getPublicationYear() { return publicationYear; }
@Override
public String toString() {
return super.toString() + ", Author: " + author + ", Year: " + publicationYear;
}
}
// Library management class implementing the interface
class LibraryManager implements LibraryOperations<Book> {
private Map<String, Book> bookInventory;
private static final int MAX_BOOKS = 100;
public LibraryManager() {
bookInventory = new HashMap<>();
}
@Override
public void addItem(Book book) throws IllegalArgumentException {
if (book == null || book.getId() == null) {
throw new IllegalArgumentException("Book or ID cannot be null");
}
if (bookInventory.size() >= MAX_BOOKS) {
throw new IllegalStateException("Library is at full capacity");
}
bookInventory.put(book.getId(), book);
}
@Override
public boolean removeItem(String id) {
if (id != null && bookInventory.containsKey(id)) {
bookInventory.remove(id);
return true;
}
return false;
}
@Override
public Book findItem(String id) {
return bookInventory.getOrDefault(id, null);
}
@Override
public List<Book> getAllItems() {
return new ArrayList<>(bookInventory.values());
}
public int getInventorySize() {
return bookInventory.size();
}
}
// Main class to run the library system
public class LibrarySystem {
public static void main(String[] args) {
LibraryManager library = new LibraryManager();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. Find a book");
System.out.println("4. List all books");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
int choice;
try {
choice = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a number.");
continue;
}
switch (choice) {
case 1:
System.out.print("Enter book ID: ");
String id = scanner.nextLine();
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author name: ");
String author = scanner.nextLine();
System.out.print("Enter publication year: ");
int year;
try {
year = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Invalid year. Book not added.");
break;
}
Book newBook = new Book(id, title, author, year);
try {
library.addItem(newBook);
System.out.println("Book added successfully: " + newBook);
} catch (IllegalArgumentException | IllegalStateException e) {
System.out.println("Error: " + e.getMessage());
}
break;
case 2:
System.out.print("Enter book ID to remove: ");
String removeId = scanner.nextLine();
if (library.removeItem(removeId)) {
System.out.println("Book removed successfully.");
} else {
System.out.println("Book not found or invalid ID.");
}
break;
case 3:
System.out.print("Enter book ID to find: ");
String findId = scanner.nextLine();
Book foundBook = library.findItem(findId);
if (foundBook != null) {
System.out.println("Book found: " + foundBook);
} else {
System.out.println("Book not found.");
}
break;
case 4:
List<Book> books = library.getAllItems();
if (books.isEmpty()) {
System.out.println("No books in the library.");
} else {
System.out.println("Library Inventory (" + library.getInventorySize() + " books):");
for (Book book : books) {
System.out.println(book);
}
}
break;
case 5:
running = false;
System.out.println("Exiting Library System. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please select 1-5.");
break;
}
}
scanner.close();
}
}
