Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.66 KB | None | 0 0
  1. package com.library.main;
  2.  
  3. import com.library.models.BookDirectory;
  4. import com.library.models.Rent;
  5. import com.library.service.Alert;
  6. import com.library.service.FileGenerator;
  7. import com.library.service.UserRegistration;
  8. import com.library.users.Customer;
  9. import com.library.users.User;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.HashSet;
  13. import java.util.List;
  14. import java.util.Set;
  15.  
  16. public class RunApplication {
  17.  
  18. public static void main(String[] args) {
  19. BookDirectory bookDirectory = new BookDirectory();
  20. HashSet<User> userSet = new HashSet<User>();
  21. HashSet<Customer> customerSet = new HashSet<Customer>();
  22. FileGenerator.setBookDirectory(bookDirectory);
  23. UserRegistration.setUsers(userSet);
  24. UserRegistration.setCustomers(customerSet);
  25. Alert.setCustomers(customerSet);
  26. }
  27. }
  28.  
  29. package com.library.models;
  30.  
  31. public class Author {
  32.  
  33. private static int nextAuthorId = 0;
  34.  
  35. private String name;
  36. private int id;
  37.  
  38. public Author(String name) {
  39. this.name = name;
  40. id = nextAuthorId;
  41. nextAuthorId++;
  42. }
  43.  
  44. public String getName() {
  45. return name;
  46. }
  47.  
  48. public void setName(String name) {
  49. this.name = name;
  50. }
  51.  
  52. public int getId() {
  53. return id;
  54. }
  55.  
  56. public void setId(int id) {
  57. this.id = id;
  58. }
  59. }
  60.  
  61. package com.library.models;
  62.  
  63. public class Book {
  64.  
  65. private BookTitle bookTitle;
  66. private int id;
  67.  
  68. public Book(BookTitle bookTitle, int id) {
  69. this.bookTitle = bookTitle;
  70. this.id = id;
  71. }
  72.  
  73. public BookTitle getBookTitle() {
  74. return bookTitle;
  75. }
  76.  
  77. public String getTitle() {
  78. return getBookTitle().getTitle();
  79. }
  80.  
  81. public void setTitle(String title) {
  82. this.getBookTitle().setTitle(title);
  83. }
  84.  
  85. public Author getAuthor() {
  86. return getBookTitle().getAuthor();
  87. }
  88.  
  89. public void setAuthor(Author author) {
  90. this.getBookTitle().setAuthor(author);
  91. }
  92.  
  93. public int getId() {
  94. return id;
  95. }
  96.  
  97. public void setId(int id) {
  98. this.id = id;
  99. }
  100.  
  101. }
  102.  
  103. package com.library.models;
  104.  
  105. import com.library.users.Customer;
  106.  
  107. import java.io.FileWriter;
  108. import java.io.IOException;
  109. import java.time.LocalTime;
  110. import java.time.Period;
  111. import java.util.ArrayList;
  112. import java.util.Collections;
  113. import java.util.HashSet;
  114. import java.util.List;
  115.  
  116. public class BookDirectory {
  117. private ArrayList<Book> books;
  118. private HashSet<Author> authors;
  119. private HashSet<BookTitle> bookTitles;
  120.  
  121. public void addBook(BookTitle bookTitle, int id) {
  122. Book newBook = new Book(bookTitle, id);
  123. books.add(newBook);
  124. }
  125.  
  126. public boolean hasBook(Book book) {
  127. return books.contains(book);
  128. }
  129.  
  130. public void deleteBook(Book book) {
  131. books.remove(book);
  132. }
  133.  
  134. public void deleteAllBooks() {
  135. books.clear();
  136. }
  137.  
  138. public int getBookCount() {
  139. return books.size();
  140. }
  141.  
  142. public void addAuthor(String name) {
  143. authors.add(new Author(name));
  144. }
  145.  
  146. public void clearBookDirectory() {
  147. books.clear();
  148. authors.clear();
  149. }
  150.  
  151. public void addBookTitle(String title, Author author, int id) {
  152. bookTitles.add(new BookTitle(title, author, id));
  153. }
  154.  
  155. public int getBookTitlesCount() {
  156. return bookTitles.size();
  157. }
  158.  
  159. public String toString() {
  160. return books.toString();
  161. }
  162.  
  163. public List<Book> getAllBooks() {
  164. return books;
  165. }
  166.  
  167. public HashSet<BookTitle> getAllBookTitles() {
  168. return bookTitles;
  169. }
  170.  
  171. }
  172.  
  173. package com.library.models;
  174.  
  175. public class BookTitle {
  176. private String title;
  177. private Author author;
  178. private int id;
  179.  
  180. public BookTitle(String title, Author author, int id) {
  181. this.title = title;
  182. this.author = author;
  183. this.id = id;
  184. }
  185.  
  186. public String getTitle() {
  187. return title;
  188. }
  189.  
  190. public void setTitle(String title) {
  191. this.title = title;
  192. }
  193.  
  194. public Author getAuthor() {
  195. return author;
  196. }
  197.  
  198. public void setAuthor(Author author) {
  199. this.author = author;
  200. }
  201.  
  202. public int getId() {
  203. return id;
  204. }
  205.  
  206. public void setId(int id) {
  207. this.id = id;
  208. }
  209. }
  210.  
  211. package com.library.models;
  212.  
  213. import com.library.users.Customer;
  214.  
  215. import java.time.LocalDate;
  216. import java.time.Period;
  217.  
  218. public class Rent {
  219. private Customer customer;
  220. private Book book;
  221. LocalDate dateOfRent;
  222. Period period;
  223.  
  224. public Rent(Customer customer, Book book, Period period) {
  225. this.customer = customer;
  226. this.book = book;
  227. this.period = period;
  228. dateOfRent = LocalDate.now();
  229. }
  230.  
  231. public Customer getCustomer() {
  232. return customer;
  233. }
  234.  
  235. public void setCustomer(Customer customer) {
  236. this.customer = customer;
  237. }
  238.  
  239. public Book getBook() {
  240. return book;
  241. }
  242.  
  243. public void setBook(Book book) {
  244. this.book = book;
  245. }
  246.  
  247. public LocalDate getDateOfRent() {
  248. return dateOfRent;
  249. }
  250.  
  251. public void setDateOfRent(LocalDate dateOfRent) {
  252. this.dateOfRent = dateOfRent;
  253. }
  254.  
  255. public Period getPeriod() {
  256. return period;
  257. }
  258.  
  259. public void setPeriod(Period period) {
  260. this.period = period;
  261. }
  262. }
  263.  
  264. package com.library.models;
  265.  
  266. public enum UserPrivilige {
  267. USER, ADMIN, SUPERADMIN
  268. }
  269.  
  270. package com.library.service;
  271.  
  272. import com.library.models.Rent;
  273. import com.library.users.Customer;
  274. import com.library.users.User;
  275.  
  276. import java.time.LocalDate;
  277. import java.time.Period;
  278. import java.util.ArrayList;
  279. import java.util.HashSet;
  280. import java.util.Set;
  281.  
  282. public class Alert {
  283.  
  284. private static HashSet<Customer> customers;
  285.  
  286. public ArrayList<Rent> getOverdueRents() {
  287. return getSoonOverdueRents(Period.ZERO);
  288. }
  289.  
  290. public static HashSet<Customer> getCustomers() {
  291. return customers;
  292. }
  293.  
  294. public static void setCustomers(HashSet<Customer> customers) {
  295. Alert.customers = customers;
  296. }
  297.  
  298. public ArrayList<Rent> getSoonOverdueRents(Period period) {
  299. ArrayList<Rent> result = new ArrayList<Rent>();
  300. for (Customer customer : customers) {
  301. for (Rent rent : customer.getRentedBooks()) {
  302. if (rent.getDateOfRent().plus(rent.getPeriod()).isAfter(LocalDate.now().minus(period))) {
  303. result.add(rent);
  304. }
  305. }
  306. }
  307. return result;
  308. }
  309.  
  310. public ArrayList<Rent> getAllRents() {
  311. ArrayList<Rent> result = new ArrayList<Rent>();
  312. for (Customer customer : customers) {
  313. for (Rent rent : customer.getRentedBooks()) {
  314. result.add(rent);
  315. }
  316. }
  317. return result;
  318. }
  319. }
  320.  
  321. package com.library.service;
  322.  
  323. import com.library.models.UserPrivilige;
  324. import com.library.users.User;
  325.  
  326. import java.util.HashSet;
  327. import java.util.List;
  328. import java.util.Set;
  329.  
  330. public class Authentification {
  331. private Set<User> userSet;
  332. public Authentification(Set<User> userSet){
  333. this.userSet = userSet;
  334. }
  335.  
  336. public boolean isLoginValid(String name, String password) {
  337. for (User user : userSet) {
  338. if (user.getName().equals(name) && user.getPassword().equals(password)) {
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344.  
  345. public boolean isAdmin(User user) {
  346. return user.getPrivilige().compareTo(UserPrivilige.ADMIN) >= 0;
  347. }
  348. }
  349.  
  350. package com.library.service;
  351.  
  352. import com.library.models.Book;
  353. import com.library.models.BookDirectory;
  354. import com.library.models.BookTitle;
  355. import com.library.models.Rent;
  356. import com.library.users.Customer;
  357. import com.library.users.User;
  358.  
  359. import java.io.FileWriter;
  360. import java.io.IOException;
  361. import java.time.LocalTime;
  362. import java.util.HashSet;
  363.  
  364. public class FileGenerator {
  365. private static BookDirectory bookDirectory = null;
  366.  
  367. public static BookDirectory getBookDirectory() {
  368. return bookDirectory;
  369. }
  370.  
  371. public static void setBookDirectory(BookDirectory bookDirectory) {
  372. FileGenerator.bookDirectory = bookDirectory;
  373. }
  374.  
  375. public static void createDirectoryBookListFile() {
  376. try {
  377. FileWriter fileWriter = new FileWriter("book_list_" + LocalTime.now().toString() + ".txt");
  378. fileWriter.write("TitletAuthortQuantityn");
  379. for (BookTitle bookTitle : bookDirectory.getAllBookTitles()) {
  380. Integer bookQuantity = 0;
  381. for (Book book : bookDirectory.getAllBooks()) {
  382. if (book.getBookTitle().equals(bookTitle))
  383. bookQuantity++;
  384. }
  385. fileWriter.write(bookTitle.getTitle() + "t" + bookTitle.getAuthor() +
  386. "t" + bookQuantity.toString() + "n");
  387. }
  388. fileWriter.close();
  389. } catch (IOException e) {
  390. e.printStackTrace();
  391. }
  392. }
  393.  
  394. public void createCustomerBookListFile(Customer customer) {
  395. try {
  396. FileWriter fileWriter = new FileWriter("book_list_" + customer.getName() + "_" +
  397. LocalTime.now().toString() + ".txt");
  398. fileWriter.write("BooktDate of RenttRent Periodn");
  399. for (Rent rent : customer.getRentedBooks()) {
  400. fileWriter.write(rent.getBook().getTitle() + "t" + rent.getDateOfRent() +
  401. "t" + rent.getPeriod() + "n");
  402. }
  403. fileWriter.close();
  404. } catch (IOException e) {
  405. e.printStackTrace();
  406. }
  407. }
  408. }
  409.  
  410. package com.library.service;
  411.  
  412. import com.library.models.Book;
  413. import com.library.models.BookDirectory;
  414.  
  415. import java.util.ArrayList;
  416. import java.util.List;
  417.  
  418. public class Search {
  419. private BookDirectory dir;
  420.  
  421. public Search(BookDirectory dir) {
  422. this.dir = dir;
  423. }
  424.  
  425. public List<Book> search(String phrase) {
  426. List<Book> results = new ArrayList<Book>();
  427. for (Book b : dir.getAllBooks()) {
  428. if (b.getAuthor().getName().equals(phrase) || b.getTitle().equals(phrase))
  429. results.add(b);
  430. }
  431. return results;
  432. }
  433. }
  434.  
  435. package com.library.service;
  436.  
  437. import com.library.users.Customer;
  438. import com.library.users.User;
  439.  
  440. import java.util.HashSet;
  441. import java.util.List;
  442. import java.util.Set;
  443.  
  444. public class UserRegistration {
  445. private String name;
  446. private String email;
  447. private String password;
  448.  
  449. private static HashSet<User> users;
  450. private static HashSet<Customer> customers;
  451.  
  452. public static HashSet<User> getUsers() {
  453. return users;
  454. }
  455.  
  456. public static void setUsers(HashSet<User> users) {
  457. UserRegistration.users = users;
  458. }
  459.  
  460. public static HashSet<Customer> getCustomers() {
  461. return customers;
  462. }
  463.  
  464. public static void setCustomers(HashSet<Customer> customers) {
  465. UserRegistration.customers = customers;
  466. }
  467.  
  468. public String getName() {
  469. return name;
  470. }
  471.  
  472. public void setName(String name) {
  473. this.name = name;
  474. }
  475.  
  476. public String getEmail() {
  477. return email;
  478. }
  479.  
  480. public void setEmail(String email) {
  481. this.email = email;
  482. }
  483.  
  484. public String getPassword() {
  485. return password;
  486. }
  487.  
  488. public void setPassword(String password) {
  489. this.password = password;
  490. }
  491.  
  492. public boolean isRegistrationValid() {
  493. int nameLength = name.length();
  494. int passwordLength = password.length();
  495. boolean passwordHasDigit = password.matches("\d+");
  496. boolean isEmailValid = email.matches("^[a-zA-Z0-9._+-]+@[a-zA-Z._-]+.[a-zA-Z0-9]]$");
  497. return nameLength > 3 && passwordLength > 6 && passwordHasDigit && isEmailValid;
  498. }
  499.  
  500. public boolean registerUser() {
  501. if (isRegistrationValid()) {
  502. boolean isNameFree = true;
  503. for (User user : users) {
  504. if (user.getName().equals(name)) {
  505. isNameFree = false;
  506. break;
  507. }
  508. }
  509. if (isNameFree) {
  510. users.add(new User(name, email, password));
  511. return true;
  512. }
  513. }
  514. return false;
  515. }
  516.  
  517. public boolean registerCustomer() {
  518. if (!registerUser())
  519. return false;
  520. customers.add(new Customer(name, email, password));
  521. return true;
  522. }
  523.  
  524. }
  525.  
  526. package com.library.users;
  527.  
  528. import com.library.models.Book;
  529. import com.library.models.UserPrivilige;
  530.  
  531. import java.util.ArrayList;
  532.  
  533. public class User {
  534. protected String name;
  535. protected String email;
  536. protected String password;
  537. private int id;
  538. private UserPrivilige privilige = UserPrivilige.USER;
  539. private static int nextUserId = 0;
  540.  
  541. public User(String name, String email, String password) {
  542. this.name = name;
  543. this.email = email;
  544. this.password = password;
  545. this.id = nextUserId;
  546. nextUserId++;
  547. }
  548.  
  549. public String getName() {
  550. return name;
  551. }
  552.  
  553. public void setName(String name) {
  554. this.name = name;
  555. }
  556.  
  557. public String getEmail() {
  558. return email;
  559. }
  560.  
  561. public void setEmail(String email) {
  562. this.email = email;
  563. }
  564.  
  565. public String getPassword() {
  566. return password;
  567. }
  568.  
  569. public void setPassword(String password) {
  570. this.password = password;
  571. }
  572.  
  573. public int getId() {
  574. return id;
  575. }
  576.  
  577. public void setId(int id) {
  578. this.id = id;
  579. }
  580.  
  581. public UserPrivilige getPrivilige() {
  582. return privilige;
  583. }
  584.  
  585. public void setPrivilige(UserPrivilige privilige) {
  586. this.privilige = privilige;
  587. }
  588. }
  589.  
  590. package com.library.users;
  591.  
  592. import com.library.models.Book;
  593. import com.library.models.BookDirectory;
  594. import com.library.models.Rent;
  595.  
  596. import java.io.FileWriter;
  597. import java.io.IOException;
  598. import java.time.LocalTime;
  599. import java.time.Period;
  600. import java.util.ArrayList;
  601.  
  602. public class Customer extends User {
  603.  
  604. private ArrayList<Rent> rentedBooks;
  605. private static BookDirectory bookDirectory;
  606.  
  607. public Customer(String name, String email, String password) {
  608. super(name, email, password);
  609. rentedBooks = new ArrayList<Rent>();
  610. }
  611.  
  612. public static BookDirectory getBookDirectory() {
  613. return bookDirectory;
  614. }
  615.  
  616. public static void setBookDirectory(BookDirectory bookDirectory) {
  617. Customer.bookDirectory = bookDirectory;
  618. }
  619.  
  620. public ArrayList<Rent> getRentedBooks() {
  621. return rentedBooks;
  622. }
  623.  
  624. public boolean rentBook(Book book, Period period) {
  625. if (bookDirectory.hasBook(book)) {
  626. getRentedBooks().add(new Rent(this, book, period));
  627. return true;
  628. }
  629. return false;
  630. }
  631.  
  632. public void returnBook(Rent rent) {
  633. rentedBooks.remove(rent);
  634. }
  635. }
  636.  
  637. package com.library.users;
  638.  
  639. import com.library.models.Author;
  640. import com.library.models.Book;
  641.  
  642. public class Admin extends User {
  643. public Admin(String name, String email, String password) {
  644. super(name, email, password);
  645. }
  646. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement