diff --git a/Answers/LMS/Admin.java b/Answers/LMS/Admin.java new file mode 100644 index 0000000..15f7736 --- /dev/null +++ b/Answers/LMS/Admin.java @@ -0,0 +1,27 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +public class Admin extends User{ + File passAdminsFile = new File("passwordAdmin.txt"); + public Admin (String userName , String userID , String phoneNumber){ + super("AdminUserName", "AdminUserID", "AdminPhoneNumber"); + + try{ + boolean isPass = passAdminsFile.createNewFile(); + + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + } + + public void pass(String password){ + try{ + FileWriter writePass = new FileWriter("passwordAdmin.txt" , true); + writePass.append(password); + writePass.close(); + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + } +} diff --git a/Answers/LMS/Book.java b/Answers/LMS/Book.java new file mode 100644 index 0000000..b7d1f6a --- /dev/null +++ b/Answers/LMS/Book.java @@ -0,0 +1,22 @@ +import java.io.File; +import java.io.IOException; + +public class Book{ + File bookIdFile = new File("bookId.txt"); + File titleFile = new File("titleBook.txt"); + File authorFile = new File("authorBook.txt"); + File statusFile = new File("statusBook.txt"); + File descriptionFile = new File("descriptionBook.txt"); + + public Book(){ + try{ + boolean isId = bookIdFile.createNewFile(); + boolean isTitle = titleFile.createNewFile(); + boolean isAuthor = authorFile.createNewFile(); + boolean isStatus = statusFile.createNewFile(); + boolean isDescription = descriptionFile.createNewFile(); + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + } +} \ No newline at end of file diff --git a/Answers/LMS/Library.java b/Answers/LMS/Library.java new file mode 100644 index 0000000..d1473fb --- /dev/null +++ b/Answers/LMS/Library.java @@ -0,0 +1,755 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Scanner; + +public class Library { + Book newBooks = new Book(); + User newNormalUsers = new NormalUser("NormalUserName", "NormalUserID", "NormalPhoneNumber"); + User newAdmins = new Admin("AdminUserName", "AdminUserID", "AdminPhoneNumber"); + Rent newRent = new Rent(); + private String libraryName; + private int libraryCapacity; + private int bookCounter = 0 ; + private String openTime; + private String closeTime; + public Library(String libraryName , int libraryCapacity) { + this.libraryName = libraryName; + this.libraryCapacity = libraryCapacity; + } + + public String getLibraryName(){ + return libraryName; + } + + public void getCapacity(){ + System.out.println("Full Capacity: "+(libraryCapacity)); + System.out.println("Free Capacity: "+ (libraryCapacity-bookCounter)); + } + + public void setTime(String openTime , String closeTime){ + this.openTime = openTime; + this.closeTime = closeTime; + } + + public void getTime(){ + System.out.println("OpenTime: " + openTime); + System.out.println("CloseTime: " + closeTime); + } + + public void bookRepository(){ + int index = 1; + File bookIdFile = new File("bookId.txt"); + File titleFile = new File("titleBook.txt"); + File authorFile = new File("authorBook.txt"); + File statusFile = new File("statusBook.txt"); + File descriptionFile = new File("descriptionBook.txt"); + + try{ + Scanner titleScanner = new Scanner(titleFile); + Scanner authorScanner = new Scanner(authorFile); + Scanner statusScanner = new Scanner(statusFile); + Scanner descriptionScanner = new Scanner(descriptionFile); + Scanner idScanner = new Scanner(bookIdFile); + + while(titleScanner.hasNextLine()){ + System.out.print(index + ". "); + System.out.println(titleScanner.nextLine()); + System.out.print("\tAuthor: "); + System.out.println(authorScanner.nextLine()); + System.out.print("\tAbout This Book: "); + System.out.println(descriptionScanner.nextLine()); + System.out.println("\tID: " + idScanner.nextLine()); + System.out.print("\tAvailability Status: "); + if(statusScanner.nextLine().equals("true")){ + System.out.println("Avilable"); + }else{ + System.out.println("Not Avilable"); + } + System.out.print("\n__________________________________________\n"); + index++; + } + idScanner.close(); + titleScanner.close(); + authorScanner.close(); + descriptionScanner.close(); + statusScanner.close(); + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + } + + public void addBook(String bookName , String bookAuthor , String description){ + if(bookCounter tmpStatus = new ArrayList<>(); + Scanner scStatus = new Scanner(statusFile); + int indexTmp = 1; + while ((scStatus.hasNextLine())) { + if(indexTmp != index){ + tmpStatus.add(scStatus.nextLine()); + }else{ + String str = scStatus.nextLine(); + tmpStatus.add("false"); + } + indexTmp++; + } + scStatus.close(); + FileWriter editStatus = new FileWriter("statusBook.txt"); + for(int i=0 ; i tmp = new ArrayList<>(); + + //edit id + Scanner idScanner = new Scanner(bookIdFile); + + while(idScanner.hasNextLine()){ + if(!id.toString().equals(idScanner.nextLine())){ + tmp.add(idScanner.nextLine()); + }else{ + FileWriter trashFileWriter = new FileWriter("trashBookID.txt" , true); + trashFileWriter.append(idScanner.nextLine() + "\n"); + trashFileWriter.close(); + index = tmpIndex; + } + tmpIndex++; + } + + FileWriter editID = new FileWriter("bookId.txt"); + for(int i=0; i tmp = new ArrayList<>(); + int index = 0; + int tmpIndex = 1; + + //ID + tmpIndex = 1; + Scanner IDScanner = new Scanner(userIDFile); + while (IDScanner.hasNextLine()) { + if(!ID.equals(IDScanner.nextLine())){ + tmp.add(IDScanner.nextLine()); + }else{ + isRemove = true; + index = tmpIndex; + } + tmpIndex++; + } + IDScanner.close(); + FileWriter editID = new FileWriter("NormalUserID.txt"); + for(int i=0; i returnBook = new ArrayList<>(); + indx=1; + Scanner returnSc = new Scanner(rentTitle); + while(returnSc.hasNextLine()){ + if(indx != tmpIndx){ + returnBook.add(returnSc.nextLine()); + }else{ + String tmpStr = returnSc.nextLine(); + tmpStr = ""; + } + indx++; + } + returnSc.close(); + FileWriter writeNameBook = new FileWriter("rentBookName.txt"); + for(int i=0 ; i< returnBook.size() ; i++){ + writeNameBook.append(returnBook.get(i) + "\n"); + } + returnBook.clear(); + writeNameBook.close(); + //done + + File returnIdBook = new File("rentBookID.txt"); + Scanner returnIdBookSc = new Scanner(returnIdBook); + indx=1; + + while(returnIdBookSc.hasNextLine()){ + if(indx != tmpIndx){ + returnBook.add(returnIdBookSc.nextLine()); + }else { + String tmpStr = returnIdBookSc.nextLine(); + } + indx++; + } + returnIdBookSc.close(); + FileWriter writeIdBook = new FileWriter("rentBookID.txt" , true); + for(int i=0 ; i 5 ){ + System.out.println("Wrong Entry!"); + }else{ + if(correctOrd[0].equalsIgnoreCase("lib") && correctOrd.length == 5){ + if(correctOrd[1].equalsIgnoreCase("add")){ + if(correctOrd[2].equalsIgnoreCase("user")) { + System.out.println("Enter User PhoneNumber:"); + String phUser = orderScanner.next(); + + try { + File passAdminsFile = new File("passwordAdmin.txt"); + Scanner passScan = new Scanner(passAdminsFile); + boolean isOk = false; + while (passScan.hasNextLine()) { + if (correctOrd[4].equals(passScan.nextLine())) { + library.createAccount(correctOrd[3] , phUser); + break; + } + } + + if (isOk == false) { + System.out.println("Wrong Entry!"); + } + passScan.close(); + }catch (IOException e){ + System.out.println("Error 404\nPage Not Found!" + e); + } + }else if(correctOrd[2].equalsIgnoreCase("book")){ + System.out.println("Add A Description For Book: "); + String desBook = orderScanner.nextLine(); + library.addBook(correctOrd[3] , correctOrd[4] , desBook); + }else { + System.out.println("Wrong Entry!"); + } + }else{ + System.out.println("Wrong Entry!"); + } + }else if(correctOrd[0].equalsIgnoreCase("lib") && correctOrd.length == 3){ + if(correctOrd[1].equalsIgnoreCase("set") && correctOrd[2].equalsIgnoreCase("time")){ + System.out.println("Set OpenTime:"); + String open = orderScanner.nextLine(); + System.out.println("Set CloseTime:"); + String close = orderScanner.nextLine(); + library.setTime(open , close); + }else if(correctOrd[1].equalsIgnoreCase("show") && correctOrd[2].equalsIgnoreCase("members")){ + library.showMembers(); + } + }else if(correctOrd[0].equalsIgnoreCase("lib") && correctOrd.length == 4){ + if(correctOrd[1].equalsIgnoreCase("remove")){ + if(correctOrd[2].equalsIgnoreCase("member")){ + try { + File userIDFile = new File("NormalUserID.txt"); + Scanner fileScan = new Scanner(userIDFile); + boolean isOK = false; + while (fileScan.hasNextLine()){ + if(correctOrd[3].equals(fileScan.nextLine())){ + isOK = true; + break; + } + } + if(isOK){ + library.removeAccount(correctOrd[3]); + }else { + System.out.println("This ID Not Found."); + } + }catch (IOException e){ + System.out.println("Error 404\nPage Not Found!" + e); + } + + + }else if(correctOrd[2].equalsIgnoreCase("book")){ + try{ + File bookIdFile = new File("bookId.txt"); + Scanner fileSc = new Scanner(bookIdFile); + boolean isIT = false; + while (fileSc.hasNextLine()){ + if(correctOrd[3].equals(fileSc.nextLine())){ + isIT = true; + break; + } + } + if (isIT) { + library.removeBook(Integer.parseInt(correctOrd[3])); + } else { + System.out.println("This ID Not Found."); + } + } catch (IOException e) { + System.out.println("Error 404\nPage Not Found!" + e); + } + } + } + } else if (correctOrd[1].equalsIgnoreCase("get") && correctOrd.length == 3) { + if (correctOrd[2].equalsIgnoreCase("books")) { + library.bookRepository(); + } + } else if (correctOrd[1].equalsIgnoreCase("get") && correctOrd.length == 3) { + if (correctOrd[2].equalsIgnoreCase("help")) { + System.out.println("Dear Admin!"); + System.out.println("To Add A User, Type \"lib add user\" Then UserName."); + System.out.println("To Set Pprating Time, Type \"lib set time\" ."); + System.out.println("To Add A Book, Type \"lib add book\" Then book\'s Name (space) Then book\'Author."); + System.out.println("To Remove A Book, Type \"lib remove book\" Then Book\'s ID."); + System.out.println("To See Scheduel Of Books, Type \"lib get books\" ."); + System.out.println("To Remove A Member, Type \"lib remove member\" Then Member\'s ID."); + System.out.println("To See Members Information, Type \"lib sho members\" ."); + System.out.println("Enter 0 To Turn Back."); + String order = orderScanner.nextLine(); + for (; ; ) { + String adminOrder1 = orderScanner.nextLine(); + if (adminOrder1.equals("0")) { + break; + } else { + System.out.println("Wrong Entry.\nTry Again."); + } + } + } + } + } + break; + //admin + + + + case "2": + for (; ; ) { + System.out.println("Wellcome To " + nameLibrary + "!\n"); + System.out.println("Note: If You Have No Idea About Using This Library, Just Type \"lib get help\" To Read Guid Book."); + System.out.println("What Do You Want? "); + String ord = orderScanner.nextLine(); + if (ord.length() < 2 || ord.equals("") || ord.equals(null)) { + System.out.println("Wrong Entry!"); + } + finalOrd = ""; + newOrd = ord; + for (; ; ) { + newOrd = newOrd.trim(); + String someOrd = ""; + for (int i = 0; i < newOrd.length(); i++) { + if (newOrd.charAt(i) != ' ') { + someOrd += newOrd.charAt(i); + } else { + finalOrd += (someOrd + " "); + newOrd = newOrd.replace(someOrd, ""); + break; + } + } + if (someOrd.equals(newOrd)) { + finalOrd += (someOrd + " "); + newOrd = newOrd.replace(someOrd, ""); + break; + } + if (newOrd.equals("")) + break; + } + + //next orders + String correctOrd1[] = finalOrd.split(" "); + + if (correctOrd1[0].equals("lib")) { + if (correctOrd1[1].equalsIgnoreCase("join")) { + System.out.println("Enter Your Name:"); + String normalName = orderScanner.nextLine(); + System.out.println("Enter Your Phone Number:"); + String normalPhone = orderScanner.nextLine(); + library.createAccount(normalName, normalPhone); + + } else if (correctOrd1[1].equalsIgnoreCase("rent")) { + if (correctOrd1.length < 4) { + System.out.println("Wrong Entry!"); + } else { + try { + File bookIdFile = new File("bookId.txt"); + Scanner bookScanner = new Scanner(bookIdFile); + boolean isBookID = false; + while (bookScanner.hasNextLine()) { + if (correctOrd1[2].equals(bookScanner.nextLine())) { + isBookID = true; + break; + } + } + File userIDFile = new File("NormalUserID.txt"); + Scanner userScanner = new Scanner(userIDFile); + boolean isUserID = false; + while (userScanner.hasNextLine()) { + if (correctOrd1[3].equals(userScanner.nextLine())) { + isUserID = true; + break; + } + } + userScanner.close(); + + if (isBookID && isUserID) { + Integer bookID = Integer.parseInt(correctOrd1[2]); + Integer userID = Integer.parseInt(correctOrd1[3]); + library.addRent(bookID, userID); + + } else { + System.out.println("Wrong Entry!"); + } + + } catch (IOException e) { + System.out.println("error 404!\npage not found!" + e); + } + } + } else if (correctOrd1[1].equalsIgnoreCase("return")) { + if (correctOrd1.length < 3 || correctOrd1.length > 3) { + System.out.println("Wrong Entry!"); + } else { + try { + File forReturnNameBook = new File("rentBookName.txt"); + Scanner forReturnSc = new Scanner(forReturnNameBook); + boolean isOk = false; + while (forReturnSc.hasNextLine()) { + if (correctOrd1[2].equals(forReturnSc.nextLine())) { + isOk = true; + break; + } + } + if (isOk) { + library.returnBook(correctOrd1[2]); + System.out.println("done!"); + } else { + System.out.println("Wrong Entry!"); + } + } catch (IOException e) { + System.out.println("error 404!\npage not found!" + e); + } + + } + } else if (correctOrd1[1].equalsIgnoreCase("get")) { + if (correctOrd1.length < 3 || correctOrd1.length > 4) { + System.out.println("Wrong Entry!"); + } else if (correctOrd1.length == 3) { + switch (correctOrd1[2]) { + case "My": + case "my": + if (correctOrd1[3].equals("ID")) { + System.out.println("What Is Your PhoneNumber?"); + String phone = orderScanner.nextLine(); + library.getID(phone); +// try { +// File userNameFile = new File("NormalUserName.txt"); +// Scanner nameScan = new Scanner(userNameFile); +// int indx = 1; +// int tmpindx = 0; +// while (nameScan.hasNextLine()) { +// if (name.equals(nameScan.nextLine())) { +// tmpindx = indx; +// break; +// } +// indx++; +// } +// nameScan.close(); +// File userIDFile = new File("NormalUserID.txt"); +// Scanner IdScan = new Scanner(userIDFile); +// indx = 1; +// while (IdScan.hasNextLine()) { +// if (indx == tmpindx) { +// System.out.println(IdScan.nextLine()); +// break; +// } +// indx++; +// } +// } catch (IOException e) { +// System.out.println("error 404!\npage not found!" + e); +// } + } else { + System.out.println("Wrong Entry!"); + } + break; + + case "hrs": + library.getTime(); + break; + + case "Help": + case "help": + System.out.println("At First Tou Have To Join And Then Use Library Options. To Join Library, Type \"lib join\" "); + System.out.println("To Rent A book, Type \"lib rent\" And Type Book ID (space) Then Your ID."); + System.out.println("To See Your ID, Type \"lib get My ID\" ."); + System.out.println("To Return A Rented Book, Type \"lib return\" And Then Book Name."); + System.out.println("To See Library Operating Hours, type \"lib get hrs\" ."); + System.out.println("To See Available Books And Their ID, Type \"lib get available books\" ."); + System.out.println("Now Enter 0 To Turn Back."); + for (; ; ) { + String forBack = orderScanner.nextLine(); + if (forBack.equals("0")) { + break; + } else { + System.out.println("Wrong Entry.\nTry Again."); + } + } + break; + + default: + System.out.println("Wrong Entry!"); + } + } + } else { + if (correctOrd1[2].equals("available") && correctOrd1[3].equals("books")) { + library.bookRepository(); + } else { + System.out.println("Wrong Entry!"); + } + } + } + } + default: + System.out.println("Wrong Entry!"); + } + } + } + +} + + diff --git a/Answers/LMS/NormalUser.java b/Answers/LMS/NormalUser.java new file mode 100644 index 0000000..9259b2f --- /dev/null +++ b/Answers/LMS/NormalUser.java @@ -0,0 +1,18 @@ +import java.io.File; +import java.io.IOException; + +public class NormalUser extends User { + File dateUserFile = new File("dateUser.txt"); + + public NormalUser(String userName , String userID , String phoneNumber ){ + super("NormalUserName", "NormalUserID", "NormalPhoneNumber"); + + try{ + boolean isDate = dateUserFile.createNewFile(); + + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + } + +} diff --git a/Answers/LMS/Rent.java b/Answers/LMS/Rent.java new file mode 100644 index 0000000..bbd589a --- /dev/null +++ b/Answers/LMS/Rent.java @@ -0,0 +1,23 @@ +import java.io.File; +import java.io.IOException; + +public class Rent { + File bookName = new File("rentBookName.txt"); + File bookID = new File("rentBookID.txt"); + File userName = new File("rentUserName.txt"); + File userID = new File("rentUserID.txt"); + File rentDate = new File("rentDate.txt"); + + public Rent(){ + try{ + boolean isRentBookName = bookName.createNewFile(); + boolean isRentBookID = bookID.createNewFile(); + boolean isRentUserName = userName.createNewFile(); + boolean isRentUserID = userID.createNewFile(); + boolean isRentDate = rentDate.createNewFile(); + }catch(IOException e){ + System.out.println("error 404!\npage not found!"); + } + + } +} diff --git a/Answers/LMS/User.java b/Answers/LMS/User.java new file mode 100644 index 0000000..fef8a96 --- /dev/null +++ b/Answers/LMS/User.java @@ -0,0 +1,21 @@ +import java.io.File; + +public class User { + + public User(String userName, String userID, String phoneNumber) { + + File userNameFile = new File(userName + ".txt"); + + File userIDFile = new File(userID + ".txt"); + + File phoneNumberFile = new File(phoneNumber + ".txt"); + try { + boolean isName = userNameFile.createNewFile(); + boolean isID = userIDFile.createNewFile(); + boolean isPhone = phoneNumberFile.createNewFile(); + } catch (Exception e) { + System.out.println("error 404!\npage not found!"); + } + + } +}