diff --git a/src/main/java/Book.java b/src/main/java/Book.java index 7cff6f8..6453fec 100644 --- a/src/main/java/Book.java +++ b/src/main/java/Book.java @@ -1,3 +1,22 @@ public class Book { //Book should contain name,author,year of publish and ISBN -} + private String bookName; + private String author; + private int year; + private int ISBN; + + public Book(String bookName, String author, int year, int ISBN) { + this.bookName = bookName; + this.author = author; + this.year = year; + this.ISBN = ISBN; + } + + public String getNameOfBook() { + return this.bookName + ""; + } + + public int getNumberOfBook() { + return this.ISBN; + } +} \ No newline at end of file diff --git a/src/main/java/Librarian.java b/src/main/java/Librarian.java index 321ce3a..14049ce 100644 --- a/src/main/java/Librarian.java +++ b/src/main/java/Librarian.java @@ -1,10 +1,17 @@ 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 - */ + private String username; + private String password; + public Librarian(String username, String password){ + this.username = username; + this.password = password; + } + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } } diff --git a/src/main/java/Library.java b/src/main/java/Library.java index 6c34354..7a76a5a 100644 --- a/src/main/java/Library.java +++ b/src/main/java/Library.java @@ -1,3 +1,7 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + public class Library { /* * The library should have a list of books. @@ -6,79 +10,200 @@ public class Library { * The library should have a list of users and a list of librarians. */ - //book related functions + private String libraryName; + private String book = "Harry Potter"; + private int num = 2; + private String librarian = "firstLibrarian"; + private String User = "Haleh"; + private String pass = "0000"; + + private static Librarian firstLibrarian = new Librarian("firstLibrarian", "0000"); + private static Book firstBook = new Book("Harry Potter", "J. K. Rowling", 1997, 2); + private static User firstUser = new User("Haleh", "0000"); + + //book + private ArrayList ExistedBooks; + private static Map NumberOfBooks; + + //librarian user + private static ArrayList Users = new ArrayList(); + private static ArrayList Librarians = new ArrayList(); + private static Map users = new HashMap(); + private static Map librarians; + + public Library(String libraryName) { + this.libraryName = libraryName; + ExistedBooks = new ArrayList(); + ExistedBooks.add(book); + NumberOfBooks = new HashMap(); + NumberOfBooks.put(book, num); - public void addBook(){ - //TODO + Librarians = new ArrayList(); + Librarians.add(firstLibrarian); + librarians = new HashMap(); + librarians.put(librarian, pass); + + Users = new ArrayList(); + Users.add(firstUser); + users = new HashMap(); + users.put(User, pass); } - public void removeBook(){ - //TODO + public ArrayList getExistedBooks() { + return ExistedBooks; } - public void searchBook(){ - //TODO + public Map getNumberOfBooks() { + return NumberOfBooks; } - public void updateBook(){ - //TODO + public ArrayList getLibrarians() { + return Librarians; } - public void doesBookExist(){ - //TODO + public Map getlibrarians() { + return librarians; } - public void increaseBook(){ - //TODO + public ArrayList getUsers() { + return Users; } - public void decreaseBook(){ - //TODO + public Map getusers() { + return users; } - //user related functions + ArrayList rentedBooks = firstUser.getRentedBooks(); + + //book related functions - public void addUser(){ - //TODO + public void addBook(Book book){ + ExistedBooks.add(book.getNameOfBook()); + NumberOfBooks.put(book.getNameOfBook(), book.getNumberOfBook()); } - public void removeUser(){ - //TODO + public void removeBook(String bookName){ + if(doesBookExist(bookName)) { + ExistedBooks.remove(bookName); + NumberOfBooks.remove(bookName); + System.out.println(bookName + " is removed!"); + } else { + System.out.println("This book doesn't exist!"); + } } - public void searchUser(){ - //TODO + public boolean doesBookExist(String bookName) { + if(NumberOfBooks.containsKey(bookName) && NumberOfBooks.get(bookName) != 0){ + return true; + } else { + System.out.println("No book in the list!"); + } + return false; } - public void updateUser(){ - //TODO + public void searchBook(String bookName){ + if(doesBookExist(bookName)) { + System.out.println("We have " + bookName); + } else { + System.out.println("No book with name " + bookName); + } } - public void doesUserExist(){ - //TODO + public void rentBook(String book){ + if(doesBookExist(book)){ + NumberOfBooks.put(book, NumberOfBooks.get(book) - 1); + firstUser.rentBook(book); + } else { + System.out.println("Number of this book = 0"); + } } - //librarian related functions + public void returnBook(String book){ + NumberOfBooks.put(book, NumberOfBooks.get(book) + 1); + firstUser.returnBook(book); + } - public void addLibrarian(){ - //TODO + //user related functions + + public void addUser(User user){ + Users.add(user); + users.put(user.getUsername(), user.getPassword()); + } + + public void removeUser(User user){ + if(doesUserExist(user)) { + Users.remove(user); + users.remove(user.getUsername()); + System.out.println(user.getUsername() + " is removed!"); + } else { + System.out.println("This book doesn't exist!"); + } + } + + public boolean doesUserExist(User user){ + if(users.containsKey(user.getUsername()) && users.containsValue(user.getPassword())){ + return true; + } else { + System.out.println("No member in the list!"); + } + return false; } - public void removeLibrarian(){ - //TODO + public void searchUser(User user){ + if(doesUserExist(user)) { + System.out.println(user.getUsername() + " exists"); + } else { + System.out.println("No member with username " + user.getUsername()); + } } - public void searchLibrarian(){ - //TODO + public void loginUser(User user){ + if(doesUserExist(user)) { + System.out.println("Hello " + user.getUsername()); + } else { + System.out.println("No member with username " + user.getUsername()); + } } - public void updateLibrarian(){ - //TODO + //librarian related functions + + public void addLibrarian(Librarian librarian){ + Librarians.add(librarian); + librarians.put(librarian.getUsername(), librarian.getPassword()); + } + + public void removeLibrarian(Librarian librarian){ + if(doesLibrarianExist(librarian)) { + Librarians.remove(librarian); + librarians.remove(librarian.getUsername()); + System.out.println(librarian.getUsername() + " is removed!"); + } else { + System.out.println("This librarian doesn't exist!"); + } } - public void doesLibrarianExist(){ - //TODO + public boolean doesLibrarianExist(Librarian librarian){ + if(librarians.containsKey(librarian.getUsername()) && librarians.containsValue(librarian.getPassword())){ + return true; + } else { + System.out.println("No librarian in the list!"); + } + return false; } + public void searchLibrarian(Librarian librarian){ + if(doesLibrarianExist(librarian)) { + System.out.println(librarian.getUsername() + " exists"); + } else { + System.out.println("No member with username " + librarian.getUsername()); + } + } + public void loginLibrarian(Librarian librarian){ + if(doesLibrarianExist(librarian)) { + System.out.println("Hello " + librarian.getUsername()); + } else { + System.out.println("No member with username " + librarian.getUsername()); + } + } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 9a347d3..2475fbc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,18 +1,161 @@ import java.util.Scanner; +import java.util.ArrayList; +import java.util.Map; 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) { + private static Scanner sc = new Scanner(System.in); + private static Library library = new Library("sun"); + //librarian + private static Librarian firstLibrarian = new Librarian("firstLibrarian", "0000"); + private static Book firstBook = new Book("Harry Potter", "J. K. Rowling", 1997, 2); + private static User firstUser = new User("Haleh", "0000"); + ArrayList ExistedBooks = library.getExistedBooks(); + Map NumberOfBooks = library.getNumberOfBooks(); + ArrayList Librarians = library.getLibrarians(); + Map librarians = library.getlibrarians(); + ArrayList Users = library.getUsers(); + Map users = library.getusers(); + + public static void main(String[] args){ + do + { System.out.println("Enter your choice from the following menu:"); + System.out.println("1.User 2.Librarian 3.Exit"); + int choice; + choice = sc.nextInt(); + if(choice == 1){ + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + User newUser = new User(username, password); + if(library.doesUserExist(newUser)){ + library.loginUser(newUser); + } else { + break; + } + } + else if(choice == 2){ + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + Librarian newLibrarian = new Librarian(username, password); + if(library.doesLibrarianExist(newLibrarian)){ + library.loginLibrarian(newLibrarian); + } else { + break; + } + } + else { + break; + } + runMenu(choice); + }while(true); } - public static void runMenu(){ - //TODO: + + public static void runMenu(int choice) { + int ex = 0; + do { + int option; + if (choice == 1) { + System.out.println("Enter your choice from the following menu:"); + System.out.println("1.Rent Book 2.Return Book 3.Exit"); + option = sc.nextInt(); + if (option == 1) { + System.out.println("Book name: "); + String bookName = sc.next(); + library.rentBook(bookName); + } else if (option == 2) { + System.out.println("Book name: "); + String bookName = sc.next(); + library.returnBook(bookName); + } else { + break; + } + System.out.println("Do you want to continue? 1.Yes 2.No"); + ex = sc.nextInt(); + } else if (choice == 2) { + System.out.println("Enter your choice from the following menu:"); + System.out.println("1.Add Book 2.Remove Book 3.Search Book"); + System.out.println("4.Add User 5.Remove User 6.Search User"); + System.out.println("7.Add Librarian 8.Remove librarian 9.Search Librarian"); + option = sc.nextInt(); + if (option == 1) { + System.out.println("Book name: "); + String bookName = sc.next(); + System.out.println("Author name: "); + String authorName = sc.next(); + System.out.println("Year of publish: "); + int Year = sc.nextInt(); + System.out.println("ISBN: "); + int ISBN = sc.nextInt(); + Book newBook = new Book(bookName, authorName, Year, ISBN); + library.addBook(newBook); + } else if (option == 2) { + System.out.println("Book name: "); + String bookName = sc.next(); + library.removeBook(bookName); + } else if (option == 3) { + System.out.println("Book name: "); + String bookName = sc.next(); + library.searchBook(bookName); + } else if (option == 4) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + User newUser = new User(username, password); + library.addUser(newUser); + } else if (option == 5) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + User newUser = new User(username, password); + library.removeUser(newUser); + } else if (option == 6) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + User newUser = new User(username, password); + library.searchUser(newUser); + } else if (option == 7) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + Librarian newLibrarian = new Librarian(username, password); + library.addLibrarian(newLibrarian); + } else if (option == 8) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + Librarian newLibrarian = new Librarian(username, password); + library.removeLibrarian(newLibrarian); + } else if (option == 9) { + System.out.println("Username: "); + String username = sc.next(); + System.out.println("Password: "); + String password = sc.next(); + Librarian newLibrarian = new Librarian(username, password); + library.searchLibrarian(newLibrarian); + } else { + break; + } + System.out.println("Do you want to continue? 1.Yes 2.No"); + ex = sc.nextInt(); + } + } while (ex == 1); } } diff --git a/src/main/java/User.java b/src/main/java/User.java index dbe4020..493e2c9 100644 --- a/src/main/java/User.java +++ b/src/main/java/User.java @@ -1,12 +1,40 @@ -public class User { +import java.util.ArrayList; + +public class User { //User should have a list of books //User should have a username and a password - public void rentBook(){ - //TODO + String book = "0"; + ArrayList rentedBooks; + private String username; + private String password; + + public User(String username, String password){ + this.username = username; + this.password = password; + rentedBooks = new ArrayList(); + rentedBooks.add(book); + } + + public ArrayList getRentedBooks() { + return rentedBooks; + } + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + public void rentBook(String book){ + rentedBooks.add(book); + System.out.println("You've rented " + book); } - public void returnBook(){ - //TODO + public void returnBook(String book){ + rentedBooks.remove(book); + System.out.println("You've returned " + book); } -} +} \ No newline at end of file