diff --git a/Report to assignment 3.pdf b/Report to assignment 3.pdf new file mode 100644 index 0000000..f75ee86 Binary files /dev/null and b/Report to assignment 3.pdf differ diff --git a/Third-Assignment-Library-Management-System/README.md b/Third-Assignment-Library-Management-System/README.md new file mode 100644 index 0000000..57d5f0a --- /dev/null +++ b/Third-Assignment-Library-Management-System/README.md @@ -0,0 +1,55 @@ +# Library Management System + +## Introduction +In this assignment, you will create an object-oriented Java program that can manage a library. The program should allow users to search for books, borrow and return books, and manage the library inventory. The program should be designed using object-oriented programming principles. + +## Objectives +- Review the concepts of object-oriented programming (OOP) and utilize them correctly +- Familiarize yourself with the concept of Encapsulation +- Learn the difference between Static and Instance variables +- Use Git for version control and collaborate on a codebase +- Write a report on the assignment + +## Tasks +1. Fork this repository and clone the fork to your local machine. Ensure to create a new Git branch before starting your work +2. Complete the following classes by adding the required parameters and implementing the predefined functions: `Book`, `Librarian`, `User`, `Library` +3. In the `runMenu()` function of the Main class, Implement an interactive menu in the command line that allows: + - Users to login, logout, borrow and return books + - Librarians to login, logout, add or delete books +4. Your menu should allow new Users to create an account by providing a username and a password. Prevent new users choosing a previously taken username. Authenticate each user before logging in +5. Your program should keep a list of all users (and the books they've borrowed), all librarians, and all books +6. Commit your changes and merge your branch into `main`. Push your commits to your own fork on Github + +## Notes +- The code provided in this repository gives you a template to work with and build your project. You are allowed to: + - Add new functions and classes to your code wherever you see fit. + - Change and swap the predefined functions as you wish, as long as the program satisfies all the requirements. + +- Your report should focus on the structure you chose for each different class, as well the OOP principles you followed while designing your project. Describe the design of your program, including the classes, objects, methods, and attributes. + +- If you implement any bonus features, be sure to include additional details about them in your report. + +## Evaluation +- Your code should compile and run without any errors +- Your code should be well-organized, readable, properly commented and follows clean code principles +- Your code should follow OOP principles and correctly use Java access modifiers +- You should use Git for version control and include meaningful commit messages (The `main` branch will be used for evaluation) + +**Attention: Using ChatGPT or any other AI generative model for any section of the assignment will result in a score of 0 without any warnings.** + +## Bonus Objectives +1. Perform Input Validation: Ensure the data or information entered by a user or system is correct, complete, and appropriate for the intended use. For instance, a user shouldn't be able to borrow the same book twice, or return a book they haven't borrowed yet. + +2. Implement Encapsulation in your code to protect the data and ensure that it can only be accessed through the defined setter/getter functions. Describe the difference between the distinct Java access modifiers in your report. + +3. Extend the User and Librarian classes to create a more secure login system. Use a hashing algorithm to hash the user's password. Give a brief explanation about the hashing process in your report. + +4. Add a simple GUI (Graphical User Interface) to your project using either Swing or JavaFX. This GUI should include all of the options offered by the command line menu you implemented earlier. Displaying pictures or icons is optional. + +## Submission +- Push your code to your fork on Github +- Upload your report to your fork on GitHub + +The deadline for submitting your code is Wednesday, March 8 (17th of Esfand). Any commit made after this date will not affect your score. + +Good luck and happy coding! diff --git a/Third-Assignment-Library-Management-System/build.gradle b/Third-Assignment-Library-Management-System/build.gradle new file mode 100644 index 0000000..6378f89 --- /dev/null +++ b/Third-Assignment-Library-Management-System/build.gradle @@ -0,0 +1,19 @@ +plugins { + id 'java' +} + +group 'org.example' +version '1.0-SNAPSHOT' + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/Book.class b/Third-Assignment-Library-Management-System/build/classes/java/main/Book.class new file mode 100644 index 0000000..9a480ca Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/Book.class differ diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/Librarian.class b/Third-Assignment-Library-Management-System/build/classes/java/main/Librarian.class new file mode 100644 index 0000000..6a2c7ee Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/Librarian.class differ diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/Library.class b/Third-Assignment-Library-Management-System/build/classes/java/main/Library.class new file mode 100644 index 0000000..56d0c83 Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/Library.class differ diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/Main.class b/Third-Assignment-Library-Management-System/build/classes/java/main/Main.class new file mode 100644 index 0000000..8055673 Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/Main.class differ diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/User.class b/Third-Assignment-Library-Management-System/build/classes/java/main/User.class new file mode 100644 index 0000000..632c217 Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/User.class differ diff --git a/Third-Assignment-Library-Management-System/build/classes/java/main/users.class b/Third-Assignment-Library-Management-System/build/classes/java/main/users.class new file mode 100644 index 0000000..ef0cfa8 Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/classes/java/main/users.class differ diff --git a/Third-Assignment-Library-Management-System/build/tmp/compileJava/previous-compilation-data.bin b/Third-Assignment-Library-Management-System/build/tmp/compileJava/previous-compilation-data.bin new file mode 100644 index 0000000..25de727 Binary files /dev/null and b/Third-Assignment-Library-Management-System/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.jar b/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..7454180 Binary files /dev/null and b/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.jar differ diff --git a/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.properties b/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..692a5d6 --- /dev/null +++ b/Third-Assignment-Library-Management-System/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/Third-Assignment-Library-Management-System/gradlew b/Third-Assignment-Library-Management-System/gradlew new file mode 100644 index 0000000..4e39589 --- /dev/null +++ b/Third-Assignment-Library-Management-System/gradlew @@ -0,0 +1,185 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MSYS* | MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/Third-Assignment-Library-Management-System/gradlew.bat b/Third-Assignment-Library-Management-System/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/Third-Assignment-Library-Management-System/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/Third-Assignment-Library-Management-System/settings.gradle b/Third-Assignment-Library-Management-System/settings.gradle new file mode 100644 index 0000000..f86bcc8 --- /dev/null +++ b/Third-Assignment-Library-Management-System/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'Third-Assignment-Library-Management-System' + diff --git a/Third-Assignment-Library-Management-System/src/main/java/Book.java b/Third-Assignment-Library-Management-System/src/main/java/Book.java new file mode 100644 index 0000000..7a14cc4 --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/Book.java @@ -0,0 +1,35 @@ +import java.util.Scanner; + +public class Book { + + public int isbn; + public String bookName; + public String authorName; + public String publishYear; + public int bookQty; + public int bookQtyCopy; + + Scanner input = new Scanner(System.in); + + public Book(){ + + System.out.println("Enter ISBN of Book:"); + this.isbn = input.nextInt(); + input.nextLine(); + + System.out.println("Enter Book Name:"); + this.bookName = input.nextLine(); + + System.out.println("Enter Author Name:"); + this.authorName = input.nextLine(); + + System.out.println("Enter publish year:"); + this.publishYear = input.nextLine(); + + System.out.println("Enter Quantity of Books:"); + this.bookQty = input.nextInt(); + bookQtyCopy = this.bookQty; + + } + +} diff --git a/Third-Assignment-Library-Management-System/src/main/java/Librarian.java b/Third-Assignment-Library-Management-System/src/main/java/Librarian.java new file mode 100644 index 0000000..208dca4 --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/Librarian.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +public class Librarian { + + String libName; + String libPass; + Scanner input = new Scanner(System.in); + + public Librarian() + { + System.out.println("Enter librarian name:"); + + this.libName = input.nextLine(); + + System.out.println("Enter librarian password:"); + + this.libPass = input.nextLine(); + } + +} diff --git a/Third-Assignment-Library-Management-System/src/main/java/Library.java b/Third-Assignment-Library-Management-System/src/main/java/Library.java new file mode 100644 index 0000000..31ecbff --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/Library.java @@ -0,0 +1,232 @@ +import java.util.Scanner; + +public class Library { + + public int BOOKcapacity; + Book theBooks[] = new Book[BOOKcapacity]; + + + public static int count; + + Scanner input = new Scanner(System.in); + + public void askingForBookCapacity(){ + + System.out.println("PLEASE ENTER THE CAPACITY OF YOUR LIBRARY FOR ADDING BOOKS"); + + BOOKcapacity = input.nextInt(); + } + + + public int compareBookObjects(Book b1, Book b2){ + + if (b1.bookName.equalsIgnoreCase(b2.bookName)){ + + System.out.println("Book of this Name Already Exists."); + + return 0; + + } + + if (b1.isbn == b2.isbn){ + + System.out.println("Book of this Serial No Already Exists."); + + return 0; + + } + + return 1; + + } + + + public void addBook(Book b) { + + for (int i = 0; i < count; i++) { + + if (this.compareBookObjects(b, this.theBooks[i]) == 0) + + return; + } + + if (count < BOOKcapacity){ + + theBooks[count] = b; + + count++; + } + + else{ + + System.out.println("No Space to Add More Books."); + } + + } + + public void searchByISBN(){ + + System.out.println("SEARCH BY SERIAL NUMBER"); + + int ISBN; + + System.out.println("Enter ISBN of Book:"); + + ISBN = input.nextInt(); + + int flag = 0; + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + flag++; + + } + } + + if (flag == 0){ + + System.out.println("No Book for ISBN " + ISBN + " Found."); + } + + } + + + public void searchByAuthorName(){ + + // Display message + System.out.println("SEARCH BY AUTHOR'S NAME"); + + System.out.println("Enter Author Name:"); + + String authorName = input.nextLine(); + + int flag = 0; + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++) { + + if (authorName.equalsIgnoreCase(theBooks[i].authorName)){ + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + flag++; + + } + + } + + + if (flag == 0){ + + System.out.println("No Books of " + authorName + " Found."); + } + + } + + public void showAllBooks(){ + + System.out.println("SHOWING ALL BOOKS"); + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++) { + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + } + + } + + + public void changeBookQty(){ + + System.out.println("CHANGE QUANTITY OF A BOOK\n"); + + System.out.println("Enter ISBN of Book"); + + int ISBN = input.nextInt(); + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + // Display message + System.out.println("Enter Number of Books to be Added or Removed \n ATTENTION: if you want to increase just type a positive number like \n2 and if you want to decrease write a negative number like -2 "); + + int addingQty = input.nextInt(); + + theBooks[i].bookQty += addingQty; + + theBooks[i].bookQtyCopy += addingQty; + + return; + } + } + } + + public int doesBookExists(int ISBN){ + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + if (theBooks[i].bookQtyCopy > 0){ + + System.out.println("Book exists."); + + return i; + } + + System.out.println("Book does not exist"); + + return -1; + } + } + + System.out.println("No Book of ISBN " + " Available in Library."); + + return -1; + } + + + public Book removeBook(){ + + System.out.println("Enter ISBN of Book to be Checked Out."); + + int ISBN = input.nextInt(); + + int bookIndex = doesBookExists(ISBN); + + if (bookIndex != -1){ + + theBooks[bookIndex].bookQtyCopy--; + + return theBooks[bookIndex]; + + } + + return null; + + } + + + public void returnBook(Book b){ + + for (int i = 0; i < count; i++){ + + if (b.equals(theBooks[i])){ + + theBooks[i].bookQtyCopy++; + + } + } + } +} + diff --git a/Third-Assignment-Library-Management-System/src/main/java/Main.java b/Third-Assignment-Library-Management-System/src/main/java/Main.java new file mode 100644 index 0000000..efbce18 --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/Main.java @@ -0,0 +1,124 @@ +import java.util.Scanner; + +public class Main { + + + + public void main(String[] args){ + + Scanner input = new Scanner(System.in); + + System.out.println("**********************************************************************"); + System.out.println(" WELLCOME TO THE LIBRARY "); + System.out.println("**********************************************************************"); + + Library bookCapacity = new Library(); + + bookCapacity.askingForBookCapacity(); + + users usersCapacity = new users(); + + usersCapacity.askingForUsersCapacity(); + + Library ob = new Library(); + + users obUser = new users(); + + int choice; + + int searchChoice; + + do { + + dispMenu(); + + choice = input.nextInt(); + + switch (choice) { + + case 1: + Book b = new Book(); + ob.addBook(b); + break; + + + case 2: + ob.changeBookQty(); + break; + + + case 3: + + System.out.println(" press 1 to Search with Book ISNB."); + + System.out.println(" Press 2 to Search with Book's Author Name."); + + searchChoice = input.nextInt(); + + if(searchChoice==1){ + + ob.searchByISBN(); + } + + else { + + ob.searchByAuthorName(); + } + + + case 4: + ob.showAllBooks(); + break; + + + case 5: + User s = new User(); + obUser. addUsers(s); + break; + + + case 6: + obUser.showAllUsers(); + break; + + + case 7: + obUser.rentBook(ob); + break; + + + case 8: + obUser.returnBook(ob); + break; + + + default: + + System.out.println("Thanks for Choosing Us !"); + } + + } + + + while (choice != 0); + + } + + + static void dispMenu(){ + + // Displaying menu + System.out.println("----------------------------------------------------------------------------------------------------------"); + System.out.println("Press 1 to Add new Book."); + System.out.println("Press 2 to Change Quantity of a Book."); + System.out.println("Press 3 to Search a Book."); + System.out.println("Press 4 to Show All Books."); + System.out.println("Press 5 to Register user."); + System.out.println("Press 6 to Show All Registered users."); + System.out.println("Press 7 to Borrow or Remove Book. "); + System.out.println("Press 8 to Return Book"); + System.out.println("Press 0 to Exit"); + System.out.println("-------------------------------------------------------------------------------------------------------"); + } + +} diff --git a/Third-Assignment-Library-Management-System/src/main/java/User.java b/Third-Assignment-Library-Management-System/src/main/java/User.java new file mode 100644 index 0000000..0b0e538 --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/User.java @@ -0,0 +1,23 @@ +import java.util.Scanner; + +public class User { + + String userName; + String passWord; + + Book borrowedBooks[] = new Book[3]; + public int booksCount = 0; + + Scanner input = new Scanner(System.in); + + public User() + { + System.out.println("Enter username:"); + + this.userName = input.nextLine(); + + System.out.println("Enter password:"); + + this.passWord = input.nextLine(); + } +} diff --git a/Third-Assignment-Library-Management-System/src/main/java/users.java b/Third-Assignment-Library-Management-System/src/main/java/users.java new file mode 100644 index 0000000..7bad7ab --- /dev/null +++ b/Third-Assignment-Library-Management-System/src/main/java/users.java @@ -0,0 +1,162 @@ +import java.util.Scanner; + +public class users { + + public int USERcapacity; + Scanner input = new Scanner(System.in); + User theUsers[] = new User[USERcapacity]; + + + + public static int count = 0; + + public void askingForUsersCapacity(){ + + System.out.println("PLEASE ENTER THE CAPACITY OF YOUR LIBRARY FOR USERS"); + + USERcapacity = input.nextInt(); + } + + + public void addUsers(User u){ + + for (int i = 0; i < count; i++) { + + if (u.passWord.equalsIgnoreCase(theUsers[i].passWord)){ + + System.out.println("user of password " + u.passWord + " is Already Registered."); + + } + + } + + if (count <= USERcapacity){ + + theUsers[count] = u; + + count++; + } + + } + + + public void showAllUsers(){ + + System.out.println("username --- password"); + + for (int i = 0; i < count; i++){ + + System.out.println(theUsers[i].userName + " --- " + theUsers[i].passWord); + + } + + } + + public int doesUserExist(){ + + System.out.println("Enter password:"); + + String password = input.nextLine(); + + for (int i = 0; i < count; i++){ + + if (theUsers[i].passWord.equalsIgnoreCase(password)){ + + return i; + + } + + } + + + System.out.println("user is not Registered."); + + System.out.println("Get Registered First."); + + return -1; + + } + + public void rentBook(Library book){ + + int userIndex = this.doesUserExist(); + + if (userIndex != -1){ + + System.out.println("renting ..."); + + book.showAllBooks(); + + Book b = book.removeBook(); + + System.out.println("renting ..."); + + if (b != null){ + + if (theUsers[userIndex].booksCount <= 3){ + + System.out.println("adding book"); + + theUsers[userIndex].borrowedBooks[theUsers[userIndex].booksCount] = b; + + theUsers[userIndex].booksCount++; + + return; + + } + + else{ + + System.out.println("User Can not Borrow more than 3 Books."); + + return; + + } + + } + + System.out.println("Book is not Available."); + } + + + } + + public void returnBook(Library book){ + + int userIndex = this.doesUserExist(); + + if (userIndex != -1){ + + System.out.println("ISBN --- Book Name --- Author Name"); + + User u = theUsers[userIndex]; + + for (int i = 0; i < u.booksCount; i++){ + + System.out.println(u.borrowedBooks[i].isbn + " --- " + u.borrowedBooks[i].bookName + " --- " + u.borrowedBooks[i].authorName); + + } + + System.out.println("Enter ISBN of Book to be returned:"); + + int ISBN = input.nextInt(); + + for (int i = 0; i < u.booksCount; i++){ + + if (ISBN == u.borrowedBooks[i].isbn){ + + book.returnBook(u.borrowedBooks[i]); + + u.borrowedBooks[i] = null; + + } + + } + + System.out.println("Book of ISBN " + ISBN + "not Found"); + + } + + } + +} diff --git a/Third-Assignment-Library-Management-System/untitled/src/Main.java b/Third-Assignment-Library-Management-System/untitled/src/Main.java new file mode 100644 index 0000000..3e59c38 --- /dev/null +++ b/Third-Assignment-Library-Management-System/untitled/src/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/Third-Assignment-Library-Management-System/untitled/untitled.iml b/Third-Assignment-Library-Management-System/untitled/untitled.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/Third-Assignment-Library-Management-System/untitled/untitled.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 7cff6f8..2dced48 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,3 +1,35 @@ +import java.util.Scanner; + public class Book { - //Book should contain name,author,year of publish and ISBN -} + + public int isbn; + public String bookName; + public String authorName; + public String publishYear; + public int bookQty; + public int bookQtyCopy; + + Scanner input = new Scanner(System.in); + + public Book(){ + + System.out.println("Enter ISBN of Book:"); + this.isbn = input.nextInt(); + input.nextLine(); + + System.out.println("Enter Book Name:"); + this.bookName = input.nextLine(); + + System.out.println("Enter Author Name:"); + this.authorName = input.nextLine(); + + System.out.println("Enter publish year:"); + this.publishYear = input.nextLine(); + + System.out.println("Enter Quantity of Books:"); + this.bookQty = input.nextInt(); + bookQtyCopy = this.bookQty; + + } + +} \ No newline at end of file diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 321ce3a..6a38784 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,10 +1,20 @@ +import java.util.Scanner; + 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 - */ + String libName; + String libPass; + Scanner input = new Scanner(System.in); + + public Librarian() + { + System.out.println("Enter librarian name:"); + + this.libName = input.nextLine(); + + System.out.println("Enter librarian password:"); + + this.libPass = input.nextLine(); + } -} +} \ No newline at end of file diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6c34354..49b762a 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,84 +1,231 @@ +import java.util.Scanner; + public class Library { - /* - * The library should have a list of books. - * The library should have a map of books ISBNs which is linked to the amount of book - -> (for example: harry potter -> 4 means there are currently 4 harry potter books) - * The library should have a list of users and a list of librarians. - */ - //book related functions + public int BOOKcapacity; + Book theBooks[] = new Book[BOOKcapacity]; - public void addBook(){ - //TODO - } - public void removeBook(){ - //TODO - } + public static int count; - public void searchBook(){ - //TODO - } + Scanner input = new Scanner(System.in); - public void updateBook(){ - //TODO - } + public void askingForBookCapacity(){ - public void doesBookExist(){ - //TODO - } + System.out.println("PLEASE ENTER THE CAPACITY OF YOUR LIBRARY FOR ADDING BOOKS"); - public void increaseBook(){ - //TODO + BOOKcapacity = input.nextInt(); } - public void decreaseBook(){ - //TODO - } - //user related functions + public int compareBookObjects(Book b1, Book b2){ - public void addUser(){ - //TODO - } + if (b1.bookName.equalsIgnoreCase(b2.bookName)){ - public void removeUser(){ - //TODO - } + System.out.println("Book of this Name Already Exists."); + + return 0; + + } + + if (b1.isbn == b2.isbn){ + + System.out.println("Book of this Serial No Already Exists."); + + return 0; + + } + + return 1; - public void searchUser(){ - //TODO } - public void updateUser(){ - //TODO + + public void addBook(Book b) { + + for (int i = 0; i < count; i++) { + + if (this.compareBookObjects(b, this.theBooks[i]) == 0) + + return; + } + + if (count < BOOKcapacity){ + + theBooks[count] = b; + + count++; + } + + else{ + + System.out.println("No Space to Add More Books."); + } + } - public void doesUserExist(){ - //TODO + public void searchByISBN(){ + + System.out.println("SEARCH BY SERIAL NUMBER"); + + int ISBN; + + System.out.println("Enter ISBN of Book:"); + + ISBN = input.nextInt(); + + int flag = 0; + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + flag++; + + } + } + + if (flag == 0){ + + System.out.println("No Book for ISBN " + ISBN + " Found."); + } + } - //librarian related functions - public void addLibrarian(){ - //TODO + public void searchByAuthorName(){ + + // Display message + System.out.println("SEARCH BY AUTHOR'S NAME"); + + System.out.println("Enter Author Name:"); + + String authorName = input.nextLine(); + + int flag = 0; + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++) { + + if (authorName.equalsIgnoreCase(theBooks[i].authorName)){ + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + flag++; + + } + + } + + + if (flag == 0){ + + System.out.println("No Books of " + authorName + " Found."); + } + } - public void removeLibrarian(){ - //TODO + public void showAllBooks(){ + + System.out.println("SHOWING ALL BOOKS"); + + System.out.println("ISBN - Name - Author - Available Qty - Total Qty - publish year"); + + for (int i = 0; i < count; i++) { + + System.out.println(theBooks[i].isbn + "\t\t" + theBooks[i].bookName + "\t\t" + theBooks[i].authorName + "\t\t" + theBooks[i].bookQtyCopy + "\t\t" + theBooks[i].bookQty + "\t\t" + theBooks[i].publishYear); + + } + } - public void searchLibrarian(){ - //TODO + + public void changeBookQty(){ + + System.out.println("CHANGE QUANTITY OF A BOOK\n"); + + System.out.println("Enter ISBN of Book"); + + int ISBN = input.nextInt(); + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + // Display message + System.out.println("Enter Number of Books to be Added or Removed \n ATTENTION: if you want to increase just type a positive number like \n2 and if you want to decrease write a negative number like -2 "); + + int addingQty = input.nextInt(); + + theBooks[i].bookQty += addingQty; + + theBooks[i].bookQtyCopy += addingQty; + + return; + } + } } - public void updateLibrarian(){ - //TODO + public int doesBookExists(int ISBN){ + + for (int i = 0; i < count; i++){ + + if (ISBN == theBooks[i].isbn){ + + if (theBooks[i].bookQtyCopy > 0){ + + System.out.println("Book exists."); + + return i; + } + + System.out.println("Book does not exist"); + + return -1; + } + } + + System.out.println("No Book of ISBN " + " Available in Library."); + + return -1; } - public void doesLibrarianExist(){ - //TODO + + public Book removeBook(){ + + System.out.println("Enter ISBN of Book to be Checked Out."); + + int ISBN = input.nextInt(); + + int bookIndex = doesBookExists(ISBN); + + if (bookIndex != -1){ + + theBooks[bookIndex].bookQtyCopy--; + + return theBooks[bookIndex]; + + } + + return null; + } + public void returnBook(Book b){ + + for (int i = 0; i < count; i++){ + + if (b.equals(theBooks[i])){ + + theBooks[i].bookQtyCopy++; + + } + } + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9a347d3..dab5d8d 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,18 +1,124 @@ import java.util.Scanner; public class Main { - /* - * make a functional library app using oop - * run the main program in Main.java and code the oop part in other classes - * don't forget to add at least 1 librarian to the library to make it functionable. - * * *** don't limit yourself to our template *** - */ - public static void main(String[] args) { + + + public void main(String[] args){ + + Scanner input = new Scanner(System.in); + + System.out.println("**********************************************************************"); + System.out.println(" WELLCOME TO THE LIBRARY "); + System.out.println("**********************************************************************"); + + Library bookCapacity = new Library(); + + bookCapacity.askingForBookCapacity(); + + users usersCapacity = new users(); + + usersCapacity.askingForUsersCapacity(); + + Library ob = new Library(); + + users obUser = new users(); + + int choice; + + int searchChoice; + + do { + + dispMenu(); + + choice = input.nextInt(); + + switch (choice) { + + case 1: + Book b = new Book(); + ob.addBook(b); + break; + + + case 2: + ob.changeBookQty(); + break; + + + case 3: + + System.out.println(" press 1 to Search with Book ISNB."); + + System.out.println(" Press 2 to Search with Book's Author Name."); + + searchChoice = input.nextInt(); + + if(searchChoice==1){ + + ob.searchByISBN(); + } + + else { + + ob.searchByAuthorName(); + } + + + case 4: + ob.showAllBooks(); + break; + + + case 5: + User s = new User(); + obUser. addUsers(s); + break; + + + case 6: + obUser.showAllUsers(); + break; + + + case 7: + obUser.rentBook(ob); + break; + + + case 8: + obUser.returnBook(ob); + break; + + + default: + + System.out.println("Thanks for Choosing Us !"); + } + + } + + + while (choice != 0); } - public static void runMenu(){ - //TODO: + + static void dispMenu(){ + + // Displaying menu + System.out.println("----------------------------------------------------------------------------------------------------------"); + System.out.println("Press 1 to Add new Book."); + System.out.println("Press 2 to Change Quantity of a Book."); + System.out.println("Press 3 to Search a Book."); + System.out.println("Press 4 to Show All Books."); + System.out.println("Press 5 to Register user."); + System.out.println("Press 6 to Show All Registered users."); + System.out.println("Press 7 to Borrow or Remove Book. "); + System.out.println("Press 8 to Return Book"); + System.out.println("Press 0 to Exit"); + System.out.println("-------------------------------------------------------------------------------------------------------"); } -} + +} \ No newline at end of file diff --git a/src/main/java/User.java b/src/main/java/User.java index dbe4020..f3a2d9f 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,12 +1,23 @@ +import java.util.Scanner; + public class User { - //User should have a list of books - //User should have a username and a password - public void rentBook(){ - //TODO - } + String userName; + String passWord; + + Book borrowedBooks[] = new Book[3]; + public int booksCount = 0; + + Scanner input = new Scanner(System.in); + + public User() + { + System.out.println("Enter username:"); + + this.userName = input.nextLine(); + + System.out.println("Enter password:"); - public void returnBook(){ - //TODO + this.passWord = input.nextLine(); } -} +} \ No newline at end of file diff --git a/src/main/java/users.java b/src/main/java/users.java new file mode 100644 index 0000000..f380a7a --- /dev/null +++ b/src/main/java/users.java @@ -0,0 +1,162 @@ +import java.util.Scanner; + +public class users { + + public int USERcapacity; + Scanner input = new Scanner(System.in); + User theUsers[] = new User[USERcapacity]; + + + + public static int count = 0; + + public void askingForUsersCapacity(){ + + System.out.println("PLEASE ENTER THE CAPACITY OF YOUR LIBRARY FOR USERS"); + + USERcapacity = input.nextInt(); + } + + + public void addUsers(User u){ + + for (int i = 0; i < count; i++) { + + if (u.passWord.equalsIgnoreCase(theUsers[i].passWord)){ + + System.out.println("user of password " + u.passWord + " is Already Registered."); + + } + + } + + if (count <= USERcapacity){ + + theUsers[count] = u; + + count++; + } + + } + + + public void showAllUsers(){ + + System.out.println("username --- password"); + + for (int i = 0; i < count; i++){ + + System.out.println(theUsers[i].userName + " --- " + theUsers[i].passWord); + + } + + } + + public int doesUserExist(){ + + System.out.println("Enter password:"); + + String password = input.nextLine(); + + for (int i = 0; i < count; i++){ + + if (theUsers[i].passWord.equalsIgnoreCase(password)){ + + return i; + + } + + } + + + System.out.println("user is not Registered."); + + System.out.println("Get Registered First."); + + return -1; + + } + + public void rentBook(Library book){ + + int userIndex = this.doesUserExist(); + + if (userIndex != -1){ + + System.out.println("renting ..."); + + book.showAllBooks(); + + Book b = book.removeBook(); + + System.out.println("renting ..."); + + if (b != null){ + + if (theUsers[userIndex].booksCount <= 3){ + + System.out.println("adding book"); + + theUsers[userIndex].borrowedBooks[theUsers[userIndex].booksCount] = b; + + theUsers[userIndex].booksCount++; + + return; + + } + + else{ + + System.out.println("User Can not Borrow more than 3 Books."); + + return; + + } + + } + + System.out.println("Book is not Available."); + } + + + } + + public void returnBook(Library book){ + + int userIndex = this.doesUserExist(); + + if (userIndex != -1){ + + System.out.println("ISBN --- Book Name --- Author Name"); + + User u = theUsers[userIndex]; + + for (int i = 0; i < u.booksCount; i++){ + + System.out.println(u.borrowedBooks[i].isbn + " --- " + u.borrowedBooks[i].bookName + " --- " + u.borrowedBooks[i].authorName); + + } + + System.out.println("Enter ISBN of Book to be returned:"); + + int ISBN = input.nextInt(); + + for (int i = 0; i < u.booksCount; i++){ + + if (ISBN == u.borrowedBooks[i].isbn){ + + book.returnBook(u.borrowedBooks[i]); + + u.borrowedBooks[i] = null; + + } + + } + + System.out.println("Book of ISBN " + ISBN + "not Found"); + + } + + } + +} \ No newline at end of file