Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.library.main;
- import com.library.models.BookDirectory;
- import com.library.models.Rent;
- import com.library.service.Alert;
- import com.library.service.FileGenerator;
- import com.library.service.UserRegistration;
- import com.library.users.Customer;
- import com.library.users.User;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class RunApplication {
- public static void main(String[] args) {
- BookDirectory bookDirectory = new BookDirectory();
- HashSet<User> userSet = new HashSet<User>();
- HashSet<Customer> customerSet = new HashSet<Customer>();
- FileGenerator.setBookDirectory(bookDirectory);
- UserRegistration.setUsers(userSet);
- UserRegistration.setCustomers(customerSet);
- Alert.setCustomers(customerSet);
- }
- }
- package com.library.models;
- public class Author {
- private static int nextAuthorId = 0;
- private String name;
- private int id;
- public Author(String name) {
- this.name = name;
- id = nextAuthorId;
- nextAuthorId++;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
- package com.library.models;
- public class Book {
- private BookTitle bookTitle;
- private int id;
- public Book(BookTitle bookTitle, int id) {
- this.bookTitle = bookTitle;
- this.id = id;
- }
- public BookTitle getBookTitle() {
- return bookTitle;
- }
- public String getTitle() {
- return getBookTitle().getTitle();
- }
- public void setTitle(String title) {
- this.getBookTitle().setTitle(title);
- }
- public Author getAuthor() {
- return getBookTitle().getAuthor();
- }
- public void setAuthor(Author author) {
- this.getBookTitle().setAuthor(author);
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
- package com.library.models;
- import com.library.users.Customer;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.time.LocalTime;
- import java.time.Period;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.List;
- public class BookDirectory {
- private ArrayList<Book> books;
- private HashSet<Author> authors;
- private HashSet<BookTitle> bookTitles;
- public void addBook(BookTitle bookTitle, int id) {
- Book newBook = new Book(bookTitle, id);
- books.add(newBook);
- }
- public boolean hasBook(Book book) {
- return books.contains(book);
- }
- public void deleteBook(Book book) {
- books.remove(book);
- }
- public void deleteAllBooks() {
- books.clear();
- }
- public int getBookCount() {
- return books.size();
- }
- public void addAuthor(String name) {
- authors.add(new Author(name));
- }
- public void clearBookDirectory() {
- books.clear();
- authors.clear();
- }
- public void addBookTitle(String title, Author author, int id) {
- bookTitles.add(new BookTitle(title, author, id));
- }
- public int getBookTitlesCount() {
- return bookTitles.size();
- }
- public String toString() {
- return books.toString();
- }
- public List<Book> getAllBooks() {
- return books;
- }
- public HashSet<BookTitle> getAllBookTitles() {
- return bookTitles;
- }
- }
- package com.library.models;
- public class BookTitle {
- private String title;
- private Author author;
- private int id;
- public BookTitle(String title, Author author, int id) {
- this.title = title;
- this.author = author;
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public Author getAuthor() {
- return author;
- }
- public void setAuthor(Author author) {
- this.author = author;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
- package com.library.models;
- import com.library.users.Customer;
- import java.time.LocalDate;
- import java.time.Period;
- public class Rent {
- private Customer customer;
- private Book book;
- LocalDate dateOfRent;
- Period period;
- public Rent(Customer customer, Book book, Period period) {
- this.customer = customer;
- this.book = book;
- this.period = period;
- dateOfRent = LocalDate.now();
- }
- public Customer getCustomer() {
- return customer;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }
- public Book getBook() {
- return book;
- }
- public void setBook(Book book) {
- this.book = book;
- }
- public LocalDate getDateOfRent() {
- return dateOfRent;
- }
- public void setDateOfRent(LocalDate dateOfRent) {
- this.dateOfRent = dateOfRent;
- }
- public Period getPeriod() {
- return period;
- }
- public void setPeriod(Period period) {
- this.period = period;
- }
- }
- package com.library.models;
- public enum UserPrivilige {
- USER, ADMIN, SUPERADMIN
- }
- package com.library.service;
- import com.library.models.Rent;
- import com.library.users.Customer;
- import com.library.users.User;
- import java.time.LocalDate;
- import java.time.Period;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Set;
- public class Alert {
- private static HashSet<Customer> customers;
- public ArrayList<Rent> getOverdueRents() {
- return getSoonOverdueRents(Period.ZERO);
- }
- public static HashSet<Customer> getCustomers() {
- return customers;
- }
- public static void setCustomers(HashSet<Customer> customers) {
- Alert.customers = customers;
- }
- public ArrayList<Rent> getSoonOverdueRents(Period period) {
- ArrayList<Rent> result = new ArrayList<Rent>();
- for (Customer customer : customers) {
- for (Rent rent : customer.getRentedBooks()) {
- if (rent.getDateOfRent().plus(rent.getPeriod()).isAfter(LocalDate.now().minus(period))) {
- result.add(rent);
- }
- }
- }
- return result;
- }
- public ArrayList<Rent> getAllRents() {
- ArrayList<Rent> result = new ArrayList<Rent>();
- for (Customer customer : customers) {
- for (Rent rent : customer.getRentedBooks()) {
- result.add(rent);
- }
- }
- return result;
- }
- }
- package com.library.service;
- import com.library.models.UserPrivilige;
- import com.library.users.User;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class Authentification {
- private Set<User> userSet;
- public Authentification(Set<User> userSet){
- this.userSet = userSet;
- }
- public boolean isLoginValid(String name, String password) {
- for (User user : userSet) {
- if (user.getName().equals(name) && user.getPassword().equals(password)) {
- return true;
- }
- }
- return false;
- }
- public boolean isAdmin(User user) {
- return user.getPrivilige().compareTo(UserPrivilige.ADMIN) >= 0;
- }
- }
- package com.library.service;
- import com.library.models.Book;
- import com.library.models.BookDirectory;
- import com.library.models.BookTitle;
- import com.library.models.Rent;
- import com.library.users.Customer;
- import com.library.users.User;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.time.LocalTime;
- import java.util.HashSet;
- public class FileGenerator {
- private static BookDirectory bookDirectory = null;
- public static BookDirectory getBookDirectory() {
- return bookDirectory;
- }
- public static void setBookDirectory(BookDirectory bookDirectory) {
- FileGenerator.bookDirectory = bookDirectory;
- }
- public static void createDirectoryBookListFile() {
- try {
- FileWriter fileWriter = new FileWriter("book_list_" + LocalTime.now().toString() + ".txt");
- fileWriter.write("TitletAuthortQuantityn");
- for (BookTitle bookTitle : bookDirectory.getAllBookTitles()) {
- Integer bookQuantity = 0;
- for (Book book : bookDirectory.getAllBooks()) {
- if (book.getBookTitle().equals(bookTitle))
- bookQuantity++;
- }
- fileWriter.write(bookTitle.getTitle() + "t" + bookTitle.getAuthor() +
- "t" + bookQuantity.toString() + "n");
- }
- fileWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void createCustomerBookListFile(Customer customer) {
- try {
- FileWriter fileWriter = new FileWriter("book_list_" + customer.getName() + "_" +
- LocalTime.now().toString() + ".txt");
- fileWriter.write("BooktDate of RenttRent Periodn");
- for (Rent rent : customer.getRentedBooks()) {
- fileWriter.write(rent.getBook().getTitle() + "t" + rent.getDateOfRent() +
- "t" + rent.getPeriod() + "n");
- }
- fileWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- package com.library.service;
- import com.library.models.Book;
- import com.library.models.BookDirectory;
- import java.util.ArrayList;
- import java.util.List;
- public class Search {
- private BookDirectory dir;
- public Search(BookDirectory dir) {
- this.dir = dir;
- }
- public List<Book> search(String phrase) {
- List<Book> results = new ArrayList<Book>();
- for (Book b : dir.getAllBooks()) {
- if (b.getAuthor().getName().equals(phrase) || b.getTitle().equals(phrase))
- results.add(b);
- }
- return results;
- }
- }
- package com.library.service;
- import com.library.users.Customer;
- import com.library.users.User;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class UserRegistration {
- private String name;
- private String email;
- private String password;
- private static HashSet<User> users;
- private static HashSet<Customer> customers;
- public static HashSet<User> getUsers() {
- return users;
- }
- public static void setUsers(HashSet<User> users) {
- UserRegistration.users = users;
- }
- public static HashSet<Customer> getCustomers() {
- return customers;
- }
- public static void setCustomers(HashSet<Customer> customers) {
- UserRegistration.customers = customers;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public boolean isRegistrationValid() {
- int nameLength = name.length();
- int passwordLength = password.length();
- boolean passwordHasDigit = password.matches("\d+");
- boolean isEmailValid = email.matches("^[a-zA-Z0-9._+-]+@[a-zA-Z._-]+.[a-zA-Z0-9]]$");
- return nameLength > 3 && passwordLength > 6 && passwordHasDigit && isEmailValid;
- }
- public boolean registerUser() {
- if (isRegistrationValid()) {
- boolean isNameFree = true;
- for (User user : users) {
- if (user.getName().equals(name)) {
- isNameFree = false;
- break;
- }
- }
- if (isNameFree) {
- users.add(new User(name, email, password));
- return true;
- }
- }
- return false;
- }
- public boolean registerCustomer() {
- if (!registerUser())
- return false;
- customers.add(new Customer(name, email, password));
- return true;
- }
- }
- package com.library.users;
- import com.library.models.Book;
- import com.library.models.UserPrivilige;
- import java.util.ArrayList;
- public class User {
- protected String name;
- protected String email;
- protected String password;
- private int id;
- private UserPrivilige privilige = UserPrivilige.USER;
- private static int nextUserId = 0;
- public User(String name, String email, String password) {
- this.name = name;
- this.email = email;
- this.password = password;
- this.id = nextUserId;
- nextUserId++;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public UserPrivilige getPrivilige() {
- return privilige;
- }
- public void setPrivilige(UserPrivilige privilige) {
- this.privilige = privilige;
- }
- }
- package com.library.users;
- import com.library.models.Book;
- import com.library.models.BookDirectory;
- import com.library.models.Rent;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.time.LocalTime;
- import java.time.Period;
- import java.util.ArrayList;
- public class Customer extends User {
- private ArrayList<Rent> rentedBooks;
- private static BookDirectory bookDirectory;
- public Customer(String name, String email, String password) {
- super(name, email, password);
- rentedBooks = new ArrayList<Rent>();
- }
- public static BookDirectory getBookDirectory() {
- return bookDirectory;
- }
- public static void setBookDirectory(BookDirectory bookDirectory) {
- Customer.bookDirectory = bookDirectory;
- }
- public ArrayList<Rent> getRentedBooks() {
- return rentedBooks;
- }
- public boolean rentBook(Book book, Period period) {
- if (bookDirectory.hasBook(book)) {
- getRentedBooks().add(new Rent(this, book, period));
- return true;
- }
- return false;
- }
- public void returnBook(Rent rent) {
- rentedBooks.remove(rent);
- }
- }
- package com.library.users;
- import com.library.models.Author;
- import com.library.models.Book;
- public class Admin extends User {
- public Admin(String name, String email, String password) {
- super(name, email, password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement