diff --git a/Answers/_40230112134/Book.txt b/Answers/_40230112134/Book.txt index 3d05472..733cab3 100644 --- a/Answers/_40230112134/Book.txt +++ b/Answers/_40230112134/Book.txt @@ -1,6 +1,3 @@ -1/a/a/a/true -2/s/s/s/true -3/d/d/d/true 5/f/f/f/true 6/g/g/g/true 7/hj/jkk/jk/true @@ -10,5 +7,6 @@ 104/sDA/ASDAS/add/true 288/4653/98465/8945632/true 940/lllllllllllll/lllllllllllll/lllllllllllll/true -66/;/;/;/true 871/;/'/'/true +66/;/;/;/true +3/d/d/d/true \ No newline at end of file diff --git a/Answers/_40230112134/src/main/java/org/example/Library.java b/Answers/_40230112134/src/main/java/org/example/Library.java index 3503d5b..a628bfa 100644 --- a/Answers/_40230112134/src/main/java/org/example/Library.java +++ b/Answers/_40230112134/src/main/java/org/example/Library.java @@ -175,8 +175,7 @@ else if (Objects.equals(list[1], "exit")) { } else if (Objects.equals(list[1], "rent") && list.length <= 3) { - Rent rent = new Rent(); - rent.RentBook(list[2]); + user.Rent(list[2]); } else if (Objects.equals(list[1], "add") && Objects.equals(list[2], "member")) { @@ -207,8 +206,7 @@ else if (Objects.equals(list[1], "remove") && Objects.equals(list[2], "member")) } else if (Objects.equals(list[1], "return")) { - Rent rent = new Rent(); - rent.ReturnBook(list[2]); + user.ReturnBook(list[2]); } } } diff --git a/Answers/_40230112134/src/main/java/org/example/Rent.java b/Answers/_40230112134/src/main/java/org/example/Rent.java index aec59fa..9b19a26 100644 --- a/Answers/_40230112134/src/main/java/org/example/Rent.java +++ b/Answers/_40230112134/src/main/java/org/example/Rent.java @@ -1,48 +1,19 @@ package org.example; +import java.io.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.*; -public class Rent extends UniqueID{ - Book book; - User user; +public class Rent { + Book book ; + User user ; LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - private final String RentalDate; + private final String RentalDate ; public String getRentalDate() { return RentalDate; } - public Rent(){ - book = new Book(); - user = new User(); + public Rent() + { RentalDate = currentDateTime.format(formatter); } - public void RentBook(String name) { - for (int i = 0; i < book.getTotal().size(); i++) - { - String[] list = book.getTotal().get(i).split("/"); - if (Objects.equals(list[1],name) || Objects.equals(list[4],"true")) - { - book.getTotal().remove(i); - System.out.println("The book has been successfully rented."); - System.out.println("enjoy , Bye."); - String temp = list[0] + "/" + list[1] + "/" + list[2] + "/" + list[3] + "/" + RentalDate ; - book.getTotal().add(temp); - break; - } - } - } - public void ReturnBook(String name) { - for (int i = 0; i < book.getTotal().size(); i++) - { - String[] list = book.getTotal().get(i).split("/"); - if (Objects.equals(list[1],name)) - { - book.getTotal().remove(i); - System.out.println("Thank you for returning the book"); - String temp = list[0] + "/" + list[1] + "/" + list[2] + "/" + list[3] + "/" + "true" ; - book.getTotal().add(temp); - break; - } - } - } } \ No newline at end of file diff --git a/Answers/_40230112134/src/main/java/org/example/User.java b/Answers/_40230112134/src/main/java/org/example/User.java index 4c31686..7df5470 100644 --- a/Answers/_40230112134/src/main/java/org/example/User.java +++ b/Answers/_40230112134/src/main/java/org/example/User.java @@ -7,6 +7,7 @@ public class User extends UniqueID { Book book = new Book(); + Rent rent = new Rent(); private String Name; private String IDUser; private String PhoneNumber; @@ -94,7 +95,8 @@ public void AddBook(String name , String author , String des) System.out.println("Your Unique ID is : " + book.getIDBook()); } } - public void ChapFileBook() { + public void ChapFileBook() + { for (int i = 0; i < book.getTotal().size(); i++) { String[] list = book.getTotal().get(i).split("/"); @@ -106,99 +108,87 @@ public void ChapFileBook() { } public void SearchBook(String sentence) { - boolean flag = false; - ReadFileBook("Book.txt"); - char[] word = sentence.toCharArray(); +// boolean flag = false; +// ReadFileBook("Book.txt"); +// char[] word = sentence.toCharArray(); +// for (int i = 0; i < book.getTotal().size(); i++) +// { +// char[] jomle = book.getTotal().get(i).toCharArray(); +// for (int j = 0; j < jomle.length; j++) +// { +// for (int k = 0; k < word.length; k++) +// { +// if (word[k] == jomle[j]) +// { +// flag = true; +// } +// } +// } +// if (flag) +// { +// flag = false; +// String[] list = book.getTotal().get(i).split("/"); +// System.out.println("Your ID book: " + list[0]); +// System.out.println("Your Title: " + list[1]); +// System.out.println("Your Author: " + list[2]); +// System.out.println("Your Description: " + list[3]); +// System.out.println("Your Availability Status: " + list[4]); +// } +// } +// WriteFileBook("Book.txt"); + } + public void Delete(String number) + { +// for (int i = 0; i < book.getTotal().size(); i++) +// { +// String line1 = book.getTotal().get(i); +// String[] list = line1.split("/"); +// if (Objects.equals(list[0], number)) +// { +// System.out.println("The deletion was successful"); +// book.getTotal().remove(i); +// break; +// } +// } +// book.setTotal(book.getTotal()); + } + public void Rent(String name) + { for (int i = 0; i < book.getTotal().size(); i++) { - char[] jomle = book.getTotal().get(i).toCharArray(); - for (int j = 0; j < jomle.length; j++) + String[] list = book.getTotal().get(i).split("/"); + if (Objects.equals(list[1],name)) { - for (int k = 0; k < word.length; k++) + if (Objects.equals(list[4],"true")) { - if (word[k] == jomle[j]) - { - flag = true; - } + book.getTotal().remove(i); + System.out.println("The book has been successfully rented."); + System.out.println("enjoy , Bye."); + String test = list[0] + "/" + list[1] + "/" + list[2] + "/" + list[3] + "/" + rent.getRentalDate() ; + book.getTotal().add(test); + break; + } + else + { + System.out.println("you can not rent this book because the book rented"); } - } - if (flag) - { - flag = false; - String[] list = book.getTotal().get(i).split("/"); - System.out.println("Your ID book: " + list[0]); - System.out.println("Your Title: " + list[1]); - System.out.println("Your Author: " + list[2]); - System.out.println("Your Description: " + list[3]); - System.out.println("Your Availability Status: " + list[4]); } } - WriteFileBook("Book.txt"); } - public void Delete(String number) + public void ReturnBook(String name) { - ReadFileBook("Book.txt"); for (int i = 0; i < book.getTotal().size(); i++) { - String line1 = book.getTotal().get(i); - String[] list = line1.split("/"); - if (Objects.equals(list[0], number)) + String[] list = book.getTotal().get(i).split("/"); + if (Objects.equals(list[1],name)) { - System.out.println("The deletion was successful"); book.getTotal().remove(i); - Rewind(i); + System.out.println("Thank you for returning the book"); + String test = list[0] + "/" + list[1] + "/" + list[2] + "/" + list[3] + "/" + "true" ; + book.getTotal().add(test); break; } } - book.setTotal(book.getTotal()); - WriteFileBook("Book.txt"); - } - public int FindBigID(ArrayList total) - { - int bigerid = 0; - for (int i = 0; i < total.size(); i++) - { - String[] list = total.get(i).split("/"); - char[] id = list[0].toCharArray(); - int[] numbersArray = new int[id.length]; - int[] numbers = new int[id.length]; - int x=0; - for (int j = 0; j < id.length; j++) - { - numbersArray[j] = ((int) id[j])-48; - } - int c=1; - for (int j = 0; j < id.length; j++) - { - x += (numbersArray[j]*c); - c *= 10; - } - if (x > bigerid) - { - bigerid = x; - } - } - return bigerid; - } - public void Rewind(int num) - { - ArrayList test = new ArrayList<>(); - for (int i = num; i < book.getTotal().size() ; i++) - { - String[] list = book.getTotal().get(i).split("/"); - int number = Integer.parseInt(list[0]) -1; - list[0] = Integer.toString(number); - String temp = list[0] + "/" + list[1] + "/" + list[2] + "/" + list[3] + "/" + list[4]; - test.add(temp); - } - for (int i = 0; i < book.getTotal().size(); i++) - { - book.getTotal().remove(num); - } - for (String s : test) - { - book.getTotal().add(s); - } } } class NormalUser extends User {