From 29f143cf37ac18707a3fb766476184ea20efb8cd Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 11:24:10 +0330 Subject: [PATCH 01/13] user classes created. --- Answers/40230212010/src/Admin.java | 15 +++++++++++++++ Answers/40230212010/src/NormalUser.java | 14 ++++++++++++++ Answers/40230212010/src/User.java | 14 ++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Answers/40230212010/src/Admin.java create mode 100644 Answers/40230212010/src/NormalUser.java create mode 100644 Answers/40230212010/src/User.java diff --git a/Answers/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java new file mode 100644 index 0000000..714b8c2 --- /dev/null +++ b/Answers/40230212010/src/Admin.java @@ -0,0 +1,15 @@ +public class Admin extends User{ + private String password; + public Admin(String name,int UniqueID,int phoneNumber,String password) { + super( name, UniqueID, phoneNumber); + this.password=password; + } + public String getPassword() { + return password; + } + public void setPassword(String password) { + this.password = password; + } + + +} diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java new file mode 100644 index 0000000..67b6377 --- /dev/null +++ b/Answers/40230212010/src/NormalUser.java @@ -0,0 +1,14 @@ +public class NormalUser extends User { + private String registrationDate; + public NormalUser(String name,int UniqueID,int phoneNumber,String registrationDate) { + super( name, UniqueID, phoneNumber); + this.registrationDate=registrationDate; + } + public String getRegistrationDate() { + return registrationDate; + } + public void setRegistrationDate(String registrationDate) { + this.registrationDate = registrationDate; + } + +} diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java new file mode 100644 index 0000000..9affd15 --- /dev/null +++ b/Answers/40230212010/src/User.java @@ -0,0 +1,14 @@ +public class User { + private String name; + private int UniqueID; + private int phoneNumber; + public User(String name,int UniqueID,int phoneNumber) { + this.name = name; + this.UniqueID = UniqueID; + this.phoneNumber = phoneNumber; + } + public String getName() { + return name; + } + +} From baac2aee2fdb978e361decb487f86ef1f7b60200 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 11:26:02 +0330 Subject: [PATCH 02/13] other classes created. --- Answers/40230212010/.vscode/settings.json | 7 +++++++ Answers/40230212010/README.md | 18 ++++++++++++++++++ Answers/40230212010/src/App.java | 5 +++++ Answers/40230212010/src/Book.java | 4 ++++ Answers/40230212010/src/CLI.java | 3 +++ Answers/40230212010/src/Libraray.java | 3 +++ Answers/40230212010/src/Rent.java | 3 +++ 7 files changed, 43 insertions(+) create mode 100644 Answers/40230212010/.vscode/settings.json create mode 100644 Answers/40230212010/README.md create mode 100644 Answers/40230212010/src/App.java create mode 100644 Answers/40230212010/src/Book.java create mode 100644 Answers/40230212010/src/CLI.java create mode 100644 Answers/40230212010/src/Libraray.java create mode 100644 Answers/40230212010/src/Rent.java 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/App.java b/Answers/40230212010/src/App.java new file mode 100644 index 0000000..7e88056 --- /dev/null +++ b/Answers/40230212010/src/App.java @@ -0,0 +1,5 @@ +public class App { + public static void main(String[] args) throws Exception { + + } +} diff --git a/Answers/40230212010/src/Book.java b/Answers/40230212010/src/Book.java new file mode 100644 index 0000000..5954d8a --- /dev/null +++ b/Answers/40230212010/src/Book.java @@ -0,0 +1,4 @@ +public class Book { + + +} diff --git a/Answers/40230212010/src/CLI.java b/Answers/40230212010/src/CLI.java new file mode 100644 index 0000000..f4ab1a4 --- /dev/null +++ b/Answers/40230212010/src/CLI.java @@ -0,0 +1,3 @@ +public class CLI { + +} diff --git a/Answers/40230212010/src/Libraray.java b/Answers/40230212010/src/Libraray.java new file mode 100644 index 0000000..d1d4c5d --- /dev/null +++ b/Answers/40230212010/src/Libraray.java @@ -0,0 +1,3 @@ +public class Libraray { + +} diff --git a/Answers/40230212010/src/Rent.java b/Answers/40230212010/src/Rent.java new file mode 100644 index 0000000..156c903 --- /dev/null +++ b/Answers/40230212010/src/Rent.java @@ -0,0 +1,3 @@ +public class Rent { + +} From eaaa3fcabef227eca9025e46d80757598d0ec2d8 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 11:41:39 +0330 Subject: [PATCH 03/13] book class created --- Answers/40230212010/src/Book.java | 15 +++++++++++++-- Answers/40230212010/src/User.java | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Answers/40230212010/src/Book.java b/Answers/40230212010/src/Book.java index 5954d8a..be8cace 100644 --- a/Answers/40230212010/src/Book.java +++ b/Answers/40230212010/src/Book.java @@ -1,4 +1,15 @@ public class Book { - - + private static int i=0; + private final int 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; + this.bookID=++i; + this.isAvailable=true; + } } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index 9affd15..5fd222a 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -1,6 +1,6 @@ public class User { private String name; - private int UniqueID; + private final int UniqueID; private int phoneNumber; public User(String name,int UniqueID,int phoneNumber) { this.name = name; From 4788358e33c60f5442d287830e88b3e19aa8d0f5 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 11:46:52 +0330 Subject: [PATCH 04/13] rent class --- Answers/40230212010/src/Rent.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Answers/40230212010/src/Rent.java b/Answers/40230212010/src/Rent.java index 156c903..88eec82 100644 --- a/Answers/40230212010/src/Rent.java +++ b/Answers/40230212010/src/Rent.java @@ -1,3 +1,15 @@ public class Rent { + private static int count=0; + private final String rentID ; + private String rentalDate; + private Book book; + private NormalUser user; + public Rent(String rentalDate, Book book, NormalUser user) { + this.rentalDate = rentalDate; + this.book = book; + this.user = user; + this.rentID=++i; + } + } From 920e90ef442f425ee54bb68533999a15bf750016 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 12:46:49 +0330 Subject: [PATCH 05/13] CLI Class --- Answers/40230212010/src/CLI.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Answers/40230212010/src/CLI.java b/Answers/40230212010/src/CLI.java index f4ab1a4..ff3c82b 100644 --- a/Answers/40230212010/src/CLI.java +++ b/Answers/40230212010/src/CLI.java @@ -1,3 +1,19 @@ + public class CLI { + Libraray libraray; + public CLI(Libraray libraray) { + this.libraray = libraray; + } + public void NormalCommand(){ + System.out.println("___________________________________________"); + System.out.println("1. lib add book \n2. lib get hrs\n3. lib rent \n4. lib get available books\n5. lib return \n6. lib rent "); + System.out.println("___________________________________________"); + } + public void AdminCommand(){ + System.out.println("___________________________________________"); + System.out.println("1. lib add member \n2. lib remove member \n3. lib add book "); + System.out.println("___________________________________________"); + } + } From 6423637aa9c327ce087375e06fdf6a483b0c2481 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 14:03:38 +0330 Subject: [PATCH 06/13] library class created --- Answers/40230212010/src/Libraray.java | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Answers/40230212010/src/Libraray.java b/Answers/40230212010/src/Libraray.java index d1d4c5d..681447e 100644 --- a/Answers/40230212010/src/Libraray.java +++ b/Answers/40230212010/src/Libraray.java @@ -1,3 +1,39 @@ +import java.util.*; public class Libraray { + private String name; + private int capacity; + private String operatingHours; + private Map books; + private List users; + private List rentals; + + public Libraray(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<>(); + } + public void addBook( String title, String description, String author) { + Book book = new Book(title, description, author); + books.put(book.getBookID(), book); + } + public void removebook(String bookID){ + 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; + } + } From f66a783fb6c1caed8479516ae1601efdd22f5576 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 14:55:42 +0330 Subject: [PATCH 07/13] remove user added --- Answers/40230212010/src/Admin.java | 1 + Answers/40230212010/src/Book.java | 16 ++++++++++++-- Answers/40230212010/src/CLI.java | 4 ++-- Answers/40230212010/src/NormalUser.java | 4 ++++ Answers/40230212010/src/Rent.java | 4 ++-- Answers/40230212010/src/User.java | 28 ++++++++++++++++++++++--- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Answers/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java index 714b8c2..968eafe 100644 --- a/Answers/40230212010/src/Admin.java +++ b/Answers/40230212010/src/Admin.java @@ -11,5 +11,6 @@ public void setPassword(String password) { this.password = password; } + } diff --git a/Answers/40230212010/src/Book.java b/Answers/40230212010/src/Book.java index be8cace..9cbc1c4 100644 --- a/Answers/40230212010/src/Book.java +++ b/Answers/40230212010/src/Book.java @@ -1,6 +1,7 @@ + public class Book { private static int i=0; - private final int bookID; + private final String bookID; private String title; private String description; private String author; @@ -9,7 +10,18 @@ public Book ( String title, String description, String author){ this.author=author; this.title=title; this.description=description; - this.bookID=++i; + i++; + this.bookID=String.valueOf(i); this.isAvailable=true; } + + public String getBookID() { + return bookID; + } + public boolean isAvailable() { + return isAvailable; + } + + + } diff --git a/Answers/40230212010/src/CLI.java b/Answers/40230212010/src/CLI.java index ff3c82b..2b372e9 100644 --- a/Answers/40230212010/src/CLI.java +++ b/Answers/40230212010/src/CLI.java @@ -7,7 +7,7 @@ public CLI(Libraray libraray) { } public void NormalCommand(){ System.out.println("___________________________________________"); - System.out.println("1. lib add book \n2. lib get hrs\n3. lib rent \n4. lib get available books\n5. lib return \n6. lib rent "); + System.out.println("1. lib add book \n2. lib get hrs\n3. lib rent \n4. lib get available books\n5. lib return \n6. lib rent \n7. Exit"); System.out.println("___________________________________________"); } public void AdminCommand(){ @@ -15,5 +15,5 @@ public void AdminCommand(){ System.out.println("1. lib add member \n2. lib remove member \n3. lib add book "); System.out.println("___________________________________________"); } - + } diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java index 67b6377..0125582 100644 --- a/Answers/40230212010/src/NormalUser.java +++ b/Answers/40230212010/src/NormalUser.java @@ -1,9 +1,13 @@ public class NormalUser extends User { + private String registrationDate; + public NormalUser(String name,int UniqueID,int phoneNumber,String registrationDate) { super( name, UniqueID, phoneNumber); this.registrationDate=registrationDate; } + + public String getRegistrationDate() { return registrationDate; } diff --git a/Answers/40230212010/src/Rent.java b/Answers/40230212010/src/Rent.java index 88eec82..c56f87e 100644 --- a/Answers/40230212010/src/Rent.java +++ b/Answers/40230212010/src/Rent.java @@ -1,6 +1,6 @@ public class Rent { private static int count=0; - private final String rentID ; + private final int rentID ; private String rentalDate; private Book book; private NormalUser user; @@ -8,7 +8,7 @@ public Rent(String rentalDate, Book book, NormalUser user) { this.rentalDate = rentalDate; this.book = book; this.user = user; - this.rentID=++i; + this.rentID=++count; } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index 5fd222a..ec354ce 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -1,14 +1,36 @@ +import java.util.*; + public class User { + private static int count = 0; private String name; private final int UniqueID; private int phoneNumber; + private List adminsList; + private List normalUsersList; + + public User(String name,int UniqueID,int phoneNumber) { this.name = name; - this.UniqueID = UniqueID; + this.UniqueID = ++count; this.phoneNumber = phoneNumber; + List normalUsersList = new ArrayList(); + List admins = new ArrayList(); } - public String getName() { - return name; + + + private void removeUser(int userID){ + for(NormalUser user : normalUsersList ){ + if (UniqueID==userID){ + normalUsersList.remove(UniqueID); + } + + for(Admin admin : adminsList ){ + if (UniqueID==userID){ + adminsList.remove(UniqueID); + } + } + } } + } From 3efd0b4f79b2f17fe4cc1757fbab8f8c63504e54 Mon Sep 17 00:00:00 2001 From: mhmd Date: Wed, 15 May 2024 15:13:12 +0330 Subject: [PATCH 08/13] add user created --- Answers/40230212010/src/Admin.java | 4 ++-- Answers/40230212010/src/NormalUser.java | 6 +++--- Answers/40230212010/src/User.java | 11 +++++++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Answers/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java index 968eafe..c93aff4 100644 --- a/Answers/40230212010/src/Admin.java +++ b/Answers/40230212010/src/Admin.java @@ -1,7 +1,7 @@ public class Admin extends User{ private String password; - public Admin(String name,int UniqueID,int phoneNumber,String password) { - super( name, UniqueID, phoneNumber); + public Admin(String name,int phoneNumber,String password) { + super( name, phoneNumber); this.password=password; } public String getPassword() { diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java index 0125582..704483f 100644 --- a/Answers/40230212010/src/NormalUser.java +++ b/Answers/40230212010/src/NormalUser.java @@ -1,9 +1,9 @@ public class NormalUser extends User { - + private String registrationDate; - public NormalUser(String name,int UniqueID,int phoneNumber,String registrationDate) { - super( name, UniqueID, phoneNumber); + public NormalUser(String name,int phoneNumber,String registrationDate) { + super( name, phoneNumber); this.registrationDate=registrationDate; } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index ec354ce..71f8810 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -9,7 +9,7 @@ public class User { private List normalUsersList; - public User(String name,int UniqueID,int phoneNumber) { + public User(String name,int phoneNumber) { this.name = name; this.UniqueID = ++count; this.phoneNumber = phoneNumber; @@ -31,6 +31,13 @@ private void removeUser(int userID){ } } } - + public void addAdmin(String name, int phoneNumber, String password){ + Admin newAdmin = new Admin(name, phoneNumber, password); + adminsList.add(newAdmin); + } + public void addNormalUser(String name,int phoneNumber,String registrationDate){ + NormalUser newNormalUser = new NormalUser(name, phoneNumber, registrationDate); + normalUsersList.add(newNormalUser); + } } From bba831a145d536df1e66fdd249a8590a67b10ba6 Mon Sep 17 00:00:00 2001 From: mhmd Date: Thu, 16 May 2024 12:18:21 +0330 Subject: [PATCH 09/13] get user created --- Answers/40230212010/src/Libraray.java | 2 ++ Answers/40230212010/src/NormalUser.java | 1 + Answers/40230212010/src/User.java | 26 ++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Answers/40230212010/src/Libraray.java b/Answers/40230212010/src/Libraray.java index 681447e..4275065 100644 --- a/Answers/40230212010/src/Libraray.java +++ b/Answers/40230212010/src/Libraray.java @@ -16,6 +16,7 @@ public Libraray(String name, int capacity, String operatingHours) { this.rentals = new ArrayList<>(); } public void addBook( String title, String description, String author) { + Book book = new Book(title, description, author); books.put(book.getBookID(), book); } @@ -35,5 +36,6 @@ public ArrayList getAvailableBooks(){ return Availablebooks; } + } diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java index 704483f..84d34ab 100644 --- a/Answers/40230212010/src/NormalUser.java +++ b/Answers/40230212010/src/NormalUser.java @@ -14,5 +14,6 @@ public String getRegistrationDate() { public void setRegistrationDate(String registrationDate) { this.registrationDate = registrationDate; } + } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index 71f8810..b1bf338 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -39,5 +39,29 @@ public void addNormalUser(String name,int phoneNumber,String registrationDate){ NormalUser newNormalUser = new NormalUser(name, phoneNumber, registrationDate); normalUsersList.add(newNormalUser); } - + public NormalUser getNormalUser(int UniqueID){ + for(NormalUser user : normalUsersList){ + if (user.getUniqueID()==UniqueID){ + return user; + } + } + return null; + } + public Admin geAdmin(String password){ + for(Admin admin : adminsList){ + if (admin.getPassword().equals(password)){ + return admin; + } + } + return null; + } + + + + public int getUniqueID() { + return UniqueID; + } + + + } From cafa35ffc88e8a1282b8335ce6b7d7f276c65cb6 Mon Sep 17 00:00:00 2001 From: mhmd Date: Thu, 16 May 2024 16:49:14 +0330 Subject: [PATCH 10/13] Update --- Answers/40230212010/src/App.java | 100 +++++++++++++++++- Answers/40230212010/src/Book.java | 36 +++++++ Answers/40230212010/src/CLI.java | 4 +- Answers/40230212010/src/Libraray.java | 41 -------- Answers/40230212010/src/Library.java | 139 ++++++++++++++++++++++++++ Answers/40230212010/src/Rent.java | 40 +++++++- Answers/40230212010/src/User.java | 8 +- 7 files changed, 316 insertions(+), 52 deletions(-) delete mode 100644 Answers/40230212010/src/Libraray.java create mode 100644 Answers/40230212010/src/Library.java diff --git a/Answers/40230212010/src/App.java b/Answers/40230212010/src/App.java index 7e88056..413388c 100644 --- a/Answers/40230212010/src/App.java +++ b/Answers/40230212010/src/App.java @@ -1,5 +1,103 @@ +import java.util.List; +import java.util.Scanner; + 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"); + User user = new User("mmd","902"); + + System.out.println("Welcome to " + library.getName() + "!"); + System.out.println("Current capacity: " + library.getCapacity()); + System.out.println("Operating hours: " + library.getOperatingHours()); + + while (true) { + 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. Exit"); + + int choice = scanner.nextInt(); + scanner.nextLine(); // Consume newline + + 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: "); + int UniqueID = scanner.nextInt(); + NormalUser users = user.getNormalUser(UniqueID); + if (user == null) { + System.out.println("User not found."); + } else { + library.rentBook(bookName, user); + } + break; + case 4: + System.out.print("Enter student ID: "); + UniqueID = scanner.nextInt(); + System.out.print("Enter password: "); + String password = scanner.nextLine(); + Admin admin = user.getAdmin(password); + if (admin == null) { + System.out.println("Invalid password."); + } else { + 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."); + } + 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: "); + bookName = scanner.nextLine(); + library.returnBook(bookName); + break; + case 7: + List 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..."); + scanner.close(); + System.exit(0); + default: + System.out.println("Invalid choice."); + } + } + } } diff --git a/Answers/40230212010/src/Book.java b/Answers/40230212010/src/Book.java index 9cbc1c4..b9a17d8 100644 --- a/Answers/40230212010/src/Book.java +++ b/Answers/40230212010/src/Book.java @@ -21,6 +21,42 @@ public String getBookID() { 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/CLI.java b/Answers/40230212010/src/CLI.java index 2b372e9..e1fb07a 100644 --- a/Answers/40230212010/src/CLI.java +++ b/Answers/40230212010/src/CLI.java @@ -1,8 +1,8 @@ public class CLI { - Libraray libraray; + Library libraray; - public CLI(Libraray libraray) { + public CLI(Library libraray) { this.libraray = libraray; } public void NormalCommand(){ diff --git a/Answers/40230212010/src/Libraray.java b/Answers/40230212010/src/Libraray.java deleted file mode 100644 index 4275065..0000000 --- a/Answers/40230212010/src/Libraray.java +++ /dev/null @@ -1,41 +0,0 @@ -import java.util.*; -public class Libraray { - private String name; - private int capacity; - private String operatingHours; - private Map books; - private List users; - private List rentals; - - public Libraray(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<>(); - } - public void addBook( String title, String description, String author) { - - Book book = new Book(title, description, author); - books.put(book.getBookID(), book); - } - public void removebook(String bookID){ - 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; - } - - - -} diff --git a/Answers/40230212010/src/Library.java b/Answers/40230212010/src/Library.java new file mode 100644 index 0000000..57e53ee --- /dev/null +++ b/Answers/40230212010/src/Library.java @@ -0,0 +1,139 @@ +import java.time.LocalDate; +import java.util.*; + +public class Library { + private String name; + private int capacity; + private String operatingHours; + private Map books; + private List users; + private List rentals; + + 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<>(); + } + + 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, NormalUser user) { + + Book book = this.books.get(bookName); + + // Check if book exists + if (book == null) { + System.out.print("book not found: " + bookName); + } + + // Check if book is available + if (!book.isAvailable()) { + System.out.println("Book is currently unavailable: " + bookName); + } + + // Generate a unique rental ID (consider using UUID for guaranteed uniqueness) + String rentalID = UUID.randomUUID().toString().substring(0, 8); + + // Set due date to 7 days from rental date + LocalDate rentalDate = LocalDate.now(); + LocalDate dueDate = rentalDate.plusDays(7); + + // Create and add a new Rent object + Rent newRent = new Rent(rentalID, rentalDate, dueDate, book, user); + rentals.add(newRent); + + // Mark book as unavailable + book.setAvailable(false); + + System.out.println("Book rented successfully. Your due date is: " + dueDate); + + // Update book availability counts (consider using an AtomicInteger for thread + // safety if needed) + // atomicIntegerBooksRented.getAndIncrement(); + + // Handle other potential exceptions as needed (e.g., BookNotAvailableException, + // DuplicateRentalException) + + } + + 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/Rent.java b/Answers/40230212010/src/Rent.java index c56f87e..afbf029 100644 --- a/Answers/40230212010/src/Rent.java +++ b/Answers/40230212010/src/Rent.java @@ -1,15 +1,47 @@ +import java.time.LocalDate; + public class Rent { private static int count=0; - private final int rentID ; - private String rentalDate; + private final String rentID ; + private LocalDate rentalDate; + private LocalDate dueDate; + private Book book; private NormalUser user; - public Rent(String rentalDate, Book book, NormalUser user) { + public Rent(String rentalID,LocalDate rentalDate,LocalDate dueDate, Book book, NormalUser 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 NormalUser getUser() { + return user; + } + public void setUser(NormalUser user) { this.user = user; - this.rentID=++count; } + } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index b1bf338..69fd32a 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -4,12 +4,12 @@ public class User { private static int count = 0; private String name; private final int UniqueID; - private int phoneNumber; + private String phoneNumber; private List adminsList; private List normalUsersList; - public User(String name,int phoneNumber) { + public User(String name,String phoneNumber) { this.name = name; this.UniqueID = ++count; this.phoneNumber = phoneNumber; @@ -31,11 +31,11 @@ private void removeUser(int userID){ } } } - public void addAdmin(String name, int phoneNumber, String password){ + public void addAdmin(String name, String phoneNumber, String password){ Admin newAdmin = new Admin(name, phoneNumber, password); adminsList.add(newAdmin); } - public void addNormalUser(String name,int phoneNumber,String registrationDate){ + public void addNormalUser(String name,String phoneNumber,String registrationDate){ NormalUser newNormalUser = new NormalUser(name, phoneNumber, registrationDate); normalUsersList.add(newNormalUser); } From 452babdcd2f952bf9d21e4caf6a352827635c6dd Mon Sep 17 00:00:00 2001 From: mhmd Date: Thu, 16 May 2024 20:09:39 +0330 Subject: [PATCH 11/13] almost finish --- Answers/40230212010/src/Admin.java | 2 +- Answers/40230212010/src/App.java | 21 +++---- Answers/40230212010/src/Library.java | 79 +++++++++++++++++++++---- Answers/40230212010/src/NormalUser.java | 2 +- Answers/40230212010/src/Rent.java | 8 +-- Answers/40230212010/src/User.java | 72 ++++++++-------------- 6 files changed, 109 insertions(+), 75 deletions(-) diff --git a/Answers/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java index c93aff4..1ea4e33 100644 --- a/Answers/40230212010/src/Admin.java +++ b/Answers/40230212010/src/Admin.java @@ -1,6 +1,6 @@ public class Admin extends User{ private String password; - public Admin(String name,int phoneNumber,String password) { + public Admin(String name,String phoneNumber,String password) { super( name, phoneNumber); this.password=password; } diff --git a/Answers/40230212010/src/App.java b/Answers/40230212010/src/App.java index 413388c..a1039d0 100644 --- a/Answers/40230212010/src/App.java +++ b/Answers/40230212010/src/App.java @@ -1,5 +1,5 @@ -import java.util.List; -import java.util.Scanner; +import java.lang.reflect.Array; +import java.util.*; public class App { public static void main(String[] args) throws Exception { @@ -43,20 +43,20 @@ public static void main(String[] args) throws Exception { System.out.print("Enter book name: "); String bookName = scanner.nextLine(); System.out.print("Enter student ID: "); - int UniqueID = scanner.nextInt(); - NormalUser users = user.getNormalUser(UniqueID); + String UniqueID = scanner.nextLine(); + NormalUser users = library.getNormalUser(UniqueID); if (user == null) { System.out.println("User not found."); } else { - library.rentBook(bookName, user); + library.rentBook(bookName ,user); } break; case 4: System.out.print("Enter student ID: "); - UniqueID = scanner.nextInt(); + UniqueID = scanner.nextLine(); System.out.print("Enter password: "); String password = scanner.nextLine(); - Admin admin = user.getAdmin(password); + Admin admin = library.getAdmin(password); if (admin == null) { System.out.println("Invalid password."); } else { @@ -76,11 +76,12 @@ public static void main(String[] args) throws Exception { break; case 6: System.out.print("Enter book name: "); - bookName = scanner.nextLine(); - library.returnBook(bookName); + String bookToReturnName = scanner.nextLine(); + Book booktoReturn= library.findBookByName(bookToReturnName); + library.returnBook(user, booktoReturn); break; case 7: - List availableBooks = library.getAvailableBooks(); + ArrayList availableBooks = library.getAvailableBooks(); if (availableBooks.isEmpty()) { System.out.println("No books available."); } else { diff --git a/Answers/40230212010/src/Library.java b/Answers/40230212010/src/Library.java index 57e53ee..73173b1 100644 --- a/Answers/40230212010/src/Library.java +++ b/Answers/40230212010/src/Library.java @@ -1,13 +1,17 @@ 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; @@ -16,6 +20,8 @@ public Library(String name, int capacity, String 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) { @@ -49,45 +55,94 @@ public ArrayList getAvailableBooks() { return Availablebooks; } - public void rentBook(String bookName, NormalUser user) { + public void rentBook(String bookName, User user) { Book book = this.books.get(bookName); - // Check if book exists if (book == null) { System.out.print("book not found: " + bookName); } - // Check if book is available if (!book.isAvailable()) { System.out.println("Book is currently unavailable: " + bookName); } - // Generate a unique rental ID (consider using UUID for guaranteed uniqueness) String rentalID = UUID.randomUUID().toString().substring(0, 8); - // Set due date to 7 days from rental date LocalDate rentalDate = LocalDate.now(); LocalDate dueDate = rentalDate.plusDays(7); - // Create and add a new Rent object Rent newRent = new Rent(rentalID, rentalDate, dueDate, book, user); rentals.add(newRent); - // Mark book as unavailable book.setAvailable(false); System.out.println("Book rented successfully. Your due date is: " + dueDate); - // Update book availability counts (consider using an AtomicInteger for thread - // safety if needed) - // atomicIntegerBooksRented.getAndIncrement(); + } + + 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()); + } + } + } + } + + - // Handle other potential exceptions as needed (e.g., BookNotAvailableException, - // DuplicateRentalException) + 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; } diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java index 84d34ab..ca8a8ea 100644 --- a/Answers/40230212010/src/NormalUser.java +++ b/Answers/40230212010/src/NormalUser.java @@ -2,7 +2,7 @@ public class NormalUser extends User { private String registrationDate; - public NormalUser(String name,int phoneNumber,String registrationDate) { + public NormalUser(String name,String phoneNumber,String registrationDate) { super( name, phoneNumber); this.registrationDate=registrationDate; } diff --git a/Answers/40230212010/src/Rent.java b/Answers/40230212010/src/Rent.java index afbf029..d6c8ec8 100644 --- a/Answers/40230212010/src/Rent.java +++ b/Answers/40230212010/src/Rent.java @@ -4,11 +4,11 @@ public class Rent { private static int count=0; private final String rentID ; private LocalDate rentalDate; - private LocalDate dueDate; + private LocalDate dueDate; private Book book; - private NormalUser user; - public Rent(String rentalID,LocalDate rentalDate,LocalDate dueDate, Book book, NormalUser user) { + private User user; + public Rent(String rentalID,LocalDate rentalDate,LocalDate dueDate, Book book, User user) { this.rentalDate = rentalDate; this.book = book; this.user = user; @@ -35,7 +35,7 @@ public Book getBook() { public void setBook(Book book) { this.book = book; } - public NormalUser getUser() { + public User getUser() { return user; } public void setUser(NormalUser user) { diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index 69fd32a..1690d34 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -1,67 +1,45 @@ import java.util.*; -public class User { +public class User { private static int count = 0; private String name; - private final int UniqueID; + private final String UniqueID; private String phoneNumber; - private List adminsList; - private List normalUsersList; - - public User(String name,String phoneNumber) { + public User(String name, String phoneNumber) { this.name = name; - this.UniqueID = ++count; + this.UniqueID = String.valueOf( ++count); this.phoneNumber = phoneNumber; - List normalUsersList = new ArrayList(); - List admins = new ArrayList(); - } - - private void removeUser(int userID){ - for(NormalUser user : normalUsersList ){ - if (UniqueID==userID){ - normalUsersList.remove(UniqueID); - } - - for(Admin admin : adminsList ){ - if (UniqueID==userID){ - adminsList.remove(UniqueID); - } - } - } } - public void addAdmin(String name, String phoneNumber, String password){ - Admin newAdmin = new Admin(name, phoneNumber, password); - adminsList.add(newAdmin); + + public String getUniqueID() { + return UniqueID; } - public void addNormalUser(String name,String phoneNumber,String registrationDate){ - NormalUser newNormalUser = new NormalUser(name, phoneNumber, registrationDate); - normalUsersList.add(newNormalUser); + + public static int getCount() { + return count; } - public NormalUser getNormalUser(int UniqueID){ - for(NormalUser user : normalUsersList){ - if (user.getUniqueID()==UniqueID){ - return user; - } - } - return null; + + public static void setCount(int count) { + User.count = count; } - public Admin geAdmin(String password){ - for(Admin admin : adminsList){ - if (admin.getPassword().equals(password)){ - return admin; - } - } - return null; + + public String getName() { + return name; } + public void setName(String name) { + this.name = name; + } - - public int getUniqueID() { - return UniqueID; + public String getPhoneNumber() { + return phoneNumber; } + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + - } From 15a3d6dd5d0ebf4ad1943ef3149e4645532857da Mon Sep 17 00:00:00 2001 From: mhmd Date: Thu, 16 May 2024 23:14:04 +0330 Subject: [PATCH 12/13] update0.2 --- Answers/40230212010/src/Admin.java | 10 +-- Answers/40230212010/src/App.java | 89 ++++++++++++++++++++----- Answers/40230212010/src/CLI.java | 19 ------ Answers/40230212010/src/NormalUser.java | 1 + Answers/40230212010/src/User.java | 13 +++- 5 files changed, 86 insertions(+), 46 deletions(-) delete mode 100644 Answers/40230212010/src/CLI.java diff --git a/Answers/40230212010/src/Admin.java b/Answers/40230212010/src/Admin.java index 1ea4e33..56edd73 100644 --- a/Answers/40230212010/src/Admin.java +++ b/Answers/40230212010/src/Admin.java @@ -1,14 +1,8 @@ public class Admin extends User{ - private String password; + public static final String password="123"; public Admin(String name,String phoneNumber,String password) { super( name, phoneNumber); - this.password=password; - } - public String getPassword() { - return password; - } - public void setPassword(String password) { - this.password = password; + } diff --git a/Answers/40230212010/src/App.java b/Answers/40230212010/src/App.java index a1039d0..786de40 100644 --- a/Answers/40230212010/src/App.java +++ b/Answers/40230212010/src/App.java @@ -3,15 +3,66 @@ public class App { public static void main(String[] args) throws Exception { - Scanner scanner = new Scanner(System.in); + Scanner scanner = new Scanner(System.in); Library library = new Library("Main Library", 1000, "9am to 5pm"); - User user = new User("mmd","902"); 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; + 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..."); + System.exit(0); + break; + default: + System.out.println("Invalid choice."); + break; + } + + } while (true) { + System.out.println("\nChoose an option:"); System.out.println("1. Add book"); System.out.println("2. Get hours"); @@ -23,7 +74,7 @@ public static void main(String[] args) throws Exception { System.out.println("8. Exit"); int choice = scanner.nextInt(); - scanner.nextLine(); // Consume newline + scanner.nextLine(); switch (choice) { case 1: @@ -42,30 +93,33 @@ public static void main(String[] args) throws Exception { case 3: System.out.print("Enter book name: "); String bookName = scanner.nextLine(); + System.out.print("Enter student ID: "); String UniqueID = scanner.nextLine(); - NormalUser users = library.getNormalUser(UniqueID); - if (user == null) { - System.out.println("User not found."); - } else { - library.rentBook(bookName ,user); - } + 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 student ID: "); + System.out.print("Enter Admin Name: "); UniqueID = scanner.nextLine(); System.out.print("Enter password: "); - String password = scanner.nextLine(); - Admin admin = library.getAdmin(password); - if (admin == null) { - System.out.println("Invalid password."); - } else { + 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: @@ -77,7 +131,7 @@ public static void main(String[] args) throws Exception { case 6: System.out.print("Enter book name: "); String bookToReturnName = scanner.nextLine(); - Book booktoReturn= library.findBookByName(bookToReturnName); + Book booktoReturn = library.findBookByName(bookToReturnName); library.returnBook(user, booktoReturn); break; case 7: @@ -98,7 +152,8 @@ public static void main(String[] args) throws Exception { default: System.out.println("Invalid choice."); } + } - + } } diff --git a/Answers/40230212010/src/CLI.java b/Answers/40230212010/src/CLI.java deleted file mode 100644 index e1fb07a..0000000 --- a/Answers/40230212010/src/CLI.java +++ /dev/null @@ -1,19 +0,0 @@ - -public class CLI { - Library libraray; - - public CLI(Library libraray) { - this.libraray = libraray; - } - public void NormalCommand(){ - System.out.println("___________________________________________"); - System.out.println("1. lib add book \n2. lib get hrs\n3. lib rent \n4. lib get available books\n5. lib return \n6. lib rent \n7. Exit"); - System.out.println("___________________________________________"); - } - public void AdminCommand(){ - System.out.println("___________________________________________"); - System.out.println("1. lib add member \n2. lib remove member \n3. lib add book "); - System.out.println("___________________________________________"); - } - -} diff --git a/Answers/40230212010/src/NormalUser.java b/Answers/40230212010/src/NormalUser.java index ca8a8ea..c69001a 100644 --- a/Answers/40230212010/src/NormalUser.java +++ b/Answers/40230212010/src/NormalUser.java @@ -14,6 +14,7 @@ public String getRegistrationDate() { public void setRegistrationDate(String registrationDate) { this.registrationDate = registrationDate; } + } diff --git a/Answers/40230212010/src/User.java b/Answers/40230212010/src/User.java index 1690d34..33fd420 100644 --- a/Answers/40230212010/src/User.java +++ b/Answers/40230212010/src/User.java @@ -2,12 +2,13 @@ public class User { private static int count = 0; - private String name; + public static String name; private final String UniqueID; private String phoneNumber; + + public static String password; public User(String name, String phoneNumber) { - this.name = name; this.UniqueID = String.valueOf( ++count); this.phoneNumber = phoneNumber; @@ -40,6 +41,14 @@ public String getPhoneNumber() { public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } + + public static String getPassword() { + return password; + } + + public static void setPassword(String password) { + User.password = password; + } } From 00e9c92e37f27c1a3af6c994774d598807060a37 Mon Sep 17 00:00:00 2001 From: mhmd Date: Thu, 16 May 2024 23:49:41 +0330 Subject: [PATCH 13/13] finish --- Answers/40230212010/src/App.java | 272 ++++++++++++++++--------------- 1 file changed, 141 insertions(+), 131 deletions(-) diff --git a/Answers/40230212010/src/App.java b/Answers/40230212010/src/App.java index 786de40..3ecbb62 100644 --- a/Answers/40230212010/src/App.java +++ b/Answers/40230212010/src/App.java @@ -11,147 +11,157 @@ public static void main(String[] args) throws Exception { System.out.println("Operating hours: " + library.getOperatingHours()); User user = new User(null, null); boolean exit = false; - 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)) { + 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 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..."); - System.exit(0); - break; - default: - System.out.println("Invalid choice."); - break; + 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) { - while (true) { + 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."); - 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. Exit"); + int choice = scanner.nextInt(); + scanner.nextLine(); - 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(); - 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()); + 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 8: - System.out.println("Exiting..."); - scanner.close(); - System.exit(0); - default: - System.out.println("Invalid choice."); + 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; + }