diff --git a/Answers/40230112113/Admin.class b/Answers/40230112113/Admin.class new file mode 100644 index 0000000..9809859 Binary files /dev/null and b/Answers/40230112113/Admin.class differ diff --git a/Answers/40230112113/Admin.java b/Answers/40230112113/Admin.java new file mode 100644 index 0000000..5abe79c --- /dev/null +++ b/Answers/40230112113/Admin.java @@ -0,0 +1,18 @@ +public class Admin extends User +{ + private String Password; + + public Admin(String name , int ID , String number , String Password) + { + super(name, ID, number); + this.setPassword(Password); + } + public String getPassword() + { + return Password; + } + public void setPassword(String Password) + { + this.Password=Password; + } +} \ No newline at end of file diff --git a/Answers/40230112113/Book.class b/Answers/40230112113/Book.class new file mode 100644 index 0000000..eea5ae1 Binary files /dev/null and b/Answers/40230112113/Book.class differ diff --git a/Answers/40230112113/Book.java b/Answers/40230112113/Book.java new file mode 100644 index 0000000..fb600d0 --- /dev/null +++ b/Answers/40230112113/Book.java @@ -0,0 +1,57 @@ +public class Book +{ + private String Title; + private String Description; + private Boolean IsAvailable; + private String Author; + private int BookID=1000; + + public Book(String Title , String Description , Boolean IsAvailable , String Author , int BookID) + { + this.Author=Author; + this.Description=Description; + this.BookID=BookID++; + this.IsAvailable=IsAvailable; + this.Title=Title; + } + public String getTitle() + { + return Title; + } + public void setTitle(String title) + { + this.Title=title; + } + public String getDescription() + { + return Description; + } + public void setDescription(String description) + { + this.Description=description; + } + public String getAuthor() + { + return Author; + } + public void setAuthor(String author) + { + this.Author=author; + } + public Boolean getIsAvailable() + { + return IsAvailable; + } + public Boolean setIsAvailable(Boolean newIsAvailable) + { + return IsAvailable=newIsAvailable; + } + public int getBookID() + { + return BookID; + } + /*public String toString() + { + return "Title: "+Title+"Description: "+Description+"Author: "+Author+"BookID: "+BookID; + }*/ +} diff --git a/Answers/40230112113/CLI.class b/Answers/40230112113/CLI.class new file mode 100644 index 0000000..8bde3bb Binary files /dev/null and b/Answers/40230112113/CLI.class differ diff --git a/Answers/40230112113/CLI.java b/Answers/40230112113/CLI.java new file mode 100644 index 0000000..e1e5397 --- /dev/null +++ b/Answers/40230112113/CLI.java @@ -0,0 +1,254 @@ +import java.util.Scanner; + +public class CLI +{ + //i shouldn't have written this class. using only the library class was sufficient :') + + public static final String RESET = "\u001B[0m"; + public static final String RED = "\u001B[31m"; + public static final String GREEN = "\u001B[32m"; + public static final String YELLOW = "\u001B[33m"; + public static final String PURPLE = "\u001B[35m"; + public static final String BLUE = "\u001B[34m"; + public static final String CYAN = "\u001B[36m"; + public static final String WHITE = "\u001B[37m"; + public static final String BLACK = "\u001B[30m"; + + static boolean IsAdmin=false; + + static Scanner sc = new Scanner(System.in); + + Library library; + public CLI(Library library) + { + this.library=library; + } + + Boolean First_Run=true; + public void Run() + { + if(First_Run) + { + System.out.println(BLUE+"WELCOME"+RESET); + First_Run=false; + } + System.out.println(PURPLE+"1-ADMIN\n"+"2-NORMAL USER\n"+"3-Exit\n"+RESET); + int a = sc.nextInt(); + switch(a) + { + case 1: + AdminPanel(); + break; + case 2: + NormalUserPanel(); + break; + case 3: + System.out.println(GREEN+"Have a nice day!"+RESET); + break; + default: + System.out.println("wrong entry. please try again."); + Run(); + } + } + + + Boolean First_time_ad=true; + public void AdminPanel() + { + System.out.println(BLUE+"WELCOME TO ADMIN PANEL"+RESET); + System.out.println("please enter your name and password"); + System.out.print("Name: "); + String AdminName = sc.nextLine(); + + if(AdminName.equalsIgnoreCase("exit")) + { + Run(); + } + if(First_time_ad) + { + First_time_ad=false; + IsAdmin=true; + library.First_admin(); + AllCommands(); + } + else if(First_time_ad==false) + { + Admin admin = null; + if(library.CheckName(AdminName)==AdminName) + { + + System.out.print("password: "); + String password=sc.nextLine(); + if(library.CheckPassword(password,AdminName)) + { + System.out.println("welcome "+AdminName); + IsAdmin=true; + AllCommands(); + } + } + else + { + System.out.println("no match found. please try again."); + AdminPanel(); + } + } + } + + + Boolean First_time_us=true; + public void NormalUserPanel() + { + if(First_time_us) + { + System.out.println("WELCOME TO RAIN LIBRARY"); + First_time_us=false; + } + System.out.println("Please enter your Name and Phone Number"); + System.out.println("to go back to the main menu please enter \"exit\" "); + String name = sc.nextLine(); + if(true) + { + System.out.print("Phone Number: "); + String phonenumber = sc.nextLine(); + if(library.Checknumber(phonenumber)) + { + System.out.println("welcome "+name); + IsAdmin=false; + AllCommands(); + } + } + } + + + public void AllCommands() + { + System.out.println("input \"lib help\" to see all the commands available"); + System.out.print("enter the command: "); + String command = sc.nextLine(); + command.replaceAll("[^a-zA-Z0-9]", ""); + String[] splitted = command.split(" "); + //boolean isCorrect=true; + //while(isCorrect) + //{ + if(splitted[0].equals("lib")) + { + switch(splitted[1].toLowerCase()) + { + case "add": + { + if(IsAdmin==true) + { + if(splitted[2].equalsIgnoreCase("book")) + { + String s=""; + int i=5; + for(;;) + { + s+=splitted[i].toString(); + i++; + if(splitted[i]==null) + break; + } + library.addbook(splitted[3],splitted[4],s); + AllCommands(); + } + else if(splitted[2].equalsIgnoreCase("admin")) + { + library.addAdmin(splitted[3],splitted[4]); + AllCommands(); + } + } + else + { + System.out.println("Permission not granted. you are not an admin. please try again."); + AllCommands(); + } + } + case "get": + { + if(splitted[2].equalsIgnoreCase("available")) + { + if((splitted[3].equalsIgnoreCase("books"))||(splitted[3].equalsIgnoreCase("book"))) + { + library.showbooks(); + AllCommands(); + } + else + { + System.out.println("wrong entry. please try again."); + AllCommands(); + } + } + else if((splitted[2].equalsIgnoreCase("hrs"))||(splitted[2].equalsIgnoreCase("hours"))||(splitted[2].equalsIgnoreCase("hour"))) + { + System.out.println(library.getWorkinghours()); + AllCommands(); + } + else + { + System.out.println("wrong entry. please try again."); + AllCommands(); + } + } + case "rent": + { + library.Rentbook(splitted[2], splitted[3], splitted[4]); + AllCommands(); + } + case "remove": + { + if(splitted[2].equalsIgnoreCase("admin")) + { + library.RemoveAdmin(splitted[3]); + } + else if(splitted[2].equalsIgnoreCase("book")) + { + library.Removebook(); + } + else + { + System.out.println("wrong entry. please try again."); + AllCommands(); + } + } + case "return": + { + library.returnbook(splitted[2]); + AllCommands(); + } + case "help": + { + HelpCommand(); + AllCommands(); + } + default: + { + System.out.println("wrong entry. please try again."); + AllCommands(); + } + } + } + else if(splitted[0].equalsIgnoreCase("exit")) + { + Run(); + } + else + { + System.out.println("wrong entry. please try again."); + AllCommands(); + } + //} + } + public void HelpCommand() + { + System.out.println(RED+"lib add book : Add a new book to the library."+RESET); + System.out.println(RED+"lib get hrs: Retrieve library operating hours."+RESET); + System.out.println(YELLOW+"lib remove book : Remove a book from the library."+RESET); + System.out.println(GREEN+"lib add admin : Add a new member to the library (admin privilege required)."+RESET); + System.out.println(GREEN+"lib rent : Rent a book for a specific member."+RESET); + System.out.println(CYAN+"lib get available books: View available books for rental."+RESET); + System.out.println(BLUE+"lib remove admin : Remove a member from the library (admin privilege required)."+RESET); + System.out.println(PURPLE+"lib return : Return a rented book to the library."+RESET); + System.out.println(PURPLE+"exit: Go back to MainMenu"+RESET); + } +} \ No newline at end of file diff --git a/Answers/40230112113/Library.class b/Answers/40230112113/Library.class new file mode 100644 index 0000000..8b9ca20 Binary files /dev/null and b/Answers/40230112113/Library.class differ diff --git a/Answers/40230112113/Library.java b/Answers/40230112113/Library.java new file mode 100644 index 0000000..9f3701a --- /dev/null +++ b/Answers/40230112113/Library.java @@ -0,0 +1,279 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Library +{ + static Scanner sc = new Scanner(System.in); + + public static final String RESET = "\u001B[0m"; + public static final String RED = "\u001B[31m"; + public static final String GREEN = "\u001B[32m"; + public static final String YELLOW = "\u001B[33m"; + public static final String PURPLE = "\u001B[35m"; + public static final String BLUE = "\u001B[34m"; + + + private String LibName; + private int capacity; + private String Workinghours; + private ArrayList books = new ArrayList<>(); + private ArrayList users = new ArrayList<>(); + private ArrayList rents = new ArrayList<>(); + + + public Library(String LibName , int capacity , String Workinghours) + { + this.setname(LibName); + this.setcapacity(capacity); + this.Workinghours=Workinghours; + this.books=new ArrayList(); + this.users=new ArrayList(); + this.rents=new ArrayList(); + } + int BookID=1; + int UserID=1; + int RentID=1; + + public String getLibName() + { + return LibName; + } + public void setname(String LibName) + { + this.LibName=LibName; + } + public int getCapacity() + { + return capacity; + } + public void setcapacity(int capacity) + { + this.capacity=capacity; + } + public String getWorkinghours() + { + return Workinghours; + } + + public void First_admin() + { + System.out.print("please enter your name: "); + String name = sc.nextLine(); + System.out.print("please enter your password: "); + String password = sc.nextLine(); + System.out.print("please enter your number: "); + boolean check_it = false; + String number=""; + while(!check_it) + { + number = sc.nextLine(); + check_it=Checknumber(number); + } + Admin admin = new Admin(name, UserID, number, password); + UserID++; + users.add(admin); + } + + public void addAdmin(String name , String password) + { + String number=""; + boolean check_it=false; + while(check_it==false) + { + System.out.println("please enter your number"); + number=sc.nextLine(); + check_it=Checknumber(number); + } + Admin admin = new Admin(name, UserID, number, password); + UserID++; + users.add(admin); + System.out.println(name+" "+password); + } + + public void RemoveAdmin(String ID) + { + Integer id = Integer.valueOf(ID); + //to convert string to integer :> + for(User i : users) + { + if(i.getID()==id) + { + users.remove(i); + } + } + } + + + public void addbook(String title , String author , String description) + { + System.out.print("availability: "); + Boolean availability=sc.nextBoolean(); + Book newbook = new Book(title, description, availability, author, BookID); + BookID++; + books.add(newbook); + } + + + public void showbooks() + { + for (Book showbook : books) + { + if(showbook.getIsAvailable()) + { + System.out.println(showbook.getTitle()+"by"+showbook.getAuthor()); + } + } + } + + public void returnbook(String name) + { + for (Book i : books) + { + if(i.getTitle().equalsIgnoreCase(name)) + { + i.setIsAvailable(true); + } + } + } + + public void Rentbook(String name , String title , String ID) + { + NormalUser u = null; + Book b = null; + Integer id = Integer.valueOf(ID); + if(CheckName(name)==name) + { + for(User i : users) + { + + if(i.getName().equalsIgnoreCase(name)) + { + if(i.getID()==id) + { + u=(NormalUser) i; + break; + } + else + { + System.out.println("incorrect id. please try again."); + return; + } + } + } + + for(Book i : books) + { + if((i.getTitle().equalsIgnoreCase(title))) + { + b=i; + break; + } + else + { + System.out.println("book not found. please try again."); + return; + } + } + Rent rent = new Rent(b, u, RentID); + RentID++; + rents.add(rent); + b.setIsAvailable(false); + } + else + { + System.out.println("user not found. please try again."); + return; + } + } + + public void Removebook() + { + System.out.print("input the book title: "); + String booktitle=sc.nextLine(); + for(Book i : books) + { + if(i.getTitle().equalsIgnoreCase(booktitle)) + { + books.remove(i); + } + } + } + + + + //***************** checking system******************// + public Boolean CheckPassword(String password, String name) + { + boolean hm=false; + for (User i : users) + { + if (CheckName(i.getName())==name) + { + //Admin admin=null; + //int admin_id=i.getID(); + if(users.indexOf(password)==users.indexOf(name)) + { + hm=true; + break; + } + else + { + hm=false; + break; + } + } + } + return hm; + } + + public String CheckName(String name) + { + for (User i : users) + { + if (i.getName().equalsIgnoreCase(name)) + { + return name; + } + } + System.out.println("no match found."); + return "false"; + } + + public Boolean Checknumber(String num) + { + Boolean haha=true; + if ((num.length()==11)&&((num.charAt(0)=='0')&&(num.charAt(1)=='9'))) + { + for(int i=2;i'9')||(num.charAt(i)<'0')) + { + haha=false; + break; + } + } + } + else if ((num.length()==10)&&(num.charAt(0)=='9')) + { + //num=0+num; + for(int i=1;i'9')||(num.charAt(i)<'0')) + { + haha=false; + break; + } + } + } + else + { + haha=false; + } + if(!haha) + System.out.println("Wrong entry. Try again."); + + return haha; + } + + //**************************************************// +} diff --git a/Answers/40230112113/MyApp.class b/Answers/40230112113/MyApp.class new file mode 100644 index 0000000..bcbcc20 Binary files /dev/null and b/Answers/40230112113/MyApp.class differ diff --git a/Answers/40230112113/MyApp.java b/Answers/40230112113/MyApp.java new file mode 100644 index 0000000..bbba8f1 --- /dev/null +++ b/Answers/40230112113/MyApp.java @@ -0,0 +1,9 @@ +public class MyApp +{ + public static void main(String[] args) + { + Library library = new Library("RAIN", 50 , "7-21"); + CLI cli = new CLI(library); + cli.Run(); + } +} diff --git a/Answers/40230112113/NormalUser.class b/Answers/40230112113/NormalUser.class new file mode 100644 index 0000000..e7bc38b Binary files /dev/null and b/Answers/40230112113/NormalUser.class differ diff --git a/Answers/40230112113/NormalUser.java b/Answers/40230112113/NormalUser.java new file mode 100644 index 0000000..993999d --- /dev/null +++ b/Answers/40230112113/NormalUser.java @@ -0,0 +1,20 @@ +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class NormalUser extends User +{ + private String Userdate; + //converting to a known format in form of String + + public NormalUser(String name , int ID , String number) + { + super(name, ID, number); + DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime userdate = LocalDateTime.now(); + this.Userdate=userdate.format(CUSTOM_FORMATTER); + } + public String getUserDate() + { + return Userdate; + } +} diff --git a/Answers/40230112113/Rent.class b/Answers/40230112113/Rent.class new file mode 100644 index 0000000..af9b354 Binary files /dev/null and b/Answers/40230112113/Rent.class differ diff --git a/Answers/40230112113/Rent.java b/Answers/40230112113/Rent.java new file mode 100644 index 0000000..0bd22d6 --- /dev/null +++ b/Answers/40230112113/Rent.java @@ -0,0 +1,52 @@ +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class Rent +{ + private Book book; + private NormalUser normaluser; + private int RentID; + + private String Rentdate; + + public Rent(Book book , NormalUser normaluser , int RentID) + { + this.RentID=RentID; + this.setBook(book); + this.setNormaluser(normaluser); + + DateTimeFormatter CUSTOM_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime rentdate = LocalDateTime.now(); + + this.Rentdate = rentdate.format(CUSTOM_FORMATTER); + } + public Book getBook() + { + return book; + } + public void setBook(Book book) + { + this.book=book; + } + public NormalUser getNormaluser() + { + return normaluser; + } + public void setNormaluser(NormalUser normaluser) + { + this.normaluser=normaluser; + } + public int getRentID() + { + return RentID; + } + public String getRentdate() + { + return Rentdate; + } + + /*public String toString() + { + return RentID+Rentdate; + }*/ +} diff --git a/Answers/40230112113/User.class b/Answers/40230112113/User.class new file mode 100644 index 0000000..2ec8413 Binary files /dev/null and b/Answers/40230112113/User.class differ diff --git a/Answers/40230112113/User.java b/Answers/40230112113/User.java new file mode 100644 index 0000000..2e5c3ba --- /dev/null +++ b/Answers/40230112113/User.java @@ -0,0 +1,40 @@ +public class User +{ + private String name; + private int ID; + private String number; + public User(String name , int ID , String number) + { + this.setName(name); + this.ID=ID; + this.setName(number); + } + //it is is better to use setter instead of "this. =" formation + + public String getName() + { + return name; + } + public void setName(String name) + { + this.name=name; + } + public String getNumber() + { + return number; + } + public void setNumber(String number) + { + this.number=number; + } + public int getID() + { + return ID; + } + //I had to search so many times to find this cool thing here :> + //I'm doing this so that i can work with arraylist easily + /*public String toString() + { + return name+ID+number; + }*/ +} \ No newline at end of file