diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 7cff6f8..ab856cf 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,3 +1,63 @@ public class Book { //Book should contain name,author,year of publish and ISBN -} + private String name; + private String author; + private String year; + private String isbn; + private boolean avaliable = true; + + public boolean isAvaliable() { + return avaliable; + } + + public void setAvaliable(boolean avaliable) { + this.avaliable = avaliable; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getYear() { + return year; + } + + public void setYear(String year) { + this.year = year; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + @Override + public String toString() { + return "Book{" + + "name='" + name + '\'' + + ", author='" + author + '\'' + + ", year='" + year + '\'' + + ", isbn='" + isbn + '\'' + + ", avaliable=" + avaliable + + '}'; + } + + public void changeStatus(){ + this.avaliable = !this.avaliable; + } +} \ No newline at end of file diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 321ce3a..8f61d76 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,10 +1,55 @@ +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 + * The librarian should be able to add,remove,update user add,remove,update_ + _ librarian and add,remove,update book */ + // Attributes + private String username; + private String password; + + // Getter & Setter + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } -} + public void changePassword(){ + System.out.println("Enter Your Current Password: "); + Scanner myScanner = new Scanner(System.in); + String Pass = myScanner.nextLine(); + if(Pass.equals(this.getPassword())){ + System.out.println("Enter Your newest Password: "); + String newPass = myScanner.nextLine(); + this.password = newPass; + } + else{ + System.out.println("Wrong Password! " + "\n" + "Do You Want Try Again?!(Yes/No) : "); + String ansewr = myScanner.nextLine(); + if(ansewr.equals("Yes") || ansewr.equals("yes")) + changePassword(); + } + } + @Override + public String toString() { + return "Librarian{" + + "username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6c34354..dc466d5 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,3 +1,10 @@ + +import javax.jws.soap.SOAPBinding; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Scanner; +import java.util.StringTokenizer; + public class Library { /* * The library should have a list of books. @@ -8,77 +15,200 @@ public class Library { //book related functions - public void addBook(){ - //TODO - } - - public void removeBook(){ - //TODO - } - - public void searchBook(){ - //TODO - } - - public void updateBook(){ - //TODO - } - - public void doesBookExist(){ - //TODO - } - - public void increaseBook(){ - //TODO - } - - public void decreaseBook(){ - //TODO - } - - //user related functions - - public void addUser(){ - //TODO - } - - public void removeUser(){ - //TODO - } - - public void searchUser(){ - //TODO - } - - public void updateUser(){ - //TODO - } - - public void doesUserExist(){ - //TODO - } - - //librarian related functions - - public void addLibrarian(){ - //TODO - } - - public void removeLibrarian(){ - //TODO - } - - public void searchLibrarian(){ - //TODO - } - - public void updateLibrarian(){ - //TODO - } - - public void doesLibrarianExist(){ - //TODO - } - - -} + // ArrayLists + private HashMapnumberOfBooks = new HashMap(); + private ArrayListBooks = new ArrayList(); + private ArrayListLibrarians = new ArrayList(); + private ArrayListUsers = new ArrayList(); + + // Public Functions + + // Book Related Functions + public void addBook(String name, String author, String year, String isbn){ + if(!this.doesBookExist(isbn)){ + Book myBook = new Book(); + myBook.setName(name); + myBook.setAuthor(author); + myBook.setYear(year); + myBook.setIsbn(isbn); + this.Books.add(myBook); + this.increaseBook(myBook); + } + } + + public void removeBook(String isbn){ + if(this.doesBookExist(isbn)){ + this.Books.remove(this.searchBook(isbn)); + } + } + + public Book searchBook(String isbn){ + Book myTargetBook = new Book(); + if(this.doesBookExist(isbn)){ + for(Book value : this.Books){ + if(isbn.equals(value.getIsbn())){ + myTargetBook = value; + break; + } + } + } + return myTargetBook; + } + + public Book searchBook(String name, String author){ + Book myTargetBook = new Book(); + if(this.doesBookExist(name, author)){ + for(Book value : Books){ + if(name.equals(value.getName()) && author.equals(value.getAuthor())){ + myTargetBook = value; + break; + } + } + } + + return myTargetBook; + } + + public void updateBook(Book myBook){ + if(doesBookExist(myBook.getIsbn())){ + for (Book book : Books) { + if (myBook.getIsbn().equals(book.getIsbn())) { + book.changeStatus(); + break; + } + } + } + else + System.out.println("This Book Is Not Exist!"); + } + + public boolean doesBookExist(String isbn){ + boolean flag = false; + for(Book value : this.Books){ + if(isbn.equals(value.getIsbn())){ + flag = true; + break; + } + } + return flag; + } + + public boolean doesBookExist(String name, String author){ + boolean flag = false; + for(Book value : this.Books){ + if(name.equals(value.getName()) && author.equals(value.getAuthor())){ + flag = true; + break; + } + } + return flag; + } + public void increaseBook(Book ibook){ + int number = numberOfBooks.get(ibook.getName()) + 1; + numberOfBooks.put(ibook.getName(), number); + } + + public void decreaseBook(Book ibook){ + int number = numberOfBooks.get(ibook.getName()) - 1; + if(number >= 0) + numberOfBooks.put(ibook.getName(), number); + } + + //User Related Functions + + public void addUser(String username, String password){ + User myUser = new User(); + if(!this.doesUserExist(username, password)){ + myUser.setUsername(username); + myUser.setPassword(password); + this.Users.add(myUser); + } + } + + public void removeUser(String username, String password){ + if(this.doesUserExist(username, password)){ + this.Users.remove(this.searchUser(username, password)); + } + } + + public User searchUser(String username, String password){ + User myTargetUser = new User(); + if(this.doesUserExist(username, password)){ + for(User value : this.Users){ + if(username.equals(value.getUsername()) && password.equals(value.getPassword())){ + myTargetUser = value; + break; + } + } + } + return myTargetUser; + } + + public void updateUser(User myuser){ + if(this.doesUserExist(myuser.getUsername(), myuser.getPassword())){ + myuser.changePassword(); + } + else + System.out.println("This User Is Not Exist!"); + } + + public boolean doesUserExist(String username, String password){ + boolean flag = false; + for(User value : this.Users){ + if(username.equals(value.getUsername()) && password.equals(value.getPassword())){ + flag = true; + break; + } + } + return flag; + } + + //Librarian Related Functions + + public void addLibrarian(String username, String password){ + if(!this.doesLibrarianExist(username, password)){ + Librarian myLibrarian = new Librarian(); + myLibrarian.setUsername(username); + myLibrarian.setPassword(password); + this.Librarians.add(myLibrarian); + } + } + + public void removeLibrarian(String username, String password){ + if(this.doesLibrarianExist(username, password)){ + this.Librarians.remove(this.searchLibrarian(username, password)); + } + } + + public Librarian searchLibrarian(String username, String password){ + Librarian myTargetLibrarian = new Librarian(); + if(this.doesLibrarianExist(username, password)){ + for(Librarian value : this.Librarians){ + if(username.equals(value.getUsername()) && password.equals(value.getPassword())){ + myTargetLibrarian = value; + break; + } + } + } + return myTargetLibrarian; + } + + public void updateLibrarian(Librarian myLibrarian){ + if(doesLibrarianExist(myLibrarian.getUsername(), myLibrarian.getPassword())){ + myLibrarian.changePassword(); + } + else + System.out.println("This Librarian IS Not Exist!"); + } + + public boolean doesLibrarianExist(String username, String password){ + boolean flag = false; + for(Librarian value : this.Librarians){ + if(username.equals(value.getUsername()) && password.equals(value.getPassword())){ + flag = true; + break; + } + } + return flag; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9a347d3..f7885ce 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,18 +1,76 @@ +import javax.jws.soap.SOAPBinding; 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 *** + * 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) { + Book book = new Book(); + Library library = new Library(); + User user = new User(); + Librarian librarian = new Librarian(); + runMenu(book, library, user, librarian); + } + + public static void runMenu(Book myBook, Library myLibrary, User myUser, Librarian myLibrarian){ + Scanner myScanner = new Scanner(System.in); + System.out.println("Welcome To Our Library"); + System.out.println("Enter Number Of Your Position : \n1: User\n2: Librarian"); + int pos = myScanner.nextInt(); + if(pos == 1){ + System.out.println("Enter Your Command: " + "1: Login" + "\n" + "2: SignUp"); + int command = myScanner.nextInt(); + if(command == 1) + loginUser(myBook, myLibrary, myUser, myLibrarian); + else + signUpUser(myBook, myLibrary, myUser, myLibrarian); + } + //else{ + + + + + + + //} + } + public static void signUpUser(Book myBook, Library myLibrary, User myUser, Librarian myLibrarian){ + Scanner input = new Scanner(System.in); + System.out.println("Enter Your Username: "); + String username = input.nextLine(); + System.out.println("Enter Your Password: "); + String password = input.nextLine(); + myUser.setUsername(username); + myUser.setPassword(password); + if(!myLibrary.doesUserExist(myUser.getUsername(), myUser.getPassword())){ + System.out.println("Ok, You Successfully SignUp!"); + myLibrary.addUser(myUser.getUsername(), myUser.getPassword()); + } + else + System.out.println("This Username Is Already Exist!"); } - public static void runMenu(){ - //TODO: + public static void loginUser(Book myBook, Library myLibrary, User myUser, Librarian myLibrarian){ + Scanner input = new Scanner(System.in); + System.out.println("Enter Your Username: "); + String username = input.nextLine(); + System.out.println("Enter Your Password: "); + String password = input.nextLine(); + myUser.setUsername(username); + myUser.setPassword(password); + if(myLibrary.doesUserExist(myUser.getUsername(), myUser.getPassword())){ + System.out.println("Ok, You Successfully Login!"); + } + else + System.out.println("Your Username or Password Went Wrong!"); + } + public static void logOut(){ + System.out.println("You Logged Out!"); } } diff --git a/src/main/java/Review b/src/main/java/Review new file mode 100644 index 0000000..f667371 --- /dev/null +++ b/src/main/java/Review @@ -0,0 +1,45 @@ +"i tried to make this application with gui but i failed due to the lack of timne , and as i wanted to give the assignment +at its own time its not the =best version of the app but , it works" +Introduction; +this is an app for handeling a library, that you can enter as an user or as a librarian, for safety reasons you need a password +and a username , when you enter the app you can do diffrent things , such as borrowing or returning and... +Design: +there are 5 classes in the project: main,librarian,user,library,book +the hardest part of this project is how to handle all the information that you get or you have to provide to the user +and ... +(user) class: +i used oop to handle all the info each object contais : name password and the name of the book that the user borrowed. +user class extends library class because i needed the array list that i created and (it is an arraylist of objects) +(librarian) class: +again in librarian class we have aconstructer and diffrent objects that contais passwords and usernames and yet again it extends +library class because our libray class is our database in our project and i needed all the methods in that class. +and it contains an array list of all the librarians. +(book) class: +again i used book class to make diffrent objects thjat in each objects we have the isbn, writer,year of publish and the name of the books +i created another variable called counter and it has the quantity of each book in the library and only the librarians can +increase or decrease the amounts of the book you have in the library, + (library) class: + as i said this class is the most important if thyem all beacause it is the data base and it has all the methods that + our app needs to run and the array list of the objects from all three classes. + addBooks method is the first method and when you call it it will add all the books in the library to the array list of them , + and then you can enter the book you want to add and it will increase or decrease or actually add your desired bok + removeBook method is work the sa=me as addBooks but in reverse:) + searchBook and doesBookExist methods d the same thing and they ask you to type thye name of the book and they will + look for the book in the wanted array list if they find it wallah! if not they will tell yu that it doesn't exist. + increseBook and decreaseBook methods work in reverse and you need to tell the the name of the book and if you want to + decrease or increase the quantity of the book you named and wallah! again. + the following addUser searchUser removeUser methods are only available for the librarians , they have top type teh + name of the user thay want and if it exist it will tell them and it will ask them if thay want to remove the user or if thay want to + adda new user to the rented arraylkist. + the following addLibrarian searchLibrarAN REMOVElibrarian doesLIbrarianExist are methods that only librarians can access + and basically they work the same as the other finding methods in the project , you type the name of the librarian and first you see if + they exist if they do now you can add them or remove them based on the method and they will be add or removed from the array list of + librarians. + (main)class; + it is obvous that its is the main method but what is really important abiut it is that + it helps the privacy of the app and it will ask you to fill multiple sheets and then it will let you to choose what you want to do , + first you need to choose if you want to enter the application as a student or a librarian then based on what you said + it will give you multiple options and if you can pass teh password and username test yopu can choose between the given options + for example for the usr you can either choose between retuenBook or renBook that you have to type the boo's name and it will + search through the arraulist of books and if it found it will check that you didn't borrow anyother books and then it will give it + to you and decrease the cpounter of books in its own arraylisty. diff --git a/src/main/java/User.java b/src/main/java/User.java index dbe4020..64e95cf 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,12 +1,81 @@ +import java.util.ArrayList; +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 + // Attributes + private String username; + private String password; + private ArrayListrentBooks = new ArrayList(); + + // Getter & Setter + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; } - public void returnBook(){ - //TODO + // Public Functions + public void rentBook(Library myLibrary, Book myBook){ + + if(myLibrary.doesBookExist(myBook.getIsbn())){ + if(myBook.isAvaliable()){ + this.rentBooks.add(myBook); + myLibrary.updateBook(myBook); + myLibrary.decreaseBook(myBook); + } + else + System.out.println("This Book Is Not Available!"); + }else + System.out.println("This Book Is Not Exist!"); + + } + + public void returnBook(Library myLibrary, Book myBook){ + for(int i = 0; i < rentBooks.size(); i++){ + if(myBook.getIsbn().equals(rentBooks.get(i).getIsbn())){ + rentBooks.remove(i); + break; + } + } + myLibrary.updateBook(myBook); + myLibrary.increaseBook(myBook); + } + + public void changePassword(){ + System.out.println("Enter Your Current Password: "); + Scanner myScanner = new Scanner(System.in); + String Pass = myScanner.nextLine(); + if(Pass.equals(this.getPassword())){ + System.out.println("Enter Your newest Password: "); + String newPass = myScanner.nextLine(); + this.password = newPass; + } + else{ + System.out.println("Wrong Password! " + "\n" + "Do You Want Try Again?!(Yes/No) : "); + String ansewr = myScanner.nextLine(); + if(ansewr.equals("Yes") || ansewr.equals("yes")) + changePassword(); + } + } + @Override + public String toString() { + return "User{" + + "username='" + username + '\'' + + ", password='" + password + '\'' + + ", rentBooks=" + rentBooks + + '}'; } -} +} \ No newline at end of file