From fb5b0addd270fe253cd0bca3ea9b8d53edd69987 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 10:56:59 +0330 Subject: [PATCH 1/9] class admin created with the necessary proproties --- Answers/LMS/src/main/java/org/example/Admin.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/Admin.java diff --git a/Answers/LMS/src/main/java/org/example/Admin.java b/Answers/LMS/src/main/java/org/example/Admin.java new file mode 100644 index 0000000..e1695e7 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/Admin.java @@ -0,0 +1,13 @@ +package org.example; + +public class Admin extends User { + private String _Password; + public String get_Password() { + return _Password;} + + public Admin(String Name, long Unique_ID, String Phone_number ,String Password) { + super( Name , Unique_ID , Phone_number); + _Password = Password; + } + +} \ No newline at end of file From 40790b412c4ba2a92748d8097abd6a9a7ba62759 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 10:57:51 +0330 Subject: [PATCH 2/9] Book class created --- .../LMS/src/main/java/org/example/Book.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/Book.java diff --git a/Answers/LMS/src/main/java/org/example/Book.java b/Answers/LMS/src/main/java/org/example/Book.java new file mode 100644 index 0000000..ae405c3 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/Book.java @@ -0,0 +1,33 @@ +package org.example; + +public class Book { + private int _bookID; + public int get_bookID() { + return _bookID;} + public void set_bookID(int bookID) { + _bookID= bookID;} + private String _Title; + public String get_Title() { + return _Title;} + public void set_Title(String title) { + _Title= title;} + + private String _Author; + private boolean _Availability_status; + public boolean get_Availability() { + return _Availability_status;} + public void set_Availability(boolean Availability) { + _Availability_status= Availability;} + private String _Description; + + public Book(int bookID, String Title, String Author, boolean Availability_status, String Description) { + _bookID = bookID; + _Title = Title; + _Author = Author; + _Availability_status = Availability_status; + _Description = Description; + + } + +} + From 06d1f49b532ab9a595536559383e9f25d1ee4834 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 10:59:08 +0330 Subject: [PATCH 3/9] User class created --- .../LMS/src/main/java/org/example/User.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/User.java diff --git a/Answers/LMS/src/main/java/org/example/User.java b/Answers/LMS/src/main/java/org/example/User.java new file mode 100644 index 0000000..5082ad8 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/User.java @@ -0,0 +1,30 @@ +package org.example; + + +public class User{ + private String _Name; + public String getName() { + return _Name;} + public void setName(String newName) { + _Name = newName;} + + private long _Unique_ID; + public long getId() { + return _Unique_ID;} +// public void setId(int ID) { +// _Unique_ID = ID;} +// + private String _Phone_number; + public String get_Phone_number() { + return _Phone_number;} + public void set_Phone_number(String phoneNumber) { + _Phone_number = phoneNumber;} + + public User(String Name , long Unique_ID , String Phone_number){ + + _Name = Name; + _Unique_ID= Unique_ID; + _Phone_number = Phone_number; + + } +} From 3f28f596baa3c803f84efcc64ef2b7eb815d54b8 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 11:03:57 +0330 Subject: [PATCH 4/9] Normal class crrated --- Answers/LMS/src/main/java/org/example/Normal.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/Normal.java diff --git a/Answers/LMS/src/main/java/org/example/Normal.java b/Answers/LMS/src/main/java/org/example/Normal.java new file mode 100644 index 0000000..48a5fa1 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/Normal.java @@ -0,0 +1,14 @@ +package org.example; + +import java.time.LocalDate; + +public class Normal extends User{ + LocalDate _Date; + public Normal(String Name, long Unique_ID, String Phone_number) { + super( Name , Unique_ID , Phone_number); + _Date = LocalDate.now(); + } + +} + + From 6673acdd9d5d68299f22efa594347d476c7c6107 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 11:05:04 +0330 Subject: [PATCH 5/9] Rent class created --- .../LMS/src/main/java/org/example/Rent.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/Rent.java diff --git a/Answers/LMS/src/main/java/org/example/Rent.java b/Answers/LMS/src/main/java/org/example/Rent.java new file mode 100644 index 0000000..5554a53 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/Rent.java @@ -0,0 +1,18 @@ +package org.example; + +import java.time.LocalDate; + +public class Rent { + Book _B ; + Normal _normal; + int _Rental_ID; + static int id=0; + LocalDate _Rental_date; + public Rent( Book B , Normal normal , LocalDate Rental_date){ + _B = B; + _normal = normal; + _Rental_ID = id++; + _Rental_date = Rental_date; + } + +} From 194e692ce45ab1a5c1e1acca74853fede9981540 Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 11:06:29 +0330 Subject: [PATCH 6/9] Library class created --- .../src/main/java/org/example/Library.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/Library.java diff --git a/Answers/LMS/src/main/java/org/example/Library.java b/Answers/LMS/src/main/java/org/example/Library.java new file mode 100644 index 0000000..e7fff9a --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/Library.java @@ -0,0 +1,32 @@ +package org.example; + +import java.util.ArrayList; + +public class Library { + String _Library_name; + public String get_Library_name() { + return _Library_name;} + public void setName(String Libraryname) { + _Library_name= Libraryname;} + + String _Capacity; + int[] _Operating_hours; + public int[] get_hours(){ + return _Operating_hours;} + public int[] set_hours(){ + return _Operating_hours;} + ArrayList _Book_repository; + ArrayList _Trustee; + ArrayList _Boss; + ArrayList _rental_registries; + public Library(String Library_name , String Capacity , int[] Operating_hours){ + _Library_name= Library_name; + _Capacity= Capacity; + _Operating_hours = Operating_hours; + _Book_repository = new ArrayList<>(); + _Trustee = new ArrayList<>(); + _Boss =new ArrayList<>(); + _rental_registries= new ArrayList<>(); + + } +} From c12ff32152de8a2330b46c6dd2fced311455601d Mon Sep 17 00:00:00 2001 From: diba Date: Sat, 11 May 2024 11:10:35 +0330 Subject: [PATCH 7/9] CLI class created with it's related methods --- .../LMS/src/main/java/org/example/CLI.java | 479 ++++++++++++++++++ 1 file changed, 479 insertions(+) create mode 100644 Answers/LMS/src/main/java/org/example/CLI.java diff --git a/Answers/LMS/src/main/java/org/example/CLI.java b/Answers/LMS/src/main/java/org/example/CLI.java new file mode 100644 index 0000000..1e99db0 --- /dev/null +++ b/Answers/LMS/src/main/java/org/example/CLI.java @@ -0,0 +1,479 @@ +package org.example; +import java.util.Objects; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CLI { + + public static void print() { + System.out.println(""" + if you want to add a new book to the library..........ENTER lib add book : . + or Retrieve library operating hours...................ENTER lib get hrs\s + or Rent a book from the library.......................ENTER lib rent + or Add a new member to the library....................ENTER lib add member + or Add a new admin to the library.....................ENTER lib add admin + or Rent a book for a specific member..................ENTER lib rent \s + or View available books for rental....................ENTER lib get available books\s + or Remove a member from the library...................ENTER lib remove member + or Return a rented book to the library................ENTER lib return """); + } + + public static void addbook(Library L, Book B) { + L._Book_repository.add(B); + } + + static int x=0; + public static int idgeneratormember() { + return ++x; + } + static int y=0; + public static int idgeneratoradmin() { + return ++y; + } + + public static Book buildingbook(int ID, String title, String author, Boolean availability, String description) { + Book b = new Book(ID, title, author, availability, description); + return b; + } + + public static void rent(Library L, String name) { + for (Book Book : L._Book_repository) { + if (Book.get_Title().equals(name)) { + if (Book.get_Availability()) { + Book.set_Availability(false); + } else { + System.out.println("It seems that this book have been rented, check it out later"); + } + } else { + System.out.println("This book is not available at the moment, We would appreciate if you add it to our library."); + } + } + } + + public static void returnbook(Library L, String name) { + for (Book Book : L._Book_repository) { + if (Book.get_Title().equals(name)) { + if (Book.get_Availability()) { + System.out.println("It seems that we already have this book, give it to someone else"); + } else { + Book.set_Availability(true); + System.out.println("Thanks for bringing our book back, hope you enjoyed it!"); + } + } else { + System.out.println("It seems that this book doesn't belong to our library "); + } + } + } + + static String phonenumber(String number) { + if (number.length() != 10) { + return "wrong"; + } + char[] num = number.toCharArray(); + if (num[0] != '9') { + return "wrong"; + } + if (!number.matches("[0-9]+")) { + return "wrong"; + } + return '0' + number; + } + + static String id(String id) { + if (id.length() > 13 || id.length() < 4) { + return "wrong"; + } + if (!id.matches("[0-9]+")) { + return "wrong"; + } else { + return id; + } + } + + public static void membership(Library L) { + Scanner scanner4 = new Scanner(System.in); + System.out.println("Please enter your name"); + String name = scanner4.next(); + long userid = idgeneratormember(); + System.out.println("Please enter your phone number without the 0"); + String number = scanner4.next(); + while (true) { + if (phonenumber(number).equals("wrong")) { + System.out.println("Wrong entry. Try again."); + number = scanner4.next(); + } + else { + break; + } + } + System.out.println("You have been successfully registered as a member.\nHere is you id: " + userid +"."); + + Normal normal_user = new Normal(name, userid, number); + L._Trustee.add(normal_user); + + } + + public static void adminship(Library L) { + Scanner scanner4 = new Scanner(System.in); + System.out.println("Please enter your name"); + String name = scanner4.next(); + long userid = idgeneratoradmin(); + System.out.println("Please enter your phone number without the 0"); + String number = scanner4.next(); + while (true) { + if (phonenumber(number).equals("wrong")) { + System.out.println("Wrong entry. Try again."); + number = scanner4.next(); + } else { + break; + } + } + System.out.println("Please enter your password"); + String password = scanner4.next(); + + System.out.println("You have been successfully registered as an admin.\nHere is you id: " + userid +"."); + Admin admin = new Admin(name, userid, number, password); + L._Boss.add(admin); + } + + public static void addmemberbyadmin(Library L){ + + Scanner scanner4 = new Scanner(System.in); + System.out.println("Please enter member's name"); + String name = scanner4.next(); + long userid = idgeneratormember(); + System.out.println("Please enter their phone number without the 0"); + String number = scanner4.next(); + while (true) { + if (phonenumber(number).equals("wrong")) { + System.out.println("Wrong entry. Try again."); + number = scanner4.nextLine(); + } else { + break; + } + } + System.out.println("Your member have been successfully registered .\nHere is their id: " + userid +"."); + Normal normal_user = new Normal(name, userid, number); + L._Trustee.add(normal_user); + } + public static void addadminbyadmin(Library L){ + + Scanner scanner4 = new Scanner(System.in); + System.out.println("Please enter admin's name"); + String name = scanner4.next(); + long userid = idgeneratoradmin(); + System.out.println("Please enter their phone number without the 0"); + String number = scanner4.next(); + while (true) { + if (phonenumber(number).equals("wrong")) { + System.out.println("Wrong entry. Try again."); + number = scanner4.nextLine(); + } else { + break; + } + } + System.out.println("Please enter their password"); + String password = scanner4.next(); + System.out.println("Your admin have been successfully registered .\nHere is their id: " + userid +"."); + Admin admin = new Admin(name, userid, number, password); + L._Boss.add(admin); + } + + public static void addmember(Library L ) { + boolean true_boss = false; + String name_boss = null; + Scanner scanner4 = new Scanner(System.in); + System.out.println("Dear admin please enter your personal password "); + String password= scanner4.next(); + for (Admin A : L._Boss) { + { + if (Objects.equals(A.get_Password(), password)) { + true_boss = true; + name_boss = A.getName(); + } + } + } + if (true_boss) { + addmemberbyadmin(L); + System.out.println("Done by " + name_boss); + } + else { + System.out.println("Sorry we didn't add your member because the admin couldn't remember their password."); + } + + } + + public static void addadmin(Library L ) { + boolean true_boss = false; + String name_boss = null; + Scanner scanner4 = new Scanner(System.in); + System.out.println("Dear admin please enter your personal password "); + String password= scanner4.next(); + for (Admin A : L._Boss) { + { + if (Objects.equals(A.get_Password(), password)) { + true_boss = true; + name_boss = A.getName(); + } + } + } + if (true_boss) { + addadminbyadmin(L); + System.out.println("Done by " + name_boss); + } + else { + System.out.println("Sorry we didn't add your member because the admin couldn't remember their password."); + } + + } + + public static void remove_member(Library L) { + System.out.println("choose the member you want to remove and type their id "); + for (Normal N : L._Trustee) { + { + System.out.println("------------------------------------"); + + System.out.println("NAME: " + N.getName()); + System.out.println("USERID: " + N.getId()); + System.out.println("------------------------------------"); + } + } + Scanner S = new Scanner(System.in); + long id = S.nextInt(); + while (true) { + boolean remove = false; + for (Normal N : L._Trustee) { + { + if (id == N.getId()) { + L._Trustee.remove(N); + System.out.println( N.getName() +" was successfully deleted."); + remove = true; + break; + } + } + } + if (remove) { + break; + } else { + System.out.println("It seems like we don't have a member with this id so you cannot remove it."); + } + } + + } + + public static void wrongpose() { + System.out.println(" Sorry! You are not at admin position so you can't do this command."); + } + + public static void showbooks(Library L) { + for (Book B : L._Book_repository) { + if (B.get_Availability()) { + System.out.println(B.get_Title()); + } + } + } + + public static void changehrs() { + Scanner scanner = new Scanner(System.in); + System.out.println("Please your library's opening hour"); + int[] h = new int[2]; + h[0] = scanner.nextInt(); + while (true) { + if (h[0] == 1 || h[0] == 2 || h[0] == 3 || h[0] == 4 || h[0] == 5 || h[0] == 6 || h[0] == 7 || h[0] == 8 || h[0] == 9 || h[0] == 10 || h[0] == 11 || h[0] == 12 + || h[0] == 13 || h[0] == 14 || h[0] == 15 || h[0] == 16 || h[0] == 17 || h[0] == 18 || h[0] == 19 || h[0] == 20 || h[0] == 21 || h[0] == 22 || h[0] == 23 || h[0] == 24) { + break; + } else { + System.out.println("what? choose an actual number that is inside the clock"); + h[0] = scanner.nextInt(); + } + } + System.out.println("Please your library's closing hour"); + h[1] = scanner.nextInt(); + while (true) { + if ((h[1] == 1 || h[1] == 2 || h[1] == 3 || h[1] == 4 || h[1] == 5 || h[1] == 6 || h[1] == 7 || h[1] == 8 || + h[1] == 9 || h[1] == 10 || h[1] == 11 || h[1] == 12 || h[1] == 13 || h[1] == 14 || h[1] == 15 || h[1] == 16 + || h[1] == 17 || h[1] == 18 || h[1] == 19 || h[1] == 20 || h[1] == 21 || h[1] == 22 || h[1] == 23 || h[1] == 24) && h[1] > h[0]) { + break; + } else if ((h[1] == 1 || h[1] == 2 || h[1] == 3 || h[1] == 4 || h[1] == 5 || h[1] == 6 || h[1] == 7 || h[1] == 8 || + h[1] == 9 || h[1] == 10 || h[1] == 11 || h[1] == 12 || h[1] == 13 || h[1] == 14 || h[1] == 15 || h[1] == 16 + || h[1] == 17 || h[1] == 18 || h[1] == 19 || h[1] == 20 || h[1] == 21 || h[1] == 22 || h[1] == 23 || h[1] == 24) && h[1] < h[0]) { + System.out.println("Wow! a magical library!! How come the opening time is after the closing time? anyway0_0"); + break; + } else { + System.out.println("what? choose an actual number that is inside the clock"); + h[0] = scanner.nextInt(); + } + } + + } + + public static void rentspecefic( String name , Library L , long id){ + boolean match =false; + while (true) { + for (Book B : L._Book_repository) { + if (B.get_Title().equals(name)) { + match = true; + break; + } + } + + } + + + } + + public static boolean checking(String c, Library L, boolean position) { + Pattern pr1 = Pattern.compile("^lib\\s+add\\s+book\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>"); + Matcher m1 = pr1.matcher(c); + if (m1.find()) { + addbook(L, buildingbook(idgeneratormember(), m1.group(1), m1.group(2), true, m1.group(3))); + return true; + } + + Pattern pr2 = Pattern.compile("^lib\\s+get\\s+hrs\\s"); + Matcher m2 = pr2.matcher(c); + if (m2.find()) { + changehrs(); + return true; + } + + Pattern pr3 = Pattern.compile("^lib\\s+rent\\s+<([0-9]+)>\\s*"); + Matcher m3 = pr3.matcher(c); + if (m3.find()) { + rent(L, m3.group(1)); + return true; + } + + Pattern pr4 = Pattern.compile("^lib\\s+add\\s+member\\s*"); + Matcher m4 = pr4.matcher(c); + if (m4.find()) { + if (position) { + addmember(L); + } else { + wrongpose(); + } + return true; + } + + Pattern pr9 = Pattern.compile("^lib\\s+add\\s+admin\\s*"); + Matcher m9 = pr9.matcher(c); + if (m9.find()) { + if (position) { + addmember(L); + } else { + wrongpose(); + } + return true; + } + + Pattern pr5 = Pattern.compile("^lib\\s+rent\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\S+<([a-zA-Z]+(?:\\s+[a-zA-Z]*)*)>\\S+<([0-9])>\\s*"); + Matcher m5 = pr5.matcher(c); + if(m5.find()){ + // rentspecefic(m5.group(1) , m5.group(2)); + } + + Pattern pr6 = Pattern.compile("^lib\\s+get\\s+available\\s+books\\s*"); + Matcher m6 = pr6.matcher(c); + if (m6.find()) { + showbooks(L); + return true; + } + + Pattern pr7 = Pattern.compile("^lib\\s+remove\\s+member\\s*"); + Matcher m7 = pr7.matcher(c); + if (m7.find()) { + if (position) { + remove_member(L); + } else { + wrongpose(); + } + return true; + } + + Pattern pr8 = Pattern.compile("^lib\\s+return\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*"); + Matcher m8 = pr8.matcher(c); + if (m8.find()) { + returnbook(L, m8.group(1)); + return true; + } + + return false; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.println("Welcome to our website! If you want to submit your library enter YES"); + while (true) { + String answer = scanner.next(); + if (Objects.equals(answer, "YES")) { + break; + } else { + System.out.println("Try again"); + } + } + scanner.nextLine(); + System.out.println("Please your library's name"); + String name = scanner.nextLine(); + System.out.println("Please your library's capacity"); + String cap = scanner.next(); + Pattern pr9 = Pattern.compile("([0-9]+)\\s*"); + while (true) { + Matcher m9 = pr9.matcher(cap); + if (m9.find()) { + break; + } else { + System.out.println("you have to answer with numbers. TRY AGAIN!"); + } + cap = scanner.next(); + } + int[] h = new int[2]; + changehrs(); + Library LIE = new Library(name, cap, h); + Scanner scanner1 = new Scanner(System.in); + System.out.println("Now tell me if you want to be a member or an admin"); + Scanner scanner2 = new Scanner(System.in); + String anwser3 = scanner2.next(); + boolean pose = true; + while (true) { + if (Objects.equals(anwser3, "member")) { + membership(LIE); + pose = false; + break; + } else if (Objects.equals(anwser3, "admin")) { + adminship(LIE); + break; + } else { + System.out.println("irrelevant:D try again"); + anwser3 = scanner2.next(); + + } + } + + print(); + Scanner scanner7 = new Scanner(System.in); + while (true) { + while (true) { + String command = scanner7.nextLine(); + if (checking(command, LIE, pose)) { + break; + } else { + System.out.println("Wrong command! Please try again."); + break; + } + } + System.out.println("if you have no other commands, enter YES"); + String answer1 = scanner1.next(); + if (Objects.equals(answer1, "YES")) { + break; + } + System.out.println("choose your command from list below:"); + print(); + } + + System.out.println("Thank you for choosing our Library Management System." + + " We're excited to provide you with an efficient and user-friendly platform for all your library needs. Happy reading!"); + } +} + From d9d2a941459482797a8455cc653deb8b3b844f43 Mon Sep 17 00:00:00 2001 From: diba Date: Thu, 16 May 2024 23:58:34 +0330 Subject: [PATCH 8/9] Final changes applied --- .../LMS/src/main/java/org/example/Book.java | 9 + .../LMS/src/main/java/org/example/CLI.java | 296 +++++++++++++----- .../LMS/src/main/java/org/example/Rent.java | 7 +- 3 files changed, 225 insertions(+), 87 deletions(-) diff --git a/Answers/LMS/src/main/java/org/example/Book.java b/Answers/LMS/src/main/java/org/example/Book.java index ae405c3..f2821f8 100644 --- a/Answers/LMS/src/main/java/org/example/Book.java +++ b/Answers/LMS/src/main/java/org/example/Book.java @@ -6,6 +6,7 @@ public int get_bookID() { return _bookID;} public void set_bookID(int bookID) { _bookID= bookID;} + private String _Title; public String get_Title() { return _Title;} @@ -13,12 +14,20 @@ public void set_Title(String title) { _Title= title;} private String _Author; + public String get_Author() { + return _Author;} + public void set_Author(String author) { + _Author= author;} private boolean _Availability_status; public boolean get_Availability() { return _Availability_status;} public void set_Availability(boolean Availability) { _Availability_status= Availability;} private String _Description; + public String get_Description() { + return _Description;} + public void set_Description(String description) { + _Description= description;} public Book(int bookID, String Title, String Author, boolean Availability_status, String Description) { _bookID = bookID; diff --git a/Answers/LMS/src/main/java/org/example/CLI.java b/Answers/LMS/src/main/java/org/example/CLI.java index 1e99db0..ee86b06 100644 --- a/Answers/LMS/src/main/java/org/example/CLI.java +++ b/Answers/LMS/src/main/java/org/example/CLI.java @@ -6,6 +6,25 @@ public class CLI { + public static void blue() { + System.out.print("\033[1;36m"); + } + public static void white() { + System.out.print("\033[0m"); + } + public static void red() { + System.out.print("\033[1;31m"); + } + public static void green() { + System.out.print("\033[1;32m"); + } + public static void yellow() { + System.out.print("\033[1;33m"); + } + public static final String ANSI_PURPLE = "\u001B[35m"; + public static final String ANSI_NILI = "\u001B[36m"; + public static final String ANSI_YELLOW = "\u001B[33m"; + public static void print() { System.out.println(""" if you want to add a new book to the library..........ENTER lib add book : . @@ -19,10 +38,35 @@ public static void print() { or Return a rented book to the library................ENTER lib return """); } + public static void printColoredText() { + System.out.println(ANSI_NILI + "If you want to add a new book to the library..........ENTER" + + ANSI_PURPLE + " lib add book \n" + + ANSI_NILI + "or Retrieve library operating hours...................ENTER" + + ANSI_PURPLE + " lib get hrs\n"+ + ANSI_NILI+ "or Rent a book from the library.......................ENTER" + + ANSI_YELLOW + " lib rent \n" + + ANSI_NILI + "or Rent a book for a specific member..................ENTER" + + ANSI_PURPLE + " lib rent \n"+ + ANSI_NILI + "or Add a new member to the library....................ENTER" + + ANSI_YELLOW + " lib add member\n"+ + ANSI_NILI+ "or Add a new admin to the library.....................ENTER" + + ANSI_YELLOW + " lib add admin\n" + + ANSI_NILI+ "or View available books for rental....................ENTER" + + ANSI_PURPLE + " lib get available books\n" + + ANSI_NILI + "or Remove a member from the library...................ENTER" + + ANSI_YELLOW + " lib remove member\n"+ + ANSI_NILI+ "or Return a rented book to the library................ENTER" + + ANSI_PURPLE + " lib return \n" + + ANSI_YELLOW + "Yellow ones are for admins only"); + blue(); + } + public static void addbook(Library L, Book B) { L._Book_repository.add(B); + yellow(); + System.out.println("Your book has been successfully registered"); + blue(); } - static int x=0; public static int idgeneratormember() { return ++x; @@ -31,26 +75,52 @@ public static int idgeneratormember() { public static int idgeneratoradmin() { return ++y; } - public static Book buildingbook(int ID, String title, String author, Boolean availability, String description) { Book b = new Book(ID, title, author, availability, description); return b; } - public static void rent(Library L, String name) { + boolean check=false; + Scanner scanner = new Scanner(System.in); for (Book Book : L._Book_repository) { if (Book.get_Title().equals(name)) { if (Book.get_Availability()) { Book.set_Availability(false); + System.out.println("Please enter your name "); + String n =scanner.next(); + System.out.println("Please enter your id "); + long id =scanner.nextInt(); + for (Normal N : L._Trustee) { + if(n == N.getName()){ + if (id == N.getId()){ + Rent rent = new Rent(Book, N, id); + L._rental_registries.add(rent); + yellow(); + System.out.println("The book has been successfully rented"); + blue(); + check=true; + break; + } + } + + } + if(check==false){ + System.out.println("We don't have a member with this name in our list. If you want to rent a book, you have to be a member first.\n" + + "enter lib add member\" otherwise press any key"); + break; + } + if (check){break;} + } else { System.out.println("It seems that this book have been rented, check it out later"); + break; } } else { System.out.println("This book is not available at the moment, We would appreciate if you add it to our library."); + break; } } } - public static void returnbook(Library L, String name) { for (Book Book : L._Book_repository) { if (Book.get_Title().equals(name)) { @@ -65,7 +135,6 @@ public static void returnbook(Library L, String name) { } } } - static String phonenumber(String number) { if (number.length() != 10) { return "wrong"; @@ -79,7 +148,6 @@ static String phonenumber(String number) { } return '0' + number; } - static String id(String id) { if (id.length() > 13 || id.length() < 4) { return "wrong"; @@ -90,7 +158,6 @@ static String id(String id) { return id; } } - public static void membership(Library L) { Scanner scanner4 = new Scanner(System.in); System.out.println("Please enter your name"); @@ -100,20 +167,22 @@ public static void membership(Library L) { String number = scanner4.next(); while (true) { if (phonenumber(number).equals("wrong")) { + red(); System.out.println("Wrong entry. Try again."); + blue(); number = scanner4.next(); } else { break; } } + yellow(); System.out.println("You have been successfully registered as a member.\nHere is you id: " + userid +"."); - + blue(); Normal normal_user = new Normal(name, userid, number); L._Trustee.add(normal_user); } - public static void adminship(Library L) { Scanner scanner4 = new Scanner(System.in); System.out.println("Please enter your name"); @@ -123,7 +192,9 @@ public static void adminship(Library L) { String number = scanner4.next(); while (true) { if (phonenumber(number).equals("wrong")) { + red(); System.out.println("Wrong entry. Try again."); + blue(); number = scanner4.next(); } else { break; @@ -131,12 +202,12 @@ public static void adminship(Library L) { } System.out.println("Please enter your password"); String password = scanner4.next(); - + yellow(); System.out.println("You have been successfully registered as an admin.\nHere is you id: " + userid +"."); + blue(); Admin admin = new Admin(name, userid, number, password); L._Boss.add(admin); } - public static void addmemberbyadmin(Library L){ Scanner scanner4 = new Scanner(System.in); @@ -147,13 +218,17 @@ public static void addmemberbyadmin(Library L){ String number = scanner4.next(); while (true) { if (phonenumber(number).equals("wrong")) { + red(); System.out.println("Wrong entry. Try again."); + blue(); number = scanner4.nextLine(); } else { break; } } + yellow(); System.out.println("Your member have been successfully registered .\nHere is their id: " + userid +"."); + blue(); Normal normal_user = new Normal(name, userid, number); L._Trustee.add(normal_user); } @@ -167,7 +242,9 @@ public static void addadminbyadmin(Library L){ String number = scanner4.next(); while (true) { if (phonenumber(number).equals("wrong")) { + red(); System.out.println("Wrong entry. Try again."); + blue(); number = scanner4.nextLine(); } else { break; @@ -175,11 +252,12 @@ public static void addadminbyadmin(Library L){ } System.out.println("Please enter their password"); String password = scanner4.next(); + yellow(); System.out.println("Your admin have been successfully registered .\nHere is their id: " + userid +"."); + blue(); Admin admin = new Admin(name, userid, number, password); L._Boss.add(admin); } - public static void addmember(Library L ) { boolean true_boss = false; String name_boss = null; @@ -196,14 +274,15 @@ public static void addmember(Library L ) { } if (true_boss) { addmemberbyadmin(L); + yellow(); System.out.println("Done by " + name_boss); + blue(); } else { System.out.println("Sorry we didn't add your member because the admin couldn't remember their password."); } } - public static void addadmin(Library L ) { boolean true_boss = false; String name_boss = null; @@ -220,23 +299,27 @@ public static void addadmin(Library L ) { } if (true_boss) { addadminbyadmin(L); + yellow(); System.out.println("Done by " + name_boss); + blue(); } else { System.out.println("Sorry we didn't add your member because the admin couldn't remember their password."); } } - public static void remove_member(Library L) { System.out.println("choose the member you want to remove and type their id "); for (Normal N : L._Trustee) { { + yellow(); System.out.println("------------------------------------"); - + blue(); System.out.println("NAME: " + N.getName()); System.out.println("USERID: " + N.getId()); + yellow(); System.out.println("------------------------------------"); + blue(); } } Scanner S = new Scanner(System.in); @@ -261,104 +344,148 @@ public static void remove_member(Library L) { } } - public static void wrongpose() { + red(); System.out.println(" Sorry! You are not at admin position so you can't do this command."); + blue(); } - public static void showbooks(Library L) { - for (Book B : L._Book_repository) { - if (B.get_Availability()) { - System.out.println(B.get_Title()); + if (L._Book_repository.isEmpty()) { + System.out.println("You have not registered a book yet.");} + else { + for (Book B : L._Book_repository) { + if (B.get_Availability()) { + yellow(); + System.out.println("----------------------------"); + blue(); + System.out.println(B.get_Title()); + yellow(); + System.out.println("----------------------------"); + blue(); + } } } } - public static void changehrs() { Scanner scanner = new Scanner(System.in); System.out.println("Please your library's opening hour"); + int[] h = new int[2]; h[0] = scanner.nextInt(); while (true) { - if (h[0] == 1 || h[0] == 2 || h[0] == 3 || h[0] == 4 || h[0] == 5 || h[0] == 6 || h[0] == 7 || h[0] == 8 || h[0] == 9 || h[0] == 10 || h[0] == 11 || h[0] == 12 - || h[0] == 13 || h[0] == 14 || h[0] == 15 || h[0] == 16 || h[0] == 17 || h[0] == 18 || h[0] == 19 || h[0] == 20 || h[0] == 21 || h[0] == 22 || h[0] == 23 || h[0] == 24) { + if (h[0]>=0 && h[0]<=24) { break; } else { + red(); System.out.println("what? choose an actual number that is inside the clock"); + blue(); h[0] = scanner.nextInt(); } } System.out.println("Please your library's closing hour"); h[1] = scanner.nextInt(); while (true) { - if ((h[1] == 1 || h[1] == 2 || h[1] == 3 || h[1] == 4 || h[1] == 5 || h[1] == 6 || h[1] == 7 || h[1] == 8 || - h[1] == 9 || h[1] == 10 || h[1] == 11 || h[1] == 12 || h[1] == 13 || h[1] == 14 || h[1] == 15 || h[1] == 16 - || h[1] == 17 || h[1] == 18 || h[1] == 19 || h[1] == 20 || h[1] == 21 || h[1] == 22 || h[1] == 23 || h[1] == 24) && h[1] > h[0]) { + if (h[1]>=0 && h[1]<=24 && h[1] > h[0]) { break; - } else if ((h[1] == 1 || h[1] == 2 || h[1] == 3 || h[1] == 4 || h[1] == 5 || h[1] == 6 || h[1] == 7 || h[1] == 8 || - h[1] == 9 || h[1] == 10 || h[1] == 11 || h[1] == 12 || h[1] == 13 || h[1] == 14 || h[1] == 15 || h[1] == 16 - || h[1] == 17 || h[1] == 18 || h[1] == 19 || h[1] == 20 || h[1] == 21 || h[1] == 22 || h[1] == 23 || h[1] == 24) && h[1] < h[0]) { + } else if (h[1]>=0 && h[1]<=24 && h[1] < h[0]) { yellow(); System.out.println("Wow! a magical library!! How come the opening time is after the closing time? anyway0_0"); + blue(); break; } else { + red(); System.out.println("what? choose an actual number that is inside the clock"); - h[0] = scanner.nextInt(); + blue(); + h[1] = scanner.nextInt(); } } } - - public static void rentspecefic( String name , Library L , long id){ + public static void rentspecefic( String name ,String membername , long id, Library L ){ boolean match =false; + boolean available=true; while (true) { for (Book B : L._Book_repository) { if (B.get_Title().equals(name)) { match = true; + if (B.get_Availability()) { + available = false; + for (Normal N : L._Trustee) { + if (Objects.equals(N.getName(), membername)) { + if (N.getId() == id) { + Rent rent = new Rent(B, N, id); + L._rental_registries.add(rent); + } + else { + System.out.println("Sorry we can't give this book because your name doesn't match with your id.");} + break; + } + else { + System.out.println("Sorry we don't have you as a member in our list so we can't give it to you.if you're really interested, sign in first"); + } + } + } + else { + System.out.println("Sorry it seems like someone else has already rented this book. check it out later"); + } break; } } - + break; + } + if (match==false) { + System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); } - - } - public static boolean checking(String c, Library L, boolean position) { Pattern pr1 = Pattern.compile("^lib\\s+add\\s+book\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>"); Matcher m1 = pr1.matcher(c); - if (m1.find()) { - addbook(L, buildingbook(idgeneratormember(), m1.group(1), m1.group(2), true, m1.group(3))); - return true; - } - Pattern pr2 = Pattern.compile("^lib\\s+get\\s+hrs\\s"); + + Pattern pr2 = Pattern.compile("^lib\\s+get\\s+hrs\\s*"); Matcher m2 = pr2.matcher(c); - if (m2.find()) { - changehrs(); - return true; - } - Pattern pr3 = Pattern.compile("^lib\\s+rent\\s+<([0-9]+)>\\s*"); + Pattern pr5 = Pattern.compile("^lib\\s+rent\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*<([0-9]+(?:\\s+[0-9]*)*)>"); + Matcher m5 = pr5.matcher(c); + + Pattern pr3 = Pattern.compile("^lib\\s+rent\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*"); Matcher m3 = pr3.matcher(c); - if (m3.find()) { - rent(L, m3.group(1)); - return true; - } + Pattern pr4 = Pattern.compile("^lib\\s+add\\s+member\\s*"); Matcher m4 = pr4.matcher(c); - if (m4.find()) { - if (position) { - addmember(L); + + + Pattern pr9 = Pattern.compile("^lib\\s+add\\s+admin\\s*"); + Matcher m9 = pr9.matcher(c); + + Pattern pr6 = Pattern.compile("^lib\\s+get\\s+available\\s+books\\s*"); + Matcher m6 = pr6.matcher(c); + + + Pattern pr7 = Pattern.compile("^lib\\s+remove\\s+member\\s*"); + Matcher m7 = pr7.matcher(c); + + + Pattern pr8 = Pattern.compile("^lib\\s+return\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*"); + Matcher m8 = pr8.matcher(c); + + if (m1.find()) { + addbook(L, buildingbook(idgeneratormember(), m1.group(1), m1.group(2), true, m1.group(3))); + return true; + } + else if (m2.find()) { + changehrs(); + return true; + } + else if (m3.matches()) { + if (position){ + rent(L, m3.group(1)); } else { wrongpose(); } return true; } - - Pattern pr9 = Pattern.compile("^lib\\s+add\\s+admin\\s*"); - Matcher m9 = pr9.matcher(c); - if (m9.find()) { + else if (m4.find()) { if (position) { addmember(L); } else { @@ -366,23 +493,15 @@ public static boolean checking(String c, Library L, boolean position) { } return true; } - - Pattern pr5 = Pattern.compile("^lib\\s+rent\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\S+<([a-zA-Z]+(?:\\s+[a-zA-Z]*)*)>\\S+<([0-9])>\\s*"); - Matcher m5 = pr5.matcher(c); - if(m5.find()){ - // rentspecefic(m5.group(1) , m5.group(2)); + else if(m5.find()){ + long id = Long.parseLong(m5.group(3)); + rentspecefic(m5.group(1) , m5.group(2) , id , L); } - - Pattern pr6 = Pattern.compile("^lib\\s+get\\s+available\\s+books\\s*"); - Matcher m6 = pr6.matcher(c); - if (m6.find()) { + else if (m6.find()) { showbooks(L); return true; } - - Pattern pr7 = Pattern.compile("^lib\\s+remove\\s+member\\s*"); - Matcher m7 = pr7.matcher(c); - if (m7.find()) { + else if (m7.find()) { if (position) { remove_member(L); } else { @@ -390,29 +509,36 @@ public static boolean checking(String c, Library L, boolean position) { } return true; } - - Pattern pr8 = Pattern.compile("^lib\\s+return\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*"); - Matcher m8 = pr8.matcher(c); - if (m8.find()) { + else if (m8.find()) { returnbook(L, m8.group(1)); return true; } - - return false; + if (m9.find()) { + if (position) { + addadmin(L); + } else { + wrongpose(); + } + return true; + } + else + { return false;} } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - + blue(); System.out.println("Welcome to our website! If you want to submit your library enter YES"); while (true) { String answer = scanner.next(); if (Objects.equals(answer, "YES")) { break; } else { + red(); System.out.println("Try again"); } } + blue(); scanner.nextLine(); System.out.println("Please your library's name"); String name = scanner.nextLine(); @@ -424,7 +550,9 @@ public static void main(String[] args) { if (m9.find()) { break; } else { + red(); System.out.println("you have to answer with numbers. TRY AGAIN!"); + blue(); } cap = scanner.next(); } @@ -445,13 +573,14 @@ public static void main(String[] args) { adminship(LIE); break; } else { + red(); System.out.println("irrelevant:D try again"); + blue(); anwser3 = scanner2.next(); } } - - print(); + printColoredText(); Scanner scanner7 = new Scanner(System.in); while (true) { while (true) { @@ -459,21 +588,22 @@ public static void main(String[] args) { if (checking(command, LIE, pose)) { break; } else { + red(); System.out.println("Wrong command! Please try again."); - break; + blue(); + continue; } } - System.out.println("if you have no other commands, enter YES"); + System.out.println("if you have no other commands, enter YES otherwise press any key"); String answer1 = scanner1.next(); if (Objects.equals(answer1, "YES")) { break; } System.out.println("choose your command from list below:"); - print(); + printColoredText(); } System.out.println("Thank you for choosing our Library Management System." + " We're excited to provide you with an efficient and user-friendly platform for all your library needs. Happy reading!"); } } - diff --git a/Answers/LMS/src/main/java/org/example/Rent.java b/Answers/LMS/src/main/java/org/example/Rent.java index 5554a53..54993ea 100644 --- a/Answers/LMS/src/main/java/org/example/Rent.java +++ b/Answers/LMS/src/main/java/org/example/Rent.java @@ -5,14 +5,13 @@ public class Rent { Book _B ; Normal _normal; - int _Rental_ID; + long _Rental_ID; static int id=0; LocalDate _Rental_date; - public Rent( Book B , Normal normal , LocalDate Rental_date){ + public Rent( Book B , Normal normal , long id){ _B = B; _normal = normal; _Rental_ID = id++; - _Rental_date = Rental_date; + _Rental_date = LocalDate.now(); } - } From e17915380d4db342b73fc632a591718773236870 Mon Sep 17 00:00:00 2001 From: diba Date: Fri, 31 May 2024 03:35:16 +0330 Subject: [PATCH 9/9] touch ups --- .../LMS/src/main/java/org/example/CLI.java | 527 +++++++++++++++--- 1 file changed, 446 insertions(+), 81 deletions(-) diff --git a/Answers/LMS/src/main/java/org/example/CLI.java b/Answers/LMS/src/main/java/org/example/CLI.java index ee86b06..54c121e 100644 --- a/Answers/LMS/src/main/java/org/example/CLI.java +++ b/Answers/LMS/src/main/java/org/example/CLI.java @@ -1,9 +1,13 @@ package org.example; +import java.util.InputMismatchException; import java.util.Objects; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static javax.print.attribute.standard.MediaSizeName.B; +import static javax.swing.text.html.HTML.Attribute.N; + public class CLI { public static void blue() { @@ -24,7 +28,7 @@ public static void yellow() { public static final String ANSI_PURPLE = "\u001B[35m"; public static final String ANSI_NILI = "\u001B[36m"; public static final String ANSI_YELLOW = "\u001B[33m"; - + public static final String ANSI_GRAY = "\u001B[37m"; public static void print() { System.out.println(""" if you want to add a new book to the library..........ENTER lib add book : . @@ -57,7 +61,10 @@ public static void printColoredText() { ANSI_YELLOW + " lib remove member\n"+ ANSI_NILI+ "or Return a rented book to the library................ENTER" + ANSI_PURPLE + " lib return \n" + + ANSI_NILI + "or View members ......................................ENTER" + + ANSI_PURPLE + " List\n" + ANSI_YELLOW + "Yellow ones are for admins only"); + blue(); } @@ -79,61 +86,258 @@ public static Book buildingbook(int ID, String title, String author, Boolean ava Book b = new Book(ID, title, author, availability, description); return b; } - public static void rent(Library L, String name) { - boolean check=false; + public static boolean duplication(Library L, String title){ + for (Book Book : L._Book_repository){ + if(Objects.equals(Book.get_Title(), title)){ + return true; + } + } + return false; + } + public static void rent(Library L , String name ){ + System.out.println("Please enter your name "); Scanner scanner = new Scanner(System.in); - for (Book Book : L._Book_repository) { - if (Book.get_Title().equals(name)) { - if (Book.get_Availability()) { - Book.set_Availability(false); - System.out.println("Please enter your name "); - String n =scanner.next(); - System.out.println("Please enter your id "); - long id =scanner.nextInt(); - for (Normal N : L._Trustee) { - if(n == N.getName()){ - if (id == N.getId()){ - Rent rent = new Rent(Book, N, id); - L._rental_registries.add(rent); - yellow(); - System.out.println("The book has been successfully rented"); - blue(); - check=true; - break; + String membername = scanner.next(); + System.out.println("Please enter your id "); + long id = scanner.nextInt(); + boolean khastam=false; + outerloop: + while (true){ + Normal member = null; + for (Normal N : L._Trustee) { + if (Objects.equals(N.getName(), membername) && N.getId() == id) { + member=N; + khastam=true; + break ; + } + else if (Objects.equals(N.getName(), membername) && N.getId() != id) { + yellow(); + System.out.println("Sorry we can't give this book because your name doesn't match with your id."); + blue(); + khastam=true; + break outerloop; + } + } + if(!khastam){ + yellow(); + System.out.println("Sorry we don't have you as a member in our list so we can't give you the book .if you're really interested, sign in first"); + blue(); + break ; + } + int count =0; + boolean avail=false; + Book bookToRent = null; + for (Book B : L._Book_repository) { + if (Objects.equals(B.get_Title(), name)) { + count++; + if(B.get_Availability()){ + avail=true; + bookToRent = B; + break; + } + } + } + if(count==0){ + yellow(); + System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); + blue(); + break ; + } + else if(count==1 && avail){ + bookToRent.set_Availability(false); + Rent rent = new Rent(bookToRent, member, id); + L._rental_registries.add(rent); + yellow(); + System.out.println("Here is your book. please take care of it"); + blue(); + break ; + } + else if(!avail){ + System.out.println("Sorry it seems like someone else has already rented this book. check it out later"); + break ; + } + } + } + public static void rents(Library L, String name) { + boolean check=false; + boolean off=false; + if (L._Book_repository.isEmpty()) { + System.out.println("You have not registered a book yet.");} + else{ + Scanner scanner = new Scanner(System.in); + for (Book Book : L._Book_repository) { + if (Book.get_Title().equals(name)) { + off = true; + if (Book.get_Availability()) { + System.out.println("Please enter your name "); + String n = scanner.next(); + System.out.println("Please enter your id "); + long id = scanner.nextInt(); + for (Normal N : L._Trustee) { + if (Objects.equals(n, N.getName())) { + if (id == N.getId()) { + Rent rent = new Rent(Book, N, id); + L._rental_registries.add(rent); + yellow(); + System.out.println("The book has been successfully rented"); + blue(); + check = true; + Book.set_Availability(false); + break; + } } } + if (!check) { + System.out.println("We don't have a member with this name in our list. If you want to rent a book, you have to be a member first.\n"); + break; + } + } else { + System.out.println("It seems that this book have been rented, check it out later"); + break; + } + } + } + } + if(!check && !off){ + System.out.println("This book is not available at the moment, We would appreciate if you add it to our library.");} + } + public static void renttt(Library L, String name) { + boolean bookFound = false; + boolean bookRented = false; + + if (L._Book_repository.isEmpty()) { + System.out.println("You have not registered a book yet."); + return; + } + + Scanner scanner = new Scanner(System.in); + + for (Book book : L._Book_repository) { + if (book.get_Title().equals(name)) { + bookFound = true; + if (book.get_Availability()) { + System.out.println("Please enter your name: "); + String memberName = scanner.next(); + System.out.println("Please enter your ID: "); + long memberId = scanner.nextLong(); + + for (Normal member : L._Trustee) { + if (Objects.equals(memberName, member.getName()) && memberId == member.getId()) { + Rent rent = new Rent(book, member, memberId); + L._rental_registries.add(rent); + System.out.println("\033[1;33mThe book has been successfully rented\033[0m"); // yellow text + book.set_Availability(false); + bookRented = true; + break; + } + } + if (!bookRented) { + System.out.println("We don't have a member with this name in our list. If you want to rent a book, you have to be a member first."); } - if(check==false){ - System.out.println("We don't have a member with this name in our list. If you want to rent a book, you have to be a member first.\n" + - "enter lib add member\" otherwise press any key"); + } else { + System.out.println("It seems that this book has already been rented, check it out later."); + } + break; // Exit the loop once the book is found and processed + } + } + + if (!bookFound) { + System.out.println("This book is not available at the moment. We would appreciate it if you added it to our library."); + } + + scanner.close(); + } + public static void rentttt(Library L, String name) { + boolean bookFound = false; + boolean bookRented = false; + if (L._Book_repository.isEmpty()) { + System.out.println("You have not registered a book yet."); + return; + } + + Scanner scanner = new Scanner(System.in); + for (Book book : L._Book_repository) { + if (book.get_Title().equals(name)) { + bookFound = true; + if (book.get_Availability()) { + System.out.print("Please enter your name: "); + String memberName = ""; + long memberId = -1; + + if (scanner.hasNext()) { + memberName = scanner.next(); + } else { + System.out.println("No input received for member name."); + break; + } + + System.out.print("Please enter your ID: "); + try { + if (scanner.hasNextLong()) { + memberId = scanner.nextLong(); + } else { + System.out.println("Invalid input for ID. Please enter a numeric value."); + scanner.next(); // Clear the invalid input + break; + } + } catch (InputMismatchException e) { + System.out.println("Invalid input for ID. Please enter a numeric value."); + scanner.next(); // Clear the invalid input break; } - if (check){break;} + for (Normal member : L._Trustee) { + if (Objects.equals(memberName, member.getName()) && memberId == member.getId()) { + Rent rent = new Rent(book, member, memberId); + L._rental_registries.add(rent); + System.out.println("\033[1;33mThe book has been successfully rented\033[0m"); // yellow text + book.set_Availability(false); + bookRented = true; + break; + } + } + + if (!bookRented) { + System.out.println("We don't have a member with this name in our list. If you want to rent a book, you have to be a member first."); + } } else { - System.out.println("It seems that this book have been rented, check it out later"); - break; + System.out.println("It seems that this book has already been rented, check it out later."); } - } else { - System.out.println("This book is not available at the moment, We would appreciate if you add it to our library."); - break; + break; // Exit the loop once the book is found and processed } } + + if (!bookFound) { + System.out.println("This book is not available at the moment. We would appreciate it if you added it to our library."); + } + + scanner.close(); } + public static void returnbook(Library L, String name) { + boolean match = true; for (Book Book : L._Book_repository) { if (Book.get_Title().equals(name)) { if (Book.get_Availability()) { + yellow(); System.out.println("It seems that we already have this book, give it to someone else"); + blue(); + match = false; + break; } else { + yellow(); Book.set_Availability(true); System.out.println("Thanks for bringing our book back, hope you enjoyed it!"); + blue(); + match =false; + break; } - } else { - System.out.println("It seems that this book doesn't belong to our library "); } } + if(match) { + System.out.println("It seems that this book doesn't belong to our library "); + } } static String phonenumber(String number) { if (number.length() != 10) { @@ -148,16 +352,6 @@ static String phonenumber(String number) { } return '0' + number; } - static String id(String id) { - if (id.length() > 13 || id.length() < 4) { - return "wrong"; - } - if (!id.matches("[0-9]+")) { - return "wrong"; - } else { - return id; - } - } public static void membership(Library L) { Scanner scanner4 = new Scanner(System.in); System.out.println("Please enter your name"); @@ -349,21 +543,27 @@ public static void wrongpose() { System.out.println(" Sorry! You are not at admin position so you can't do this command."); blue(); } + public static void showbooks(Library L) { if (L._Book_repository.isEmpty()) { - System.out.println("You have not registered a book yet.");} - else { + System.out.println("You have not registered a book yet."); + } else { for (Book B : L._Book_repository) { + yellow(); + System.out.println("------------------------------"); if (B.get_Availability()) { - yellow(); - System.out.println("----------------------------"); blue(); - System.out.println(B.get_Title()); + System.out.println(B.get_Title() + " available"); yellow(); - System.out.println("----------------------------"); + } else { blue(); + System.out.println(B.get_Title() + ANSI_GRAY + " rented"); + yellow(); } + System.out.println("------------------------------"); + blue(); } + } } public static void changehrs() { @@ -400,40 +600,189 @@ public static void changehrs() { } } +// public static void rentspeceficc( String name ,String membername , long id, Library L ){ +// System.out.println("sher o ver aval"); +// if (L._Book_repository.isEmpty()) { +// System.out.println("You have not registered a book yet.");} +// else{ +// System.out.println("sher o ver dovom"); +// boolean match = false; +// boolean available = true; +// while (true) { +// for (Book B : L._Book_repository) { +// if (B.get_Title().equals(name)) { +// System.out.println("sher o ver sevom"); +// match = true; +// if (B.get_Availability()) { +// for (Normal N : L._Trustee) { +// if (Objects.equals(N.getName(), membername)) { +// System.out.println("fcfcfgcgcfgccfgc"); +// if (N.getId() == id) { +// available = false; +// Rent rent = new Rent(B, N, id); +// L._rental_registries.add(rent); +// yellow(); +// System.out.println("Here is your book. please take care of it"); +// blue(); +// } else { +// System.out.println("Sorry we can't give this book because your name doesn't match with your id."); +// } +// } else { +// System.out.println("Sorry we don't have you as a member in our list so we can't give it to you.if you're really interested, sign in first"); +// } +// } +// } else if(available) { +// System.out.println("Sorry it seems like someone else has already rented this book. check it out later"); +// } +// break; +// } +// } +// break; +// } +// if (match == false) { +// } +// } +// } +// public static void rentspecefic1(String name, String membername, long id, Library L) { +// System.out.println("Initial check"); +// if (L._Book_repository.isEmpty()) { +// System.out.println("You have not registered a book yet."); +// return; +// } +// boolean bookFound = false; +// boolean bookAvailable = false; +// for (Book B : L._Book_repository) { +// if (B.get_Title().equals(name)) { +// bookFound = true; +// if (B.get_Availability()) { +// for (Normal N : L._Trustee) { +// if (Objects.equals(N.getName(), membername)) { +// if (N.getId() == id) { +// Rent rent = new Rent(B, N, id); +// L._rental_registries.add(rent); +// System.out.println("Here is your book. Please take care of it."); +// return; +// } else { +// System.out.println("Sorry we can't give this book because your name doesn't match with your ID."); +// return; +// } +// } +// } +// System.out.println("Sorry we don't have you as a member in our list, so we can't give it to you. If you're really interested, sign in first."); +// return; +// } else { +// bookAvailable = false; +// } +// break; +// } +// } +// +// if (!bookFound) { +// System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); +// } else { +// System.out.println("Sorry it seems like someone else has already rented this book. Check it out later."); +// } +// } public static void rentspecefic( String name ,String membername , long id, Library L ){ - boolean match =false; - boolean available=true; - while (true) { + boolean khastam=false; + outerloop: + while (true){ + Normal member = null; + for (Normal N : L._Trustee) { + if (Objects.equals(N.getName(), membername) && N.getId() == id) { + member=N; + khastam=true; + break ; + } + else if (Objects.equals(N.getName(), membername) && N.getId() != id) { + yellow(); + System.out.println("Sorry we can't give this book because your name doesn't match with your id."); + blue(); + khastam=true; + break outerloop; + } + } + if(!khastam){ + yellow(); + System.out.println("Sorry we don't have you as a member in our list so we can't give you the book .if you're really interested, sign in first"); + blue(); + break ; + } + int count =0; + boolean avail=false; + Book bookToRent = null; for (Book B : L._Book_repository) { - if (B.get_Title().equals(name)) { - match = true; - if (B.get_Availability()) { - available = false; - for (Normal N : L._Trustee) { - if (Objects.equals(N.getName(), membername)) { - if (N.getId() == id) { - Rent rent = new Rent(B, N, id); - L._rental_registries.add(rent); - } - else { - System.out.println("Sorry we can't give this book because your name doesn't match with your id.");} - break; - } - else { - System.out.println("Sorry we don't have you as a member in our list so we can't give it to you.if you're really interested, sign in first"); - } - } - } - else { - System.out.println("Sorry it seems like someone else has already rented this book. check it out later"); + if (Objects.equals(B.get_Title(), name)) { + count++; + if(B.get_Availability()){ + avail=true; + bookToRent = B; + break; } - break; } } - break; + if(count==0){ + yellow(); + System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); + blue(); + break ; + } + else if(count==1 && avail){ + bookToRent.set_Availability(false); + Rent rent = new Rent(bookToRent, member, id); + L._rental_registries.add(rent); + yellow(); + System.out.println("Here is your book. please take care of it"); + blue(); + break ; + } + else if(!avail){ + System.out.println("Sorry it seems like someone else has already rented this book. check it out later"); + break ; + } } - if (match==false) { - System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); + } +// public static void rentttt(String name, String membername, long id, Library L) { +// Normal member = null; +// for (Normal N : L._Trustee) { +// if (Objects.equals(N.getName(), membername)) { +// if (N.getId() == id) { +// member = N; +// break; +// } else { +// System.out.println("Sorry we can't give this book because your name doesn't match with your id."); +// return; +// } +// } +// } +// if (member == null) { +// System.out.println("Sorry we don't have you as a member in our list so we can't give you the book. If you're really interested, sign in first."); +// return; +// } +// Book bookToRent = null; +// int count = 0; +// for (Book B : L._Book_repository) { +// if (Objects.equals(B.get_Title(), name)) { +// bookToRent = B; +// count++; +// } +// } +// if (count == 0) { +// System.out.println("Sorry it seems like we don't have this book. You can add it to our library."); +// return; +// } +// if (count == 1) { +// Rent rent = new Rent(bookToRent, member, id); +// L._rental_registries.add(rent); +// System.out.println("Book rented successfully."); +// } else { +// System.out.println("Sorry, there are multiple copies of the book. Please specify which one."); +// } +// } + + public static void list(Library L){ + for (Normal N : L._Trustee) { + System.out.println( N.getName() + "--->" + N.getId()); } } public static boolean checking(String c, Library L, boolean position) { @@ -469,8 +818,17 @@ public static boolean checking(String c, Library L, boolean position) { Pattern pr8 = Pattern.compile("^lib\\s+return\\s+<([a-zA-Z0-9]+(?:\\s+[a-zA-Z0-9]*)*)>\\s*"); Matcher m8 = pr8.matcher(c); + Pattern pr10 = Pattern.compile("^List"); + Matcher m10 = pr10.matcher(c); + + if (m1.find()) { - addbook(L, buildingbook(idgeneratormember(), m1.group(1), m1.group(2), true, m1.group(3))); + if(!duplication(L, m1.group(1))) { + addbook(L, buildingbook(idgeneratormember(), m1.group(1), m1.group(2), true, m1.group(3))); + } + else{ + System.out.println("We already own a book with this title. If you want to add it choose another name or put numbers in it's title"); + } return true; } else if (m2.find()) { @@ -496,6 +854,7 @@ else if (m4.find()) { else if(m5.find()){ long id = Long.parseLong(m5.group(3)); rentspecefic(m5.group(1) , m5.group(2) , id , L); + return true; } else if (m6.find()) { showbooks(L); @@ -521,10 +880,15 @@ else if (m8.find()) { } return true; } + if (m10.find()) { + list(L); + return true; + } else { return false;} } + public static void main(String[] args) { Scanner scanner = new Scanner(System.in); blue(); @@ -591,10 +955,10 @@ public static void main(String[] args) { red(); System.out.println("Wrong command! Please try again."); blue(); - continue; } } - System.out.println("if you have no other commands, enter YES otherwise press any key"); + + System.out.println(ANSI_PURPLE + "if you have no other commands, enter YES otherwise press any key" + ANSI_NILI); String answer1 = scanner1.next(); if (Objects.equals(answer1, "YES")) { break; @@ -603,7 +967,8 @@ public static void main(String[] args) { printColoredText(); } - System.out.println("Thank you for choosing our Library Management System." + - " We're excited to provide you with an efficient and user-friendly platform for all your library needs. Happy reading!"); + yellow(); + System.out.println("Thank you for choosing our Library Management System.\n" + + "We're excited to provide you with an efficient and user-friendly platform for all your library needs. Happy reading!"); } }