Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Answers/Admin.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

66 changes: 66 additions & 0 deletions Answers/Book.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

64 changes: 64 additions & 0 deletions Answers/Library.java
Original file line number Diff line number Diff line change
@@ -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<Book> books;
private List<Rent> rents;
private List<User> 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;
}
}

219 changes: 219 additions & 0 deletions Answers/MyApp.java
Original file line number Diff line number Diff line change
@@ -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> 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<Book> 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<Book> books , List<Admin> admins , Library library){
String[] cheekcommand = command.split("/");
if(cheekcommand[0].equals("lib")){
if(cheekcommand[1].equals("-h")){
System.out.println("lib/add/book/<name>/<author>/<subtitle>. --> Add a new book to the library.\n" +
"lib/get/hrs. --> Retrieve library operating hours.\n" +
"lib/add/member/<name>/<studentID>/<PhoneNumber>/<password>. --> Add a new member to the library.\n" +
"lib/remove/member/<memberID>. --> 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<Book> 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/<bookId>. --> Rent a book from the library.\n" +
"lib/rent/<bookId>/<memberName>. --> Rent a book for a specific member.\n" +
"lib/get/available/books. --> View available books for rental.\n" +
"lib/return/<bookName>. --> 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<Admin> admins){
Iterator<Admin> 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.");
}
}
}
17 changes: 17 additions & 0 deletions Answers/NormalUser.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading