diff --git a/Answers/40230212010/.vscode/settings.json b/Answers/40230212010/.vscode/settings.json new file mode 100644 index 0000000..e112a70 --- /dev/null +++ b/Answers/40230212010/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "java.project.sourcePaths": ["src"], + "java.project.outputPath": "bin", + "java.project.referencedLibraries": [ + "lib/**/*.jar" + ] +} diff --git a/Answers/40230212010/README.md b/Answers/40230212010/README.md new file mode 100644 index 0000000..7c03a53 --- /dev/null +++ b/Answers/40230212010/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/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java new file mode 100644 index 0000000..56edd73 --- /dev/null +++ b/Answers/40230212010/src/Admin.java @@ -0,0 +1,10 @@ +public class Admin extends User{ + public static final String password="123"; + public Admin(String name,String phoneNumber,String password) { + super( name, phoneNumber); + + } + + + +} diff --git a/Answers/40230212010/src/App.java b/Answers/40230212010/src/App.java new file mode 100644 index 0000000..3ecbb62 --- /dev/null +++ b/Answers/40230212010/src/App.java @@ -0,0 +1,169 @@ +import java.lang.reflect.Array; +import java.util.*; + +public class App { + public static void main(String[] args) throws Exception { + Scanner scanner = new Scanner(System.in); + Library library = new Library("Main Library", 1000, "9am to 5pm"); + + System.out.println("Welcome to " + library.getName() + "!"); + System.out.println("Current capacity: " + library.getCapacity()); + System.out.println("Operating hours: " + library.getOperatingHours()); + User user = new User(null, null); + boolean exit = false; + boolean exit2 = false; + boolean exit3 = false; + + while (!exit3) { + while (!exit) { + System.out.println("Choose your role:"); + System.out.println("1. Admin"); + System.out.println("2. Normal User"); + System.out.println("3. Register"); + System.out.println("4. exit."); + int choice2 = Integer.parseInt(scanner.nextLine()); + switch (choice2) { + case 1: + System.out.println("Enter Password:"); + String password = scanner.nextLine(); + if (password.equals(Admin.password)) { + exit = true; + } else { + System.out.println("Wrong Password!"); + } + break; + case 2: + System.out.println("Enter your name:"); + String name = scanner.nextLine(); + System.out.println("Enter Password:"); + String pass2 = scanner.nextLine(); + if (name.equals(User.name)) { + if (pass2.equals(User.password)) { + exit = true; + } + } + break; + case 3: + System.out.println("Enter your name:"); + String name1 = scanner.nextLine(); + System.out.println("Enter your Phone Number:"); + String phoneNumber = scanner.nextLine(); + System.out.println("Enter Password:"); + String password3 = scanner.nextLine(); + user = new User(name1, phoneNumber); + User.password = password3; + exit = true; + break; + case 4: + System.out.println("thank you..."); + exit3=true; + System.exit(0); + break; + default: + System.out.println("Invalid choice."); + break; + } + + } + exit=false; + + while (!exit2) { + + System.out.println("\nChoose an option:"); + System.out.println("1. Add book"); + System.out.println("2. Get hours"); + System.out.println("3. Rent book"); + System.out.println("4. Add member"); + System.out.println("5. Remove member"); + System.out.println("6. Return book"); + System.out.println("7. Get available books"); + System.out.println("8. Back to main menu."); + + int choice = scanner.nextInt(); + scanner.nextLine(); + + switch (choice) { + case 1: + System.out.print("Enter book title: "); + String title = scanner.nextLine(); + System.out.print("Enter book author: "); + String author = scanner.nextLine(); + System.out.print("Enter book description: "); + String description = scanner.nextLine(); + library.addBook(title, author, description); + System.out.println("Book added successfully."); + break; + case 2: + System.out.println("Operating hours: " + library.getOperatingHours()); + break; + case 3: + System.out.print("Enter book name: "); + String bookName = scanner.nextLine(); + + System.out.print("Enter student ID: "); + String UniqueID = scanner.nextLine(); + library.rentBook(bookName, user); + // NormalUser users = library.getNormalUser(UniqueID); + // if (user == null) { + // System.out.println("User not found."); + // } else { + // library.rentBook(bookName, user); + // } + break; + case 4: + System.out.print("Enter Admin Name: "); + UniqueID = scanner.nextLine(); + System.out.print("Enter password: "); + String passwordd = scanner.nextLine(); + + if (passwordd == Admin.password) { + System.out.print("Enter new member name: "); + String newName = scanner.nextLine(); + System.out.print("Enter new member phone number: "); + String phoneNumber = scanner.nextLine(); + library.addNormalUser(newName, UniqueID, phoneNumber); + System.out.println("New member added successfully."); + } else { + + System.out.println("Invalid password."); + } + break; + case 5: + System.out.print("Enter member ID: "); + String memberID = scanner.nextLine(); + library.removeUser(memberID); + System.out.println("Member removed successfully."); + break; + case 6: + System.out.print("Enter book name: "); + String bookToReturnName = scanner.nextLine(); + Book booktoReturn = library.findBookByName(bookToReturnName); + library.returnBook(user, booktoReturn); + break; + case 7: + ArrayList availableBooks = library.getAvailableBooks(); + if (availableBooks.isEmpty()) { + System.out.println("No books available."); + } else { + System.out.println("Available books:"); + for (Book book : availableBooks) { + System.out.println("- " + book.getTitle() + " by " + book.getAuthor()); + } + } + break; + case 8: + System.out.println("Exiting..."); + exit2 = true; + break; + default: + System.out.println("Invalid choice.."); + } + + } + exit2=false; + + + } + + } +} diff --git a/Answers/40230212010/src/Book.java b/Answers/40230212010/src/Book.java new file mode 100644 index 0000000..b9a17d8 --- /dev/null +++ b/Answers/40230212010/src/Book.java @@ -0,0 +1,63 @@ + +public class Book { + private static int i=0; + private final String bookID; + private String title; + private String description; + private String author; + private boolean isAvailable; + public Book ( String title, String description, String author){ + this.author=author; + this.title=title; + this.description=description; + i++; + this.bookID=String.valueOf(i); + this.isAvailable=true; + } + + public String getBookID() { + return bookID; + } + public boolean isAvailable() { + return isAvailable; + } + public boolean setAvailable(boolean isAvailable) { + return isAvailable; + } + + public static int getI() { + return i; + } + + public static void setI(int i) { + Book.i = i; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + + + +} diff --git a/Answers/40230212010/src/Library.java b/Answers/40230212010/src/Library.java new file mode 100644 index 0000000..73173b1 --- /dev/null +++ b/Answers/40230212010/src/Library.java @@ -0,0 +1,194 @@ +import java.time.LocalDate; +import java.util.*; +import java.lang.*;; + +public class Library { + private User user; + private String name; + private int capacity; + private String operatingHours; + private Map books; + private List users; + private List rentals; + private List adminsList; + private List normalUsersList; + + public Library(String name, int capacity, String operatingHours) { + this.name = name; + this.capacity = capacity; + this.operatingHours = operatingHours; + this.books = new HashMap<>(); + this.users = new ArrayList<>(); + this.rentals = new ArrayList<>(); + List normalUsersList = new ArrayList(); + List admins = new ArrayList(); + } + + public void addBook(String title, String description, String author) { + + if (this.books.containsValue(new Book(title, description, author))) { + System.out.println("This book already exists in the library."); + return; + } + + Book book = new Book(title, description, author); + this.books.put(book.getBookID(), book); + System.out.println("Book added successfully."); + } + + public void removebook(String bookID) { + this.books.remove(bookID); + } + + public Book getBooks(String bookID) { + + return books.get(bookID); + } + + public ArrayList getAvailableBooks() { + ArrayList Availablebooks = new ArrayList<>(); + for (Book book : books.values()) { + if (book.isAvailable()) { + Availablebooks.add(book); + } + } + return Availablebooks; + } + + public void rentBook(String bookName, User user) { + + Book book = this.books.get(bookName); + + if (book == null) { + System.out.print("book not found: " + bookName); + } + + if (!book.isAvailable()) { + System.out.println("Book is currently unavailable: " + bookName); + } + + String rentalID = UUID.randomUUID().toString().substring(0, 8); + + LocalDate rentalDate = LocalDate.now(); + LocalDate dueDate = rentalDate.plusDays(7); + + Rent newRent = new Rent(rentalID, rentalDate, dueDate, book, user); + rentals.add(newRent); + + book.setAvailable(false); + + System.out.println("Book rented successfully. Your due date is: " + dueDate); + + } + + public Book findBookByName(String bookName) { + return books.get(bookName); + } + + public void removeUser(String userID) { + for (NormalUser user : normalUsersList) { + if (user.getUniqueID()== userID) { + normalUsersList.remove(userID); + } + + for (Admin admin : adminsList) { + if (user.getUniqueID() == userID) { + adminsList.remove(user.getUniqueID()); + } + } + } + } + + + + public void addAdmin(String name, String phoneNumber, String password) { + Admin newAdmin = new Admin(name, phoneNumber, password); + adminsList.add(newAdmin); + } + + public void addNormalUser(String name, String phoneNumber, String registrationDate) { + NormalUser newNormalUser = new NormalUser(name, phoneNumber, registrationDate); + normalUsersList.add(newNormalUser); + } + + public NormalUser getNormalUser(String UniqueID) { + for (NormalUser user : normalUsersList) { + if (user.getUniqueID().equals(UniqueID) ) { + return user; + } + } + return null; + } + + public Admin getAdmin(String password) { + for (Admin admin : adminsList) { + if (admin.getPassword().equals(password)) { + return admin; + } + } + return null; + } + + + public void returnBook (User user, Book book) { + if (book.isAvailable()) { + Rent rent = new Rent(name, null, null, book, null); + rentals.add(rent); + book.setAvailable(false); + System.out.println("book returned successfully!"); + } + else{ + System.out.println("not available!"); + } + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public String getOperatingHours() { + return operatingHours; + } + + public void setOperatingHours(String operatingHours) { + this.operatingHours = operatingHours; + } + + public Map getBooks() { + return books; + } + + public void setBooks(Map books) { + this.books = books; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public List getRentals() { + return rentals; + } + + public void setRentals(List rentals) { + this.rentals = rentals; + } + +} diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java new file mode 100644 index 0000000..c69001a --- /dev/null +++ b/Answers/40230212010/src/NormalUser.java @@ -0,0 +1,20 @@ +public class NormalUser extends User { + + private String registrationDate; + + public NormalUser(String name,String phoneNumber,String registrationDate) { + super( name, phoneNumber); + this.registrationDate=registrationDate; + } + + + public String getRegistrationDate() { + return registrationDate; + } + public void setRegistrationDate(String registrationDate) { + this.registrationDate = registrationDate; + } + + + +} diff --git a/Answers/40230212010/src/Rent.java b/Answers/40230212010/src/Rent.java new file mode 100644 index 0000000..d6c8ec8 --- /dev/null +++ b/Answers/40230212010/src/Rent.java @@ -0,0 +1,47 @@ +import java.time.LocalDate; + +public class Rent { + private static int count=0; + private final String rentID ; + private LocalDate rentalDate; + private LocalDate dueDate; + + private Book book; + private User user; + public Rent(String rentalID,LocalDate rentalDate,LocalDate dueDate, Book book, User user) { + this.rentalDate = rentalDate; + this.book = book; + this.user = user; + this.rentID=String.valueOf(++count); + } + public static int getCount() { + return count; + } + public static void setCount(int count) { + Rent.count = count; + } + public String getRentID() { + return rentID; + } + public LocalDate getRentalDate() { + return rentalDate; + } + public void setRentalDate(LocalDate rentalDate) { + this.rentalDate = rentalDate; + } + public Book getBook() { + return book; + } + public void setBook(Book book) { + this.book = book; + } + public User getUser() { + return user; + } + public void setUser(NormalUser user) { + this.user = user; + } + + + +} diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java new file mode 100644 index 0000000..33fd420 --- /dev/null +++ b/Answers/40230212010/src/User.java @@ -0,0 +1,54 @@ +import java.util.*; + +public class User { + private static int count = 0; + public static String name; + private final String UniqueID; + private String phoneNumber; + + public static String password; + + public User(String name, String phoneNumber) { + this.UniqueID = String.valueOf( ++count); + this.phoneNumber = phoneNumber; + + } + + public String getUniqueID() { + return UniqueID; + } + + public static int getCount() { + return count; + } + + public static void setCount(int count) { + User.count = count; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public static String getPassword() { + return password; + } + + public static void setPassword(String password) { + User.password = password; + } + + +}