From af3dd212d18cac8b01f436f5f1076491b1707e9e Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Tue, 4 Jul 2017 16:15:19 -0700 Subject: [PATCH] Converted User class to Kotlin --- .travis.yml | 2 - src/main/java/libsys/BookFactory.java | 2 +- src/main/java/libsys/User.java | 154 -------------------------- src/main/kotlin/libsys/Book.kt | 6 +- src/main/kotlin/libsys/User.kt | 114 +++++++++++++++++++ 5 files changed, 118 insertions(+), 160 deletions(-) delete mode 100644 src/main/java/libsys/User.java create mode 100644 src/main/kotlin/libsys/User.kt diff --git a/.travis.yml b/.travis.yml index f300081..563eb3a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,6 @@ matrix: - os: osx - os: linux jdk: oraclejdk8 - - os: linux - jdk: oraclejdk7 script: "mvn cobertura:cobertura" diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index 023b893..8129859 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -1,7 +1,7 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 6/4/2017 + * Last edited : 7/4/2017 */ import java.util.List; diff --git a/src/main/java/libsys/User.java b/src/main/java/libsys/User.java deleted file mode 100644 index 1da9357..0000000 --- a/src/main/java/libsys/User.java +++ /dev/null @@ -1,154 +0,0 @@ -package libsys; -/* - * Written by : Bin Hong Lee - * Last edited : 6/4/2017 - */ - -import java.util.*; - -/** - * User object - */ -public class User -{ - private String name; - private int id; - private int limit; - private List books = new ArrayList(); - - /** - * Creates a new User with name, id and limit (usually used to add new User) - * @param name Name of this User - * @param id id of this User - * @param limit Limit of Book this User can borrow - */ - public User(String name, int id, int limit) - { - this.name = name; - this.id = id; - this.limit = limit; - } - - /** - * Creates a new User with name, id, limit and books (usually used to initialize from database) - * @param name Name of this User - * @param id id of this User - * @param limit Limit of Book this User can borrow - * @param books ArrayList of Book ids borrowed by the User - */ - public User(String name, int id, int limit, ArrayList books) - { - this.name = name; - this.id = id; - this.limit = limit; - this.books = books; - } - - /** - * Gets name of this User - * @return Name - */ - public String getName() - { - return name; - } - - /** - * Gets id of this User - * @return id - */ - public int getId() - { - return id; - } - - /** - * Gets limit of Book this User can borrow - * @return Book borrowing limit - */ - public int getLimit() - { - return limit; - } - - /** - * Change name of this User - * @param name New name of the User - */ - public void setName(String name) - { - this.name = name; - } - - /** - * Change limit of this User - * @param limit New limit of the User - */ - public void setLimit(int limit) - { - this.limit = limit; - } - - /** - * Check status of the User - * @return If the User is already at the borrowing limit - */ - public boolean status() - { - return books.size() < limit; - - } - - /** - * Get Book ids of the books - * @return Array of Book ids borrowed by this User - */ - public int[] bookStatus() - { - int[] currentBooks = new int[books.size()]; - - for (int i = 0; i < books.size(); i++) - { - currentBooks[i] = books.get(i); - } - - return currentBooks; - } - - /** - * User borrows a Book - * @param id id of the Book to tbe borrowed - * @return If the borrowing operation is successful - */ - boolean borrowNewBook(int id) - { - if (status()) - { - books.add(id); - return true; - } - else - { - return false; - } - } - - /** - * User returns a Book - * @param id id of the Book to be returned - * @return If the returning operation is successful - */ - boolean returnBook(int id) - { - for (int i = 0; i < books.size(); i++) - { - if (books.get(i) == id) - { - books.remove(i); - return true; - } - } - - return false; - } -} diff --git a/src/main/kotlin/libsys/Book.kt b/src/main/kotlin/libsys/Book.kt index 91c130b..42a1553 100644 --- a/src/main/kotlin/libsys/Book.kt +++ b/src/main/kotlin/libsys/Book.kt @@ -45,7 +45,7 @@ class Book get /** - * Create new Book with given id (Usually used as placeholders) + * @constructor Create new Book with given id (Usually used as placeholders) * @param id Identification number of the Book */ constructor(id: Int) { @@ -54,7 +54,7 @@ class Book } /** - * Create new Book with given title, id and status (Usually used for new Book creation) + * @constructor 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 @@ -66,7 +66,7 @@ class Book } /** - * Create new Book with given id, title, status and dueDate (Usually used for initialization from database) + * @constructor 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 diff --git a/src/main/kotlin/libsys/User.kt b/src/main/kotlin/libsys/User.kt new file mode 100644 index 0000000..2522a92 --- /dev/null +++ b/src/main/kotlin/libsys/User.kt @@ -0,0 +1,114 @@ +package libsys + +/* + * Written by : Bin Hong Lee + * Last edited : 7/4/2017 + */ + +import java.util.* + +/** + * User object + */ +class User { + + /** + * Name of the User + */ + var name: String? = null + set + get + /** + * ID of the User + */ + var id: Int = 0 + private set + get + /** + * Limit of the User + */ + var limit: Int = 0 + set + get + + private var books = ArrayList() + + /** + * @constructor Creates a new User with name, id and limit (usually used to add new User) + * @param name Name of this User + * @param id id of this User + * @param limit Limit of Book this User can borrow + */ + constructor(name: String, id: Int, limit: Int) { + this.name = name + this.id = id + this.limit = limit + } + + /** + * @constructor Creates a new User with name, id, limit and books (usually used to initialize from database) + * @param name Name of this User + * @param id id of this User + * @param limit Limit of Book this User can borrow + * @param books ArrayList of Book ids borrowed by the User + */ + constructor(name: String, id: Int, limit: Int, books: ArrayList) { + this.name = name + this.id = id + this.limit = limit + this.books = books + } + + /** + * Check status of the User + * @return If the User is already at the borrowing limit + */ + fun status(): Boolean { + return books.size < limit + + } + + /** + * Get Book ids of the books + * @return Array of Book ids borrowed by this User + */ + fun bookStatus(): IntArray { + val currentBooks = IntArray(books.size) + + for (i in books.indices) { + currentBooks[i] = books[i] + } + + return currentBooks + } + + /** + * User borrows a Book + * @param id id of the Book to tbe borrowed + * @return If the borrowing operation is successful + */ + fun borrowNewBook(id: Int): Boolean { + if (status()) { + books.add(id) + return true + } else { + return false + } + } + + /** + * User returns a Book + * @param id id of the Book to be returned + * @return If the returning operation is successful + */ + fun returnBook(id: Int): Boolean { + for (i in books.indices) { + if (books[i] == id) { + books.removeAt(i) + return true + } + } + + return false + } +}