From 86ce20c1ac32e5d4a66e4a098106e8032d5e12dd Mon Sep 17 00:00:00 2001 From: Kiana Ghamsari Date: Wed, 8 Mar 2023 23:44:04 +0330 Subject: [PATCH 1/3] first commit --- src/main/java/Book.java | 62 ++++++++ src/main/java/Librarian.java | 27 +++- src/main/java/Library.java | 265 ++++++++++++++++++++++++++++++----- src/main/java/User.java | 87 +++++++++++- 4 files changed, 395 insertions(+), 46 deletions(-) diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 7cff6f8..f12696c 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,3 +1,65 @@ public class Book { //Book should contain name,author,year of publish and ISBN + private String name; + private String author; + private int yearOfPublish; + private String ISBN; + + private int amountOfBook = 0; + + public int getAmountOfBook() { + return amountOfBook; + } + + public void setAmountOfBook(int amountOfBook) { + this.amountOfBook = amountOfBook; + } + + public Book(String name, String author, int yearOfPublish, String ISBN) { + this.name = name; + this.author = author; + this.yearOfPublish = yearOfPublish; + this.ISBN = ISBN; + + this.amountOfBook++; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public int getYearOfPublish() { + return yearOfPublish; + } + + public void setYearOfPublish(int yearOfPublish) { + this.yearOfPublish = yearOfPublish; + } + + public String getISBN() { + return ISBN; + } + + public void setISBN(String ISBN) { + this.ISBN = ISBN; + } + + @Override + public String toString() { + return "Here is the information of " + this.getName() + "book:\nThe Book's Name: " + getName() + "\nThe Author's Name: " + getAuthor() + "\nThe Book's Year of Publish: " + getYearOfPublish() + "\nThe Book's ISBN: " + getISBN(); + } + + } diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 321ce3a..7f4e248 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,10 +1,23 @@ -public class Librarian { - /* - * The librarian should have a username and a password - * The librarian should be able to search users, librarians and books - * The librarian should be able to add\remove\update user add\remove\update_ - _ librarian and add\remove\update book - */ +public class Librarian extends User { + // /* + // * The librarian should have a username and a password + // * The librarian should be able to search users, librarians and books + // * The librarian should be able to add\remove\//update user add\remove\//update_ + // _ librarian and add\remove\//upd//ate book + // */ + + public Librarian() { + super(); + } + + public Librarian(String username, String password) { + super(username, password); + } + + // @Override + // public String toString() { + // super.toString(); + // } } diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6c34354..0174555 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,3 +1,7 @@ +import java.util.ArrayList; +import java.util.HashMap; +import javax.swing.*; + public class Library { /* * The library should have a list of books. @@ -6,79 +10,270 @@ public class Library { * The library should have a list of users and a list of librarians. */ + private ArrayList books; + private ArrayList users; + private ArrayList librarians; + private HashMap amountOfTheBook; + + + public Library() { + this.books = new ArrayList(); + this.librarians = new ArrayList(); + this.users = new ArrayList(); + this.amountOfTheBook = new HashMap(); + + } + + public Library(Book books, Librarian librarians, User users) { + this.books = new ArrayList(); + this.librarians = new ArrayList(); + this.users = new ArrayList(); + this.amountOfTheBook = new HashMap(); + } + + + public ArrayList getBooks() { + return books; + } + + public void setBooks(ArrayList books) { + this.books = books; + } + + public ArrayList getUsers() { + return users; + } + + public void setUsers(ArrayList users) { + this.users = users; + } + + public ArrayList getLibrarians() { + return librarians; + } + + public void setLibrarians(ArrayList librarians) { + this.librarians = librarians; + } + + public HashMap getAmountOfTheBook() { + return amountOfTheBook; + } + + public void setAmountOfTheBook(HashMap amountOfTheBook) { + this.amountOfTheBook = amountOfTheBook; + } + + + + + + //book related functions - public void addBook(){ - //TODO + public void addBook(Book book){ + //Book addedBook = new Book(name, author, yearOfPublish, ISBN); + // books.add(book); + // increaseBook(book.getISBN()); + if (doesBookExist(book.getISBN())) { + increaseBook(book.getISBN()); + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " added successfully!"); + + } else { + books.add(book); + increaseBook(book.getISBN()); + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " added successfully!"); + } + } - public void removeBook(){ - //TODO + public void removeBook(Book book){ + if (doesBookExist(book.getISBN())) { + books.remove(book); + decreaseBook(book.getISBN()); + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " has been removed."); + + } else { + JOptionPane.showMessageDialog(null,"The book doesn't exist."); + } } - public void searchBook(){ - //TODO + public Book searchBook( String ISBN ){ + if (doesBookExist(ISBN)) { + for (Book book : books) { + if (book.getISBN().equals(ISBN)) { + //System.out.println(book); + JOptionPane.showMessageDialog(null,book); + return book; + } + } + } + JOptionPane.showMessageDialog(null,"The book doesn't exist."); + return null; + } - public void updateBook(){ - //TODO + public void updateBook(String ISBN, String newName, String newAuthor, int newYearOfPublish){ + if ( doesBookExist(ISBN)){ + for (Book book : books) { + if (book.getISBN().equals(ISBN)) { + book.setName(newName); + book.setAuthor(newAuthor); + book.setYearOfPublish(newYearOfPublish); + } + } + JOptionPane.showMessageDialog(null,"Updated successfully!"); + } else { + JOptionPane.showMessageDialog(null,"The book doesn't exist."); + } } - public void doesBookExist(){ - //TODO + public boolean doesBookExist( String ISBN ){ + for (Book book : books) { + if (book.getISBN().equals(ISBN)) + return true; + + } + return false; + } - public void increaseBook(){ - //TODO + public void increaseBook(String ISBN){ + if (doesBookExist(ISBN)) { + amountOfTheBook.replace(ISBN, amountOfTheBook.get(ISBN) + 1); + } else { + amountOfTheBook.put(ISBN, 1); + + } + } - public void decreaseBook(){ - //TODO + public void decreaseBook(String ISBN){ + if (doesBookExist(ISBN)) { + amountOfTheBook.replace(ISBN, amountOfTheBook.get(ISBN) - 1); + + } else { + JOptionPane.showMessageDialog(null,"The book doesn't exist."); + } } //user related functions - public void addUser(){ - //TODO + + public void addUser(User user){ + if (doesUserExist(user.getUsername())) { + JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " already exists."); + + } else { + users.add(user); + JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " added successfully!"); + } + } - public void removeUser(){ - //TODO + public void removeUser(User user){ + if (doesUserExist(user.getUsername())) { + users.remove(user); + JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " has been removed."); + + } else { + JOptionPane.showMessageDialog(null,"The username doesn't exist."); + } } - public void searchUser(){ - //TODO + public User searchUser(String username){ + if (doesUserExist(username)) { + for (User user : users) { + if (user.getUsername().equals(username)) { + //System.out.println(user); + JOptionPane.showMessageDialog(null,user); + return user; + } + } + } + JOptionPane.showMessageDialog(null,"The username doesn't exist."); + return null; } - public void updateUser(){ - //TODO + public void updateUser(String username, String newUsername, String newPassword){ + if ( doesUserExist(username) ) { + for (User user : users) { + if (user.getUsername().equals(username)) { + user.setUsername(newUsername); + user.setPassword(newPassword); + } + } + JOptionPane.showMessageDialog(null,"Updated successfully!"); + } else { + JOptionPane.showMessageDialog(null,"The username doesn't exist."); + + } } - public void doesUserExist(){ - //TODO + public boolean doesUserExist(String username) { + for (User user : users) { + if (user.getUsername().equals(username)) + return true; + } + return false; } //librarian related functions - public void addLibrarian(){ - //TODO - } + public void addLibrarian(Librarian librarian){ + if (doesLibrarianExist(librarian.getUsername())) { + JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " already exists."); - public void removeLibrarian(){ - //TODO + } else { + librarians.add(librarian); + JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " added successfully!"); + } } - public void searchLibrarian(){ - //TODO + public void removeLibrarian(Librarian librarian){ + if (doesLibrarianExist(librarian.getUsername())) { + librarians.remove(librarian); + JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " has been removed."); + + } else { + JOptionPane.showMessageDialog(null,"The librarian doesn't exist."); + } } - public void updateLibrarian(){ - //TODO + public Librarian searchLibrarian(String username){ + if (doesLibrarianExist(username)) { + for (Librarian librarian : librarians) { + if (librarian.getUsername().equals(username)) { + JOptionPane.showMessageDialog(null,librarian); + return librarian; + } + } + } + JOptionPane.showMessageDialog(null,"The librarian doesn't exist."); + return null; } - public void doesLibrarianExist(){ - //TODO + public void updateLibrarian(String username, String newUsername, String newPassword){ + if ( doesLibrarianExist(username) ) { + for (Librarian librarian : librarians) { + if (librarian.getUsername().equals(username)) { + librarian.setUsername(newUsername); + librarian.setPassword(newPassword); + } + } + JOptionPane.showMessageDialog(null,"Updated successfully!"); + } else { + JOptionPane.showMessageDialog(null,"The librarian doesn't exist."); + + } } + public boolean doesLibrarianExist(String username){ + for (Librarian librarian : librarians) { + if (librarian.getUsername().equals(username)) + return true; + } + return false; + } } diff --git a/src/main/java/User.java b/src/main/java/User.java index dbe4020..40650ee 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,12 +1,91 @@ +import java.util.ArrayList; +import javax.swing.*; + public class User { //User should have a list of books //User should have a username and a password - public void rentBook(){ - //TODO + private String username; + private String password; + private ArrayList userBooks; + + + public User() { + + } + + public User(String username, String password) { + this.username = username; + this.password = password; + this.userBooks = new ArrayList(); + } + + public String getUsername() { + return username; } - public void returnBook(){ - //TODO + public void setUsername(String username) { + this.username = username; } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public ArrayList getUserBooks() { + return userBooks; + } + + public void setUserBooks(ArrayList userBooks) { + this.userBooks = userBooks; + } + + public boolean areMatched(String first, String second) { + if (first.equals(second)) { + return true; + } + return false; + } + + public boolean userIsAuthenticated(String username, String password) { + if (areMatched(username,this.username) && areMatched(password,this.password)) + return true; + return false; + } + public void rentBook(Book book){ + if (doesRentedBookExist(book.getISBN())){ + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " already exists."); + + } else { + userBooks.add(book); + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " added to your list successfully!"); + } + } + + public void returnBook(Book book){ + if (doesRentedBookExist(book.getISBN())) { + userBooks.remove(book); + JOptionPane.showMessageDialog(null,"The book " + book.getName() + " has been removed from your list."); + } else { + JOptionPane.showMessageDialog(null,"The book doesn't exist."); + } + } + + public boolean doesRentedBookExist(String ISBN) { + for (Book book : userBooks) { + if (book.getISBN().equals(ISBN)) + return true; + } + return false; + } + + @Override + public String toString() { + return "Hello, it's " + this.getUsername() + "!"; + } + } From d4255b165fd22a8c93988da2783ea7ae4d5cf2ec Mon Sep 17 00:00:00 2001 From: Kiana Ghamsari Date: Fri, 10 Mar 2023 23:54:26 +0330 Subject: [PATCH 2/3] second commit --- src/main/java/Library.java | 30 ++++ src/main/java/Main.java | 317 ++++++++++++++++++++++++++++++++++++- src/main/java/User.java | 26 +-- 3 files changed, 358 insertions(+), 15 deletions(-) diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 0174555..6e340f3 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -218,6 +218,24 @@ public boolean doesUserExist(String username) { return false; } + public boolean areMatched(String first, String second) { + if (first.equals(second)) { + return true; + } + return false; + } + + public boolean userIsAuthenticated(String username, String password) { + if (doesUserExist(username)) { + for (User user : users) { + if (areMatched(user.getUsername(), username) && areMatched(user.getPassword(), password)) { + return true; + } + } + } + return false; + } + //librarian related functions public void addLibrarian(Librarian librarian){ @@ -276,4 +294,16 @@ public boolean doesLibrarianExist(String username){ return false; } + public boolean librarianIsAuthenticated(String username, String password) { + if (doesLibrarianExist(username)) { + for (Librarian librarian : librarians) { + if (areMatched(librarian.getUsername(), username) && areMatched(librarian.getPassword(), password)) { + return true; + } + } + } + return false; + } + } + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9a347d3..93dfbab 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,13 @@ import java.util.Scanner; +import javax.swing.*; public class Main { + static Library library = new Library(); + static User user = new User(); + static Librarian librarian = new Librarian(); + + //static User user = new User(); + /* * make a functional library app using oop * run the main program in Main.java and code the oop part in other classes @@ -9,10 +16,316 @@ public class Main { */ public static void main(String[] args) { - + //System.out.println(displayLibrarianMenu_Book()); + runMenu(); } public static void runMenu(){ - //TODO: + while (true) { + //Scanner sc = new Scanner(System.in); + //System.out.println("Hello! Please enter your name."); + String firstName = JOptionPane.showInputDialog(null,"Hello! Please enter your name."); + String firstChosenOption = JOptionPane.showInputDialog(null,"Welcome to the library, dear " + firstName + "!\n" + printChooseAnOption() + "\n" + displayFirstOption()); + switch (firstChosenOption) { + case "1": // Sign Up as a New User + String signUpChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displaySignUpMenu()); + switch (signUpChosenOption) { + case "1": // Sign Up as a User + newUserSignUp(); + LoginAsAUser(); + break; + case "2": // Sign Up as a Librarian + newLibrarianSignUp(); + LoginAsALibrarian(); + break; + + default: + runMenu(); + break; + } + + break; + + case "2": // Already Have an Account? Let's Login. + String loginChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displayLoginMenu()); + switch (loginChosenOption) { + case "1": // Login as a user + LoginAsAUser(); + break; + + case "2": // Login as a Librarian + LoginAsALibrarian(); + break; + + default: + runMenu(); + break; + } + break; + default: + runMenu(); + break; + } + + //String firstName = sc.nextLine(); + //System.out.println("Welcome to the library, dear " + firstName + "!\n" + printChooseAnOption() + "\n" + displayLoginMenu()); + // if (loginChosenOption.equals("1")) { + // newUserSignUp(); + // } + //JOptionPane.showMessageDialog(null, "meow"); + //System.out.println(loginChosenOption); + for (int i = 0; i < library.getUsers().size(); i++) { + System.out.println(library.getUsers().get(i)); + } + //sc.close(); + } + } + + public static String printChooseAnOption() { + return "Please Choose One Option: "; + } + public static String displayFirstOption() { + return "1: Sign Up as a New User\n2: Already Have an Account? Let's Login."; + } + public static String displaySignUpMenu() { + return "1: Sign Up as a User\n2: Sign Up as a Librarian"; + } + public static String displayLoginMenu() { + return "1: Login as a User\n2: Login as a Librarian"; + } + public static String displayUserMenu() { + return "1: Borrow a Book\n2: Return a Book"; + } + public static String displayLibrarianMenu_Book() { + //System.out.println(displayUserMenu()); + return displayUserMenu() + "3: Add a Book\n4: Remove a Book\n5: Search a Book\n6: Update a Book"; + } + public static String displayLibrarianMenu_User() { + return "1: Add a User\n2: Remove a User\n3: Search a User\n4: Update a User"; + } + public static String displayLibrarianMenu_Librarian() { + return "1: Add a Librarian\n2: Remove a Librarian\n3: Search a Librarian\n4: Update a Librarian"; + } + public static void newUserSignUp() { + String username = JOptionPane.showInputDialog(null, "Please enter a username: "); + String password = JOptionPane.showInputDialog(null, "Let's create a password: "); + User newUser =new User(username,password); + library.addUser(newUser); + + } + + public static void newLibrarianSignUp() { + String username = JOptionPane.showInputDialog(null, "Please enter a username: "); + String password = JOptionPane.showInputDialog(null, "Let's create a password: "); + Librarian newUser =new Librarian(username,password); + library.addLibrarian(newUser); + + } + + public static void LoginAsALibrarian() { + String username = JOptionPane.showInputDialog(null, "Please enter your username: "); + String password = JOptionPane.showInputDialog(null, "Please enter your password: "); + if (library.doesLibrarianExist(username)) { + if (library.librarianIsAuthenticated(username, password)) { + String librarianFirstMenuOption = JOptionPane.showInputDialog(null, "Welcome to your page dear " + username + "!\n" + printChooseAnOption() + "\n1: User Menu Options\n2: Librarian Menu Options\n3: Book Menu Options"); + switch (librarianFirstMenuOption) { + case "1": // User Menu Options + String librarianUserMenuOption = JOptionPane.showInputDialog(null, "*USER MENU OPTIONS*\n" + displayLibrarianMenu_User()); + switch (librarianUserMenuOption) { + case "1": // Add a User + //library.addUser(user); + newUserSignUp(); + break; + case "2": // Remove a User + String remove_searchUserViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to remove: "); + User removedUser = library.searchUser(remove_searchUserViaUsername); + library.removeUser(removedUser); + break; + case "3": // Search a User + String searchUserViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to search: "); + User searchedUser = library.searchUser(searchUserViaUsername); + JOptionPane.showMessageDialog(null, searchedUser); + break; + case "4": // Update a Book + String update_searchUserViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to update: "); + User updatedUser = library.searchUser(update_searchUserViaUsername); + String newPassword = JOptionPane.showInputDialog(null, "Enter the new password: "); + library.updateUser(updatedUser.getUsername(), updatedUser.getPassword(), newPassword); ////////////////////////////////////////////////////////////////////////// + break; + default: + runMenu(); + break; + } + break; + case "2": // Librarian Menu Options + String librarianLibrarianMenuOption = JOptionPane.showInputDialog(null, "*LIBRARIAN MENU OPTIONS*\n" + displayLibrarianMenu_Librarian()); + //String librarianUserMenuOption = JOptionPane.showInputDialog(null, "*USER MENU OPTIONS*\n" + displayLibrarianMenu_User()); + switch (librarianLibrarianMenuOption) { + case "1": // Add a Librarian + //library.addUser(user); + //newUserSignUp(); + newLibrarianSignUp(); + break; + case "2": // Remove a Librarian + String remove_searchLibrarianViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to remove: "); + User removedUser = library.searchUser(remove_searchLibrarianViaUsername); + library.removeUser(removedUser); + break; + case "3": // Search a Librarian + String searchLibrarianViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to search: "); + User searchedUser = library.searchUser(searchLibrarianViaUsername); + JOptionPane.showMessageDialog(null, searchedUser); + break; + case "4": // Update a Librarian + String update_searchLibrarianViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to update: "); + User updatedUser = library.searchUser(update_searchLibrarianViaUsername); + String newPassword = JOptionPane.showInputDialog(null, "Enter the new password: "); + library.updateUser(updatedUser.getUsername(), updatedUser.getPassword(), newPassword); + break; + default: + runMenu(); + break; + } + break; + + case "3": // Book Menu Options + String librarianBookMenuOption = JOptionPane.showInputDialog(null, "*BOOK MENU OPTIONS*\n" + displayLibrarianMenu_Book()); + switch (librarianBookMenuOption) { + case "1": // Borrow a Book + String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); + Book rentBook = library.searchBook(rent_searchViaISBN); + //user.rentBook(rentBook); + librarian.rentBook(rentBook); + break; + case "2": // Return a Book + String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); + Book returnBook = library.searchBook(return_searchViaISBN); + //user.returnBook(returnBook); + librarian.rentBook(returnBook); + break; + case "3": // Add a Book + String add_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to add: "); + Book addBook = library.searchBook(add_searchViaISBN); + library.addBook(addBook); + break; + case "4": // Remove a Book + String remove_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to remove: "); + Book removeBook = library.searchBook(remove_searchViaISBN); + library.removeBook(removeBook); + break; + case "5": // Search a Book + String searchBookViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to search: "); + Book searchedBook = library.searchBook(searchBookViaISBN); + JOptionPane.showMessageDialog(null, searchedBook); + break; + case "6": // Update a Book + String update_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to update: "); + //String update_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to update: "); + ////////////////////////////////////// + // Book addBook = library.searchBook(add_searchViaISBN); + // library.addBook(addBook); + break; + default: + break; + + + } + break; + + default: + runMenu(); + break; + } + + String librarianMenuOption = JOptionPane.showInputDialog(null, "Welcome to your page dear " + username + "!" + "\nWhat would you like to do?\n" + displayUserMenu()); + switch (librarianMenuOption) { //////////////////////////////////////////////////////// user/book/librarian field ////////////////////////////////////////////////////////////////////////////////////////// + case "1": // Borrow a Book + String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); + Book rentBook = library.searchBook(rent_searchViaISBN); + user.rentBook(rentBook); + + break; + case "2": // Return a Book + String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); + Book returnBook = library.searchBook(return_searchViaISBN); + user.returnBook(returnBook); + break; + + default: + runMenu(); + break; + } + + } else { + JOptionPane.showMessageDialog(null, "The username & password you entered do not match. Please try again: "); + LoginAsAUser(); + + } + } else { + String wrongUsername = JOptionPane.showInputDialog(null, "This username doesn't exist. Do you want to Sign up as a new user?\n1: Yes, let's create a new account\n2: No, let's try again."); + switch (wrongUsername) { + case "1": + newUserSignUp(); + break; + case "2": + LoginAsAUser(); + break; + + default: + runMenu(); + break; + } + + } + + } + + public static void LoginAsAUser() { + String username = JOptionPane.showInputDialog(null, "Please enter your username: "); + String password = JOptionPane.showInputDialog(null, "Please enter your password: "); + if (library.doesUserExist(username)) { + if (library.userIsAuthenticated(username, password)) { + String userMenuOption = JOptionPane.showInputDialog(null, "Welcome to your page dear " + username + "!" + "\nWhat would you like to do?\n" + displayUserMenu()); + switch (userMenuOption) { + case "1": // Borrow a Book + String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); + Book rentBook = library.searchBook(rent_searchViaISBN); + user.rentBook(rentBook); + + break; + case "2": // Return a Book + String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); + Book returnBook = library.searchBook(return_searchViaISBN); + user.returnBook(returnBook); + break; + + default: + runMenu(); + break; + } + + } else { + JOptionPane.showMessageDialog(null, "The username & password you entered do not match. Please try again: "); + LoginAsAUser(); + + } + } else { + String wrongUsername = JOptionPane.showInputDialog(null, "This username doesn't exist. Do you want to Sign up as a new user?\n1: Yes, let's create a new account\n2: No, let's try again."); + switch (wrongUsername) { + case "1": + newUserSignUp(); + break; + case "2": + LoginAsAUser(); + break; + + default: + runMenu(); + break; + } + + } + } + } diff --git a/src/main/java/User.java b/src/main/java/User.java index 40650ee..7ba0d23 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -44,18 +44,18 @@ public void setUserBooks(ArrayList userBooks) { this.userBooks = userBooks; } - public boolean areMatched(String first, String second) { - if (first.equals(second)) { - return true; - } - return false; - } - - public boolean userIsAuthenticated(String username, String password) { - if (areMatched(username,this.username) && areMatched(password,this.password)) - return true; - return false; - } + // public boolean areMatched(String first, String second) { + // if (first.equals(second)) { + // return true; + // } + // return false; + // } + + // public boolean userIsAuthenticated(String username, String password) { + // if (areMatched(username,this.username) && areMatched(password,this.password)) + // return true; + // return false; + // } public void rentBook(Book book){ if (doesRentedBookExist(book.getISBN())){ JOptionPane.showMessageDialog(null,"The book " + book.getName() + " already exists."); @@ -85,7 +85,7 @@ public boolean doesRentedBookExist(String ISBN) { @Override public String toString() { - return "Hello, it's " + this.getUsername() + "!"; + return "Hello, it's " + this.getUsername() + "!" + "\nThe Password: " + this.getPassword(); } } From ac2422757d124676a0573efbfdce87c979c9b895 Mon Sep 17 00:00:00 2001 From: Kiana Ghamsari Date: Sat, 11 Mar 2023 23:25:07 +0330 Subject: [PATCH 3/3] last commit --- report.md | 44 ++++++++++ src/main/java/Book.java | 13 --- src/main/java/Librarian.java | 6 -- src/main/java/Library.java | 30 ------- src/main/java/Main.java | 150 ++++++++++++----------------------- src/main/java/User.java | 12 --- 6 files changed, 96 insertions(+), 159 deletions(-) create mode 100644 report.md diff --git a/report.md b/report.md new file mode 100644 index 0000000..2022ee5 --- /dev/null +++ b/report.md @@ -0,0 +1,44 @@ +# 3rd Assignment Report + +![](https://github.com/kianaghamsari/Second-Assignment/blob/develop/uni.png) + +## Kiana Ghamsari - 400222079 + + +# Introduction + +The purpose of the application is to manage a library using Java Object-Oriented Programming. + +The application includes 5 classes: +* Book +* Librarian +* Library +* Main +* User + +You can sign up or login if you already have an account as a ordinary user who can borrow a book, or as a librarian who manages the library. + + +# Structure + +* The `Library` class is the most important , and also, class `Main` runs the program by calling the method called `runMenu()`. +* The methods `newUserSignUp()` and `newLibrarianSignUp()` are created in `Main` to let new users sign up as either an ordinary user or a librarian. +* Users can borrow and return books from the library by `rentBook()` and `returnBook()` methods, respectively, defined in `User` class. +* The add, update, search, remove operations on all books and users objects are accessed only by librarians. These methods are defined in `Library` class. +* A Librarian can also borrow or return books due to it’s inheritance of `User` class. +* Librarian can, also, add or remove another librarian. + +# Bonus +In my implementation: + +* Usernames are unique +* A book cannot be borrowed twice +* The user cannot return the book that has not been borrowed +* All fields are private, and proper getters and setters are defined +* All functionalities are available from GUI + +# Conclusion: +* By this project, I've used basic concepts of object-oriented programming in an example. +* I, also, experienced working with access modifiers, public and private methods. +* I broke my code into individual classes that helped me increasing the readability of my code +* I didn't provide a back and exit command \ No newline at end of file diff --git a/src/main/java/Book.java b/src/main/java/Book.java index f12696c..84bc60a 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -5,23 +5,11 @@ public class Book { private int yearOfPublish; private String ISBN; - private int amountOfBook = 0; - - public int getAmountOfBook() { - return amountOfBook; - } - - public void setAmountOfBook(int amountOfBook) { - this.amountOfBook = amountOfBook; - } - public Book(String name, String author, int yearOfPublish, String ISBN) { this.name = name; this.author = author; this.yearOfPublish = yearOfPublish; this.ISBN = ISBN; - - this.amountOfBook++; } public String getName() { @@ -61,5 +49,4 @@ public String toString() { return "Here is the information of " + this.getName() + "book:\nThe Book's Name: " + getName() + "\nThe Author's Name: " + getAuthor() + "\nThe Book's Year of Publish: " + getYearOfPublish() + "\nThe Book's ISBN: " + getISBN(); } - } diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 7f4e248..1126171 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -14,10 +14,4 @@ public Librarian(String username, String password) { super(username, password); } - // @Override - // public String toString() { - // super.toString(); - // } - - } diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6e340f3..03df917 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -21,7 +21,6 @@ public Library() { this.librarians = new ArrayList(); this.users = new ArrayList(); this.amountOfTheBook = new HashMap(); - } public Library(Book books, Librarian librarians, User users) { @@ -63,28 +62,18 @@ public HashMap getAmountOfTheBook() { public void setAmountOfTheBook(HashMap amountOfTheBook) { this.amountOfTheBook = amountOfTheBook; } - - - - - //book related functions public void addBook(Book book){ - //Book addedBook = new Book(name, author, yearOfPublish, ISBN); - // books.add(book); - // increaseBook(book.getISBN()); if (doesBookExist(book.getISBN())) { increaseBook(book.getISBN()); JOptionPane.showMessageDialog(null,"The book " + book.getName() + " added successfully!"); - } else { books.add(book); increaseBook(book.getISBN()); JOptionPane.showMessageDialog(null,"The book " + book.getName() + " added successfully!"); } - } public void removeBook(Book book){ @@ -92,7 +81,6 @@ public void removeBook(Book book){ books.remove(book); decreaseBook(book.getISBN()); JOptionPane.showMessageDialog(null,"The book " + book.getName() + " has been removed."); - } else { JOptionPane.showMessageDialog(null,"The book doesn't exist."); } @@ -102,7 +90,6 @@ public Book searchBook( String ISBN ){ if (doesBookExist(ISBN)) { for (Book book : books) { if (book.getISBN().equals(ISBN)) { - //System.out.println(book); JOptionPane.showMessageDialog(null,book); return book; } @@ -110,7 +97,6 @@ public Book searchBook( String ISBN ){ } JOptionPane.showMessageDialog(null,"The book doesn't exist."); return null; - } public void updateBook(String ISBN, String newName, String newAuthor, int newYearOfPublish){ @@ -132,10 +118,8 @@ public boolean doesBookExist( String ISBN ){ for (Book book : books) { if (book.getISBN().equals(ISBN)) return true; - } return false; - } public void increaseBook(String ISBN){ @@ -143,15 +127,12 @@ public void increaseBook(String ISBN){ amountOfTheBook.replace(ISBN, amountOfTheBook.get(ISBN) + 1); } else { amountOfTheBook.put(ISBN, 1); - } - } public void decreaseBook(String ISBN){ if (doesBookExist(ISBN)) { amountOfTheBook.replace(ISBN, amountOfTheBook.get(ISBN) - 1); - } else { JOptionPane.showMessageDialog(null,"The book doesn't exist."); } @@ -159,23 +140,19 @@ public void decreaseBook(String ISBN){ //user related functions - public void addUser(User user){ if (doesUserExist(user.getUsername())) { JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " already exists."); - } else { users.add(user); JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " added successfully!"); } - } public void removeUser(User user){ if (doesUserExist(user.getUsername())) { users.remove(user); JOptionPane.showMessageDialog(null,"The user " + user.getUsername() + " has been removed."); - } else { JOptionPane.showMessageDialog(null,"The username doesn't exist."); } @@ -185,7 +162,6 @@ public User searchUser(String username){ if (doesUserExist(username)) { for (User user : users) { if (user.getUsername().equals(username)) { - //System.out.println(user); JOptionPane.showMessageDialog(null,user); return user; } @@ -206,7 +182,6 @@ public void updateUser(String username, String newUsername, String newPassword){ JOptionPane.showMessageDialog(null,"Updated successfully!"); } else { JOptionPane.showMessageDialog(null,"The username doesn't exist."); - } } @@ -241,7 +216,6 @@ public boolean userIsAuthenticated(String username, String password) { public void addLibrarian(Librarian librarian){ if (doesLibrarianExist(librarian.getUsername())) { JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " already exists."); - } else { librarians.add(librarian); JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " added successfully!"); @@ -252,7 +226,6 @@ public void removeLibrarian(Librarian librarian){ if (doesLibrarianExist(librarian.getUsername())) { librarians.remove(librarian); JOptionPane.showMessageDialog(null,"The librarian " + librarian.getUsername() + " has been removed."); - } else { JOptionPane.showMessageDialog(null,"The librarian doesn't exist."); } @@ -282,7 +255,6 @@ public void updateLibrarian(String username, String newUsername, String newPassw JOptionPane.showMessageDialog(null,"Updated successfully!"); } else { JOptionPane.showMessageDialog(null,"The librarian doesn't exist."); - } } @@ -304,6 +276,4 @@ public boolean librarianIsAuthenticated(String username, String password) { } return false; } - } - diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 93dfbab..b762361 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,4 +1,3 @@ -import java.util.Scanner; import javax.swing.*; public class Main { @@ -6,8 +5,6 @@ public class Main { static User user = new User(); static Librarian librarian = new Librarian(); - //static User user = new User(); - /* * make a functional library app using oop * run the main program in Main.java and code the oop part in other classes @@ -16,68 +13,49 @@ public class Main { */ public static void main(String[] args) { - //System.out.println(displayLibrarianMenu_Book()); runMenu(); } public static void runMenu(){ while (true) { - //Scanner sc = new Scanner(System.in); - //System.out.println("Hello! Please enter your name."); - String firstName = JOptionPane.showInputDialog(null,"Hello! Please enter your name."); - String firstChosenOption = JOptionPane.showInputDialog(null,"Welcome to the library, dear " + firstName + "!\n" + printChooseAnOption() + "\n" + displayFirstOption()); - switch (firstChosenOption) { - case "1": // Sign Up as a New User - String signUpChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displaySignUpMenu()); - switch (signUpChosenOption) { - case "1": // Sign Up as a User - newUserSignUp(); - LoginAsAUser(); - break; - case "2": // Sign Up as a Librarian - newLibrarianSignUp(); - LoginAsALibrarian(); - break; - - default: - runMenu(); - break; - } - - break; - - case "2": // Already Have an Account? Let's Login. - String loginChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displayLoginMenu()); - switch (loginChosenOption) { - case "1": // Login as a user - LoginAsAUser(); - break; - - case "2": // Login as a Librarian - LoginAsALibrarian(); - break; - - default: - runMenu(); - break; - } - break; - default: - runMenu(); - break; - } + String firstName = JOptionPane.showInputDialog(null,"Hello! Please enter your name."); + String firstChosenOption = JOptionPane.showInputDialog(null,"Welcome to the library, dear " + firstName + "!\n" + printChooseAnOption() + "\n" + displayFirstOption()); + switch (firstChosenOption) { + case "1": // Sign Up as a New User + String signUpChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displaySignUpMenu()); + switch (signUpChosenOption) { + case "1": // Sign Up as a User + newUserSignUp(); + LoginAsAUser(); + break; + case "2": // Sign Up as a Librarian + newLibrarianSignUp(); + LoginAsALibrarian(); + break; + default: + runMenu(); + break; + } + break; + case "2": // Already Have an Account? Let's Login. + String loginChosenOption = JOptionPane.showInputDialog(null, printChooseAnOption() + "\n" + displayLoginMenu()); + switch (loginChosenOption) { + case "1": // Login as a user + LoginAsAUser(); + break; + case "2": // Login as a Librarian + LoginAsALibrarian(); + break; + default: + runMenu(); + break; + } + break; + default: + runMenu(); + break; + } - //String firstName = sc.nextLine(); - //System.out.println("Welcome to the library, dear " + firstName + "!\n" + printChooseAnOption() + "\n" + displayLoginMenu()); - // if (loginChosenOption.equals("1")) { - // newUserSignUp(); - // } - //JOptionPane.showMessageDialog(null, "meow"); - //System.out.println(loginChosenOption); - for (int i = 0; i < library.getUsers().size(); i++) { - System.out.println(library.getUsers().get(i)); - } - //sc.close(); } } @@ -97,8 +75,7 @@ public static String displayUserMenu() { return "1: Borrow a Book\n2: Return a Book"; } public static String displayLibrarianMenu_Book() { - //System.out.println(displayUserMenu()); - return displayUserMenu() + "3: Add a Book\n4: Remove a Book\n5: Search a Book\n6: Update a Book"; + return displayUserMenu() + "\n3: Add a Book\n4: Remove a Book\n5: Search a Book\n6: Update a Book"; } public static String displayLibrarianMenu_User() { return "1: Add a User\n2: Remove a User\n3: Search a User\n4: Update a User"; @@ -111,7 +88,6 @@ public static void newUserSignUp() { String password = JOptionPane.showInputDialog(null, "Let's create a password: "); User newUser =new User(username,password); library.addUser(newUser); - } public static void newLibrarianSignUp() { @@ -119,7 +95,6 @@ public static void newLibrarianSignUp() { String password = JOptionPane.showInputDialog(null, "Let's create a password: "); Librarian newUser =new Librarian(username,password); library.addLibrarian(newUser); - } public static void LoginAsALibrarian() { @@ -133,7 +108,6 @@ public static void LoginAsALibrarian() { String librarianUserMenuOption = JOptionPane.showInputDialog(null, "*USER MENU OPTIONS*\n" + displayLibrarianMenu_User()); switch (librarianUserMenuOption) { case "1": // Add a User - //library.addUser(user); newUserSignUp(); break; case "2": // Remove a User @@ -146,11 +120,11 @@ public static void LoginAsALibrarian() { User searchedUser = library.searchUser(searchUserViaUsername); JOptionPane.showMessageDialog(null, searchedUser); break; - case "4": // Update a Book + case "4": // Update a User String update_searchUserViaUsername = JOptionPane.showInputDialog(null, "Enter the username you want to update: "); User updatedUser = library.searchUser(update_searchUserViaUsername); String newPassword = JOptionPane.showInputDialog(null, "Enter the new password: "); - library.updateUser(updatedUser.getUsername(), updatedUser.getPassword(), newPassword); ////////////////////////////////////////////////////////////////////////// + library.updateUser(updatedUser.getUsername(), updatedUser.getPassword(), newPassword); break; default: runMenu(); @@ -159,11 +133,8 @@ public static void LoginAsALibrarian() { break; case "2": // Librarian Menu Options String librarianLibrarianMenuOption = JOptionPane.showInputDialog(null, "*LIBRARIAN MENU OPTIONS*\n" + displayLibrarianMenu_Librarian()); - //String librarianUserMenuOption = JOptionPane.showInputDialog(null, "*USER MENU OPTIONS*\n" + displayLibrarianMenu_User()); switch (librarianLibrarianMenuOption) { case "1": // Add a Librarian - //library.addUser(user); - //newUserSignUp(); newLibrarianSignUp(); break; case "2": // Remove a Librarian @@ -187,26 +158,26 @@ public static void LoginAsALibrarian() { break; } break; - case "3": // Book Menu Options String librarianBookMenuOption = JOptionPane.showInputDialog(null, "*BOOK MENU OPTIONS*\n" + displayLibrarianMenu_Book()); switch (librarianBookMenuOption) { case "1": // Borrow a Book String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); Book rentBook = library.searchBook(rent_searchViaISBN); - //user.rentBook(rentBook); librarian.rentBook(rentBook); break; case "2": // Return a Book String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); Book returnBook = library.searchBook(return_searchViaISBN); - //user.returnBook(returnBook); - librarian.rentBook(returnBook); + librarian.returnBook(returnBook); break; case "3": // Add a Book - String add_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to add: "); - Book addBook = library.searchBook(add_searchViaISBN); - library.addBook(addBook); + String add_bookName = JOptionPane.showInputDialog(null, "Enter the name of the book you want to add: "); + String add_bookAuthor = JOptionPane.showInputDialog(null, "Enter the author of the book you want to add: "); + String add_bookYearOfPublish = JOptionPane.showInputDialog(null, "Enter the publish year of the book you want to add: "); + String add_bookISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to add: "); + Book newBook = new Book(add_bookName, add_bookAuthor, Integer.parseInt(add_bookYearOfPublish), add_bookISBN); + library.addBook(newBook); break; case "4": // Remove a Book String remove_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to remove: "); @@ -220,46 +191,39 @@ public static void LoginAsALibrarian() { break; case "6": // Update a Book String update_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to update: "); - //String update_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to update: "); - ////////////////////////////////////// - // Book addBook = library.searchBook(add_searchViaISBN); - // library.addBook(addBook); + String newBookName = JOptionPane.showInputDialog(null, "Enter the new name of the book you want to update: "); + String newBookAuthor = JOptionPane.showInputDialog(null, "Enter the new author of the book you want to update: "); + String newBookYearOfPublish = JOptionPane.showInputDialog(null, "Enter the new publish year of the book you want to update: "); + library.updateBook(update_searchViaISBN, newBookName, newBookAuthor, Integer.parseInt(newBookYearOfPublish)); break; default: break; - - } break; - default: runMenu(); break; } String librarianMenuOption = JOptionPane.showInputDialog(null, "Welcome to your page dear " + username + "!" + "\nWhat would you like to do?\n" + displayUserMenu()); - switch (librarianMenuOption) { //////////////////////////////////////////////////////// user/book/librarian field ////////////////////////////////////////////////////////////////////////////////////////// + switch (librarianMenuOption) { case "1": // Borrow a Book String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); Book rentBook = library.searchBook(rent_searchViaISBN); user.rentBook(rentBook); - break; case "2": // Return a Book String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); Book returnBook = library.searchBook(return_searchViaISBN); user.returnBook(returnBook); break; - default: runMenu(); break; } - } else { JOptionPane.showMessageDialog(null, "The username & password you entered do not match. Please try again: "); LoginAsAUser(); - } } else { String wrongUsername = JOptionPane.showInputDialog(null, "This username doesn't exist. Do you want to Sign up as a new user?\n1: Yes, let's create a new account\n2: No, let's try again."); @@ -275,9 +239,7 @@ public static void LoginAsALibrarian() { runMenu(); break; } - } - } public static void LoginAsAUser() { @@ -291,23 +253,19 @@ public static void LoginAsAUser() { String rent_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to rent: "); Book rentBook = library.searchBook(rent_searchViaISBN); user.rentBook(rentBook); - break; case "2": // Return a Book String return_searchViaISBN = JOptionPane.showInputDialog(null, "Enter the ISBN of the book you want to return: "); Book returnBook = library.searchBook(return_searchViaISBN); user.returnBook(returnBook); break; - default: runMenu(); break; } - } else { JOptionPane.showMessageDialog(null, "The username & password you entered do not match. Please try again: "); LoginAsAUser(); - } } else { String wrongUsername = JOptionPane.showInputDialog(null, "This username doesn't exist. Do you want to Sign up as a new user?\n1: Yes, let's create a new account\n2: No, let's try again."); @@ -318,14 +276,10 @@ public static void LoginAsAUser() { case "2": LoginAsAUser(); break; - default: runMenu(); break; } - } - } - } diff --git a/src/main/java/User.java b/src/main/java/User.java index 7ba0d23..5c8b327 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -44,18 +44,6 @@ public void setUserBooks(ArrayList userBooks) { this.userBooks = userBooks; } - // public boolean areMatched(String first, String second) { - // if (first.equals(second)) { - // return true; - // } - // return false; - // } - - // public boolean userIsAuthenticated(String username, String password) { - // if (areMatched(username,this.username) && areMatched(password,this.password)) - // return true; - // return false; - // } public void rentBook(Book book){ if (doesRentedBookExist(book.getISBN())){ JOptionPane.showMessageDialog(null,"The book " + book.getName() + " already exists.");