diff --git a/Answers/40230212013/library_management_system/.vscode/settings.json b/Answers/40230212013/library_management_system/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/Answers/40230212013/library_management_system/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/Answers/40230212013/library_management_system/README.md b/Answers/40230212013/library_management_system/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/Answers/40230212013/library_management_system/README.md @@ -0,0 +1,18 @@ +## Getting Started + +Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. + +## Folder Structure + +The workspace contains two folders by default, where: + +- `src`: the folder to maintain sources +- `lib`: the folder to maintain dependencies + +Meanwhile, the compiled output files will be generated in the `bin` folder by default. + +> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. + +## Dependency Management + +The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). diff --git a/Answers/40230212013/library_management_system/bin/App.class b/Answers/40230212013/library_management_system/bin/App.class new file mode 100644 index 0000000..0307f8f Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/App.class differ diff --git a/Answers/40230212013/library_management_system/bin/admin.class b/Answers/40230212013/library_management_system/bin/admin.class new file mode 100644 index 0000000..3f477a1 Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/admin.class differ diff --git a/Answers/40230212013/library_management_system/bin/book.class b/Answers/40230212013/library_management_system/bin/book.class new file mode 100644 index 0000000..b20701c Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/book.class differ diff --git a/Answers/40230212013/library_management_system/bin/cli.class b/Answers/40230212013/library_management_system/bin/cli.class new file mode 100644 index 0000000..ab59ff5 Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/cli.class differ diff --git a/Answers/40230212013/library_management_system/bin/library.class b/Answers/40230212013/library_management_system/bin/library.class new file mode 100644 index 0000000..74aebb1 Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/library.class differ diff --git a/Answers/40230212013/library_management_system/bin/normalUser.class b/Answers/40230212013/library_management_system/bin/normalUser.class new file mode 100644 index 0000000..80e2230 Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/normalUser.class differ diff --git a/Answers/40230212013/library_management_system/bin/rent.class b/Answers/40230212013/library_management_system/bin/rent.class new file mode 100644 index 0000000..707217b Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/rent.class differ diff --git a/Answers/40230212013/library_management_system/bin/user.class b/Answers/40230212013/library_management_system/bin/user.class new file mode 100644 index 0000000..3ba0bd3 Binary files /dev/null and b/Answers/40230212013/library_management_system/bin/user.class differ diff --git a/Answers/40230212013/library_management_system/src/App.class b/Answers/40230212013/library_management_system/src/App.class new file mode 100644 index 0000000..4c56d7d Binary files /dev/null and b/Answers/40230212013/library_management_system/src/App.class differ diff --git a/Answers/40230212013/library_management_system/src/App.java b/Answers/40230212013/library_management_system/src/App.java new file mode 100644 index 0000000..fb02457 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/App.java @@ -0,0 +1,7 @@ +public class App { + public static void main(String[] args) { + library library = new library("My Library", 100, "9:00 AM - 5:00 PM"); + cli cli = new cli(library); + cli.start(); + } +} diff --git a/Answers/40230212013/library_management_system/src/admin.class b/Answers/40230212013/library_management_system/src/admin.class new file mode 100644 index 0000000..41bd71a Binary files /dev/null and b/Answers/40230212013/library_management_system/src/admin.class differ diff --git a/Answers/40230212013/library_management_system/src/admin.java b/Answers/40230212013/library_management_system/src/admin.java new file mode 100644 index 0000000..6498c2a --- /dev/null +++ b/Answers/40230212013/library_management_system/src/admin.java @@ -0,0 +1,15 @@ +public class admin extends user{ + private int password; + public admin(String name,int id, String phoneNumber, int password) + { + super(name, id, phoneNumber); + this.password = password; + } + public int getPassword() { + return password; + } + public void setPassword(int password) { + this.password = password; + } + +} \ No newline at end of file diff --git a/Answers/40230212013/library_management_system/src/book.class b/Answers/40230212013/library_management_system/src/book.class new file mode 100644 index 0000000..ea4ba36 Binary files /dev/null and b/Answers/40230212013/library_management_system/src/book.class differ diff --git a/Answers/40230212013/library_management_system/src/book.java b/Answers/40230212013/library_management_system/src/book.java new file mode 100644 index 0000000..a423132 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/book.java @@ -0,0 +1,58 @@ +import java.util.UUID; + +public class book { + private int bookId; + private String title; + private String author; + private boolean availabilityStatus; + private String description; + public book(String title, String author, String description) + { + this.bookId = UUID.randomUUID().hashCode(); + this.title = title; + this.author = author; + this.availabilityStatus = true; + this.description = description; + } + @Override + public String toString() { + return "Book{" + + "bookID=" + bookId + + ", title='" + title + '\'' + + ", author='" + author + '\'' + + ", available=" + availabilityStatus + + ", description='" + description + '\'' + + '}'; + } + public int getBookId() { + return bookId; + } + public void setBookId(int bookId) { + this.bookId = bookId; + } + public String getTitle() { + return title; + } + public void setTitle(String title) { + this.title = title; + } + public String getAuthor() { + return author; + } + public void setAuthor(String author) { + this.author = author; + } + public boolean isAvailabilityStatus() { + return availabilityStatus; + } + public void setAvailabilityStatus(boolean availabilityStatus) { + this.availabilityStatus = availabilityStatus; + } + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/Answers/40230212013/library_management_system/src/cli.class b/Answers/40230212013/library_management_system/src/cli.class new file mode 100644 index 0000000..7f4552e Binary files /dev/null and b/Answers/40230212013/library_management_system/src/cli.class differ diff --git a/Answers/40230212013/library_management_system/src/cli.java b/Answers/40230212013/library_management_system/src/cli.java new file mode 100644 index 0000000..c5c5245 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/cli.java @@ -0,0 +1,319 @@ +import java.util.List; +import java.util.Scanner; +import java.util.UUID; + +public class cli { + private library library; + private Scanner input; + public cli(library library) + { + this.library = library; + this.input = new Scanner(System.in); + } + public void start() + { + while (true) { + System.out.println("Enter 'log admin' for Login Admin"); + System.out.println("Enter 'log user' for Login user"); + System.out.println("enter 'exit' for close the program"); + System.out.print("> "); + String command = input.nextLine().toLowerCase(); + switch (command) + { + case "log admin": + loginAdmin(); + break; + case "log user": + loginUser(); + break; + case "exit": + System.out.println("Exiting the system..."); + return; + default: + System.out.println("Invalid command. Please try again."); + } + } + } +private void loginAdmin() + { + System.out.print("Enter name: "); + String name = input.nextLine(); + System.out.print("Enter ID: "); + int id = input.nextInt(); + String phoneNumber = input.nextLine(); + System.out.print("Enter password: "); + int password = input.nextInt(); + + admin admin = library.getAdmin(name, id, phoneNumber, password); + if (admin != null) { + System.out.println("Admin login successful!"); + adminMenu(admin); + } else + { + System.out.println("Invalid credentials. Please try again."); + } +} +private void loginUser() + { + System.out.print("Enter name: "); + String name = input.nextLine(); + System.out.print("Enter phone number: "); + String phoneNumber = input.nextLine(); + System.out.print("Enter ID: "); + int id = input.nextInt(); + normalUser user = library.getNormalUser(name, phoneNumber, id); + if (user != null) { + System.out.println("User login successful!"); + userMenu(user); + } else + { + System.out.println("Invalid credentials. Please try again."); + } +} +private void adminMenu(admin admin) + { + while (true) + { + System.out.println("\nAdmin Menu:"); + System.out.println("'lib add book' for Add new book"); + System.out.println("'lib del book' for Remove book"); + System.out.println("'lib add member' for Add new member"); + System.out.println("'lib del member' for Remove member"); + System.out.println("'lib rent' for Rent book"); + System.out.println("'lib return' for Return book"); + System.out.println("'lib get available books' for View available books"); + System.out.println("'lib get hrs' for View library operating hours"); + System.out.println("'lib out' for Logout"); + System.out.print(">>> "); + String choice = input.nextLine(); +switch (choice) { + case "lib add book": + addNewBook(admin); + break; + case "lib del book": + removeBook(admin); + break; + case "lib add member": + addNewMember(admin); + break; + case "lib del member": + removeMember(admin); + break; + case "lib rent": + rentBook(admin); + break; + case "lib return": + returnBook(admin); + break; + case "lib get available books": + viewAvailableBooks(); + break; + case "lib get hrs": + viewLibraryHours(); + break; + case "lib out": + System.out.println("Admin logout successful!"); + return; + default: + System.out.println("Invalid choice. Please try again."); + } + } +} +private void userMenu(normalUser user) { + while (true) { + System.out.println("\nNormal User Menu:"); + System.out.println("'lib add book' for Add new book"); + System.out.println("'lib rent' for Rent book"); + System.out.println("'lib return' for Return book"); + System.out.println("'lib get available books' for View available books"); + System.out.println("'lib get hrs' for View library operating hours"); + System.out.println("'lib out' for Logout"); + System.out.print(">>> "); + String choice = input.nextLine(); + + switch (choice) { + case "lib add book": + addNewBook(user); + break; + case "lib rent": + rentBook(user); + break; + case "lib return": + returnBook(user); + break; + case "lib get available books": + viewAvailableBooks(); + break; + case "lib get hrs": + viewLibraryHours(); + break; + case "lib out": + System.out.println("User logout successful!"); + return; + default: + System.out.println("Invalid choice. Please try again."); + } + } +} +//************************************************************************************************************************************************************* */ +private void addNewBook(admin admin) { + System.out.print("Enter book title: "); + String title = input.nextLine(); + System.out.print("Enter author: "); + String author = input.nextLine(); + System.out.print("Enter description: "); + String description = input.nextLine(); + + + book book = new book(title, author, description); + library.addBook(book); + System.out.println("Book added successfully!"); +} + +private void removeBook(admin admin) { + System.out.print("Enter book ID: "); + int bookId = input.nextInt(); + + book book = library.removeBook(bookId); + if (book != null) { + System.out.println("Book removed successfully!"); + } else { + System.out.println("Book with ID " + bookId + " not found."); + } +} + +private void addNewMember(admin admin) { + System.out.print("Enter member name: "); + String name = input.nextLine(); + System.out.print("Enter member phone number: "); + String phoneNumber = input.nextLine(); + int id = UUID.randomUUID().hashCode(); + System.out.print("Enter Member's Sign Up Date: "); + String signUpDate = input.nextLine(); + normalUser user = new normalUser(name, id, phoneNumber, signUpDate); + library.addUser(user); + System.out.println("Member added successfully!"); + System.out.println("Member ID is: "+ id); +} + +private void removeMember(admin admin) { + System.out.print("Enter member ID: "); + int memberId = input.nextInt(); + + normalUser user = library.removeUser(memberId); + if (user != null) { + System.out.println("Member removed successfully!"); + } else { + System.out.println("Member with ID " + memberId + " not found."); + } +} +private void rentBook(admin admin) { + System.out.print("Enter book title: "); + String bookTitle = input.nextLine(); + System.out.print("Enter member name: "); + String memberName = input.nextLine(); + System.out.print("Enter member ID: "); + int memberId = input.nextInt(); + /*System.out.print("Enter member phone number: ");*/ + String memberPhone = input.nextLine(); + + normalUser user = library.getNormalUser(memberName, memberPhone, memberId); + book book = library.getBook(bookTitle); + + if ((user != null && book != null && book.isAvailabilityStatus())|| (memberName == "Navid" && memberId == 123456789 && book != null && book.isAvailabilityStatus())) { + library.rentBook(user, book); + System.out.println("Book rented successfully for " + user.getName() + "!"); + } else { + if (user == null) { + System.out.println("Member with name " + memberName + " and ID " + memberId + " not found."); + } else if (book == null) { + System.out.println("Book with title " + bookTitle + " not found."); + } else { + System.out.println("Book is not available for rent."); + } + } +} + +private void returnBook(admin admin) { + System.out.print("Enter book title: "); + String bookTitle = input.nextLine(); + + book book = library.getBook(bookTitle); + if (book != null && !book.isAvailabilityStatus()) { + library.returnBook(book); + System.out.println("Book returned successfully!"); + } else { + if (book == null) { + System.out.println("Book with title " + bookTitle + " not found."); + } else { + System.out.println("Book is already available."); + } + } +} +//************************************************************************************************************************************************************* */ +private void addNewBook(normalUser user) { + System.out.print("Enter book title: "); + String title = input.nextLine(); + System.out.print("Enter author: "); + String author = input.nextLine(); + System.out.print("Enter description: "); + String description = input.nextLine(); + + book book = new book(title, author, description); + library.addBook(book); + System.out.println("Book added successfully!"); +} +private void rentBook(normalUser user) { + System.out.print("Enter book title: "); + String bookTitle = input.nextLine(); + book book = library.getAvailableBook(bookTitle); + if (book != null) { + library.rentBook(user, book); + System.out.println("Book rented successfully!"); + } else { + System.out.println("Book not available for rent."); + } +} +private void returnBook(normalUser user) { + System.out.print("Enter book title: "); + String bookTitle = input.nextLine(); + System.out.print("Enter member name: "); + String memberName = input.nextLine(); + System.out.print("Enter member ID: "); + int memberId = input.nextInt(); + /*System.out.print("Enter member phone number: ");*/ + String memberPhone = input.nextLine(); + normalUser user1 = library.getNormalUser(memberName, memberPhone, memberId); + book book = library.getBook(bookTitle); + + if (user1 != null && book != null && book.isAvailabilityStatus()) { + library.rentBook(user1, book); + System.out.println("Book rented successfully for " + user.getName() + "!"); + } else { + if (user1 == null) { + System.out.println("Member with name " + memberName + " and ID " + memberId + " not found."); + } else if (book == null) { + System.out.println("Book with title " + bookTitle + " not found."); + } else { + System.out.println("Book is not available for rent."); + } + } +} + +private void viewAvailableBooks() { + List availableBooks = library.getAvailableBooks(); + if (!availableBooks.isEmpty()) { + System.out.println("\nAvailable Books:"); + for (book book : availableBooks) { + System.out.println(book); + } + } else { + System.out.println("No books currently available for rent."); + } +} + +private void viewLibraryHours() { + System.out.println("\nLibrary Hours:"); + System.out.println(library.getOperatingHours()); +} +} \ No newline at end of file diff --git a/Answers/40230212013/library_management_system/src/library.class b/Answers/40230212013/library_management_system/src/library.class new file mode 100644 index 0000000..9dae6a1 Binary files /dev/null and b/Answers/40230212013/library_management_system/src/library.class differ diff --git a/Answers/40230212013/library_management_system/src/library.java b/Answers/40230212013/library_management_system/src/library.java new file mode 100644 index 0000000..b5534d7 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/library.java @@ -0,0 +1,147 @@ +import java.util.ArrayList; +import java.util.List; + +public class library { + private String libraryName; + private int capacity; + private String operatingHours; + private List books; + private List users; + private List rentals; + public library(String libraryName, int capacity, String operatingHours) { + this.libraryName = libraryName; + this.capacity = capacity; + this.operatingHours = operatingHours; + this.books = new ArrayList<>(); + this.users = new ArrayList<>(); + this.rentals = new ArrayList<>(); + } + public String getLibraryName() { + return libraryName; + } + public int getCapacity() { + return capacity; + } + public String getOperatingHours() { + return operatingHours; + } + public void addBook(book book) { + if (books.size() < capacity) { + books.add(book); + System.out.println("Book added successfully!"); + System.out.println(book.getBookId()); + } else { + System.out.println("Library at full capacity. Book cannot be added."); + } +} +public book removeBook(int bookId) { + book removedBook = null; + for (int i = 0; i < books.size(); i++) { + if (books.get(i).getBookId() == bookId) { + removedBook = books.remove(i); + break; + } + } + if (removedBook != null) { + System.out.println("Book removed successfully!"); + } else { + System.out.println("Book with ID " + bookId + " not found."); + } + return removedBook; +} +public void addUser(normalUser user) { + users.add(user); + System.out.println("Member added successfully!"); +} +public normalUser removeUser(int memberId) { + normalUser removedUser = null; + for (int i = 0; i < users.size(); i++) { + if (users.get(i).getId() == memberId) { + removedUser = users.remove(i); + break; + } + } + if (removedUser != null) { + System.out.println("Member removed successfully!"); + } else { + System.out.println("Member with ID " + memberId + " not found."); + } + return removedUser; +} +public void rentBook(normalUser user, book book) { + if (book.isAvailabilityStatus()) { + rent rent = new rent(book, user); + rentals.add(rent); + book.setAvailabilityStatus(false); + System.out.println("Book rented successfully for " + user.getName() + "!"); + } else { + System.out.println("Book is not available for rent."); + } +} +public void returnBook(book book) { + boolean bookReturned = false; + for (rent rent : rentals) { + if (rent.getBook().equals(book)) { + rentals.remove(rent); + book.setAvailabilityStatus(true); + bookReturned = true; + System.out.println("Book returned successfully!"); + break; + } + } + if (!bookReturned) { + System.out.println("Book not found or not currently rented."); + } +} + +public book getBook(String bookTitle) { + for (book book : books) { + if (book.getTitle().equalsIgnoreCase(bookTitle)) { + return book; + } + } + return null; +} +public List getAvailableBooks() { + List availableBooks = new ArrayList<>(); + for (book book : books) { + if (book.isAvailabilityStatus()) { + availableBooks.add(book); + } + } + return availableBooks; +} +public book getAvailableBook(String bookTitle) { + for (book book : books) { + if (book.getTitle().equalsIgnoreCase(bookTitle) && book.isAvailabilityStatus()) { + return book; + } + } + return null; +} +public book getBorrowedBook(normalUser user, String bookTitle) { + for (rent rent : rentals) { + if (rent.getBook().getTitle().equalsIgnoreCase(bookTitle) && rent.getUser().equals(user)) { + return rent.getBook(); + } + } + return null; +} +public admin getAdmin(String name, int id, String phoneNumber, int password) { + admin admin1 = new admin("Navid", 123456789, "9204201533", 12345); + + if (admin1 instanceof admin && admin1.getName().equals(name) && admin1.getId() == id && ((admin) admin1).getPassword() == password) { + return (admin) admin1; + } + return null; +} +public normalUser getNormalUser(String name, String phoneNumber, int id) { + for (user user : users) { + if (user instanceof normalUser && user.getName().equals(name) && user.getId() == id) { + return (normalUser) user; + } + + } + return null; +} +} \ No newline at end of file diff --git a/Answers/40230212013/library_management_system/src/normalUser.class b/Answers/40230212013/library_management_system/src/normalUser.class new file mode 100644 index 0000000..e26be67 Binary files /dev/null and b/Answers/40230212013/library_management_system/src/normalUser.class differ diff --git a/Answers/40230212013/library_management_system/src/normalUser.java b/Answers/40230212013/library_management_system/src/normalUser.java new file mode 100644 index 0000000..69f2bb7 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/normalUser.java @@ -0,0 +1,18 @@ +public class normalUser extends user { + private String signUpDate; + + public normalUser(String name, int id, String phoneNumber, String signUpDate) + { + super(name,id, phoneNumber); + this.signUpDate = signUpDate; + } + + public String getSignUpDate() { + return signUpDate; + } + + public void setSignUpDate(String signUpDate) { + this.signUpDate = signUpDate; + } + +} diff --git a/Answers/40230212013/library_management_system/src/rent.class b/Answers/40230212013/library_management_system/src/rent.class new file mode 100644 index 0000000..56cfba6 Binary files /dev/null and b/Answers/40230212013/library_management_system/src/rent.class differ diff --git a/Answers/40230212013/library_management_system/src/rent.java b/Answers/40230212013/library_management_system/src/rent.java new file mode 100644 index 0000000..673cf44 --- /dev/null +++ b/Answers/40230212013/library_management_system/src/rent.java @@ -0,0 +1,38 @@ +public class rent { + private book book; + private user user; + private int rentalId; + private String rentalDate; + public rent(book book, user user) + { + this.book = book; + this.user = user; + this.rentalId = rentalId; + this.rentalDate = rentalDate; + } + public book getBook() { + return book; + } + public void setBook(book book) { + this.book = book; + } + public user getUser() { + return user; + } + public void setUser(user user) { + this.user = user; + } + public int getRentId() { + return rentalId; + } + public void setRentId(int rentId) { + this.rentalId = rentalId; + } + public String getRentDate() { + return rentalDate; + } + public void setRentDate(String rentDate) { + this.rentalDate = rentalDate; + } + +} diff --git a/Answers/40230212013/library_management_system/src/user.class b/Answers/40230212013/library_management_system/src/user.class new file mode 100644 index 0000000..9ab6076 Binary files /dev/null and b/Answers/40230212013/library_management_system/src/user.class differ diff --git a/Answers/40230212013/library_management_system/src/user.java b/Answers/40230212013/library_management_system/src/user.java new file mode 100644 index 0000000..53ad38b --- /dev/null +++ b/Answers/40230212013/library_management_system/src/user.java @@ -0,0 +1,29 @@ +public class user { + private String name; + private int id; + private String phoneNumber; + public user(String name,int id, String phoneNumber) + { + this.name = name; + this.id = id; + this.phoneNumber = phoneNumber; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getId() { + return id; + } + public void setId(int id) { + this.id = id; + } + public String getPhoneNumber() { + return phoneNumber; + } + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } +}