From 00f9e5c388c423181fcc00fd8ace374de0cbcc87 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Tue, 4 Jul 2017 16:02:24 -0700 Subject: [PATCH] Converted Book class to Kotlin --- pom.xml | 83 ++++++++++-- src/main/java/libsys/Book.java | 187 -------------------------- src/main/java/libsys/BookFactory.java | 7 +- src/main/java/libsys/MainGUI.java | 11 +- src/main/kotlin/libsys/Book.kt | 162 ++++++++++++++++++++++ 5 files changed, 242 insertions(+), 208 deletions(-) delete mode 100644 src/main/java/libsys/Book.java create mode 100644 src/main/kotlin/libsys/Book.kt diff --git a/pom.xml b/pom.xml index fcb5339..569d1dc 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,12 @@ 1.0 LibrarySystem http://maven.apache.org + + + 1.1.3-2 + + + junit @@ -19,6 +25,17 @@ org.json chargebee-1.0 + + org.jetbrains.kotlin + kotlin-stdlib-jre8 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test + ${kotlin.version} + test + @@ -30,15 +47,6 @@ libsys.Main - - org.apache.maven.plugins - maven-compiler-plugin - 3.5 - - 1.6 - 1.6 - - org.codehaus.mojo cobertura-maven-plugin @@ -64,6 +72,63 @@ + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + process-sources + + compile + + + + src/main/java + src/main/kotlin + + + + + test-compile + process-test-sources + + test-compile + + + + src/test/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5 + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + + + 1.6 + 1.6 + + diff --git a/src/main/java/libsys/Book.java b/src/main/java/libsys/Book.java deleted file mode 100644 index 400e04f..0000000 --- a/src/main/java/libsys/Book.java +++ /dev/null @@ -1,187 +0,0 @@ -package libsys; -/* - * Written by : Bin Hong Lee - * Last edited : 6/4/2017 - */ - -/** - * Book object - */ -public class Book -{ - private String title; - private int id; - private String status; - private int[] dueDate = new int[3]; - - /** - * Create new Book with given id (Usually used as placeholders) - * @param id Identification number of the Book - */ - public Book(int id) - { - this.id = id; - this.status = "NOT AVAILABLE"; - } - - /** - * Create new Book with given title, id and status (Usually used for new Book creation) - * @param title Title of the Book - * @param id Identification number of the Book - * @param status Status of the Book - */ - public Book(String title, int id, String status) - { - this.title = title; - this.id = id; - this.status = status; - } - - /** - * Create new Book with given id, title, status and dueDate (Usually used for initialization from database) - * @param id Identification number of the Book - * @param title Title of the Book - * @param status Status of the Book - * @param dueDate Due date of the Book - */ - public Book(int id, String title, String status, int[] dueDate) - { - this.title = title; - this.id = id; - this.status = status; - this.dueDate = dueDate; - } - - /** - * Returns the title of the Book - * @return title - */ - public String getTitle() - { - if (title!=null) - { - return title; - } else { - return "UNDEFINED"; - } - } - - /** - * Returns the status of the Book - * @return status (AVAILABLE || RENTED || RESERVED || NOT AVAILABLE) - */ - public String getStatus() - { - return status; - } - - /** - * Returns the id of the Book - * @return id - */ - public int getId() - { - return id; - } - - /** - * Returns the due date of the Book - * @return due date ([yyyy][mm][dd]) - */ - int[] getDueDate() - { - return dueDate; - } - - /** - * Set the title to the given title - * @param title The new title of the Book - */ - public void setTitle(String title) - { - this.title = title; - } - - /** - * Call to rent book, return if renting is successful - * @param dueDate The new due date of the Book - * @return if renting is successful - */ - boolean rent(int[] dueDate) - { - //Check if the book is available to be rented - if(!"AVAILABLE".equals(status)) - { - return false; - } - - //Update the due date of the book - this.dueDate = dueDate; - //Update book status - status = "RENTED"; - return true; - } - - /** - * Return the Book to the shelf - */ - void returned() - { - //Update book status - status = "AVAILABLE"; - } - - /* - * Calculate the overdue fine - * @param currentDay Today's date - * @return Price of the fine - */ - /* - public double overdueFine(int[] currentDay) - { - double fine; - - if(currentDay[0] < dueDate[0] || (currentDay[0] == dueDate[0] && currentDay[1] < dueDate[1]) || (currentDay[0] == dueDate[0] && currentDay[1] == dueDate[1] && currentDay[2] <= dueDate[2])) - { - return 0; - } - - while(currentDay[0] > dueDate[0]) - { - currentDay[0]--; - currentDay[1]+=12; - } - - while(currentDay[1] > dueDate[1]) - { - 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; - } - else - { - return fine; - } - } - */ -} diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index 5fc3f85..023b893 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -124,7 +124,7 @@ public class BookFactory * @param index id of the Book to be found * @return Book with the given id */ - public Book getBook(int index) + Book getBook(int index) { return search(index, 0, books.size() - 1); } @@ -169,10 +169,11 @@ public class BookFactory * @param title Title of the Book to be found * @return Book with the given title */ - public Book getBook(String title) + Book getBook(String title) { for (Book temp : books) { - if (temp.getTitle().equals(title)) { + if (temp.getTitle().equals(title)) + { return temp; } } diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 7614bca..99a3d5c 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -1,12 +1,11 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 6/4/2017 + * Last edited : 7/4/2017 */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.concurrent.CountDownLatch; /** * Main Graphic User Interface @@ -15,11 +14,9 @@ import java.util.concurrent.CountDownLatch; public class MainGUI extends javax.swing.JFrame { Handler handler; - Settings settings; + private Settings settings; Book book; User user; - boolean bookExist = false; - boolean userExist = false; /** * Creates a new MainGUI @@ -425,7 +422,6 @@ public class MainGUI extends javax.swing.JFrame bookDueDate.setText("Due date : "); } book = thisBook; - bookExist = true; editBookBtn.setVisible(true); } catch (Exception e) @@ -434,7 +430,6 @@ public class MainGUI extends javax.swing.JFrame bookTitle.setText(""); bookStatus.setText(""); bookDueDate.setText(""); - bookExist = false; editBookBtn.setVisible(false); } } @@ -452,7 +447,6 @@ public class MainGUI extends javax.swing.JFrame returnBtn.setVisible(thisUser.bookStatus().length > 0); editUserBtn.setVisible(true); user = thisUser; - userExist = true; } catch (Exception e) { @@ -462,7 +456,6 @@ public class MainGUI extends javax.swing.JFrame userBooks.setText(""); rentBtn.setVisible(false); returnBtn.setVisible(false); - userExist = false; editUserBtn.setVisible(false); } } diff --git a/src/main/kotlin/libsys/Book.kt b/src/main/kotlin/libsys/Book.kt new file mode 100644 index 0000000..91c130b --- /dev/null +++ b/src/main/kotlin/libsys/Book.kt @@ -0,0 +1,162 @@ +package libsys + +/* + * Written by : Bin Hong Lee + * Last edited : 7/4/2017 + */ + +/** + * Book object + */ +class Book +{ + /** + * Title of the Book + */ + var title: String? = null + set + get() + { + if (field != null) + { + return field + } + else + { + return "UNDEFINED" + } + } + + /** + * ID of the Book + */ + var id: Int = 0 + private set + /** + * Status of the Book ( AVAILABLE | NOT AVAILABLE | RESERVED | RENTED ) + */ + var status: String? = null + private set + /** + * Due date for the book to be returned (if rented) + */ + var dueDate = IntArray(3) + private set + get + + /** + * Create new Book with given id (Usually used as placeholders) + * @param id Identification number of the Book + */ + constructor(id: Int) { + this.id = id + this.status = "NOT AVAILABLE" + } + + /** + * Create new Book with given title, id and status (Usually used for new Book creation) + * @param title Title of the Book + * @param id Identification number of the Book + * @param status Status of the Book + */ + constructor(title: String, id: Int, status: String) { + this.title = title + this.id = id + this.status = status + } + + /** + * Create new Book with given id, title, status and dueDate (Usually used for initialization from database) + * @param id Identification number of the Book + * @param title Title of the Book + * @param status Status of the Book + * @param dueDate Due date of the Book + */ + constructor(id: Int, title: String, status: String, dueDate: IntArray) { + this.title = title + this.id = id + this.status = status + this.dueDate = dueDate + } + + /** + * Call to rent book, return if renting is successful + * @param dueDate The new due date of the Book + * @return if renting is successful + */ + fun rent(dueDate: IntArray): Boolean { + //Check if the book is available to be rented + if ("AVAILABLE" != status) { + return false + } + + //Update the due date of the book + this.dueDate = dueDate + //Update book status + status = "RENTED" + return true + } + + /** + * Return the Book to the shelf + */ + fun returned() { + //Update book status + status = "AVAILABLE" + } + + /* + * Calculate the overdue fine + * @param currentDay Today's date + * @return Price of the fine + */ + + /* + public double overdueFine(int[] currentDay) + { + double fine; + + if(currentDay[0] < dueDate[0] || (currentDay[0] == dueDate[0] && currentDay[1] < dueDate[1]) || (currentDay[0] == dueDate[0] && currentDay[1] == dueDate[1] && currentDay[2] <= dueDate[2])) + { + return 0; + } + + while(currentDay[0] > dueDate[0]) + { + currentDay[0]--; + currentDay[1]+=12; + } + + while(currentDay[1] > dueDate[1]) + { + 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; + } + else + { + return fine; + } + } + */ +}