From 94a4a90cec4f3b9266787bc83b61d178fcc71c3d Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Fri, 29 Apr 2016 23:12:33 -0700 Subject: [PATCH] overdueFine(int[]), newBook() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BookFactory.java - newBook() now returns the new ‘Book’ created instead of ‘void’ - if getBook() failed, it now returns an empty ‘Book’ Book.java - overdueFine(int[]) is now rewritten --- Book.java | 32 ++++++++++++++++++++++++-------- BookFactory.java | 11 +++++++---- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Book.java b/Book.java index db695a1..4173a3e 100644 --- a/Book.java +++ b/Book.java @@ -83,20 +83,36 @@ class Book public double overdueFine(int[] currentDay) { double fine; + boolean monChange = false; - if(currentDay[0] > dueDate[0] || currentDay[1] > (dueDate[1] + 1)) + while(currentDay[0] > dueDate[0]) { - return 5; - } - else if(currentDay[1] > dueDate[1]) - { - fine = (double)(currentDay[2] + 30 - dueDate[2]) * 0.25; + currentDay[0]--; + currentDay[1]+=12; } - else + + while(currentDay[1] > dueDate[1]) { - fine = (double)(currentDay[2] - dueDate[2]) * 0.25; + int mon = currentDay[1] % 12; + + if(mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) + { + currentDay[2]+=31; + } + else if(mon == 2) + { + currentDay[2]+=28; + } + else + { + currentDay[2]+=30; + } + + currentDay[1]--; } + fine = (double)(currentDay[2] - dueDate[2]) * 0.25; + if(fine > 5) { return 5; diff --git a/BookFactory.java b/BookFactory.java index b3b38c7..804630a 100644 --- a/BookFactory.java +++ b/BookFactory.java @@ -15,20 +15,24 @@ class BookFactory id = 0; } - public void newBook() + public Book newBook() { Book temp = new Book(id); books.add(temp); id++; + + return temp; } - public void newBook(String title) + public Book newBook(String title) { Book temp = new Book(title, id); books.add(temp); id++; + + return temp; } public Book getBook(int index) @@ -50,7 +54,6 @@ class BookFactory System.out.println("Error 404 : Book not found"); - Book empty = new Book(); - return empty; + return newBook(); } }