commit 9f8250ae15b9e15eada365795708d4ff81d61933 Author: James Musselman Date: Thu Jan 16 19:37:13 2025 -0600 Upload project files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b184195 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ +LibraryManagementClient/target/* +LibraryManagementServer/target/* +LibraryManagementLibrary/target/* +LibraryManagementClient/.* +LibraryManagementLibrary/.* +LibraryManagementServer/.* +target/ diff --git a/LibraryManagementClient/pom.xml b/LibraryManagementClient/pom.xml new file mode 100644 index 0000000..2a2a73e --- /dev/null +++ b/LibraryManagementClient/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.pawprintstudios + LibraryManagementSystem + 1.0-SNAPSHOT + + + + + maven-assembly-plugin + + + package + + single + + + + + + + true + librarymanagementsystem.Client + + + + jar-with-dependencies + + + + + + LibraryManagementClient + jar + + UTF-8 + 17 + 17 + + + + com.pawprintstudios + LibraryManagementLibrary + 1.0-SNAPSHOT + compile + + + diff --git a/LibraryManagementClient/src/main/java/librarymanagementsystem/Client.java b/LibraryManagementClient/src/main/java/librarymanagementsystem/Client.java new file mode 100644 index 0000000..d3b3a1e --- /dev/null +++ b/LibraryManagementClient/src/main/java/librarymanagementsystem/Client.java @@ -0,0 +1,492 @@ +package librarymanagementsystem; + +import java.io.*; +import java.net.Socket; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; +import java.util.Scanner; + +/** + * Client + * + * @author Pawprint Studios + */ +public class Client +{ + static final int PORT = 51042; + static String userCommand = null; + static String command = null; + static String userType = null; + static String usersName = null; + static Long userId = null; + static boolean userLoggedIn = false; + static PrintWriter outToServer = null; + static Scanner inFromServer = null; + + public static void main(String[] args) + { + + int userInput = -1; + String searchedBook; + String borrowedBook; + String returnBook; + boolean exitLibrary = false; + Scanner scan = new Scanner(System.in); + + + + System.out.println("*** Library Client ***\n"); + + System.out.println("Below are commands to use the library."); + System.out.println("Some commands are hidden as they require login."); + System.out.println("Input 0 to exit the client\n"); + while (exitLibrary == false) + { + + // print options for those not logged in + while (userLoggedIn == false) + { + System.out.println("Welcome Guest!"); + System.out.println("\n1. Search The Library"); + System.out.println("\n2. Apply For Membership"); + System.out.println("\n3. Log in"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + while (userInput < 0 || userInput > 3) + { + System.out.println("Please Enter a valid option"); + System.out.print("\nWhat would you like to do?: "); + userInput = scan.nextInt(); + } + scan.nextLine(); + + switch (userInput) + { + case 0: + System.out.println("\nThanks for using the Library!"); + System.exit(0); + case 1: + command = "search, "; + System.out.print("\nWhat would you like to search for: "); + searchedBook = scan.nextLine().trim(); + userCommand = command + searchedBook; + break; + case 2: + command = "apply, "; + System.out.println("\nUsernames cannot have special" + + " charecters or spaces."); + System.out.print("\nPlease enter your desired username: "); + String chosenUsername = scan.nextLine().trim() + ", "; + System.out.print("\nPlease enter your desired password: "); + String chosenPassword = scan.nextLine().trim() + ", "; + System.out.print("\nPlease enter your first and last " + + "name: "); + String name = scan.nextLine().trim() + ", "; + System.out.print("\nPlease enter your prefered email " + + "address: "); + String preferedEmail = scan.nextLine().trim() + ", "; + System.out.print("\nPlease enter your prefered " + + "phone number: "); + boolean badPhoneInput = true ; + String preferedPhoneNum = null; + while (badPhoneInput) + { + if (scan.hasNextLong()) + { + badPhoneInput = false; + preferedPhoneNum = scan.nextLine().trim() + ", "; + } + else + { + System.out.println("Please enter Valid Phone " + + "Number"); + System.out.print("\nPlease enter your prefered " + + "phone number: "); + preferedPhoneNum = scan.nextLine().trim() + ", "; + } + } + + System.out.print(""); + + userCommand = command + chosenUsername + chosenPassword + + name + preferedEmail + preferedPhoneNum; + break; + case 3: + command = "login, "; + System.out.println("Please enter your credentials."); + System.out.print("\nUsername: "); + String username = scan.next(); + System.out.print("\nPassword: "); + String password = scan.next(); + userCommand = command + username + ", " + password; + break; + default: + break; + } + getResults(); + } + + while (userLoggedIn && userType.equals("Patron")) + { + System.out.println("Welcome back, " + usersName + "!"); + System.out.println("\n1. Search The Library"); + System.out.println("\n2. Borrow Book"); + System.out.println("\n3. View Loans"); + System.out.println("\n4. Return Book"); + System.out.println("\n5. Logout"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + while (userInput < 0 || userInput > 5) + { + System.out.println("Please Enter a valid option"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + } + scan.nextLine(); + + switch (userInput) + { + case 0: + System.out.println("\nThanks for using the Library!"); + System.exit(0); + case 1: + command = "search, "; + System.out.print("\nWhat would you like to search for: "); + searchedBook = scan.nextLine().trim(); + userCommand = command + searchedBook; + break; + case 2: + command = "borrow, "; + System.out.print("\nWhat book would you like to borrow " + + "(use the Books ID Number): "); + borrowedBook = scan.nextLine().trim() + ", "; + userCommand = command + borrowedBook + userId; + break; + case 3: + command = "loans, "; + userCommand = command + userId; + break; + case 4: + command = "return, "; + System.out.print("\nWhat book would you like to return: "); + returnBook = scan.nextLine().trim() + ", "; + userCommand = command + returnBook + userId; + break; + case 5: + command = "logout, "; + userLoggedIn = false; + userType = null; + break; + } + getResults(); + } + + + + + + while (userLoggedIn && userType.equals("Librarian")) + { + System.out.println("Welcome back, " + usersName + "!"); + + System.out.println("\n1. Search The Library"); + System.out.println("\n2. Add Book to Library"); + System.out.println("\n3. Retire Book to Library"); + System.out.println("\n4. Log out"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + while (userInput < 0 || userInput > 5) + { + System.out.println("Please Enter a valid option"); + System.out.print("\nWhat would you like to do?: "); + userInput = scan.nextInt(); + } + scan.nextLine(); + + switch (userInput) + { + case 0: + System.out.println("Thanks for using the Library!"); + System.exit(0); + case 1: + command = "search, "; + System.out.print("\nWhat would you like to search for: "); + searchedBook = scan.next(); + userCommand = command + searchedBook; + break; + + case 2: + command = "addBook, "; + System.out.println("Please enter the book details."); + System.out.print("\nTitle: "); + String title = scan.nextLine().trim() + ", "; + System.out.print("\nAuthor: "); + String author = scan.nextLine().trim() + ", "; + System.out.print("\nYear released: "); + boolean badYearInput = true ; + String year = null; + while (badYearInput) + { + if (scan.hasNextInt()) + { + badYearInput = false; + year = scan.nextLine().trim() + ", "; + } + else + { + System.out.println("Please enter Valid Year"); + System.out.print("\nYear released: "); + year = scan.nextLine().trim() + ", "; + } + } + + System.out.print("\nPublisher: "); + String publisher = scan.nextLine().trim() + ", "; + System.out.print("\nDate Published: "); + String publisherDate = scan.nextLine().trim() + ", "; + System.out.print("\nEdition: "); + boolean badEditionInput = true ; + String edition = null; + while (badEditionInput) + { + if (scan.hasNextInt()) + { + badEditionInput = false; + edition = scan.nextLine().trim() + ", "; + } + else + { + System.out.println("Please enter a Valid Edition Number"); + System.out.print("\nEdition: "); + edition = scan.nextLine().trim() + ", "; + } + } + + System.out.print("\nISB Number: "); + boolean badISBNInput = true ; + String isbn = null; + while (badISBNInput) + { + if (scan.hasNextLong()) + { + badISBNInput = false; + isbn = scan.nextLine().trim() + ", "; + } + else + { + System.out.println("Please enter a valid ISB Number"); + System.out.print("\nISB Number: "); + isbn = scan.nextLine().trim() + ", "; + } + } + + System.out.print("\nNumber of Copies the Library has: "); + boolean badCopiesInput = true ; + String numCopies = null; + while (badCopiesInput) + { + if (scan.hasNextInt()) + { + int copies = scan.nextInt(); + if (copies > 0) + { + badCopiesInput = false; + numCopies = copies + ", "; + } + else + { + System.out.println("Please enter a Valid Number of Copies"); + System.out.print("\nNumber of Copies the Library has: "); + numCopies = scan.nextLine().trim() + ", "; + } + } + else + { + System.out.println("Please enter a Valid Number of Copies"); + System.out.print("\nNumber of Copies the Library has: "); + numCopies = scan.nextLine().trim() + ", "; + } + } + scan.nextLine(); + + System.out.print("\nPrice of the Book: "); + boolean badPriceInput = true ; + String price = null; + while (badPriceInput) + { + if (scan.hasNextDouble()) + { + double priceDouble = scan.nextDouble(); + if (priceDouble > 0) + { + badPriceInput = false; + price = priceDouble + ""; + } + else + { + System.out.println("Please enter a Valid Price"); + System.out.print("\nPrice of the Book: "); + price = scan.nextLine().trim(); + } + } + else + { + System.out.println("Please enter a Valid Price"); + System.out.print("\nPrice of the Book: "); + price = scan.nextLine().trim(); + } + } + scan.nextLine(); + + userCommand = command + title + author + year + publisher + + publisherDate + edition + isbn + numCopies + price; + break; + + case 3: + command = "retireBook, "; + System.out.print("\nWhat book would you like to retire " + + "(use the ID Number): "); + String retiringBook = scan.nextLine().trim(); + userCommand = command + retiringBook; + break; + case 4: + command = "logout, "; + userLoggedIn = false; + userType = null; + break; + + } + getResults(); + } + + + while (userLoggedIn && userType.equals("Admin")) + { + System.out.println("Welcome back, " + usersName + "!"); + System.out.println("\n1. Search The Library"); + System.out.println("\n2. Backup logs and database"); + System.out.println("\n3. Remove a User"); + System.out.println("\n4. Log out"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + while (userInput < 0 || userInput > 4) + { + System.out.println("Please enter a valid option"); + System.out.print("\nWhat would you like to do: "); + userInput = scan.nextInt(); + } + scan.nextLine(); + + switch (userInput) + { + case 0: + System.out.println("\nThanks for using the Library!"); + System.exit(0); + case 1: + command = "search, "; + System.out.print("\nWhat would you like to search for: "); + searchedBook = scan.nextLine().trim(); + userCommand = command + searchedBook; + break; + case 2: + command = "backup, "; + userCommand = command; + break; + case 3: + command = "removeUser, "; + System.out.print("\nWhat user would you like to remove " + + "(Use ID Number): "); + String removedUser = scan.nextLine().trim(); + userCommand = command + removedUser; + break; + case 4: + command = "logout, "; + userLoggedIn = false; + userType = null; + break; + } + getResults(); + } + } + scan.close(); + } + + private static void getResults() + { + try + { + Socket clientToServer = new Socket("localhost", PORT); + + outToServer = new PrintWriter( + clientToServer.getOutputStream()); + inFromServer = new Scanner( + clientToServer.getInputStream()); + + outToServer.println(userCommand); + outToServer.flush(); + + String results = inFromServer.nextLine(); + if (command.equals("search, ") && results.equals("Found")) + { + results = searchReturn(inFromServer); + } + else if (command.equals("loans, ")) + { + results += searchReturn(inFromServer); + } + else if (command.equals("logout, ")) + { + results = "Successfully logged out."; + } + System.out.println("\n\t\t\t+++Results From Library+++ " + + "\n"); + System.out.println(results); + System.out.println("\n\t\t\t+++--------------------+++ " + + "\n"); + + if (command.equals("login, ")) + { + userType = inFromServer.nextLine(); + usersName = inFromServer.nextLine(); + userId = inFromServer.nextLong(); + userLoggedIn = true; + } + + clientToServer.close(); + } + catch (IOException e) + { + System.err.println(e); + } + catch (InputMismatchException e) + { + System.out.println("You must input a number."); + } + catch (NoSuchElementException e) + { + System.out.println(e); + } + } + + /** + * Search the book database within server and prints results. + * + * @param inFromServer represents the results coming in from server. + * @return results + */ + private static String searchReturn(Scanner inFromServer) + { + int numResults = inFromServer.nextInt(); + inFromServer.nextLine(); + String results = ""; + for (int i = 0; i < numResults; i++) + { + for (int j = 0; j < 5; j++) + { + results += "\n" + inFromServer.nextLine(); + } + results += "\n"; + } + return results; + } +} diff --git a/LibraryManagementLibrary/pom.xml b/LibraryManagementLibrary/pom.xml new file mode 100644 index 0000000..dec037a --- /dev/null +++ b/LibraryManagementLibrary/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + com.pawprintstudios + LibraryManagementSystem + 1.0-SNAPSHOT + + LibraryManagementLibrary + jar + + UTF-8 + 17 + 17 + + diff --git a/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Book.java b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Book.java new file mode 100644 index 0000000..1061fec --- /dev/null +++ b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Book.java @@ -0,0 +1,283 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package librarymanagementsystem; + +import java.util.ArrayList; + +/** + * Represents a book's identification including title, author, year, publisher, + * publisherDate, edition, isbn, numCopies, numCheckedCopies, currentHolders, + * idNum, and price. + * @author Pawprint Studios + */ +public class Book +{ + + public String title; + public String author; + public int year; + public String publisher; + public String publisherDate; + public int edition; + public long isbn; + public int numCopies; + public int numCheckedCopies; + private long idNum; + private double price; + private ArrayList currentHolders; + + // Ryan Reminder: We need to add more constructors to this class that + // require less parameters, i.e. one constructor only requires the Title and + // the Author. + + /** + * Represents the title of the book. + * @param bookTitle represents the title of the book + */ + public Book(String bookTitle) + { + title = bookTitle; + } + + /** + * Represents the book's title, author, year, number of copies, number of + * checked out copies, current holders, and Library Id number. + * @param bookTitle represents the title of the book. + * @param bookAuthor represents the author of the book. + * @param bookYear represents the year the book was published. + * @param bookNumCopies represents the number of copies library holds of + * book. + * @param bookNumCheckedCopies represents the number of checked out copies + * of a book. + * @param bookIdNum represents the book's ID number. + * @param bookCurrentHolders represents the current holders of a book. + */ + public Book(String bookTitle, String bookAuthor, int bookYear, + int bookNumCopies, int bookNumCheckedCopies, long bookIdNum, + ArrayList bookCurrentHolders) + { + title = bookTitle; + author = bookAuthor; + year = bookYear; + numCopies = bookNumCopies; + numCheckedCopies = bookNumCheckedCopies; + idNum = bookIdNum; + currentHolders = bookCurrentHolders; + } + + + + /** + * Book information containing book's title, author, released year, + * publisher, edition, Isbn, number of copies, number of checked copies, + * who all is has a hold on the book, book's Id number, price. + * @param bookTitle this represents the title of the book + * @param bookAuthor represents the author of the book. + * @param bookYear represents the year the book was published. + * @param bookPublisher represents the publisher of the book. + * @param bookPublisherDate represents date the book was published. + * @param bookEdition represents the edition of the book. + * @param bookIsbn represents the ISBN of the book. + * @param bookNumCopies represents the number of copies of a book within + * library. + * @param bookNumCheckedCopies represents the number of checked out copies + * of a book. + * @param bookIdNum represents the book's ID number. + * @param bookPrice represents the price of the book. + * @param bookCurrentHolders represents the current holders of a book. + */ + public Book(String bookTitle, String bookAuthor, int bookYear, + String bookPublisher, String bookPublisherDate, int bookEdition, + long bookIsbn, int bookNumCopies, int bookNumCheckedCopies, + long bookIdNum, double bookPrice, ArrayList bookCurrentHolders) + { + title = bookTitle; + author = bookAuthor; + year = bookYear; + publisher = bookPublisher; + publisherDate = bookPublisherDate; + edition = bookEdition; + isbn = bookIsbn; + numCopies = bookNumCopies; + numCheckedCopies = bookNumCheckedCopies; + idNum = bookIdNum; + price = bookPrice; + currentHolders = bookCurrentHolders; + } + + + + // *** Accessors. *** + + public String getTitle() + { + return title; + } + + public String getAuthor() + { + return author; + } + + public int getYear() + { + return year; + } + + public String getPublisher() + { + return publisher; + } + + public String getPublisherDate() + { + return publisherDate; + } + + public int getEdition() + { + return edition; + } + + public long getIsbn() + { + return edition; + } + + public int getNumCopies() + { + return numCopies; + } + + public int getNumCheckedCopies() + { + return numCheckedCopies; + } + + public long getIdNum() + { + return idNum; + } + + public double getPrice() + { + return price; + } + + public String getCurrentHolders() + { + String holders = ""; + if (numCheckedCopies > 0) + { + for (int i = 0; i < numCheckedCopies; i++) + { + holders += currentHolders.get(i); + if (i != numCheckedCopies - 1) + { + holders += ","; + } + } + } + else + { + holders += currentHolders.get(0); + } + return holders; + } + + + + // *** Other Methods. *** + + public void loanBook(long userIdNum) + { + currentHolders.add(userIdNum); + numCheckedCopies++; + } + + + public void returnBook(long userIdNum) + { + currentHolders.remove(userIdNum); + numCheckedCopies--; + } + + + + // *** Other Methods. *** + + /** + * Compares one book to another. + * @param otherBook represents other book + * @return result + */ + public boolean equalsBook(String otherBook) + { + boolean result = false; + + if (otherBook != null) + { + result = title.equalsIgnoreCase(otherBook); + } + + return result; + } + + + /** + * Prints the book and identifications. + * @return books identifications + */ + public String printLoan() + { + return "Title: " + title + "\nAuthor: " + author + "\nYear: " + year + + "\nPrice: " + price + "\nID Number: " + idNum + "\n"; + } + + public String printBook() + { + return "Title: " + title + "\nAuthor: " + author + "\nYear: " + year + + "\nNumber of Copies Available: " + (numCopies - numCheckedCopies) + + " out of " + numCopies + "\nID Number: " + idNum + "\n"; + } + + + public String printFullBook() + { + return "Title: " + title + "\nAuthor: " + author + "\nYear: " + year + + "\nPublisher: " + publisher + "\nPublication Date: " + + publisherDate + "\nEdition: " + edition + "\nISBN: " + isbn + + "\nNumber of Copies: " + numCopies + + "\nNumber of Copies Checked: " + numCheckedCopies + + "\nCurrent Holders: " + currentHolders + "\nID Number: " + idNum + + "\nPrice: " + price; + } + + + //It's returning an object for current holder instead of username. + /** + * Prints the string version of book and all it's identifications. + * @return book's identifications. + */ + @Override + public String toString() + { + String formattedIdNum = String.format("%06d", idNum); + String toString = "\n" + title + "," + author + "," + year + "," + + publisher + "," + publisherDate + "," + edition + "," + isbn + + "," + numCopies + "," + numCheckedCopies + "," + formattedIdNum + + "," + price; + + if (numCheckedCopies > 0) + { + for(Long userId : currentHolders) + { + toString += "," + userId; + } + } + + return toString; + } +} \ No newline at end of file diff --git a/LibraryManagementLibrary/src/main/java/librarymanagementsystem/GeneralUser.java b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/GeneralUser.java new file mode 100644 index 0000000..ea54dfd --- /dev/null +++ b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/GeneralUser.java @@ -0,0 +1,339 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package librarymanagementsystem; + +import java.util.ArrayList; + +/** + * Represents the basic privileges for every user (Patron, Librarian, Admin). + * @author Pawprint Studios + */ +public class GeneralUser +{ + public String username; + protected String password; + protected long userId; + protected String userType; + + + protected ArrayList currentLoans; + + private String patronName; + private String email; + private long phoneNumber; + private int numLoans; + + + + /** + * + */ + public GeneralUser() + { + // Ryan Note: I am not sure if I need to put anything in this default + // constructor. + } + + + /** + * General information of user, including username, password, userId, + * and user type (Patron, Librarian, Admin). + * @param personUsername represents the user's username. + * @param personPassword represents the user's password. + * @param personUserId represents the user's ID. + * @param personUserType represents the user's type. + */ + public GeneralUser(String personUsername, String personPassword, + long personUserId, String personUserType) + { + username = personUsername; + password = personPassword; + userId = personUserId; + userType = personUserType; + } + + + public GeneralUser(String personUsername, String personPassword, + long personUserId, String personUserType, String name, + String patronEmail, long patronPhoneNumber) + { + username = personUsername; + password = personPassword; + userId = personUserId; + userType = personUserType; + patronName = name; + email = patronEmail; + phoneNumber = patronPhoneNumber; + } + + + public GeneralUser(String personUsername, String personPassword, + long personUserId, String personUserType, String name, + String patronEmail, long patronPhoneNumber, int patronNumLoans) + { + username = personUsername; + password = personPassword; + userId = personUserId; + userType = personUserType; + patronName = name; + email = patronEmail; + phoneNumber = patronPhoneNumber; + numLoans = patronNumLoans; + } + + + public GeneralUser(String personUsername, String personPassword, + long personUserId, String personUserType, String name, + String patronEmail, long patronPhoneNumber, int patronNumLoans, + ArrayList patronCurrentLoans) + { + username = personUsername; + password = personPassword; + userId = personUserId; + userType = personUserType; + patronName = name; + email = patronEmail; + phoneNumber = patronPhoneNumber; + numLoans = patronNumLoans; + currentLoans = patronCurrentLoans; + } + + + + // *** Accessors. *** + + /** + * Gets username. + * @return username + */ + public String getUsername() + { + return this.username; + } + + + /** + * Gets password. + * @return password + */ + public String getPassword() + { + return this.password; + } + + + /** + * Gets user ID. + * @return userId + */ + public long getUserId() + { + return this.userId; + } + + + /** + * Gets the user type. + * @return userType + */ + public String getUserType() + { + return this.userType; + } + + + public ArrayList getCurrentLoans() + { + return this.currentLoans; + } + + + public String getPatronName() + { + return this.patronName; + } + + + public String getEmail() + { + return this.email; + } + + + public long getPhoneNumber() + { + return this.phoneNumber; + } + + + public int getNumLoans() + { + return this.numLoans; + } + + + + // *** Mutators. *** + + /** + * Sets username. + * @param nameUser represents the current username. + */ + public void setUsername(String nameUser) + { + this.username = nameUser; + } + + + /** + * Sets password + * @param wordPass represents if passwords is validated. + */ + public void setPassword(String wordPass) + { + this.password = wordPass; + } + + + public void setCurrentLoans(ArrayList patronsCurrentLoans) + { + currentLoans = patronsCurrentLoans; + } + + + public void setPatronName(String name) + { + patronName = name; + } + + + public void setEmail(String patronsEmail) + { + email = patronsEmail; + } + + + public void setPhoneNumber(long patronsPhoneNumber) + { + phoneNumber = patronsPhoneNumber; + } + + + public void setNumLoans(int patronsNumLoans) + { + numLoans = patronsNumLoans; + } + + + + // *** Book Stuff. *** + + public void loanBook(long bookIdNum) + { + currentLoans.add(bookIdNum); + numLoans++; + } + + + public void returnBook(long bookIdNum) + { + + currentLoans.remove(bookIdNum); + numLoans--; + } + + + + // *** Other Methods. *** + + /** + * Determines if users match. + * @param obj represents other username. + * @return result + */ + public boolean equals(Object obj) + { + boolean result = false; + + if (obj != null && getClass() == obj.getClass()) + { + GeneralUser other = (GeneralUser) obj; + result = username.equalsIgnoreCase(other.username) + && password.equals(other.password) + && userId == other.userId + && userType.equals(other.userType); + } + + return result; + + } + + + public String printUser() + { + String formattedIdNum = String.format("%06d", userId); + + String printUser = "Username: " + username + "\nID Number: " + + formattedIdNum + "\nUser Type: " + userType + + "\nFirst and Last Name: " + patronName; + + if (numLoans > 0) + { + printUser += "/n" + numLoans + " Current Loans: "; + for(Long bookId : currentLoans) + { + printUser += "," + bookId; + } + } + return printUser; + } + + + public String printFullUser() + { + String formattedIdNum = String.format("%06d", userId); + + String printUser = "Username: " + username + "\nPassword: " + password + + "\nID Number: " + formattedIdNum + "\nUser Type: " + userType + + "\nFirst and Last Name: " + patronName + "\nEmail Address: " + + email + "\nPhone Number: " + phoneNumber + "\nNumber of Loans: " + + numLoans; + + if (numLoans > 0) + { + printUser += "/nCurrent Loans: "; + for(Long bookId : currentLoans) + { + printUser += "," + bookId; + } + } + return printUser; + } + + + // Ryan Note: Update this later at some point + /** + * Prints the user's name and password. + * @return username and password + */ + @Override + public String toString() + { + String formattedIdNum = String.format("%06d", userId); + String toString = "\n" + username + "," + password + "," + formattedIdNum + + "," + userType + "," + patronName + "," + email + "," + + phoneNumber + "," + numLoans; + + if (numLoans > 0) + { + for(Long bookId : currentLoans) + { + toString += "," + bookId; + } + } + + return toString; + } +} diff --git a/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Library.java b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Library.java new file mode 100644 index 0000000..67f507b --- /dev/null +++ b/LibraryManagementLibrary/src/main/java/librarymanagementsystem/Library.java @@ -0,0 +1,557 @@ +package librarymanagementsystem; + +import java.io.*; +import java.util.*; + +/** + * Represents the library of a Library Management System. + * + * @author Pawprint Studios + */ +public class Library +{ + + public String libraryName; + public String libraryAddress; + public static double dailyLateFine = 0.25; + public int maxLoans = 30; + + public ArrayList libraryBooks = new ArrayList<>(); + protected ArrayList libraryUsers = new ArrayList<>(); + + protected long highestUserID; + protected long highestBookID; + + private String bookLocation; + private String userLocation; + + /** + * Represents the databases of books and users. + */ + public Library() + { + this("../resources/Books.csv", "../resources/Users.csv"); + } + + /** + * Represents where the databases are held. + * + * @param bookLocation represents the location of where the book database + * is being held. + * @param userLocation represents the location of where the book database + * is being held. + */ + public Library(String bookLocation, String userLocation) + { + this.bookLocation = bookLocation; + this.userLocation = userLocation; + + updateBooksArray(); + updateUsersArray(); + + highestUserID = libraryUsers.size() + 1; + highestBookID = libraryBooks.size() + 1; + } + + + + // *** Accessors. *** + + /** + * Get the library name. + * + * @return libraryname + */ + public String getLibraryName() + { + return libraryName; + } + + /** + * Get the library address. + * + * @return libraryAddress + */ + public String getLibraryAddress() + { + return libraryAddress; + } + + /** + * Get the daily late fine. + * + * @return dailyLateFine + */ + public double getDailyLateFine() + { + return dailyLateFine; + } + + /** + * Get the Highest User ID. + * + * @return highestUserID + */ + public long getHighestUserID() + { + return highestUserID; + } + + public long getHighestBookID() + { + return highestBookID; + } + + /** + * Get all the library books within the library. + * + * @return printList + */ + public String getLibraryBooks() + { + String printList = ""; + for (Book printBook : libraryBooks) { + printList += printBook; + } + return printList; + } + + /** + * Get all the users within the Library. + * + * @return printList + */ + public String getLibraryUsers() + { + String printList = ""; + for (GeneralUser printUser : libraryUsers) { + printList += printUser; + } + return printList; + } + + + + // *** Mutators. *** + + /** + * Set name of the Library management system. + * + * @param name represents the name of the library. + */ + protected void setLibraryName(String name) + { + libraryName = name; + } + + /** + * Set the address for the Library. + * + * @param address represents the address of the library. + */ + protected void setLibraryAddress(String address) + { + libraryAddress = address; + } + + /** + * Set how much a user will get fined for each day a book isn't returned. + * + * @param dailyFine represents the daily late fine for a user. + */ + protected void setDailyLateFine(Double dailyFine) + { + dailyLateFine = dailyFine; + } + + /** + * Update the database for the book and adds books. + */ + public void updateBooksArray() + { + libraryBooks.clear(); + + try (Scanner bookScan = new Scanner(new File(bookLocation))) + { + bookScan.nextLine(); + while (bookScan.hasNextLine()) + { + String[] bookParts = bookScan.nextLine().trim().split(","); + + String title = bookParts[0]; + String author = bookParts[1]; + int year = Integer.parseInt(bookParts[2]); + String publisher = bookParts[3]; + String publisherDate = bookParts[4]; + int edition = Integer.parseInt(bookParts[5]); + long isbn = Long.parseLong(bookParts[6]); + int numCopies = Integer.parseInt(bookParts[7]); + int numCheckedCopies = Integer.parseInt(bookParts[8]); + + long idNum = Long.parseLong(bookParts[9]); + double price = Double.parseDouble(bookParts[10]); + + ArrayList currentHolders = new ArrayList<>(numCopies); + for (int i = 11; i < 10 + numCheckedCopies; i++) + { + currentHolders.add(Long.parseLong(bookParts[i])); + } + + libraryBooks.add(new Book(title, author, year, publisher, + publisherDate, edition, isbn, numCopies, numCheckedCopies, + idNum, price, currentHolders)); + } + } + catch (FileNotFoundException e) + { + System.out.println("Could not find file: " + bookLocation); + System.exit(1); + } + } + + /** + * Update the user database and adds users. + */ + public void updateUsersArray() + { + libraryUsers.clear(); + + try (Scanner fileScan = new Scanner(new File(userLocation))) + { + fileScan.nextLine(); + while (fileScan.hasNextLine()) + { + String[] userParts = fileScan.nextLine().trim().split(","); + + String username = userParts[0]; + String password = userParts[1]; + long userId = Long.parseLong(userParts[2]); + String userType = userParts[3]; + + String name = userParts[4]; + String email = userParts[5]; + long phoneNumber = Long.parseLong(userParts[6]); + + if (userType.equals("Patron")) + { + + int numLoans = Integer.parseInt(userParts[7]); + + ArrayList currentLoans = new ArrayList(maxLoans); + for (int i = 8; i <= 7 + numLoans; i++) { + currentLoans.add(Long.parseLong(userParts[i])); + } + + libraryUsers.add(new GeneralUser(username, password, userId, + userType, name, email, phoneNumber, numLoans, + currentLoans )); + } + else + { + libraryUsers.add(new GeneralUser(username, password, userId, + userType, name, email, phoneNumber)); + + } + } + } + catch (FileNotFoundException e) + { + System.out.println("Could not find file: " + userLocation); + System.exit(2); + // Ryan Note: I still don't know what to put in the catch blocks. + } + } + + public void updateBooksFile(String location) + { + try (PrintWriter outFile = new PrintWriter( + new BufferedWriter(new FileWriter(location)))) + { + outFile.print("Title,Author,Year,Publisher,PublishDate,ISBN," + + "NumCopies,NumChecked,IdNum,Price,CurrentHolders"); + for (Book libBook : libraryBooks) + { + outFile.print(libBook.toString()); + } + } + catch (IOException e) + { + System.out.println(e); + } + } + + public void updateUsersFile(String location) + { + try (PrintWriter outFile = new PrintWriter( + new BufferedWriter(new FileWriter(location)))) + { + outFile.print("Username,Password,UserID,UserType,Name,Email," + + "PhoneNumber,NumLoans,CurrentLoans"); + for (GeneralUser libUser : libraryUsers) + { + outFile.print(libUser.toString()); + } + } + catch (IOException e) + { + System.out.println(e); + } + } + + + // *** Abilities that all users can access. *** + + /** + * Verify the login of a user. + * + * @param username represents the username of a user. + * @param password represents the password of a user. + * @return loginUser + */ + public GeneralUser verifyLogin(String username, String password) + { + GeneralUser loginUser = null; + + for (GeneralUser arrayUser : libraryUsers) + { + if (username.equalsIgnoreCase(arrayUser.getUsername()) + && password.equals(arrayUser.getPassword())) + { + loginUser = arrayUser; + } + } + + return loginUser; + } + + /** + * Allow guest to apply for Membership of a Patron + * + * @param username represents the username given by user. + * @param password represents the password given by user. + * @return successfulAplication represents if user was created + */ + public boolean applyMembership(String username, String password, + String name, String email, long phoneNumber) + { + boolean successfulAplication = false; + if (findUser(username) == null) + { + libraryUsers.add(new GeneralUser(username, password, highestUserID, + "Patron", name, email, phoneNumber, 0, + new ArrayList<>(maxLoans))); + updateUsersFile(userLocation); + highestUserID++; + successfulAplication = true; + } + else + { + System.out.println("A user with that username already exists!"); + } + return successfulAplication; + } + + /** + * Search the library for a book/media. + * + * @param bookSearch represents the name of the book being searched. + */ + public ArrayList searchLibrary(String bookSearch) + { + ArrayList foundBooks = new ArrayList<>(); + + String searchBook = bookSearch.toLowerCase(); + for (Book arrayBook : libraryBooks) + { + if (arrayBook.getTitle().toLowerCase().contains(searchBook) + || arrayBook.getAuthor().toLowerCase().contains(searchBook)) + { + foundBooks.add(arrayBook); + } + } + + return foundBooks; + } + + + + // *** Abilities that Patrons can access. *** + + public Book findBook(long libIdNum) + { + for (Book arrayBook : libraryBooks) + { + if (arrayBook.getIdNum() == libIdNum) + { + return arrayBook; + } + } + return null; + } + + public Book findBook(String bookTitle) + { + for (Book arrayBook : libraryBooks) + { + if (arrayBook.getTitle().contains(bookTitle)) + { + return arrayBook; + } + } + return null; + } + + public String borrowBook(long userIdNum, Book bookSearch) + { + String successfulBorrow = ""; + GeneralUser user = findUser(userIdNum); + long bookIdNum = bookSearch.getIdNum(); + + if (!user.currentLoans.contains(bookIdNum)) + { + if (user.getNumLoans() < maxLoans) + { + user.loanBook(bookIdNum); + + bookSearch.loanBook(userIdNum); + + updateBooksFile(bookLocation); + updateUsersFile(userLocation); + + successfulBorrow = "Borrowed"; + } + else + { + successfulBorrow = "You already have " + maxLoans + " books."; + } + } + else + { + successfulBorrow = "You already have " + bookSearch.getTitle() + + " checked out."; + } + return successfulBorrow; + } + + public ArrayList viewLoans(long userIdNum) + { + ArrayList loanedBooks = new ArrayList<>(); + for (long lo : findUser(userIdNum).getCurrentLoans()) + { + loanedBooks.add(findBook(lo)); + } + return loanedBooks; + } + + public String returnBook(long userIdNum, long bookIdNum) + { + String successfulReturn = ""; + GeneralUser user = findUser(userIdNum); + if (user.currentLoans.contains(bookIdNum)) + { + user.returnBook(bookIdNum); + findBook(bookIdNum).returnBook(userIdNum); + updateBooksFile(bookLocation); + updateUsersFile(userLocation); + successfulReturn = "Returned"; + } + else + { + successfulReturn = "You don't have a copy of " + findBook(bookIdNum) + .getTitle() + " to return."; + } + return successfulReturn; + } + + + + + // *** Abilities that Librarians can access. *** + + /** + * Add book into array list. + * + * @param addingBook represents the book being added to the array list. + */ + public void addBook(Book addingBook) + { + libraryBooks.add(addingBook); + updateBooksFile(bookLocation); + highestBookID++; + } + + /** + * Retire/remove book from array list. + * + * @param retireIdNum represents the ID number of the book being retired. + */ + public void retireBook(long retireIdNum) + { + libraryBooks.remove(findBook(retireIdNum)); + updateBooksFile(bookLocation); + } + + /** + * Find user from user array list. + * + * @param userSearch represents what the user searched. + * @return + */ + public GeneralUser findUser(String userSearch) + { + + for (GeneralUser arrayUser : libraryUsers) + { + if (userSearch.equalsIgnoreCase(arrayUser.getUsername())) + { + return arrayUser; + } + } + + return null; + } + + /** + * Find user from user array list. + * + * @param userIdSearch represents what the user searched. + * @return GeneralUser + */ + public GeneralUser findUser(long userIdSearch) + { + + for (GeneralUser arrayUser : libraryUsers) + { + if (userIdSearch == arrayUser.getUserId()) + { + return arrayUser; + } + } + + return null; + } + + // *** Abilities that Admins can access. *** + + /** + * Remove users. + * + * @param User + */ + public void removeUser(long userId) + { + libraryUsers.remove(findUser(userId)); + updateUsersFile(userLocation); + } + + /** + * Backup database. + * @param backupLocation + */ + public void backupDatabase() + { + updateBooksFile("../backups/Books.csv"); + updateUsersFile("../backups/Users.csv"); + } + +} diff --git a/LibraryManagementLibrary/src/test/java/librarymanagementsystem/BookTest.java b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/BookTest.java new file mode 100644 index 0000000..b9f7cc3 --- /dev/null +++ b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/BookTest.java @@ -0,0 +1,152 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/JUnit4TestClass.java to edit this template + */ +package librarymanagementsystem; + +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import java.util.ArrayList; + +/** + * Represents testing for the Book class. + * @author Pawprint Studios + */ +public class BookTest +{ + private Book testBook; + + public BookTest() + { + ArrayList holders = new ArrayList(1); + testBook = new Book("Title","Author",2021,"Publisher","April 1st", + 1,22222,1,1,121212,2.1,holders); + } + + @BeforeClass + public static void setUpClass() + { + + } + + /** + * Test of getTitle method, of class Book. + */ + @Test + public void testGetTitle() + { + String testTitle = "Title"; + + assertEquals(testTitle, testBook.getTitle()); + } + + /** + * Test of getAuthor method, of class Book. + */ + @Test + public void testGetAuthor() + { + String testAuthor = "Author"; + + assertEquals(testAuthor, testBook.getAuthor()); + } + + /** + * Test of getYear method, of class Book. + */ + @Test + public void testGetYear() + { + int testYear = 2021; + + assertEquals(testYear, testBook.getYear()); + } + + /** + * Test of getPublisher method, of class Book. + */ + @Test + public void testGetPublisher() + { + String testPublisher = "Publisher"; + + assertEquals(testPublisher, testBook.getPublisher()); + } + + /** + * Test of getPublisherDate method, of class Book. + */ + @Test + public void testGetPublisherDate() + { + String testPublisherDate = "April 1st"; + + assertEquals(testPublisherDate, testBook.getPublisherDate()); + } + /** + * Test of getEdition method, of class Book. + */ + @Test + public void testGetEdition() + { + int testEdition = 1; + + assertEquals(testEdition, testBook.getEdition()); + } + + /** + * Test of getIsbn method, of class Book. + */ + @Test + public void testGetIsbn() + { + long testIsbn = 1; + + assertEquals(testIsbn, testBook.getIsbn()); + } + + /** + * Test of getNumCopies method, of class Book. + */ + @Test + public void testGetNumCopies() + { + int testNumCopies = 1; + + assertEquals(testNumCopies, testBook.getNumCopies()); + } + + /** + * Test of getNumCheckedCopies method, of class Book. + */ + @Test + public void testGetNumCheckedCopies() + { + int testNumCheckedCopies = 1; + + assertEquals(testNumCheckedCopies, testBook.getNumCheckedCopies()); + } + + /** + * Test of getIdNum method, of class Book. + */ + @Test + public void testGetIdNum() + { + long testIdNum = 121212; + + assertEquals(testIdNum, testBook.getIdNum()); + } + + /** + * Test of getPrice method, of class Book. + */ + @Test + public void testGetPrice() + { + double testPrice = 2.1; + + assertEquals(testPrice, testBook.getPrice(),2.1); + } +} \ No newline at end of file diff --git a/LibraryManagementLibrary/src/test/java/librarymanagementsystem/GeneralUserTest.java b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/GeneralUserTest.java new file mode 100644 index 0000000..a81fbfa --- /dev/null +++ b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/GeneralUserTest.java @@ -0,0 +1,187 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/UnitTests/JUnit4TestClass.java to edit this template + */ +package librarymanagementsystem; + +import java.util.ArrayList; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * Represents testing for the GeneralUser class. + * @author Pawprint Studios + */ +public class GeneralUserTest +{ + private GeneralUser testGeneralUser; + + public GeneralUserTest() + { + testGeneralUser = new GeneralUser("pat", "pat", + 000007, "Patron", "John Johnson", "JohnJohn@John.com", + 5558976, 0); + } + + @BeforeClass + public static void setUpClass() + { + + } + + /** + * Test of getUsername method, of class GeneralUser. + */ + @Test + public void testGetUsername() + { + String testUsername = "pat"; + + assertEquals(testUsername, testGeneralUser.getUsername()); + } + + /** + * Test of getPassword method, of class GeneralUser. + */ + @Test + public void testGetPassword() + { + String testPassword = "pat"; + + assertEquals(testPassword, testGeneralUser.getPassword()); + } + + /** + * Test of getUserId method, of class GeneralUser. + */ + @Test + public void testGetUserId() + { + long testUserId = 000007; + + assertEquals(testUserId, testGeneralUser.getUserId()); + } + + /** + * Test of getUserType method, of class GeneralUser. + */ + @Test + public void testGetUserType() + { + String testUserType = "Patron"; + + assertEquals(testUserType, testGeneralUser.getUserType()); + } + + /** + * Test of getPatronName method, of class GeneralUser. + */ + @Test + public void testGetPatronName() + { + String testPatronName = "John Johnson"; + + assertEquals(testPatronName, testGeneralUser.getPatronName()); + } + + /** + * Test of getEmail method, of class GeneralUser. + */ + @Test + public void testGetEmail() + { + String testEmail = "JohnJohn@John.com"; + + assertEquals(testEmail, testGeneralUser.getEmail()); + } + + /** + * Test of getPhoneNumber method, of class GeneralUser. + */ + @Test + public void testGetPhoneNumber() + { + long testPhoneNumber = 5558976; + + assertEquals(testPhoneNumber, testGeneralUser.getPhoneNumber()); + } + + /** + * Test of setUsername method, of class GeneralUser. + */ + @Test + public void testSetUsername() + { + String testUsername = "Lebron"; + + testGeneralUser.setUsername("Lebron"); + + assertEquals(testUsername, testGeneralUser.getUsername()); + } + + /** + * Test of setPassword method, of class GeneralUser. + */ + @Test + public void testSetPassword() + { + String testPassword = "James"; + + testGeneralUser.setPassword("James"); + + assertEquals(testPassword, testGeneralUser.getPassword()); + } + + /** + * Test of setPatronName method, of class GeneralUser. + */ + @Test + public void testSetPatronName() + { + String testPatronName = "Harry Potter"; + + testGeneralUser.setPatronName("Harry Potter"); + + assertEquals(testPatronName, testGeneralUser.getPatronName()); + } + + /** + * Test of setEmail method, of class GeneralUser. + */ + @Test + public void testSetEmail() + { + String testEmail = "dogcat@email.net"; + + testGeneralUser.setEmail("dogcat@email.net"); + + assertEquals(testEmail, testGeneralUser.getEmail()); + } + + /** + * Test of setPhoneNumber method, of class GeneralUser. + */ + @Test + public void testSetPhoneNumber() + { + long testPhoneNumber = 888333; + + testGeneralUser.setPhoneNumber(888333); + + assertEquals(testPhoneNumber, testGeneralUser.getPhoneNumber()); + } + + /** + * Test of setNumLoans method, of class GeneralUser. + */ + @Test + public void testSetNumLoans() + { + int testNumLoans = 3; + + testGeneralUser.setNumLoans(3); + + assertEquals(testNumLoans, testGeneralUser.getNumLoans()); + } +} \ No newline at end of file diff --git a/LibraryManagementLibrary/src/test/java/librarymanagementsystem/LibraryTest.java b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/LibraryTest.java new file mode 100644 index 0000000..8fdc884 --- /dev/null +++ b/LibraryManagementLibrary/src/test/java/librarymanagementsystem/LibraryTest.java @@ -0,0 +1,88 @@ +package librarymanagementsystem; + +import java.io.*; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author Pawprint Studios + */ +public class LibraryTest +{ + private Library testLibrary; + + public LibraryTest() + { + /* + PrintWriter bookWriter = new PrintWriter(new BufferedWriter(new FileWriter("testBooks.csv"))); + bookWriter.println("Title,Author,Year,Publisher,PublishDate,ISBN,NumCopies,NumChecked,IdNum,Price,CurrentHolders"); + bookWriter.println("Ahsoka,E. K. Johnston,2016,Disney–Lucasfilm Press,October 11,1,1484705661,3,2,243578,15.99,10000,76543"); + bookWriter.println("Dark Disciple,Christie Golden,2015,Del Rey,July 7,1,9780345511539,2,0,849332,9.99, "); + bookWriter.println("Star Wars: Episode III Revenge of the Sith,Matthew Stover,2005,Del Rey,April 7,2005,0345428838,3,0,65365,12.99, "); + + testLibrary = new Library("testBooks.csv", "testUsers.csv"); + */ + testLibrary = new Library(); + } + + @Test + public void testSetLibraryName() + { + String testLibraryName = "DingleFloopyNoop"; + + testLibrary.setLibraryName("DingleFloopyNoop"); + + assertEquals(testLibraryName, testLibrary.getLibraryName()); + } + + @Test + public void testSetLibraryAddress() + { + String testLibraryAddress = "2000 C Wourt Road"; + + testLibrary.setLibraryAddress("2000 C Wourt Road"); + + assertEquals(testLibraryAddress, testLibrary.getLibraryAddress()); + } + + + @Test + public void testSetDailyLateFine() + { + double testDailyLateFine = 4.2; + + testLibrary.setDailyLateFine(4.2); + + assertEquals(testDailyLateFine, testLibrary.getDailyLateFine(), 0); + } + + + +// Library verifyLibrary = new Library("Books.csv", "Users.csv"); + + @Test + public void testVerifyLogin() + { + GeneralUser testUser = new GeneralUser("RyanTheGreyJedi", + "HanShotFirst", 2, "Librarian"); + + GeneralUser verifyUser = testLibrary.verifyLogin("RyanTheGreyJedi", + "HanShotFirst"); + + assertEquals(testUser, verifyUser); + } + + @Test + public void testSearchLibrary() + { + /* + Book testBook = new Book("________________________"); + + Book searchBook = testLibrary.searchLibrary("________________________"); + + assertEquals(testBook, searchBook); + */ + } + +} diff --git a/LibraryManagementLibrary/src/test/java/librarymanagementsystemt/AdminTest.java b/LibraryManagementLibrary/src/test/java/librarymanagementsystemt/AdminTest.java new file mode 100644 index 0000000..9baada3 --- /dev/null +++ b/LibraryManagementLibrary/src/test/java/librarymanagementsystemt/AdminTest.java @@ -0,0 +1,31 @@ +package librarymanagementsystemt; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.*; + +/** + * + * @author qrow + */ +public class AdminTest { + + public AdminTest() + { + String logName = "log"; + } + + @Before + public void setUp() + { + } + + @Test + public void testViewLogs() + { + + } +} diff --git a/LibraryManagementServer/pom.xml b/LibraryManagementServer/pom.xml new file mode 100644 index 0000000..2b579f0 --- /dev/null +++ b/LibraryManagementServer/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.pawprintstudios + LibraryManagementSystem + 1.0-SNAPSHOT + + + + + maven-assembly-plugin + + + package + + single + + + + + + + true + librarymanagementsystem.Server + + + + jar-with-dependencies + + + + + + LibraryManagementServer + jar + + UTF-8 + 17 + 17 + + + + com.pawprintstudios + LibraryManagementLibrary + 1.0-SNAPSHOT + compile + + + diff --git a/LibraryManagementServer/src/main/java/librarymanagementsystem/Server.java b/LibraryManagementServer/src/main/java/librarymanagementsystem/Server.java new file mode 100644 index 0000000..59435eb --- /dev/null +++ b/LibraryManagementServer/src/main/java/librarymanagementsystem/Server.java @@ -0,0 +1,230 @@ +package librarymanagementsystem; + +import java.io.*; +import java.net.Socket; +import java.net.ServerSocket; +import java.util.ArrayList; +import java.util.Scanner; + +/** + * Represents the server. + * + * @author Pawprint Studios + */ +public class Server +{ + public static void main(String[] args) + { + final int PORT = 51042; + System.out.println("***Library Server***"); + Library serverLibrary = new Library(); + + try + { + ServerSocket welcomeSocket = new ServerSocket(PORT); + + System.out.printf("Listening on port %d...\n", PORT); + + while (true) + { + Socket connectionSocket = welcomeSocket.accept(); + System.out.println("Client connected..."); + + Scanner inFromClient = new Scanner( + connectionSocket.getInputStream()); + PrintWriter outToClient = new PrintWriter( + connectionSocket.getOutputStream()); + + String clientRequest = inFromClient.nextLine(); + + // James: Here we would split up the input to do the command with the data. + System.out.println(clientRequest); + // Parse clientRequest + String[] userInput = clientRequest.split(", "); + + + switch (userInput[0]) + { + case "search": + ArrayList matchedBook = serverLibrary.searchLibrary( + userInput[1]); + System.out.println("Server is searching for: " + + userInput[1]); + if (!matchedBook.isEmpty()) + { + System.out.println("Found " + matchedBook.size() + + " Books"); + outToClient.println("Found"); + outToClient.println(matchedBook.size()); + for (Book searchedBook : matchedBook) + { + System.out.println("Found: " + searchedBook.printBook()); + outToClient.print(searchedBook.printBook()); + } + + } + else + { + System.out.println("Could not find: " + userInput[1]); + outToClient.println("Could not find any matching " + + "titles for the query: " + userInput[1]); + + } + break; + + case "apply": + String desiredUsername = userInput[1]; + String desiredPassword = userInput[2]; + String desiredName = userInput[3]; + String desiredEmail = userInput[4]; + long desiredPhoneNumber = Long.parseLong(userInput[5]); + boolean creation = serverLibrary.applyMembership( + desiredUsername, desiredPassword, desiredName, + desiredEmail, desiredPhoneNumber); + if (creation) + { + outToClient.println("Application succeeded!"); + } + else + { + outToClient.println("A user with that name " + + "already exists.\nPlease try again " + + "with annother name."); + } + break; + + case "login": + String username = userInput[1]; + String password = userInput[2]; + System.out.println("Verify User: " + + username ); + GeneralUser user = serverLibrary.verifyLogin( + username, password); + if (user != null) + { + outToClient.println("Successfuly Logged in!"); + outToClient.println(user.getUserType()); + outToClient.println(user.getPatronName()); + outToClient.println(user.getUserId()); + } + else + { + outToClient.println("Invalid username or password."); + + } + break; + + case "borrow": + Book searchBook = serverLibrary.findBook( + Long.parseLong(userInput[1])); + long userId = Long.parseLong(userInput[2]); + String borrowSuccess = serverLibrary + .borrowBook(userId, searchBook); + + if (borrowSuccess.equals("Borrowed")) + { + System.out.println(searchBook.getTitle() + + " was added to your shelf!"); + outToClient.println(searchBook.getTitle() + + " was added to your shelf!"); + } + else + { + System.out.println(borrowSuccess); + outToClient.println(borrowSuccess); + } + break; + + case "loans": + userId = Long.parseLong(userInput[1]); + outToClient.println("Your Shelf:"); + + outToClient.println(serverLibrary.findUser(userId) + .getNumLoans()); + + ArrayList loanedBooks = serverLibrary.viewLoans( + userId); + for (Book loan : loanedBooks) + { + System.out.print(loan.printLoan()); + outToClient.print(loan.printLoan()); + } + break; + + case "return": + long bookId = Long.parseLong(userInput[1]); + userId = Long.parseLong(userInput[2]); + String returnSuccess = serverLibrary + .returnBook(userId, bookId); + Book returnBook = serverLibrary.findBook(bookId); + + if (returnSuccess.equals("Returned")) + { + System.out.println(returnBook.getTitle() + + " was removed from your shelf!"); + outToClient.println(returnBook.getTitle() + + " was removed from your shelf!"); + } + else + { + System.out.println(returnSuccess); + outToClient.println(returnSuccess); + } + break; + + case "addBook": + String title = userInput[1]; + String author = userInput[2]; + int year = Integer.parseInt(userInput[3]); + String publisher = userInput[4]; + String publisherDate = userInput[5]; + int edition = Integer.parseInt(userInput[6]); + long isbn = Long.parseLong(userInput[7]); + int numCopies = Integer.parseInt(userInput[8]); + double price = Double.parseDouble(userInput[9]); + ArrayList currentHolders = new ArrayList<>(1); + + serverLibrary.addBook(new Book(title, author, year, + publisher, publisherDate, edition, isbn, numCopies, + 0, serverLibrary.getHighestBookID(), price, + currentHolders)); + + outToClient.println(userInput[1] + + " was added to Library!"); + break; + case "retireBook": + long retiringBook = Long.parseLong(userInput[1]); + serverLibrary.retireBook(retiringBook); + outToClient.println("Successfully retired book!"); + break; + + case "backup": + serverLibrary.backupDatabase(); + outToClient.println("Successfully backed up database!"); + break; + + case "removeUser": + long removedUser = Long.parseLong(userInput[1]); + String usersName = serverLibrary.findUser(removedUser) + .getPatronName(); + serverLibrary.removeUser(removedUser); + outToClient.println(usersName + + " was removed form Library."); + break; + + default: + break; + } + + // Send info to client + + outToClient.flush(); + connectionSocket.close(); + } + + } catch (IOException e) + { + System.err.println(e); + } + } +} diff --git a/LibraryManagementServer/src/main/java/librarymanagementsystem/ServerTask.java b/LibraryManagementServer/src/main/java/librarymanagementsystem/ServerTask.java new file mode 100644 index 0000000..6eaec67 --- /dev/null +++ b/LibraryManagementServer/src/main/java/librarymanagementsystem/ServerTask.java @@ -0,0 +1,52 @@ +package librarymanagementsystem; + +import java.io.*; +import java.net.Socket; +import java.net.InetAddress; +import java.util.Scanner; + + +/** + * Represents a ServerTask. +* @author James + */ + +public class ServerTask implements Runnable +{ + private Socket socket; + private int clientNumber; + + public ServerTask(int clientNumber, Socket socket) + { + this.socket = socket; + this.clientNumber = clientNumber; + } + + public void run() + { + try + { + InetAddress clientAddress = socket.getInetAddress(); + System.out.println("Client " + clientNumber + + "'s IP address is " + + clientAddress.getHostAddress()); + + PrintWriter outToClient = new PrintWriter( + socket.getOutputStream()); + Scanner inFromClient = new Scanner( + socket.getInputStream()); + System.out.println("Are we setting up da pipes?"); + + String tmpMsg = "The ServerTask does only this currently."; + outToClient.println(tmpMsg); + outToClient.flush(); + socket.close(); + + } + catch (IOException e) + { + System.err.println(e); + } + } +} + diff --git a/doc/HOW_TO_COMPILE.md b/doc/HOW_TO_COMPILE.md new file mode 100644 index 0000000..8882219 --- /dev/null +++ b/doc/HOW_TO_COMPILE.md @@ -0,0 +1,14 @@ +``` +# When you make changes to the library +$ cd LibraryManagementLibrary +$ mvn clean install + +# Any time you make changes (in the top-level project directory) +$ mvn clean package + +# Run the server +$ java -jar LibraryManagementServer/target/LibraryManagementServer-1.0-SNAPSHOT.jar + +# Run the client +$ java -jar LibraryManagementClient/target/LibraryManagementClient-1.0-SNAPSHOT.jar +``` diff --git a/doc/tempformatingfile.txt b/doc/tempformatingfile.txt new file mode 100644 index 0000000..681abf8 --- /dev/null +++ b/doc/tempformatingfile.txt @@ -0,0 +1,2 @@ +_Title_, _Author_, _Year_, _Publisher_, _PublishDate_, _ISBN_, _NumCopies_, _NumChecked_, _IdNum_, _Price_, _CurrentHolders_ + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..dbfb38a --- /dev/null +++ b/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + com.pawprintstudios + LibraryManagementSystem + 1.0-SNAPSHOT + pom + + LibraryManagementLibrary + LibraryManagementClient + LibraryManagementServer + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + + org.apache.maven.surefire + surefire-junit4 + 2.22.0 + + + + + **/*.java + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.3.2 + + private + true + + + + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.22.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.3.2 + + ${basedir}/src/main/javadoc/stylesheet.css + public + + + + + + UTF-8 + 17 + 17 + + + + commons-io + commons-io + 2.11.0 + + + junit + junit + 4.12 + test + + + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2c15179 --- /dev/null +++ b/readme.md @@ -0,0 +1,285 @@ +# Library Managemnet System +--- + +## Todo +- [X] Uml Diagrams +- [X] Redo UML Diagrams (possibly with mermaid) +- [ ] Set up database +- [X] decide how to do ui +---- + +## UML +###### Class + + + +```mermaid +%%{ "theme": "default", "flowchart": { "curve": "linear" } }%% +classDiagram + direction TB + Library <-- Book + Library <-- GeneralUser +class Library{ + +libraryName : String + +libraryAddress : String + +dailyLateFine : double + +maxLoans : int + +libraryBooks : ArrayList~Book~ + +libraryUsers : ArrayList~User~ + + #highestUserID : long + #highestBookID : long + + +Library() + +getLibraryName() String + +getLibraryAddress() String + +getDailyLateFine() double + +getHigestUserID() long + +getHigestBookID() long + +getLibraryBooks() String + +getLibraryUsers() String + + #setLibraryName() + #setLibaryAddress() + #setDailyLateFine() + +updateBooksArray() + +updateUsersArray() + +updateBooksFile() + +updateUsersFile() + + + +verifyLogin() GeneralUser + +applyMambership() boolean + +searchLibrary() ArrayList~Book~ + +findBook() Book + +borrowBook() String + +viewLoans() ArrayList~Book~ + +returnBook() String + +addBook() + +retireBook() + +viewLogs() + +removeUser() + +backupDatabase() +} +class Book{ + +title : String + +author : String + +publisher : String + +publisher : date + +year : int + +edition : int + +numCopies : int + +numCheckedCopies : int + -idnum : long + -price : double + -currentHolders : ArrayList~long~ + + +Book() + + +getTitle() String + +getAuthor() String + +getYear() int + +getPublisher() String + +getEdition() int + +getIsbn() int + +getNumCopies() int + +getIdNum() long + +getPrice() double + +getPublisherDate() String + +getCurrentHolders() String + +getNumCheckedCopies() int + + +loanBook(long userIdNum) void + +returnBook(long userIdNum) void + +equalsBook(String otherBook) boolean + + +printBook() String + +printFullBook() String + +toString() String + } + class GeneralUser{ + +username : String + #password : String + #userId : long + #userType : String + #currentLoans : ArrayList~Long~ + -patronNames : String + -email : String + -phoneNumber long + -numLoans : int + + + +GeneralUser() + +getUsername() String + +getPassword() String + +getUserId() long + +getUserType() String + +getCurrentLoans() ArrayList~Long~ + +getPatronName() String + +getEmail() String + +getPhoneNumber() long + +getNumLoans() int + + +setUserName() + +setPassword() + +setCurrentLoans() + +setPatronName() + +setEmail() + +setPhoneNumber() + +setNumLoans() + +loanBook() + +returnBook() + +equals() boolean + +printUser() String + +printFullUser() String + +toString() String +} +``` +###### Sequence +```mermaid +sequenceDiagram + participant u as User + participant c as Client + participant s as Server + participant d as (Library) Database + note over u,d : basic client server proccess + u->>c: get command and data + c->>s:Establish Connection and send command + activate s + s-->d:Use database if neeeded + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + note over u,d : Verify Login + u->>c:get user login info + c->>s:Send login info + activate s + s-->d:Use database to verify credentials + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Apply for membership + u->>c:get user data + c->>s:Send command and data to server + activate s + s-->d: add user to database + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Search Library + u->>c:get users book query + c->>s:Send command and book data + activate s + s-->d:Use database to attempt to find book + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Borrow Book + u->>c:get users book query + c->>s:Send command, user id, and book data + activate s + s-->d:Use database to attempt to find and borrow book + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : View Loans + u->>c:get users command + c->>s:Send command and user id + activate s + s-->d:Use database to look up users loans + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Return Book + u->>c:get users book + c->>s:Send command, user id, and book data + activate s + s-->d:Use database to attempt to find and borrow book + s->>c:Receive info and send response + c->>s:close connection + deactivate s + c->>u:Display results + + note over u,d : Add Book + u->>c:get book information + c->>s:Send command and book data + activate s + s-->d:Add book to database + s->>c:Send response upon creation + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Retire Book + u->>c:get users book to remove + c->>s:Send command and book id + activate s + s-->d:Remove book from database + s->>c:Send response upon book removal + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Backup Database + c->>s:Send command and location + activate s + s-->d:Create database backup + s->>c:Send response upon creation + c->>s:close connection + deactivate s + c->>u:Display results + + + note over u,d : Remove User + u->>c:get users id to remove + c->>s:Send command and user id for removal + activate s + s-->d:Delete User that matches id + s->>c:Send response upon users deletion + c->>s:close connection + deactivate s + c->>u:Display results +``` +##### Use case + +![Use Case](https://user-images.githubusercontent.com/74808546/164376684-c1cd8e5d-acba-45b6-8799-4de8109bb5bf.svg) + +## Old Diagrams +1. Old UML Diagrams are in the drop-down to minimize the space they take up but are left to allow for review +
+ Click to reveal + + + +![LMS Class Diagram](https://user-images.githubusercontent.com/74808546/159811086-966a799e-94a7-4f6f-9969-3e6eb8679868.svg) + +![LMS Case Diagram](https://user-images.githubusercontent.com/74808546/159811113-f2a9b001-bae1-4b5e-8688-8c8fa85b465c.svg) + +![Guest](https://user-images.githubusercontent.com/74808546/159811184-8f13fdfc-9e27-4278-a399-2665ab852f2a.svg) + +![Patron](https://user-images.githubusercontent.com/74808546/159811165-dc9ec0ec-36bb-4c0e-8ec0-e08f4be714d5.svg) + +![Librarian](https://user-images.githubusercontent.com/74808546/159811039-c7d9512d-c9a7-4c7c-9278-a92536cccab7.svg) + +![Admin](https://user-images.githubusercontent.com/74808546/159811206-483a2260-88a0-484a-a9e9-c19a4aa9b2eb.svg) + +
diff --git a/resources/Books.csv b/resources/Books.csv new file mode 100644 index 0000000..1086867 --- /dev/null +++ b/resources/Books.csv @@ -0,0 +1,5 @@ +Title,Author,Year,Publisher,PublishDate,ISBN,NumCopies,NumChecked,IdNum,Price,CurrentHolders +Ahsoka,E. K. Johnston,2016,Disney-Lucasfilm Press,October 11,1,1484705661,3,1,000001,15.99 +Dark Disciple,Christie Golden,2015,Del Rey,July 7,1,9780345511539,2,0,000002,9.99 +Star Wars: Episode III Revenge of the Sith,Matthew Stover,2005,Del Rey,April 7,2005,345428838,3,0,000003,12.99 +Star Wars: Episode II Attack of the Clones,Matthew Stover,2003,Del Rey,April 7,2003,7567756775,2,0,000004,12.99 \ No newline at end of file diff --git a/resources/Users.csv b/resources/Users.csv new file mode 100644 index 0000000..7b7a60c --- /dev/null +++ b/resources/Users.csv @@ -0,0 +1,9 @@ +Username,Password,UserID,UserType,Name,Email,PhoneNumber,NumLoans,CurrentLoans +BryceChimene,JiffyPeanutbutter,000001,Patron,Bryce Chimene,Bryyce@email.com,5551234,0 +RyanTheGreyJedi,HanShotFirst,000002,Librarian,Ryan McFarland, Ryan_mcfarland@TheSenate.com,5557567,0 +JamesTheGreatBookwyrm,UnlimitedPowers,000003,Admin,J Jonah Jameson,jjj@DailyBugle.net,5552916,0 +SamwiseGamer,4FrodoTheJeweler,000004,Patron,Samwise Gamgee,SamwiseTheLoyal@gondor.com,5552345,0 +TonyStank,IronManSucks,000005,Patron,Bruce Wayne,Batman@WayneInc.com,555847342,0 +lib,lib,000006,Librarian,Libby App,Libby@Libby.com,5558976,0 +pat,pat,000007,Patron,John Johnson,JohnJohn@John.com,5555646,1,1 +CaptainRex,CT-7567,000008,Patron,Rex,CapRex@GAR.gov,5557567,0 \ No newline at end of file