Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 0 additions & 5 deletions src/Main.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/com/librarysystem/main/LibrarySystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.librarysystem.main;

import com.librarysystem.model.*;
import com.librarysystem.manager.*;
import com.librarysystem.service.*;

public class LibrarySystem {
public static void main(String[] args) {
Library library = new Library();
BookManager bookManager = new BookManager(library);
UserManager userManager = new UserManager(library);
LibraryService libraryService = new LibraryService(library);

Author author = new Author("George Orwell");
Category category = new Category("Dystopian");
Book book = new Book("1", "1984", author, category);
bookManager.addBook(book);

User user = new User("1", "Alice");
userManager.addUser (user);

libraryService.loanBook(user, book);

}
}
16 changes: 16 additions & 0 deletions src/com/librarysystem/manager/BookManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.librarysystem.manager;

import com.librarysystem.model.Book;
import com.librarysystem.model.Library;

public class BookManager {
private Library library;

public BookManager(Library library) {
this.library = library;
}

public void addBook(Book book) {
library.addBook(book);
}
}
16 changes: 16 additions & 0 deletions src/com/librarysystem/manager/UserManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.librarysystem.manager;

import com.librarysystem.model.User;
import com.librarysystem.model.Library;

public class UserManager {
private Library library;

public UserManager(Library library) {
this.library = library;
}

public void addUser (User user) {
library.addUser (user);
}
}
12 changes: 12 additions & 0 deletions src/com/librarysystem/model/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.librarysystem.model;

public class Author {
private String name;

public Author(String name) {
this.name = name;
}

// Getters
public String getName() { return name; }
}
26 changes: 26 additions & 0 deletions src/com/librarysystem/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.librarysystem.model;

public class Book {
private String id;
private String title;
private Author author;
private Category category;
private boolean isAvailable;

public Book(String id, String title, Author author, Category category) {
this.id = id;
this.title = title;
this.author = author;
this.category = category;
this.isAvailable = true;
}

// Getters and Setters
public String getId() { return id; }
public String getTitle() { return title; }
public Author getAuthor() { return author; }
public Category getCategory() { return category; }
public boolean isAvailable() { return isAvailable; }
public void setAvailable(boolean available) { isAvailable = available; }
public void setTitle(String title) { this.title = title; }
}
12 changes: 12 additions & 0 deletions src/com/librarysystem/model/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.librarysystem.model;

public class Category {
private String name;

public Category(String name) {
this.name = name;
}

// Getters
public String getName() { return name; }
}
16 changes: 16 additions & 0 deletions src/com/librarysystem/model/Invoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.librarysystem.model;

public class Invoice {
private User user;
private Book book;
private double amount;

public Invoice(User user, Book book, double amount) {
this.user = user;
this.book = book;
this.amount = amount;
}

// Getters
public double getAmount() { return amount; }
}
56 changes: 56 additions & 0 deletions src/com/librarysystem/model/Library.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.librarysystem.model;

import com.librarysystem.service.ILoanable;

import java.util.ArrayList;
import java.util.List;

public class Library implements ILoanable {
private List<Book> books;
private List<User> users;
private List<Loan> loans;

public Library() {
this.books = new ArrayList<>();
this.users = new ArrayList<>();
this.loans = new ArrayList<>();
}

public void addBook(Book book) {
books.add(book);
}

public void addUser (User user) {
users.add(user);
}

public List<Book> getBooks() {
return books;
}

public List<User> getUsers() {
return users;
}

@Override
public void loanBook(User user, Book book) {
if (book.isAvailable()) {
book.setAvailable(false);
loans.add(new Loan(user, book));
System.out.println(user.getName() + " has borrowed " + book.getTitle());
} else {
System.out.println("The book is not available.");
}
}

@Override
public void returnBook(User user, Book book) {
if (loans.stream().anyMatch(loan -> loan.getUser ().equals(user) && loan.getBook().equals(book))) {
book.setAvailable(true);
loans.removeIf(loan -> loan.getUser ().equals(user) && loan.getBook().equals(book));
System.out.println(user.getName() + " has returned " + book.getTitle());
} else {
System.out.println("This book was not borrowed by the user.");
}
}
}
20 changes: 20 additions & 0 deletions src/com/librarysystem/model/Loan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.librarysystem.model;

import java.util.Date;

public class Loan {
private User user;
private Book book;
private Date loanDate;

public Loan(User user, Book book) {
this.user = user;
this.book = book;
this.loanDate = new Date();
}

// Getters
public User getUser () { return user; }
public Book getBook() { return book; }
public Date getLoanDate() { return loanDate; }
}
21 changes: 21 additions & 0 deletions src/com/librarysystem/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.librarysystem.model;

import java.util.HashSet;
import java.util.Set;

public class User {
private String id;
private String name;
private Set<Book> borrowedBooks;

public User(String id, String name) {
this.id = id;
this.name = name;
this.borrowedBooks = new HashSet<>();
}

// Getters and Setters
public String getId() { return id; }
public String getName() { return name; }
public Set<Book> getBorrowedBooks() { return borrowedBooks; }
}
9 changes: 9 additions & 0 deletions src/com/librarysystem/service/ILoanable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.librarysystem.service;

import com.librarysystem.model.User;
import com.librarysystem.model.Book;

public interface ILoanable {
void loanBook(User user, Book book);
void returnBook(User user, Book book);
}
21 changes: 21 additions & 0 deletions src/com/librarysystem/service/LibraryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.librarysystem.service;

import com.librarysystem.model.Library;
import com.librarysystem.model.User;
import com.librarysystem.model.Book;

public class LibraryService {
private Library library;

public LibraryService(Library library) {
this.library = library;
}

public void loanBook(User user, Book book) {
library.loanBook(user, book);
}

public void returnBook(User user, Book book) {
library.returnBook(user, book);
}
}