From abbfddadcee3e5f60bb34574321174fbe480da29 Mon Sep 17 00:00:00 2001 From: mobinshakery Date: Wed, 8 Mar 2023 23:08:18 +0330 Subject: [PATCH 1/3] YUP, THAT'S IT. --- src/main/java/Book.java | 40 ++++- src/main/java/Librarian.java | 31 +++- src/main/java/Library.java | 304 ++++++++++++++++++++++++++--------- src/main/java/Main.java | 129 +++++++++++++-- src/main/java/User.java | 34 +++- 5 files changed, 431 insertions(+), 107 deletions(-) diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 7cff6f8..1ef223e 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,3 +1,39 @@ -public class Book { - //Book should contain name,author,year of publish and ISBN +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Collection; + +public class Book implements ActionListener { + String name; + int ISBN; + String author; + int year; + int counter; + Book(){ + name = "default name"; + ISBN = 1234; + author = "me"; + year = 1234; + counter = 100; + } + Book(String name , int ISBN , String author , int year , int counter){ + this.name = name; + this.ISBN = ISBN; + this.author = author; + this.year = year; + this.counter = counter; + } + static Book book1 = new Book("PERKS OF BEING A WALLFLOWER",1234567,"STEPHEN CHOBUSKY",1991,5); + static Book book2 = new Book("ANOTHER LIFE ",89101112,"ALBERTO MORAVIA",1990,7); + static Book book3 = new Book("HONDA",13141516,"ROBERT EL SHOOK",1962,3); + static Book book4 = new Book("PAINTING",17181920,"ZHOZE SARAMAGO",1989,6); + static Book book5 = new Book("NUDGE",21222324,"RICHARD TAYLOR",2017,4); + public Book(Book book6) { + } + + @Override + public void actionPerformed(ActionEvent e) { + + } + } diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 321ce3a..f4cac00 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,10 +1,27 @@ -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 - */ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class Librarian implements ActionListener { + String username; + String password; + + Librarian(){ + username = "dafault"; + password = "dafault"; + + } + Librarian(String username , String password) { + this.username= username; + this.password=password; + } + static Librarian librarian1 = new Librarian("MOBIN","0912"); + public Librarian(Librarian libarian2) { + super(); + } + + @Override + public void actionPerformed(ActionEvent e) { + } } + diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6c34354..4d1bd29 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,84 +1,228 @@ -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 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 +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Scanner; +import java.util.Iterator; +import java.util.ArrayList; + +public class Library implements ActionListener { + + static ArrayList books = new ArrayList(); + static ArrayList rented = new ArrayList(); + static ArrayList librarian = new ArrayList(); + + + public static void addBook () { + books.add(Book.book1); + books.add(Book.book2); + books.add(Book.book3); + books.add(Book.book4); + books.add(Book.book5); + Scanner newBook = new Scanner(System.in); + String name = newBook.next(); + int ISBN = newBook.nextInt(); + String author = newBook.next(); + int year = newBook.nextInt(); + int counter = newBook.nextInt(); + Book book6 = new Book(name,ISBN,author,year,counter); + books.add(new Book(book6)); + } + + public static void removeBook () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for( int i = 0 ; i < books.size() ; i++){ + if(scan1.equals(books.get(i))){ + books.remove(books.get(i)); + } + } + } + + public static void searchBook () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < books.size() ; i++){ + if(scan1.equals(books.get(i))){ + System.out.println("we have"); + }else{ + System.out.println("SORRY!"); + } + } + } + public static void doesBookExist () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < books.size() ; i++){ + if(scan1.equals(books.get(i))){ + System.out.println("the book does exist"); + }else{ + System.out.println("SORRY!"); + } + } + } + public static void increaseBook () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + int scan2 = scan.nextInt(); + for(int i = 0 ; i < books.size() ; i++){ + if(scan1.equals(books.get(i))) { + books.get(i).counter = books.get(i).counter + scan2; + } + } + } + + public static void decreaseBook () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + int scan2 = scan.nextInt(); + for(int i = 0 ; i < books.size() ; i++){ + if(scan1.equals(books.get(i))) { + books.get(i).counter = books.get(i).counter - scan2; + } + } + } + + //user related functions + + public static void addUser () { + rented.add(User.user1); + rented.add(User.user2); + rented.add(User.user3); + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + String scan2 = scan.nextLine(); + User user4 = new User(scan1,scan2,null); + rented.add(new User(user4)); + } + + public static void removeUser () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < rented.size() ;i++){ + if(scan1.equals(rented.get(i))){ + rented.remove(rented.get(i)); + }else{ + System.out.println("user doesn't exist"); + } + } + } + + public static void searchUser () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < rented.size() ;i++){ + if(scan1.equals(rented.get(i))){ + System.out.println(rented.get(i).name); + }else{ + System.out.println("user doesn't exist"); + } + } + } + + public static void doesUserExist () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < rented.size() ;i++){ + if(scan1.equals(rented.get(i))){ + System.out.println(rented.get(i).name); + System.out.println("does exists"); + }else{ + System.out.println("user doesn't exist"); + } + } + } + + //librarian related functions + + public static void addLibrarian () { + librarian.add(Librarian.librarian1); + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + String scan2 = scan.nextLine(); + for(int i = 0 ; 4 > i; i++){ + if(scan1.equals(librarian.get(i))){ + System.out.println("we already have him/her"); + }else{ + Librarian librarian2 = new Librarian(scan1,scan2); + librarian.add(new Librarian(librarian2)); + } + } + } + + public static void removeLibrarian () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; 4 > i; i++){ + if(scan1.equals(librarian.get(i))){ + librarian.remove(librarian.get(i)); + }else{ + System.out.println("librarian doesn't exist"); + } + } + } + + public static void searchLibrarian () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; 4 > i; i++){ + if(scan1.equals(librarian.get(i))){ + System.out.println(librarian.get(i).username); + System.out.println("librarian exist"); + }else{ + System.out.println("librarian doesn't exist"); + } + } + + } + + public static void doesLibrarianExist () { + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; 4 > i; i++){ + if(scan1.equals(librarian.get(i))){ + System.out.println("librarian exist"); + }else{ + System.out.println("librarian doesn't exist"); + } + } + } + public static void rentBook(){ + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < 12 ; i++){ + if(scan1.equals(books.get(i))){ + if( books.get(i).counter != 0){ + books.get(i).counter = books.get(i).counter - 1; + + }else { + System.out.println("sorry the book is not available!"); + } + }else{ + System.out.println("sorry we don't have the book!"); + } + + } + } + + public static void returnBook(){ + Scanner scan = new Scanner(System.in); + String scan1 = scan.nextLine(); + for(int i = 0 ; i < 12 ; i++){ + if(scan1.equals(books.get(i))){ + books.get(i).counter = books.get(i).counter + 1; + } + else{ + System.out.println("are you sure you got that from us?!"); + } + + } + } + + + @Override + public void actionPerformed (ActionEvent e){ + + } } - 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 - } - - -} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9a347d3..d923102 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,18 +1,125 @@ +import javax.swing.*; +import java.awt.event.ActionListener; +import java.util.ArrayList; import java.util.Scanner; +import java.lang.String; +import java.util.Iterator; -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 class Main extends Library { public static void main(String[] args) { - + runMenu(); } - public static void runMenu(){ - //TODO: + public static void runMenu() { + System.out.println("hi welcome to third assignment library"); + System.out.println("if you are a librarian type 1 and if you are a user type 2"); + Scanner scan = new Scanner(System.in); + int scan1 = scan.nextInt(); + if (scan1 == 1) { + System.out.println("first we need some information to make sure you are a librarian"); + System.out.println("what is your username?"); + String UserName = scan.nextLine(); + for (int i = 0; i < 4; i++) { + if (UserName.equals(librarian.get(i).username)) { + System.out.println("enter your password"); + String Password = scan.nextLine(); + if (Password.equals(librarian.get(i).password)) { + System.out.println("welcome back"); + System.out.println("as a librerian you can choose the number between 1 to 14, go ahead "); + System.out.println("addBook1,removeBook2,searchBook3 ,doesBookExist4,increaseBook5,decreaseBook6,addUser7 " + + "removeUser8,searchUser9,doesUserExist10,addLibrarian11,removeLibrarian12,searchLibrarian13" + + ",doesLibrarianExist14"); + int number = scan.nextInt(); + switch (number) { + case 1: + addBook(); + break; + case 2: + removeBook(); + break; + case 3: + searchBook(); + break; + case 4: + doesBookExist(); + break; + case 5: + increaseBook(); + break; + case 6: + decreaseBook(); + break; + case 7: + addUser(); + break; + case 8: + removeUser(); + break; + case 9: + searchUser(); + break; + case 10: + doesUserExist(); + break; + case 11: + addLibrarian(); + break; + case 12: + removeLibrarian(); + break; + case 13: + searchLibrarian(); + break; + case 14: + doesLibrarianExist(); + break; + } + } else { + System.out.println("password is invalid"); + } + } else { + System.out.println("username is invalid"); + } + } + } else { + System.out.println("plz enter you name"); + String Name = scan.nextLine(); + String password = scan.nextLine(); + for (int i = 0; i < 4; i++) { + if (Name.equals(rented.get(i).name)) { + if(password.equals(rented.get(i).password)){ + System.out.println("hi! as a user you have four options , plz go ahead and pick one "); + System.out.println("searchBook1,doesBookExist2,rentBook3,returnBook4"); + int number1 = scan.nextInt(); + switch(number1){ + case 1: + searchBook(); + break; + case 2: + doesBookExist(); + break; + case 3: + rentBook(); + rented.get(i).booksName= books.get(i).name; + break; + case 4: + returnBook(); + rented.get(i).booksName = null; + break; + } + + + }else{ + System.out.println("invalid password"); + } + }else{ + System.out.println("invalid name for a user"); + } + + + } + } + } -} +} \ No newline at end of file diff --git a/src/main/java/User.java b/src/main/java/User.java index dbe4020..8c982fb 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,12 +1,32 @@ -public class User { - //User should have a list of books - //User should have a username and a password +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Scanner; - public void rentBook(){ - //TODO + +public class User extends Library { + String name; + String password; + String booksName; + + User(){ + name = "alex"; + password = "hi!"; + booksName = "hi"; + } + User(String name , String password , String booksName){ + this.name = name; + this.password = password; + this.booksName= booksName; } + static User user1 = new User("ALEX","0935",null); + static User user2 = new User("DAVE","093541",null); + static User user3 = new User("TED","09354146",null); + public User(User user4) { + super(); + } + + @Override + public void actionPerformed(ActionEvent e) { - public void returnBook(){ - //TODO } } From 8bd568749a3af074e57e709e949e935aebd3b9bf Mon Sep 17 00:00:00 2001 From: mobinshakery Date: Wed, 8 Mar 2023 23:48:22 +0330 Subject: [PATCH 2/3] Create Review --- src/main/java/Review | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/Review 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. From 3a95375af0842e5493408bc3b0762d1433a28a20 Mon Sep 17 00:00:00 2001 From: mobinshakery Date: Thu, 25 May 2023 16:24:35 +0430 Subject: [PATCH 3/3] Update Update --- src/main/java/Book.java | 84 ++++++--- src/main/java/Librarian.java | 64 +++++-- src/main/java/Library.java | 346 +++++++++++++++++------------------ src/main/java/Main.java | 175 +++++++----------- src/main/java/User.java | 91 ++++++--- 5 files changed, 399 insertions(+), 361 deletions(-) diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 1ef223e..ab856cf 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,39 +1,63 @@ -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.ArrayList; -import java.util.Collection; - -public class Book implements ActionListener { - String name; - int ISBN; - String author; - int year; - int counter; - Book(){ - name = "default name"; - ISBN = 1234; - author = "me"; - year = 1234; - counter = 100; - } - Book(String name , int ISBN , String author , int year , int counter){ +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; - this.ISBN = ISBN; + } + + 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; - this.counter = counter; } - static Book book1 = new Book("PERKS OF BEING A WALLFLOWER",1234567,"STEPHEN CHOBUSKY",1991,5); - static Book book2 = new Book("ANOTHER LIFE ",89101112,"ALBERTO MORAVIA",1990,7); - static Book book3 = new Book("HONDA",13141516,"ROBERT EL SHOOK",1962,3); - static Book book4 = new Book("PAINTING",17181920,"ZHOZE SARAMAGO",1989,6); - static Book book5 = new Book("NUDGE",21222324,"RICHARD TAYLOR",2017,4); - public Book(Book book6) { + + public String getIsbn() { + return isbn; } - @Override - public void actionPerformed(ActionEvent e) { + 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 f4cac00..8f61d76 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,27 +1,55 @@ -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import java.util.Scanner; -public class Librarian implements ActionListener { - String username; - String password; +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 + */ - Librarian(){ - username = "dafault"; - password = "dafault"; + // Attributes + private String username; + private String password; + // Getter & Setter + public String getUsername() { + return username; } - Librarian(String username , String password) { - this.username= username; - this.password=password; - } - static Librarian librarian1 = new Librarian("MOBIN","0912"); - public Librarian(Librarian libarian2) { - super(); + + public void setUsername(String username) { + this.username = username; } - @Override - public void actionPerformed(ActionEvent e) { + 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 4d1bd29..dc466d5 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,228 +1,214 @@ - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.util.Scanner; -import java.util.Iterator; +import javax.jws.soap.SOAPBinding; import java.util.ArrayList; - -public class Library implements ActionListener { - - static ArrayList books = new ArrayList(); - static ArrayList rented = new ArrayList(); - static ArrayList librarian = new ArrayList(); - - - public static void addBook () { - books.add(Book.book1); - books.add(Book.book2); - books.add(Book.book3); - books.add(Book.book4); - books.add(Book.book5); - Scanner newBook = new Scanner(System.in); - String name = newBook.next(); - int ISBN = newBook.nextInt(); - String author = newBook.next(); - int year = newBook.nextInt(); - int counter = newBook.nextInt(); - Book book6 = new Book(name,ISBN,author,year,counter); - books.add(new Book(book6)); - } - - public static void removeBook () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for( int i = 0 ; i < books.size() ; i++){ - if(scan1.equals(books.get(i))){ - books.remove(books.get(i)); - } - } +import java.util.HashMap; +import java.util.Scanner; +import java.util.StringTokenizer; + +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 + + // 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 static void searchBook () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < books.size() ; i++){ - if(scan1.equals(books.get(i))){ - System.out.println("we have"); - }else{ - System.out.println("SORRY!"); - } - } - } - public static void doesBookExist () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < books.size() ; i++){ - if(scan1.equals(books.get(i))){ - System.out.println("the book does exist"); - }else{ - System.out.println("SORRY!"); - } - } + public void removeBook(String isbn){ + if(this.doesBookExist(isbn)){ + this.Books.remove(this.searchBook(isbn)); } - public static void increaseBook () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - int scan2 = scan.nextInt(); - for(int i = 0 ; i < books.size() ; i++){ - if(scan1.equals(books.get(i))) { - books.get(i).counter = books.get(i).counter + scan2; + } + + 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 static void decreaseBook () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - int scan2 = scan.nextInt(); - for(int i = 0 ; i < books.size() ; i++){ - if(scan1.equals(books.get(i))) { - books.get(i).counter = books.get(i).counter - scan2; + 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; } } } - //user related functions - - public static void addUser () { - rented.add(User.user1); - rented.add(User.user2); - rented.add(User.user3); - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - String scan2 = scan.nextLine(); - User user4 = new User(scan1,scan2,null); - rented.add(new User(user4)); - } + return myTargetBook; + } - public static void removeUser () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < rented.size() ;i++){ - if(scan1.equals(rented.get(i))){ - rented.remove(rented.get(i)); - }else{ - System.out.println("user doesn't exist"); + 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 static void searchUser () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < rented.size() ;i++){ - if(scan1.equals(rented.get(i))){ - System.out.println(rented.get(i).name); - }else{ - System.out.println("user doesn't 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 static void doesUserExist () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < rented.size() ;i++){ - if(scan1.equals(rented.get(i))){ - System.out.println(rented.get(i).name); - System.out.println("does exists"); - }else{ - System.out.println("user doesn't exist"); - } + 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); + } - //librarian related functions + public void decreaseBook(Book ibook){ + int number = numberOfBooks.get(ibook.getName()) - 1; + if(number >= 0) + numberOfBooks.put(ibook.getName(), number); + } - public static void addLibrarian () { - librarian.add(Librarian.librarian1); - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - String scan2 = scan.nextLine(); - for(int i = 0 ; 4 > i; i++){ - if(scan1.equals(librarian.get(i))){ - System.out.println("we already have him/her"); - }else{ - Librarian librarian2 = new Librarian(scan1,scan2); - librarian.add(new Librarian(librarian2)); - } - } + //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 static void removeLibrarian () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; 4 > i; i++){ - if(scan1.equals(librarian.get(i))){ - librarian.remove(librarian.get(i)); - }else{ - System.out.println("librarian doesn't exist"); - } - } + public void removeUser(String username, String password){ + if(this.doesUserExist(username, password)){ + this.Users.remove(this.searchUser(username, password)); } + } - public static void searchLibrarian () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; 4 > i; i++){ - if(scan1.equals(librarian.get(i))){ - System.out.println(librarian.get(i).username); - System.out.println("librarian exist"); - }else{ - System.out.println("librarian doesn't exist"); + 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 static void doesLibrarianExist () { - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; 4 > i; i++){ - if(scan1.equals(librarian.get(i))){ - System.out.println("librarian exist"); - }else{ - System.out.println("librarian doesn't exist"); - } - } + public void updateUser(User myuser){ + if(this.doesUserExist(myuser.getUsername(), myuser.getPassword())){ + myuser.changePassword(); } - public static void rentBook(){ - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < 12 ; i++){ - if(scan1.equals(books.get(i))){ - if( books.get(i).counter != 0){ - books.get(i).counter = books.get(i).counter - 1; + else + System.out.println("This User Is Not Exist!"); + } - }else { - System.out.println("sorry the book is not available!"); - } - }else{ - System.out.println("sorry we don't have the book!"); + 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; } - public static void returnBook(){ - Scanner scan = new Scanner(System.in); - String scan1 = scan.nextLine(); - for(int i = 0 ; i < 12 ; i++){ - if(scan1.equals(books.get(i))){ - books.get(i).counter = books.get(i).counter + 1; - } - else{ - System.out.println("are you sure you got that from us?!"); - } + //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)); + } + } - @Override - public void actionPerformed (ActionEvent e){ + 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 d923102..f7885ce 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,125 +1,76 @@ -import javax.swing.*; -import java.awt.event.ActionListener; -import java.util.ArrayList; +import javax.jws.soap.SOAPBinding; import java.util.Scanner; -import java.lang.String; -import java.util.Iterator; +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 class Main extends Library { public static void main(String[] args) { - runMenu(); + 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() { - System.out.println("hi welcome to third assignment library"); - System.out.println("if you are a librarian type 1 and if you are a user type 2"); - Scanner scan = new Scanner(System.in); - int scan1 = scan.nextInt(); - if (scan1 == 1) { - System.out.println("first we need some information to make sure you are a librarian"); - System.out.println("what is your username?"); - String UserName = scan.nextLine(); - for (int i = 0; i < 4; i++) { - if (UserName.equals(librarian.get(i).username)) { - System.out.println("enter your password"); - String Password = scan.nextLine(); - if (Password.equals(librarian.get(i).password)) { - System.out.println("welcome back"); - System.out.println("as a librerian you can choose the number between 1 to 14, go ahead "); - System.out.println("addBook1,removeBook2,searchBook3 ,doesBookExist4,increaseBook5,decreaseBook6,addUser7 " + - "removeUser8,searchUser9,doesUserExist10,addLibrarian11,removeLibrarian12,searchLibrarian13" + - ",doesLibrarianExist14"); - int number = scan.nextInt(); - switch (number) { - case 1: - addBook(); - break; - case 2: - removeBook(); - break; - case 3: - searchBook(); - break; - case 4: - doesBookExist(); - break; - case 5: - increaseBook(); - break; - case 6: - decreaseBook(); - break; - case 7: - addUser(); - break; - case 8: - removeUser(); - break; - case 9: - searchUser(); - break; - case 10: - doesUserExist(); - break; - case 11: - addLibrarian(); - break; - case 12: - removeLibrarian(); - break; - case 13: - searchLibrarian(); - break; - case 14: - doesLibrarianExist(); - break; - } - } else { - System.out.println("password is invalid"); - } - } else { - System.out.println("username is invalid"); - } - } - } else { - System.out.println("plz enter you name"); - String Name = scan.nextLine(); - String password = scan.nextLine(); - for (int i = 0; i < 4; i++) { - if (Name.equals(rented.get(i).name)) { - if(password.equals(rented.get(i).password)){ - System.out.println("hi! as a user you have four options , plz go ahead and pick one "); - System.out.println("searchBook1,doesBookExist2,rentBook3,returnBook4"); - int number1 = scan.nextInt(); - switch(number1){ - case 1: - searchBook(); - break; - case 2: - doesBookExist(); - break; - case 3: - rentBook(); - rented.get(i).booksName= books.get(i).name; - break; - case 4: - returnBook(); - rented.get(i).booksName = null; - break; - } + 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{ + + + - }else{ - System.out.println("invalid password"); - } - }else{ - System.out.println("invalid name for a user"); - } - } + //} + } + 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 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!"); } -} \ No newline at end of file +} diff --git a/src/main/java/User.java b/src/main/java/User.java index 8c982fb..64e95cf 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,32 +1,81 @@ -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +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 class User extends Library { - String name; - String password; - String booksName; + // Attributes + private String username; + private String password; + private ArrayListrentBooks = new ArrayList(); - User(){ - name = "alex"; - password = "hi!"; - booksName = "hi"; + // Getter & Setter + public String getUsername() { + return username; } - User(String name , String password , String booksName){ - this.name = name; + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { this.password = password; - this.booksName= booksName; } - static User user1 = new User("ALEX","0935",null); - static User user2 = new User("DAVE","093541",null); - static User user3 = new User("TED","09354146",null); - public User(User user4) { - super(); + + // 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 void actionPerformed(ActionEvent e) { - + public String toString() { + return "User{" + + "username='" + username + '\'' + + ", password='" + password + '\'' + + ", rentBooks=" + rentBooks + + '}'; } -} +} \ No newline at end of file