Skip to content

Commit

Permalink
add rent and return method
Browse files Browse the repository at this point in the history
  • Loading branch information
Homa-A committed May 17, 2024
1 parent a539f44 commit fc1f0b4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 122 deletions.
6 changes: 2 additions & 4 deletions Answers/_40230112134/Book.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
6 changes: 2 additions & 4 deletions Answers/_40230112134/src/main/java/org/example/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down Expand Up @@ -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]);
}
}
}
Expand Down
43 changes: 7 additions & 36 deletions Answers/_40230112134/src/main/java/org/example/Rent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
146 changes: 68 additions & 78 deletions Answers/_40230112134/src/main/java/org/example/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("/");
Expand All @@ -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<String> 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<String> 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 {
Expand Down

0 comments on commit fc1f0b4

Please sign in to comment.