diff --git a/Answers/Admin.java b/Answers/Admin.java new file mode 100644 index 0000000..c6a8365 --- /dev/null +++ b/Answers/Admin.java @@ -0,0 +1,18 @@ +public class Admin extends User { + private String password; + String uniqueAId; + public Admin(String name, String uniqueID, String phoneNumber, String password) { + super(name, uniqueID, phoneNumber); + this.password = password; + this.uniqueAId = uniqueID; + } + + public String getPassword() { + return password; + } + public String getUniqueId() { + return uniqueAId; + } + +} + diff --git a/Answers/Book.java b/Answers/Book.java new file mode 100644 index 0000000..4f86d78 --- /dev/null +++ b/Answers/Book.java @@ -0,0 +1,66 @@ +public class Book { + private int bookID; + private String title; + private String author; + private boolean availability; + private String description; + private boolean onLoan; + private String borrower; + private static int nextid = 1; + + public Book(String title, String author, String description) { + this.bookID = nextid++; + this.title = title; + this.author = author; + this.availability = true; + this.description = description; + this.onLoan = false; + this.borrower = null; + } + + public int getBookID() { + return bookID; + } + + public int getUniqueId() { + return bookID; + } + public void setUniqueId(int bookID){ + this.bookID = bookID; + } + public String getTitle() { + return title; + } + + public String getAuthor() { + return author; + } + + public boolean isAvailable() { + return availability; + } + + public void setAvailability(boolean availability){ + this.availability = availability; + } + public String getDescription() { + return description; + } + + public boolean isOnLoan() { + return onLoan; + } + + public String getBorrower() { + return borrower; + } + public void setOnLoan(boolean onLoan, String borrower) { + this.onLoan = onLoan; + this.borrower = borrower; + } + public String toString() { + return "ID: "+bookID+ "\nTitle: " + title + "\nAuthor: " + author + "\nSubtitle: " + description + + "\nAvailability: " + availability; + } +} + diff --git a/Answers/Library.java b/Answers/Library.java new file mode 100644 index 0000000..3d7c94d --- /dev/null +++ b/Answers/Library.java @@ -0,0 +1,64 @@ +import java.util.ArrayList; +import java.util.List; + +class Library { + private String name; + private int capacity; + private String operatingHours; + private static List books; + private List rents; + private List users; + + public Library(String name, int capacity, String operatingHours) { + this.name = name; + this.capacity = capacity; + this.operatingHours = operatingHours; + this.books = new ArrayList<>(); + this.rents = new ArrayList<>(); + this.users = new ArrayList<>(); + } + + public void addBook(Book book) { + if (books.size() < capacity) { + books.add(book); + System.out.println("Book added to the library: " + book.getTitle()); + } else { + System.out.println("Library is at full capacity. Cannot add more books."); + } + } + public static Book findBookById(int Id){ + for (Book book : books){ + if(book.getBookID() == Id){ + book.setAvailability(false); + System.out.println("Book found "+book.getTitle()); + return book; + } + } + System.out.println("Book not found in library"); + return null; + } + + public static void lendBook(int bookId, String borrower) { + Book book = findBookById(bookId); + if (book != null && !book.isOnLoan()) { + book.setOnLoan(true, borrower); + System.out.println("Book with ID " + bookId + " has been lent to " + borrower); + } else if (book != null && book.isOnLoan()) { + System.out.println("Book with ID " + bookId + " is already on loan"); + } else { + System.out.println("Book with ID " + bookId + " not found in the library"); + } + } + public static Book returnBook(int bookId) { + for (Book book : books){ + if(book.getBookID() == bookId){ + book.setAvailability(true); + System.out.println("Book found "+book.getTitle()); + return book; + } + } + System.out.println("Book not found in library"); + return null; + } +} + diff --git a/Answers/MyApp.java b/Answers/MyApp.java new file mode 100644 index 0000000..03552fc --- /dev/null +++ b/Answers/MyApp.java @@ -0,0 +1,219 @@ +import javax.lang.model.element.Name; +import java.util.*; +public class MyApp { + + public static void main(String[] args){ + Library library= new Library("My Library" , 100 , "9:00 AM - 10:00 PM"); + ArrayList admin = new ArrayList<>(); + admin.add(new Admin("Vania" , "40230212088","09333564285","1384")); + /**************************************************************************************************/ + Book book1 = new Book("The Shadows Between Us","Tricia Levenseller" , "Fantasy"); + Book book2 = new Book("Funny Story","Emily Henry" , "Comedy"); + Book book3 = new Book("The Women","Kristin Hannah" , "Homeric"); + List bookList = new ArrayList<>(); + bookList.add(book1); + bookList.add(book2); + bookList.add(book3); + library.addBook(book1); + library.addBook(book2); + library.addBook(book3); + Scanner enter = new Scanner(System.in); + String choice , name , password , command , Name , Password , Command ; + boolean m = true, t = true, n = true; + System.out.println("Welcome to my library."); + /********************************************************************************************/ + while (m){ + System.out.println("Identify yourself: "); + System.out.println("1.Admin"); + System.out.println("2.User"); + System.out.println("3.Exit"); + choice = enter.nextLine().trim().toLowerCase(); + switch (choice){ + case "admin": + while (t) { + System.out.println("Enter your name: "); + name = enter.nextLine().trim().toLowerCase(); + if (name.equals("Vania") || name.equals("vania")) { + System.out.println("Enter password: "); + password = enter.nextLine().trim().toLowerCase(); + if (password.equals("1384")) { + System.out.println("Hi Vania!"); + do { + System.out.println("What you want to do? (instead of using space write\"/\"(not for books name) and dont use <>"); + System.out.println("If you need help use(lib/-h) or you can \"Exit\""); + command = enter.nextLine(); + CheekAdminCommand(command, bookList, admin , library); + } while (!command.equals("Exit")); + t = false; + } else { + System.out.println("Wrong password."); + } + } else { + System.out.println("Wrong name."); + } + } + break; + /****************************************************************************************/ + case "user": + while (n) { + System.out.println("Enter your name: "); + Name = enter.nextLine().trim().toLowerCase(); + if (Name.equals("vani")) { + System.out.println("Enter your password: "); + Password = enter.nextLine().trim().toLowerCase(); + if (Password.equals("1111")) { + System.out.println("Hi!"); + do { + System.out.println("What you want to do? (instead of using space write\"/\"(not for books name) and dont use <>"); + System.out.println("If you need help use(lib/-h) or you can \"Exit\""); + Command = enter.nextLine().toLowerCase().trim(); + CheekUserCommand(Command, bookList); + } while (!Command.equals("Exit")); + n = false; + } else { + System.out.println("Wrong password."); + } + } else { + System.out.println("Wrong user name."); + } + } + break; + /***************************************************************************************/ + case "exit": + System.out.println("bye bye:)"); + m = false; + break; + default: + System.out.println("It's wrong."); + } + } + } +/*********************************************************************************************************/ + public static void CheekAdminCommand(String command , List books , List admins , Library library){ + String[] cheekcommand = command.split("/"); + if(cheekcommand[0].equals("lib")){ + if(cheekcommand[1].equals("-h")){ + System.out.println("lib/add/book///. --> Add a new book to the library.\n" + + "lib/get/hrs. --> Retrieve library operating hours.\n" + + "lib/add/member////. --> Add a new member to the library.\n" + + "lib/remove/member/. --> Remove a member from the library.\n" + + "lib/get/available/books. --> View available books." ); + } + else if(cheekcommand[1].equals("add")){ + + if (cheekcommand[2].equals("book")){ + Book book4 = new Book(cheekcommand[3],cheekcommand[4],cheekcommand[5]); + books.add(book4); + library.addBook(book4); + System.out.println("Book added successfully"); + + } + else if (cheekcommand[2].equals("member")) { + admins.add(new Admin(cheekcommand[3],cheekcommand[4],cheekcommand[5],cheekcommand[6])); + System.out.println("New Admin added successfully"); + } + else{ + System.out.println("Wrong command."); + } + + } + else if (cheekcommand[1].equals("get")) { + + if (cheekcommand[2].equals("hrs")){ + System.out.println("Library is open from 9.am to 10.pm"); + + } + else if (cheekcommand[2].equals("available")){ + for (Book book : books){ + System.out.println("************************************************"); + System.out.println(book.toString()); + } + } + else{ + System.out.println("Wrong command."); + } + } + else if (cheekcommand[1].equals("remove")) { + String uniqueAId = cheekcommand[3]; + removeAdmin(uniqueAId,admins); + } + else { + System.out.println("Its Wrong."); + } + + } + else if (cheekcommand[0].equals("Exit")){ + System.out.println("leaving Admin panel."); + }else { + System.out.println("Wrong command."); + } + } +/*********************************************************************************************************/ + public static void CheekUserCommand(String command , List books){ + String[] cheekcommand = command.split("/"); + if (cheekcommand[0].equals("lib")) { + if(cheekcommand[1].equals("-h")){ + System.out.println("lib/get/hrs. --> Retrieve library operating hours.\n" + + "lib/rent/. --> Rent a book from the library.\n" + + "lib/rent//. --> Rent a book for a specific member.\n" + + "lib/get/available/books. --> View available books for rental.\n" + + "lib/return/. --> Return a rented book to the library.\n"); + } + else if (cheekcommand[1].equals("rent")) { + if (cheekcommand.length == 6) { + int number = Integer.parseInt(cheekcommand[2]); + String memberName = cheekcommand[3]; + Library.lendBook(number,memberName); + } + else { + int number = Integer.parseInt(cheekcommand[2]); + Library.findBookById(number); + } + + } else if (cheekcommand[1].equals("get")) { + if (cheekcommand[2].equals("hrs")) { + System.out.println("Library is open from 9.am to 10.pm"); + } + else if (cheekcommand[2].equals("available")) { + for (Book book : books){ + System.out.println("************************************************"); + System.out.println(book.toString()); + } + } + else { + System.out.println("Wrong command."); + } + } + else if (cheekcommand[1].equals("return")) { + int number = Integer.parseInt(cheekcommand[2]); + Library.returnBook(number); + } + else { + System.out.println("Wrong command."); + } + } + else if (cheekcommand[0].equals("Exit")){ + System.out.println("leaving User panel."); + }else { + System.out.println("Wrong command."); + } + } +/*********************************************************************************************************/ + private static void removeAdmin(String uniqueAId , List admins){ + Iterator iterator = admins.iterator(); + boolean found = false; + while (iterator.hasNext()){ + Admin admin = iterator.next(); + if (admin.getUniqueId().equals(uniqueAId)){ + iterator.remove(); + found = true; + break; + } + } + if(!found){ + System.out.println("There isn't admin with this id."); + }else { + System.out.println("Admin deleted."); + } + } +} diff --git a/Answers/NormalUser.java b/Answers/NormalUser.java new file mode 100644 index 0000000..1b588f6 --- /dev/null +++ b/Answers/NormalUser.java @@ -0,0 +1,17 @@ +import java.util.Date; +class NormalUser extends User { + private Date registrationDate; + + public NormalUser(String name, String uniqueID, String phoneNumber) { + super(name, uniqueID, phoneNumber); + this.registrationDate = new Date(); + } + + public Date getRegistrationDate() { + return registrationDate; + } + + public void setRegistrationDate(Date registrationDate) { + this.registrationDate = registrationDate; + } +} diff --git a/Answers/Rent.java b/Answers/Rent.java new file mode 100644 index 0000000..e53560a --- /dev/null +++ b/Answers/Rent.java @@ -0,0 +1,14 @@ +import java.util.Date; +class Rent { + private Book book; + private NormalUser normalUser; + private int rentalID; + private Date rentalDate; + public Rent(Book book, NormalUser normalUser, int rentalID, Date rentalDate) { + this.book = book; + this.normalUser = normalUser; + this.rentalID = rentalID; + this.rentalDate = rentalDate; + } + +} diff --git a/Answers/User.java b/Answers/User.java new file mode 100644 index 0000000..6562b1f --- /dev/null +++ b/Answers/User.java @@ -0,0 +1,36 @@ +class User { + private String name; + private String uniqueID; + private String phoneNumber; + + public User(String name, String uniqueID, String phoneNumber) { + this.name = name; + this.uniqueID = uniqueID; + this.phoneNumber = phoneNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUniqueID() { + return uniqueID; + } + + public void setUniqueID(String uniqueID) { + this.uniqueID = uniqueID; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } +} +