From 729bbf9033214accf061141346ed6dfc94aba41e Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Fri, 10 Jun 2016 16:25:30 -0700 Subject: [PATCH] Main.java - Added returnBook(User, Book) which will update both the users and the books upon success on returning the book - Added currentDay() that would return the current date in int array - Added calDueDate(int) that takes the current date from currentDay() and calculate the due date from there --- Main.java | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/Main.java b/Main.java index c99d7f5..cef4a95 100644 --- a/Main.java +++ b/Main.java @@ -3,14 +3,114 @@ * Last edited : 5/31/2016 */ +import java.util.Date + class Main { Exception BookNotFound = new Exception("Error 404 : Book not found"); static BookFactory books = new BookFactory(); + static UserFactory users = new UserFactory(); + static Date date = new Date(); public static void main(String[] args) { } + public static boolean returnBook(User user, Book book) + { + User newUser = user; + + if(newUser.returnBook(book.getId())) + { + book.returned(); + + books.update(book); + users.update(user, newUser); + + return true; + } + + return false; + } + + public static int[] calDueDate(int days) + { + int[] dueDate = currentDay(); + + for (int i = 0; i < days; i++) + { + if(dueDate[2] < 28) + { + dueDate[2]++; + } + else if(dueDate[1] == 12) + { + if(dueDate[2] == 31) + { + dueDate[0]++; + dueDate[1] = 1; + dueDate[2] = 1; + } + } + else if(dueDate[1] == 2) + { + if((dueDate[0] % 4) == 0) + { + if(dueDate[2] == 29) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[2] == 28) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[1] == 4 || dueDate[1] == 6 || dueDate[1] == 9 || dueDate[1] == 11) + { + if(dueDate[2] == 30) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[2] == 31) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + + return dueDate; + } + + public static int[] currentDay() + { + int[] currentDay = new int[3](); + + currentDay[0] = date.getYear(); + currentDay[1] = date.getMonth(); + currentDay[2] = date.getDate(); + + return currentDay; + } }