diff --git a/Answers/40230112090/Admin.java b/Answers/40230112090/Admin.java
new file mode 100644
index 0000000..adae026
--- /dev/null
+++ b/Answers/40230112090/Admin.java
@@ -0,0 +1,155 @@
+package org.example;
+
+import java.sql.*;
+import java.util.Random;
+
+public class Admin extends NormalUsers {
+
+ public static final String URL = "jdbc:mysql://localhost/Library";
+ public static final String USER = "javalibrary";
+ public static final String PASS = "1384";
+ public static final String INSERT_QUERY = "INSERT INTO admins (id, adminName, adminPassword, phoneNumber) VALUES (?, ?, ?, ?)";
+ public static final String INSERT_QUERY2 = "INSERT INTO books (id, title, writer, description) VALUES (?, ?, ?, ?)";
+ private final String admin_id;
+ Book book1 = new Book();
+
+ public Admin() {
+ this.admin_id = String.valueOf(new Random().nextInt(20000) + 10000);
+
+
+ }
+
+ public void addAdmin(String adminName, String password, String phoneNumber) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS)) {
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY);
+ ResultSet resultSet = statement.executeQuery("SELECT * FROM admins");
+ if (resultSet.next()) {
+ String admin_name = resultSet.getString("adminName");
+ String admin_password = resultSet.getString("adminPassword");
+ String admin_phoneNumber = resultSet.getString("phoneNumber");
+ if (adminName.equals(admin_name) && password.equals(admin_password) && phoneNumber.equals(admin_phoneNumber)) {
+ System.out.print("Another person is registered with the profile");
+
+ } else {
+ statement.setString(1, this.admin_id);
+ statement.setString(2, adminName);
+ statement.setString(3, password);
+ statement.setString(4, super.phoneNumber);
+ System.out.print("Dear admin,your profile has been registered");
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ public void removeAdmin(String adminName, String password) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS)) {
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY);
+ Statement statement1 = connection.createStatement();
+ ResultSet resultSet = statement.executeQuery("SELECT * FROM admins");
+ if (resultSet.next()) {
+ String admin_name = resultSet.getString("adminName");
+ String admin_password = resultSet.getString("adminPassword");
+ if (adminName.equals(admin_name) && password.equals(admin_password)) {
+ statement1.executeUpdate("DELETE FROM admins WHERE adminPassword = " + password);
+ } else {
+ System.out.println("Admin with this profile was not found");
+ }
+ }
+
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void addUser(String username, String phoneNumber, String password, int number, int capacity) {
+ if (number < capacity) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement("INSERT INTO normal_users (id, username, phoneNumber, password, localDate) VALUES (?, ?, ?, ?, ?)")) {
+ statement.setString(1, super.id);
+ statement.setString(2, username);
+ statement.setString(3, phoneNumber);
+ statement.setString(4, password);
+ statement.setString(5, this.localDate.toString());
+ statement.executeUpdate();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ } else {
+ System.out.println();
+ }
+ }
+
+ public void removeUser(String userName, String userPassword) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement("INSERT INTO normal_users (id, username, phoneNumber, password, localDate) VALUES (?, ?, ?, ?, ?)")) {
+ Statement statement1 = connection.createStatement();
+ ResultSet resultSet = statement.executeQuery("SELECT * FROM normal_users");
+ if (resultSet.next()) {
+ String user_name = resultSet.getString("username");
+ String user_password = resultSet.getString("password");
+ if (userName.equals(user_name) && userPassword.equals("user_password")) {
+ statement1.executeUpdate("DELETE FROM normal_users WHERE adminPassword = " + userPassword);
+ } else {
+ System.out.println("User with this profile was not found");
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void addBook(String title, String writer, String description) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY2)) {
+ ResultSet resultSet = statement.executeQuery("SELECT * FROM books");
+ if (resultSet.next()) {
+ String bookName = resultSet.getString("title");
+ String bookWriter = resultSet.getString("writer");
+ if (title.equals(bookName) && writer.equals(bookWriter)) {
+ System.out.println("Whit this specification, a book has already been registered");
+ }
+ } else {
+ statement.setString(1, book1.id);
+ statement.setString(2, title);
+ statement.setString(3, writer);
+ statement.setString(4, description);
+ statement.executeUpdate();
+ }
+
+ statement.executeUpdate();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void removeBook(String bookName, String bookWriter) {
+ try (Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY2);
+ PreparedStatement statement1 = connection.prepareStatement("DELETE FROM normal_users WHERE title = ? AND writer = ?");
+ ResultSet resultSet = statement.executeQuery("SELECT title , writer from books");) {
+ String name_book = resultSet.getString("title");
+ String writer_book = resultSet.getString("writer");
+ if (bookName.equals(name_book) && bookWriter.equals(writer_book)) {
+ }
+ statement1.setString(1, bookName);
+ statement1.setString(2, bookWriter);
+ statement1.executeUpdate();
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+}
+
+
+
+
+
+
+
diff --git a/Answers/40230112090/Book.java b/Answers/40230112090/Book.java
new file mode 100644
index 0000000..718adf8
--- /dev/null
+++ b/Answers/40230112090/Book.java
@@ -0,0 +1,82 @@
+package org.example;
+
+import java.sql.*;
+import java.util.*;
+
+public class Book {
+
+ private static final String URL = "jdbc:mysql://localhost/Library";
+ private static final String USER = "javalibrary";
+ private static final String PASS = "1384";
+ private static final String INSERT_QUERY = "INSERT INTO books (id, title, writer, description) VALUES (?, ?, ?, ?)";
+ private static final String SELECT_RENT_QUERY = "SELECT rents.rent, rents.title FROM rents JOIN books ON books.id = rents.id_of_title ;";
+
+
+ public String title;
+ public String id;
+
+ public Book() {
+ this.id = String.valueOf(new Random().nextInt(20000) + 10000);
+ }
+
+
+ public String title(String input) {
+ Scanner scanner = new Scanner(System.in);
+ input = input.trim();
+ while (input.isEmpty()) {
+ System.out.print("Please enter your first name and last name : ");
+ input = scanner.nextLine().trim();
+ }
+ String[] words = input.split(" ");
+ StringBuilder formatted = new StringBuilder();
+ for (String word : words) {
+ formatted.append(Character.toUpperCase(word.charAt(0)))
+ .append(word.substring(1).toLowerCase())
+ .append(" ");
+ }
+ return formatted.toString().trim();
+ }
+
+
+ public void viewAvailableBooks() {
+ // View available books for rental
+ try {
+ Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY);
+ ResultSet resultSet = statement.executeQuery("SELECT books.id , books.title , books.writer , rents.rent FROM books LEFT JOIN rents ON rents.id_of_title = books.id;");
+ int n = 1;
+
+ while (resultSet.next()) {
+// System.out.println(resultSet.getString("rent"));
+ if ("null".equals(String.valueOf(resultSet.getString("rents.rent")))) {
+ System.out.println(resultSet.getString("id") + "-" + resultSet.getString("title") + " from " + resultSet.getString("writer"));
+ n++;
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public void viewRentBooks() {
+
+ try {
+ Connection connection = DriverManager.getConnection(URL, USER, PASS);
+ PreparedStatement statement = connection.prepareStatement(INSERT_QUERY);
+ ResultSet resultSet = statement.executeQuery(SELECT_RENT_QUERY);
+ int n = 1;
+ while (resultSet.next()) {
+ if ("rent".equals(String.valueOf(resultSet.getString("rent")))) {
+ System.out.println(n + "-" + resultSet.getString("title"));
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+}
diff --git a/Answers/40230112090/Library.java b/Answers/40230112090/Library.java
new file mode 100644
index 0000000..57f014b
--- /dev/null
+++ b/Answers/40230112090/Library.java
@@ -0,0 +1,181 @@
+
+package org.example;
+
+import java.time.*;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class Library {
+ public Library() {
+ Scanner input = new Scanner(System.in);
+ Admin admin = new Admin();
+ NormalUsers normalUsers = new NormalUsers();
+ Book book = new Book();
+ Rent rent = new Rent();
+ String idInMysql = "SELECT id FROM normal_users WHERE username = ? AND password = ?";
+ String updateIdInMysql = "UPDATE normal_users SET password = ? WHERE username = ? AND phoneNumber = ?";
+ String idOfAdminInMysql = "SELECT id FROM admins WHERE adminName = ? AND adminPassword = ?";
+ String updateOfAdminInMysql = "UPDATE admin SET adminPassword = ? WHERE adminName = ? AND phoneNumber = ?";
+ LocalTime localTime = LocalTime.now();
+ int hoursOfWork = localTime.getHour();
+ int capacity = 0, number = 0;
+ if (hoursOfWork > 9 && hoursOfWork < 17) {
+ String name;
+ String password;
+ System.out.println(" Hell, you are very welcome!!");
+
+ while (true) {
+ System.out.println("Please enter your name and password :");
+ System.out.print("Name : ");
+ name = input.nextLine();
+ System.out.print("Password : ");
+ password = input.nextLine();
+
+ if (normalUsers.getPassword(name, password, idOfAdminInMysql, updateOfAdminInMysql).equals("success")) {
+ if (capacity < 50) {
+ while (true) {
+ System.out.println(" >>>Admin section<<<");
+ System.out.print("""
+ 1-lib add book
+ 2-lib remove book
+ 3-lib add user
+ 4-lib remove user
+ 5-lib add admin
+ 6-lib remove admin
+ 7-lib increase capacity
+ 8-lib capacity reduction
+ 7-lib
+ Please enter the desired word :
+ """);
+ String word = (input.nextLine()).toLowerCase();
+ Pattern input1 = Pattern.compile("^lib\\s+(rent|return)\\s+<([a-z\\s]+|\\d{2})>\\s+<([a-z\\s]+|\\d{10})>\\s+<([a-z\\s]+|\\d{4,9})>$");
+ Matcher input11 = input1.matcher(word);
+ String firstValue = "";
+ String secondValue = "";
+ String thirdValue = "";
+ if (input11.matches()) {
+ firstValue = input11.group(2);
+ secondValue = input11.group(3);
+ thirdValue = input11.group(4);
+ }
+ if (word.equals("lib add book <" + firstValue + "> <" + secondValue + "> <" + thirdValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = book.title(secondValue);
+ thirdValue = book.title(thirdValue);
+ admin.addBook(firstValue, secondValue, thirdValue);
+ }
+ if (word.equals("lib remove book <" + firstValue + "> <" + secondValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = book.title(secondValue);
+ admin.removeBook(firstValue, secondValue);
+ }
+ if (word.equals("lib add user <" + firstValue + "> <" + secondValue + "> <" + thirdValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = "+98" + secondValue;
+ admin.addUser(firstValue, secondValue, thirdValue, number, capacity);
+ number++;
+ }
+ if (word.equals("lib remove user <" + firstValue + "> <" + secondValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = "+98" + secondValue;
+ admin.removeUser(firstValue, secondValue);
+ }
+ if (word.equals("lib add admin <" + firstValue + "> <" + secondValue + "> <" + thirdValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = "+98" + secondValue;
+ admin.addAdmin(firstValue, thirdValue, secondValue);
+ }
+ if (word.equals("lib remove admin <" + firstValue + "> <" + secondValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = "+98" + secondValue;
+ admin.removeAdmin(firstValue, secondValue);
+ }
+ if (word.equals("lib <" + firstValue + ">")) {
+ break;
+ }
+ if (word.equals("lib increase capacity <" + firstValue + ">")) {
+ capacity = capacity + Integer.parseInt(firstValue);
+ }
+ if (word.equals("lib capacity reduction <" + firstValue + ">")) {
+ int amount = capacity - number;
+ if (capacity - Integer.parseInt(firstValue) < number) {
+ System.out.println("You can't reduce the capacity of the library by " + firstValue + " people because the gap between the number of people who have registered and the capacity of the library is " + amount + " people");
+ } else {
+ capacity = capacity - Integer.parseInt(firstValue);
+ }
+ }
+ }
+ } else {
+ System.out.println();
+ }
+ }
+
+ if (normalUsers.getPassword(name, password, idInMysql, updateIdInMysql).equals("success")) {
+
+ while (true) {
+ System.out.println(" >>>User section<<<");
+ System.out.print("""
+ Guide:
+ 1-lib get available books
+ 2-lib rent
+ 3-lib rent a specific member.
+ 3-lib return
+ 5-lib my books
+ 6-lib
+ Please enter the desired word :
+ """);
+ String word = (input.nextLine()).toLowerCase();
+ Pattern input1 = Pattern.compile("^lib\\s+(rent|return)\\s+<([a-z\\s]+)>\\s+<([a-z\\s]+)>\\s+<([a-z\\s]+)>\\s+<(\\d{4,9})>$");
+ Matcher input11 = input1.matcher(word);
+ String firstValue = "";
+ String secondValue = "";
+ String thirdValue = "";
+ String fourthValue = "";
+ if (input11.matches()) {
+ firstValue = input11.group(2);
+ secondValue = input11.group(3);
+ thirdValue = input11.group(4);
+ fourthValue = String.valueOf(input11.group(5));
+
+ }
+
+ if (word.equals("lib get available books")) {
+ book.viewAvailableBooks();
+ }
+ if (word.equals("lib rent <" + firstValue + "> <" + secondValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = book.title(secondValue);
+ rent.rentBook(firstValue, secondValue);
+
+ }
+ if (word.equals("lib rent <" + firstValue + "> <" + secondValue + "> <" + thirdValue + "> <" + fourthValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = book.title(secondValue);
+ thirdValue = book.title(thirdValue);
+ fourthValue = book.title(fourthValue);
+ rent.rentBookForSpecialMember(firstValue, secondValue, thirdValue, fourthValue);
+ }
+ if (word.equals(" return <" + firstValue + "> <" + secondValue + ">")) {
+ firstValue = book.title(firstValue);
+ secondValue = book.title(secondValue);
+ rent.returnBook(firstValue, secondValue);
+ }
+
+ if (word.equals("lib my books")) {
+ rent.rentedOfBook();
+ }
+ if (word.equals("lib <" + firstValue + ">")) {
+ break;
+ }
+ }
+ }
+
+ }
+ } else {
+ System.out.println("I apologize,but the working hours of the library are from 9 am to 5 pm.Please visit the library tomorrow.");
+ }
+ }
+}
+
+
diff --git a/Answers/40230112090/Main.java b/Answers/40230112090/Main.java
new file mode 100644
index 0000000..5a02451
--- /dev/null
+++ b/Answers/40230112090/Main.java
@@ -0,0 +1,15 @@
+package org.example;
+
+import java.io.*;
+import java.sql.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Scanner;
+
+
+public class Main {
+ public static void main(String[] args){
+ new MyApp();
+ }
+}
diff --git a/Answers/40230112090/MyApp.java b/Answers/40230112090/MyApp.java
new file mode 100644
index 0000000..51cdd47
--- /dev/null
+++ b/Answers/40230112090/MyApp.java
@@ -0,0 +1,7 @@
+package org.example;
+
+public class MyApp {
+ public MyApp() {
+ new Library();
+ }
+}
diff --git a/Answers/40230112090/NormalUsers.java b/Answers/40230112090/NormalUsers.java
new file mode 100644
index 0000000..ea4ecd0
--- /dev/null
+++ b/Answers/40230112090/NormalUsers.java
@@ -0,0 +1,147 @@
+package org.example;
+
+import java.sql.*;
+import java.time.LocalDate;
+import java.util.*;
+
+public class NormalUsers extends Book {
+ public String id;
+ public String phoneNumber;
+ public LocalDate localDate;
+ public HashMap map = new HashMap<>();
+ public Scanner input = new Scanner(System.in);
+
+
+ public NormalUsers() {
+ this.localDate = LocalDate.now();
+ Random random = new Random();
+ this.id = String.format("%04d", random.nextInt(10000));
+ }
+
+ public String getPassword(String name, String password, String idInMysql, String updateIdInMysql) {
+ String userName1;
+ String password1;
+ int numberOfRounds = 0;
+ Scanner input = new Scanner(System.in);
+
+ while (true) {
+ if (numberOfRounds > 0) {
+ System.out.println("Please enter the correct password: \n(Enter the number 1 to enter the forgotten password section and enter 2 to exit)");
+ String numberForForget = input.nextLine();
+ if ("2".equals(numberForForget)) {
+ return "exit";
+ } else if ("1".equals(numberForForget)) {
+ forgetPassword(updateIdInMysql); // Call to a method that handles password recovery
+ return "Please log in to the user section again";
+ }
+ }
+
+ userName1 = title(name.trim());
+ password1 = password.trim();
+
+ if (password1.length() > 3 && password1.matches("\\d{4,9}")) {
+ if (userApproval(userName1, password1, idInMysql)) {
+ return "success";
+ } else {
+ System.out.println("Incorrect username or password.");
+ }
+ } else {
+ System.out.println("Password must be longer than 3 digits and contain only numbers.");
+ }
+ numberOfRounds++;
+ }
+
+ }
+
+ private boolean userApproval(String userName, String password, String idInMysql) {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384");
+ PreparedStatement statement = connection.prepareStatement(idInMysql)) {
+ statement.setString(1, userName);
+ statement.setString(2, password);
+ ResultSet resultSet = statement.executeQuery();
+ if (resultSet.next()) {
+ String idOfMember = resultSet.getString("id");
+ map.put(idOfMember, userName);
+ return true;
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+
+ public void forgetPassword(String updateIdInMysql) {
+ System.out.print("Please enter your username: ");
+ String username = input.nextLine().trim();
+ String username1 = title(username);
+ int numberOfRounds = 0;
+ while (true) {
+ if (numberOfRounds > 0) {
+ System.out.println("Please enter the correct mobile number:");
+ } else {
+ System.out.print("Please enter your phone number: ");
+ }
+
+ String phoneNumber = "+98" + input.nextLine().trim();
+ String newPassword = "";
+ if (phoneNumber.matches("\\+98\\d{10}")) {
+
+ int numberOfRounds2 = 0;
+ while (true) {
+ if (numberOfRounds2 > 0) {
+ System.out.println("Please enter the correct password:");
+ }
+ System.out.print("New Password: ");
+ newPassword = input.nextLine().trim();
+
+ if (newPassword.matches("\\d{4,9}")) {
+ } else {
+ System.out.println("Invalid password. Password must be at least 4 digits long and contain only numbers.");
+ }
+ numberOfRounds++;
+
+ if (newPassword != null) {
+ updatePasswordInDatabase(username1, phoneNumber, newPassword, updateIdInMysql);
+ break;
+ }
+ }
+ } else {
+ System.out.println("Invalid phone number. Please try again.");
+ }
+ numberOfRounds++;
+ if (!newPassword.isEmpty()) {
+ break;
+ }
+ }
+ }
+
+ private void updatePasswordInDatabase(String username, String phoneNumber, String newPassword, String updateIdInMysql) {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384");
+ PreparedStatement statement = connection.prepareStatement(updateIdInMysql)) {
+ statement.setString(1, newPassword);
+ statement.setString(2, username);
+ statement.setString(3, phoneNumber);
+ int rowsAffected = statement.executeUpdate();
+ if (rowsAffected > 0) {
+ System.out.println("Password changed successfully.");
+ } else {
+ System.out.println("No user found with the provided username and phone number.");
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Answers/40230112090/Rent.java b/Answers/40230112090/Rent.java
new file mode 100644
index 0000000..b00a6f8
--- /dev/null
+++ b/Answers/40230112090/Rent.java
@@ -0,0 +1,156 @@
+
+
+package org.example;
+
+import java.sql.*;
+import java.util.*;
+
+public class Rent extends NormalUsers {
+ org.example.Book book = new org.example.Book();
+
+ public void rentBook(String titleForBook, String writerForBook) {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384")) {
+ try (PreparedStatement statement = connection.prepareStatement("SELECT books.id, books.title, books.writer, normal_users.id AS user_id, normal_users.username FROM books JOIN normal_users LEFT JOIN rents ON books.id = rents.id_of_title WHERE books.title = ? AND books.writer = ? AND rents.id_of_title IS NULL")) {
+ statement.setString(1, titleForBook);
+ statement.setString(2, writerForBook);
+ ResultSet resultSet = statement.executeQuery();
+ if (resultSet.next()) {
+ String bookId = resultSet.getString("id");
+ String userId = resultSet.getString("user_id");
+ String username = resultSet.getString("username");
+
+ try (PreparedStatement insert = connection.prepareStatement(" INSERT INTO rents (id_of_member, member, id_of_title, title) VALUES ('12343', 'amir', '1111', 'summer');")) {
+ insert.setString(1, userId);
+ insert.setString(2, username);
+ insert.setString(3, bookId);
+ insert.setString(4, titleForBook);
+ insert.setString(5, "rent");
+ insert.executeUpdate();
+ System.out.println("Book rented successfully.");
+ }
+ } else {
+ System.out.println("Book not available for rent or does not exist.");
+ }
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void rentBookForSpecialMember(String titleForBook, String writerForBook, String nameForMember, String passwordForMember) {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384")) {
+ try (PreparedStatement statement = connection.prepareStatement("SELECT books.id AS book_id, books.title, books.writer, normal_users.id AS user_id, normal_users.username FROM books JOIN normal_users LEFT JOIN rents ON books.id = rents.id_of_title WHERE books.title = ? AND books.writer = ? AND rents.id_of_title IS NULL")) {
+ statement.setString(1, titleForBook);
+ statement.setString(2, writerForBook);
+ ResultSet resultSet = statement.executeQuery();
+ if (resultSet.next()) {
+ String bookId = resultSet.getString("book_id");
+ try (PreparedStatement statement1 = connection.prepareStatement(
+ "SELECT id, username FROM normal_users WHERE username = ? AND password = ?")) {
+ statement1.setString(1, nameForMember);
+ statement1.setString(2, passwordForMember);
+ ResultSet resultSet1 = statement1.executeQuery();
+ if (resultSet1.next()) {
+ String userId = resultSet1.getString("id");
+ String username = resultSet1.getString("username");
+ try (PreparedStatement insert = connection.prepareStatement(
+ "INSERT INTO rents (id_of_member, member, id_of_title, title , rent) VALUES (?, ?, ?, ? , ?)")) {
+ insert.setString(1, userId);
+ insert.setString(2, username);
+ insert.setString(3, bookId);
+ insert.setString(4, titleForBook);
+ insert.setString(5, "rent");
+ insert.executeUpdate();
+ System.out.println("Book rented successfully.");
+ }
+ } else {
+ System.out.println("Member not found or incorrect password.");
+ }
+ }
+ } else {
+ System.out.println("Book not available for rent or does not exist.");
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ public void returnBook(String nameOfBook, String writerOfBook) {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384")) {
+
+ try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT books.id, books.title, books.writer, rents.id_of_member, rents.member, rents.rent FROM books LEFT JOIN rents ON books.id = rents.id_of_title WHERE books.title = ? AND books.writer = ?")) {
+ preparedStatement.setString(1, nameOfBook);
+ preparedStatement.setString(2, writerOfBook);
+ ResultSet resultSet = preparedStatement.executeQuery();
+ if (resultSet.next()) {
+ String bookId = resultSet.getString("id");
+ String memberId = resultSet.getString("id_of_member");
+ String rentStatus = resultSet.getString("rent");
+ for (Map.Entry entry : map.entrySet()) {
+ if ("rent".equals(rentStatus) && memberId.equals(entry.getKey())) {
+ try (PreparedStatement updateBook = connection.prepareStatement("SELECT books.id, books.title, books.writer, rents.id_of_member, rents.member, rents.rent FROM books LEFT JOIN rents ON books.id = rents.id_of_title WHERE books.title = ? AND books.writer = ?")) {
+ updateBook.setString(1, "available");
+ updateBook.setString(2, bookId);
+ updateBook.executeUpdate();
+ System.out.println("The book has been returned to the library.");
+ }
+ } else {
+ System.out.println("The book is not currently rented out or does not match the member's records.");
+ }
+ }
+ } else {
+ System.out.println("The desired book was not found.");
+ }
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ public void rentedOfBook() {
+ try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/Library", "javalibrary", "1384")) {
+
+ try (PreparedStatement statement = connection.prepareStatement("SELECT books.id, books.title, books.writer, rents.id_of_member, rents.member, rents.rent FROM books LEFT JOIN rents ON books.id = rents.id_of_title WHERE rents.rent = 'Rent'")) {
+ ResultSet resultSet = statement.executeQuery();
+ while (resultSet.next()) {
+ String memberId = resultSet.getString("id_of_member");
+ String memberName = resultSet.getString("member");
+ String bookId = resultSet.getString("id");
+ String bookTitle = resultSet.getString("title");
+ String bookWriter = resultSet.getString("writer");
+ String rentStatus = resultSet.getString("rent");
+
+ if ("rent".equals(rentStatus) && map.containsKey(memberId) && map.containsValue(memberName)) {
+ System.out.println("id: " + bookId + " title: " + bookTitle + " writer: " + bookWriter);
+ }
+ }
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+