Last active
December 20, 2018 19:34
-
-
Save must1/95fc8d7f4eee567102aaec64dac2e22b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.controller.penalty; | |
import bookrental.service.penalty.PenaltyPaymentService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.DeleteMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
public class PenaltyController { | |
private final PenaltyPaymentService penaltyPaymentService; | |
@Autowired | |
public PenaltyController(PenaltyPaymentService penaltyPaymentService) { | |
this.penaltyPaymentService = penaltyPaymentService; | |
} | |
@PutMapping("paypenalty/{userID}") | |
public String payPenalty(@PathVariable int userID) { | |
return penaltyPaymentService.payPenalty(userID); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.service.penalty; | |
import bookrental.model.account.User; | |
import bookrental.repository.account.UserRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
@Service | |
public class PenaltyPaymentService { | |
private final UserRepository userRepository; | |
@Autowired | |
public PenaltyPaymentService(UserRepository userRepository) { | |
this.userRepository = userRepository; | |
} | |
public String payPenalty(int userID) { | |
User user = userRepository.findById(userID).orElse(null); | |
if (userRepository.doesAccountExistsWithGivenID(userID)) { | |
assert user != null; | |
long amountOfCashToPay = user.getAmountOfCashToPay(); | |
if (amountOfCashToPay == 0) { | |
return "You have nothing to pay!"; | |
} else { | |
user.setAmountOfCashToPay(0); | |
System.out.println(user.getAmountOfCashToPay()); | |
return "Thanks for paying! To next!"; | |
} | |
} else | |
return "Account does not exist"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.service.penalty; | |
import bookrental.model.account.User; | |
import bookrental.model.book.BookRentals; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.stereotype.Service; | |
import java.time.temporal.ChronoUnit; | |
@Slf4j | |
@Service | |
public class PenaltyService { | |
private long hours; | |
private long minutes; | |
private boolean checkIfPenaltyIsNeeded(BookRentals bookToReturn) { | |
hours = ChronoUnit.HOURS.between(bookToReturn.getDateOfRental(), bookToReturn.getDateOfReturn()); | |
minutes = ChronoUnit.MINUTES.between(bookToReturn.getDateOfRental(), bookToReturn.getDateOfReturn()); | |
log.info("hours: " + hours + " minutes: " + minutes); | |
return minutes > 1 || hours > 0; | |
} | |
private long calculatePenalty() { | |
long amountOfCashToPay; | |
amountOfCashToPay = hours * 60 + minutes - 1; // I took away 1, because one minute is free. Next minutes are paid. | |
return amountOfCashToPay; | |
} | |
public void executePenaltyProcess(BookRentals bookToReturn, User user) { | |
if (checkIfPenaltyIsNeeded(bookToReturn)) { | |
long amountOfCashToPay = calculatePenalty(); | |
System.out.println("You need to pay: " + amountOfCashToPay + " $. Please go to paypenalty/userID to settle " + | |
"the arrears, otherwise you can not rent more books!"); | |
user.setAmountOfCashToPay(amountOfCashToPay); | |
} else { | |
System.out.println("You have nothing to pay! To next!"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"name": "admin", | |
"password": "123", | |
"amountOfCashToPay": 0 | |
}, | |
{ | |
"id": 2, | |
"name": "piotri", | |
"password": "123,", | |
"amountOfCashToPay": 2 <<<<<<<<<< | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bookrental.service.book.rentals; | |
import bookrental.model.account.User; | |
import bookrental.model.book.Book; | |
import bookrental.model.book.BookRentals; | |
import bookrental.repository.account.UserRepository; | |
import bookrental.repository.book.BookRentalsRepository; | |
import bookrental.repository.book.BookRepository; | |
import bookrental.service.penalty.PenaltyService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.time.LocalDateTime; | |
@Service | |
public class ReturnBookService { | |
private final BookRentalsRepository bookRentalsRepository; | |
private final BookRepository bookRepository; | |
private final UserRepository userRepository; | |
private final PenaltyService penaltyCheckerService; | |
@Autowired | |
public ReturnBookService(BookRentalsRepository bookRentalsRepository, BookRepository bookRepository, UserRepository userRepository, PenaltyService penaltyCheckerService) { | |
this.bookRentalsRepository = bookRentalsRepository; | |
this.bookRepository = bookRepository; | |
this.userRepository = userRepository; | |
this.penaltyCheckerService = penaltyCheckerService; | |
} | |
public String returnBook(int userID, int bookID) { | |
if (bookRentalsRepository.isBookRentedWithGivenIDByUserWithGivenID(bookID, userID)) { | |
BookRentals bookToReturn = bookRentalsRepository.getBookBybBookID(bookID); | |
User user = userRepository.findById(userID).orElse(null); | |
assert bookToReturn != null; | |
BookRentals preparedBookToReturn = prepareBookToReturn(bookToReturn); | |
penaltyCheckerService.executePenaltyProcess(bookToReturn,user); | |
updateBookAvailabilityAndSaveToDb(bookID); | |
bookRentalsRepository.delete(preparedBookToReturn); | |
} else { | |
throw new IllegalArgumentException("Book was not rented!"); | |
} | |
return "Book was returned"; | |
} | |
private BookRentals prepareBookToReturn(BookRentals preparedBookToReturn) { | |
assert preparedBookToReturn != null; | |
preparedBookToReturn.setDateOfReturn(LocalDateTime.now()); | |
return preparedBookToReturn; | |
} | |
private void updateBookAvailabilityAndSaveToDb(int bookID) { | |
Book bookToReturn = bookRepository.findById(bookID).orElse(null); | |
assert bookToReturn != null; | |
bookToReturn.setAvailable(true); | |
bookRepository.save(bookToReturn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment