Коммит 1b09a756 создал по автору dmitrii's avatar dmitrii 💬
Просмотр файлов

Загрузить новый файл

владельцы
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Book {
private String title;
private String author;
private boolean isAvailable;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isAvailable() {
return isAvailable;
}
public void borrowBook() {
if (isAvailable) {
isAvailable = false;
System.out.println("Book '" + title + "' has been borrowed.");
} else {
System.out.println("Book '" + title + "' is not available.");
}
}
public void returnBook() {
if (!isAvailable) {
isAvailable = true;
System.out.println("Book '" + title + "' has been returned.");
} else {
System.out.println("Book '" + title + "' was not borrowed.");
}
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Available: " + isAvailable;
}
}
class User {
private String name;
private List<Book> borrowedBooks;
public User(String name) {
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
public String getName() {
return name;
}
public void borrowBook(Book book) {
if (book.isAvailable()) {
borrowedBooks.add(book);
book.borrowBook();
} else {
System.out.println("Book '" + book.getTitle() + "' is not available.");
}
}
public void returnBook(Book book) {
if (borrowedBooks.contains(book)) {
borrowedBooks.remove(book);
book.returnBook();
} else {
System.out.println("You did not borrow the book '" + book.getTitle() + "'.");
}
}
public List<Book> getBorrowedBooks() {
return borrowedBooks;
}
@Override
public String toString() {
return "User: " + name + ", Borrowed Books: " + borrowedBooks.size();
}
}
class Library {
private List<Book> books;
private List<User> users;
public Library() {
this.books = new ArrayList<>();
this.users = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void addUser(User user) {
users.add(user);
}
public Book findBookByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public User findUserByName(String name) {
for (User user : users) {
if (user.getName().equalsIgnoreCase(name)) {
return user;
}
}
return null;
}
public void displayBooks() {
for (Book book : books) {
System.out.println(book);
}
}
public void displayUsers() {
for (User user : users) {
System.out.println(user);
}
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nLibrary Management System");
System.out.println("1. Add Book");
System.out.println("2. Add User");
System.out.println("3. Borrow Book");
System.out.println("4. Return Book");
System.out.println("5. Display Books");
System.out.println("6. Display Users");
System.out.println("7. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter book author: ");
String author = scanner.nextLine();
Book book = new Book(title, author);
library.addBook(book);
System.out.println("Book added successfully.");
break;
case 2:
System.out.print("Enter user name: ");
String name = scanner.nextLine();
User user = new User(name);
library.addUser(user);
System.out.println("User added successfully.");
break;
case 3:
System.out.print("Enter user name: ");
String userName = scanner.nextLine();
User borrower = library.findUserByName(userName);
if (borrower != null) {
System.out.print("Enter book title: ");
String bookTitle = scanner.nextLine();
Book borrowedBook = library.findBookByTitle(bookTitle);
if (borrowedBook != null) {
borrower.borrowBook(borrowedBook);
} else {
System.out.println("Book not found.");
}
} else {
System.out.println("User not found.");
}
break;
case 4:
System.out.print("Enter user name: ");
String returnerName = scanner.nextLine();
User returner = library.findUserByName(returnerName);
if (returner != null) {
System.out.print("Enter book title: ");
String returnBookTitle = scanner.nextLine();
Book returnedBook = library.findBookByTitle(returnBookTitle);
if (returnedBook != null) {
returner.returnBook(returnedBook);
} else {
System.out.println("Book not found.");
}
} else {
System.out.println("User not found.");
}
break;
case 5:
library.displayBooks();
break;
case 6:
library.displayUsers();
break;
case 7:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать