diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d5f086e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Answers/40230112119/.gitignore b/Answers/40230112119/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/Answers/40230112119/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/Answers/40230112119/src/main/java/org/example/Admin.java b/Answers/40230112119/src/main/java/org/example/Admin.java new file mode 100644 index 0000000..3c85433 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/Admin.java @@ -0,0 +1,134 @@ +package org.example; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Admin extends User{ + private String password; + public Admin(String name, int uniqueID, String phone, String password) { + super(name, uniqueID, phone); + this.password = password; + } + public Admin() { + super(); + this.password = null; + } + public void setPassword(String password) { + this.password = password; + } + public String getPassword() { return password; } + + public void removeBook(ArrayList booksList, String title) { + for (int i = 0; i < booksList.size(); i++) { + if (title.equals(booksList.get(i))) { + booksList.remove(i); + break; + } + } + } + public void addNewUser(String[] cm) { + Scanner sc = new Scanner(System.in); + Library lib = new Library(); + boolean flag = true; + do { + flag = true; + for (int i = 0; i < lib.getUserList().size(); i++) { + if (cm[3].equalsIgnoreCase(Integer.toString(lib.getUserList().get(i).getUniqueID()))) { + flag = false; + break; + } + } + + if (!flag) { + System.out.println("This ID already exists. Enter another one. "); + cm[3] = sc.nextLine(); + } + } while(!flag); + + System.out.println("Enter their name: "); + String name = sc.nextLine(); + String phone = null; + flag = true; + do { + System.out.println("Enter your phone: "); + phone = sc.nextLine(); + String regex = "^(\\+98|0)?9\\d{9}$"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(phone); + + if (m.matches()) { + flag = true; + } else { + flag = false; + System.out.println("Invalid phone number. Try again."); + } + + } while (!flag); + + String timeStamp = new SimpleDateFormat("yyyy.MM.dd HH:mm").format(new java.util.Date()); + NormalUser newUser = new NormalUser(name, Integer.valueOf(cm[3]), phone, timeStamp); + lib.getUserList().add(newUser); + } + public boolean removeMember(int id) { + Library lib = new Library(); + boolean result = false; + for (int i = 0; i < lib.getUserList().size(); i++) { + if (id == lib.getUserList().get(i).getUniqueID()) { + lib.getUserList().remove(i); + result = true; + return result; + } + } + return result; + } + public void addNewAdmin(){ + Library lib = new Library(); + Scanner sc = new Scanner(System.in); + String name; + boolean flag = true; + do { + flag = false; + System.out.println("Enter the name of the user: "); + name = sc.nextLine(); + boolean user_found = false; + for (NormalUser i : lib.getUserList()) { + if (i.getName().equals(name)) { + flag = true; + user_found = true; + break; + } + } + if (user_found) + System.out.println("No user found. Try again."); + } while (!flag); + + System.out.println("Enter a password for them: "); + String password = sc.nextLine(); + + flag = true; + int uniqueID = 0; + do { + Random id = new Random(); + int tmpID = id.nextInt(9000) + 1000; + + flag = true; + + for(NormalUser i : lib.getUserList()) { + if (i.getUniqueID() == tmpID) { + flag = false; + break; + } + } + + if (flag) { + uniqueID = tmpID; // Update uniqueID only if ID is unique + } + } while(!flag); + +// lib.getAdminList().add() + } +} diff --git a/Answers/40230112119/src/main/java/org/example/Book.java b/Answers/40230112119/src/main/java/org/example/Book.java new file mode 100644 index 0000000..a135865 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/Book.java @@ -0,0 +1,43 @@ +package org.example; + +public class Book { + private int bookID; + private String title; + private String author; + private boolean status; + private String desc; + public Book(int bookID, String title, String author, String desc) { + this.bookID = bookID; + this.title = title; + this.author = author; + this.status = true; + this.desc = desc; + } + public Book() { + this.bookID = -1; + this.title = null; + this.author = null; + this.status = true; + this.desc = null; + } + public void setBookID(int bookID) { + this.bookID = bookID; + } + public void setTitle(String title) { + this.title = title; + } + public void setAuthor(String author) { + this.author = author; + } + public void setStatus(boolean status) { + this.status = status; + } + public void setDesc(String desc) { + this.desc = desc; + } + public int getBookID() { return bookID; } + public String getTitle() { return title; } + public String getAuthor() { return author; } + public boolean getStatus() { return status; } + public String getDesc() { return desc; } +} diff --git a/Answers/40230112119/src/main/java/org/example/Database.java b/Answers/40230112119/src/main/java/org/example/Database.java new file mode 100644 index 0000000..fa73bd9 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/Database.java @@ -0,0 +1,33 @@ +package org.example; + +import java.sql.*; + +public class Database { + static final String DB_URL = "jjdbc:mysql://localhost:3306/LMS"; + static final String USER = "root"; + static final String PASS = "lms613"; + public void db(String table, int command) { + try { + Connection con = DriverManager.getConnection(DB_URL, USER, PASS); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT * FROM " + table); + + switch(table) { + case "books": + switch(command) { + case 1: //update info + rs.moveToInsertRow(); + rs.updateString("title","John"); + rs.updateString("last","Paul"); + rs.updateInt("age",40); + case 2: + } + } + + } + catch(SQLException e) { + e.printStackTrace(); + } + } +} + diff --git a/Answers/40230112119/src/main/java/org/example/Library.java b/Answers/40230112119/src/main/java/org/example/Library.java new file mode 100644 index 0000000..73c6b19 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/Library.java @@ -0,0 +1,378 @@ +package org.example; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Random; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Library { + private final String operatingHours = "From 8 A.M. to 6 P.M."; + private ArrayList booksList = new ArrayList<>(); + private ArrayList userList = new ArrayList<>(); + private ArrayList adminList = new ArrayList<>(); + + public void addBookElement(Book obj) { + booksList.add(obj); + } + public void addUserElement(NormalUser obj) { + userList.add(obj); + } + public void addAdminElement(Admin obj) { adminList.add(obj); } + public String getOperatingHours() { + return operatingHours; + } + + public ArrayList getBooksList() { + return booksList; + } + + public ArrayList getUserList() { + return userList; + } + + public ArrayList getAdminList() { + return adminList; + } + + public void signup() { + Scanner sc = new Scanner(System.in); + System.out.println("Enter your name: "); + String name = sc.nextLine(); + String phone = null; + boolean flag = true; + do { + System.out.println("Enter your phone: "); + phone = sc.nextLine(); + String regex = "^(\\+98|0)?9\\d{9}$"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(phone); + + if (m.matches()) { + flag = true; + } else { + flag = false; + System.out.println("Invalid phone number. Try again."); + } + + } while (!flag); + + flag = true; + int uniqueID = 0; + do { + Random id = new Random(); + int tmpID = id.nextInt(9000) + 1000; + + flag = true; + + for(NormalUser i : userList) { + if (i.getUniqueID() == tmpID) { + flag = false; + break; + } + } + + if (flag) { + uniqueID = tmpID; // Update uniqueID only if ID is unique + } + } while(!flag); + + String timeStamp = new SimpleDateFormat("yyyy.MM.dd HH:mm").format(new java.util.Date()); + + System.out.println("\n\n\t\tWelcome " + name + "! Here is your unique ID: " + uniqueID + + "\t\t[Date of registration: " + timeStamp + "]"); + System.out.println("\nThank you for choosing our Library Management System." + + "\nWe're excited to provide you with an efficient and user-friendly platform for all your library needs." + + "\nHappy reading!"); + NormalUser newUser = new NormalUser(name, uniqueID, phone, timeStamp); + userList.add(newUser); + } + public String login(boolean isUser) { + System.out.println("Welcome back! Enter your name:"); + Scanner sc = new Scanner(System.in); + String name = sc.nextLine(); + String password; + + boolean flag = true; + if (isUser) { + do { + System.out.println("Enter your unique ID: "); + int uniqueID = sc.nextInt(); + flag = false; + + for(NormalUser i : userList) { + if (i.getUniqueID() == uniqueID) { + if (i.getName().equalsIgnoreCase(name)) { + flag = true; + break; + } + } + } + + if (!flag) { + System.out.println("Invalid ID; Try again. "); + } + } while(!flag); + + } + else { + do { + System.out.println("Enter your unique ID: "); + int uniqueID = sc.nextInt(); + flag = false; + + for(Admin i : adminList) { + if (i.getUniqueID() == uniqueID) { + if (i.getName().equalsIgnoreCase(name)) { + flag = true; + break; + } + } + } + + if (!flag) { + System.out.println("Invalid ID; Try again. "); + } + } while(!flag); + + do { + System.out.println("Enter your password: "); + password = sc.nextLine(); + flag = false; + + for(Admin i : adminList) { + if (i.getPassword().equals(password)) { + flag = true; + return password; + } + } + + if (!flag) { + System.out.println("Invalid password; Try again. "); + } + } while(!flag); + } + return null; + + } + + public void homePage() { + System.out.println("\nWelcome to our library! Please sign up or log in."); + System.out.println("1- Sign Up\t\t\t2- Log In"); + Scanner sc = new Scanner(System.in); + int choice = sc.nextInt(); + sc.nextLine(); + boolean isUser = true; + switch(choice) { + case 1: + signup(); + break; + case 2: + System.out.println("Choose your position.\n1- User\t\t\t2- Admin"); + int pos = sc.nextInt(); + isUser = true; + switch(pos) { + case 1: + login(isUser); + break; + case 2: + isUser = false; + login(isUser); + break; + } + } + + User u = new User(); + Book b = new Book(); + Admin a = new Admin(); + boolean flag = true; + outerLoop: + do { + flag = true; + System.out.println("\nWhat do you want to do?"); + Scanner scs = new Scanner(System.in); + String command = scs.nextLine(); + String[] cm = command.split(" "); + if (cm[0].equalsIgnoreCase("lib")) { + if (cm[1].equalsIgnoreCase("add")) { + if (cm[2].equalsIgnoreCase("book")) { + if (cm.length == 6) { + boolean retry = u.addBook(cm); + if (retry) { + flag = false; + } else { + System.out.println("Book added successfully. What do you want to do next?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } else if (cm[2].equalsIgnoreCase("member")) { + if (!isUser) { + if (cm.length == 5) { + a.addNewUser(cm); + System.out.println("Member added successfully. What do you want to do next?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } else { + System.out.println("Only admins can do this. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + else if (cm[1].equalsIgnoreCase("get")) { + if (cm[2].equalsIgnoreCase("hrs")) { + System.out.println(operatingHours); + flag = false; + } else if (cm[2].equalsIgnoreCase("available")) { + if (cm[3].equalsIgnoreCase("books")) { + for (Book l : getBooksList()) { + for (int k = 0; k < booksList.size(); k++) { + System.out.println("bookID\t\ttitle\t\tauthor"); + System.out.println(getBooksList().get(k).getBookID() + + "\t\t\t" + getBooksList().get(k).getTitle() + + "\t\t\t" + getBooksList().get(k).getAuthor()); + System.out.println("\nWhat do you want to do next?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + } + } else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + else if (cm[1].equalsIgnoreCase("rent")) { + + } + else if (cm[1].equalsIgnoreCase("remove")) { + if (!isUser) { + if (cm.length == 4) { + if (cm[2].equalsIgnoreCase("member")) { + int id = Integer.valueOf(cm[3]); + boolean result = a.removeMember(id); + if(result) { + System.out.println("Member successfully removed. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + else { + System.out.println("No member found. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + } else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + else { + System.out.println("Only admins can do this. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } + else { + System.out.println("Invalid command. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + choice = sc.nextInt(); + if (choice == 1) { + flag = false; + } else { + break outerLoop; + } + } + } while(!flag); + + System.out.println("Goodbye!!"); + } +} diff --git a/Answers/40230112119/src/main/java/org/example/MyApp.java b/Answers/40230112119/src/main/java/org/example/MyApp.java new file mode 100644 index 0000000..16ea8c0 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/MyApp.java @@ -0,0 +1,17 @@ +package org.example; + +import java.util.ArrayList; + +public class MyApp { + public static void main(String[] args) { + Book mine = new Book(1, "Dorian", "Wilde", "whatever"); + Library lib = new Library(); + lib.addBookElement(mine); + NormalUser p1 = new NormalUser("Harry", 8765, "09033457685", "2020.03.04 12:34"); + lib.addUserElement(p1); + Admin owner = new Admin("Mahta", 1234, "09036339284", "1234"); + lib.addAdminElement(owner); + + lib.homePage(); + } +} diff --git a/Answers/40230112119/src/main/java/org/example/NormalUser.java b/Answers/40230112119/src/main/java/org/example/NormalUser.java new file mode 100644 index 0000000..687a7ec --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/NormalUser.java @@ -0,0 +1,17 @@ +package org.example; + +import java.util.ArrayList; + +public class NormalUser extends User{ + private String date; + + public NormalUser(String name, int uniqueID, String phone, String date) { + super(name, uniqueID, phone); + this.date = date; + } + public NormalUser() { + User u = new User(); + this.date = null; + } + +} diff --git a/Answers/40230112119/src/main/java/org/example/Rent.java b/Answers/40230112119/src/main/java/org/example/Rent.java new file mode 100644 index 0000000..7d5813e --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/Rent.java @@ -0,0 +1,18 @@ +package org.example; + +public class Rent { + private int rentalID; + private int rentalDate; + Book toRent = new Book(); + NormalUser tenant = new NormalUser(); + + public void rentABook(Book title) { + Library lib = new Library(); + int index = lib.getBooksList().indexOf(title); + Book wanted = lib.getBooksList().get(index); + if (wanted.getStatus()) { + + wanted.setStatus(false); + } + } +} diff --git a/Answers/40230112119/src/main/java/org/example/User.java b/Answers/40230112119/src/main/java/org/example/User.java new file mode 100644 index 0000000..71a9497 --- /dev/null +++ b/Answers/40230112119/src/main/java/org/example/User.java @@ -0,0 +1,66 @@ +package org.example; + +import java.util.ArrayList; +import java.util.Scanner; + +public class User { + private String name; + private int uniqueID; + private String phone; + public User(String name, int uniqueID, String phone) { + this.name = name; + this.uniqueID = uniqueID; + this.phone = phone; + } + public User() { + this.name = ""; + this.uniqueID = -1; + this.phone = "0"; + } + public void setName(String name) { + this.name = name; + } + public void setUniqueID(int uniqueID) { + this.uniqueID = uniqueID; + } + public void setPhone(String phone) { + this.phone = phone; + } + + public String getName() { return name; } + public int getUniqueID() { return uniqueID; } + public String getPhone() { return phone; } + + public boolean addBook(String[] cm) { + Library lib = new Library(); + Scanner sc = new Scanner(System.in); + for (Book j : lib.getBooksList()) { + if (cm[3].equalsIgnoreCase(j.getTitle())) { + if (cm[4].equalsIgnoreCase(j.getAuthor())) { + System.out.println("We already have that book. What do you want to do?" + + "\n1- Try another command\t\t\t2- Exit"); + int choice = sc.nextInt(); + if (choice == 1) { + return true; + } else { + break; + } + } + } + } + + int bookID; + if (lib.getBooksList().isEmpty()) { + bookID = 1; + } + else { + Book lastbook = lib.getBooksList().get(lib.getBooksList().size() - 1); + bookID = lastbook.getBookID(); + bookID++; + } + Book newBook = new Book(bookID, cm[3], cm[4], cm[5]); + lib.getBooksList().add(newBook); + return false; + } + +} diff --git a/Answers/Add Your Projects Here.txt b/Answers/Add Your Projects Here.txt deleted file mode 100644 index e69de29..0000000 diff --git a/Library-Management-System.iml b/Library-Management-System.iml new file mode 100644 index 0000000..11ba312 --- /dev/null +++ b/Library-Management-System.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/Library-Management-System/org/example/Admin.class b/out/production/Library-Management-System/org/example/Admin.class new file mode 100644 index 0000000..d0610c8 Binary files /dev/null and b/out/production/Library-Management-System/org/example/Admin.class differ diff --git a/out/production/Library-Management-System/org/example/Book.class b/out/production/Library-Management-System/org/example/Book.class new file mode 100644 index 0000000..079e41e Binary files /dev/null and b/out/production/Library-Management-System/org/example/Book.class differ diff --git a/out/production/Library-Management-System/org/example/Database.class b/out/production/Library-Management-System/org/example/Database.class new file mode 100644 index 0000000..929d44f Binary files /dev/null and b/out/production/Library-Management-System/org/example/Database.class differ diff --git a/out/production/Library-Management-System/org/example/Library.class b/out/production/Library-Management-System/org/example/Library.class new file mode 100644 index 0000000..bf93681 Binary files /dev/null and b/out/production/Library-Management-System/org/example/Library.class differ diff --git a/out/production/Library-Management-System/org/example/MyApp.class b/out/production/Library-Management-System/org/example/MyApp.class new file mode 100644 index 0000000..88a0921 Binary files /dev/null and b/out/production/Library-Management-System/org/example/MyApp.class differ diff --git a/out/production/Library-Management-System/org/example/NormalUser.class b/out/production/Library-Management-System/org/example/NormalUser.class new file mode 100644 index 0000000..d94f78e Binary files /dev/null and b/out/production/Library-Management-System/org/example/NormalUser.class differ diff --git a/out/production/Library-Management-System/org/example/Rent.class b/out/production/Library-Management-System/org/example/Rent.class new file mode 100644 index 0000000..0982d7e Binary files /dev/null and b/out/production/Library-Management-System/org/example/Rent.class differ diff --git a/out/production/Library-Management-System/org/example/User.class b/out/production/Library-Management-System/org/example/User.class new file mode 100644 index 0000000..5c74e76 Binary files /dev/null and b/out/production/Library-Management-System/org/example/User.class differ