Browse Source

Converted User class to Kotlin

master
BinHong Lee 7 years ago
parent
commit
af3dd212d1
5 changed files with 118 additions and 160 deletions
  1. +0
    -2
      .travis.yml
  2. +1
    -1
      src/main/java/libsys/BookFactory.java
  3. +0
    -154
      src/main/java/libsys/User.java
  4. +3
    -3
      src/main/kotlin/libsys/Book.kt
  5. +114
    -0
      src/main/kotlin/libsys/User.kt

+ 0
- 2
.travis.yml View File

@@ -4,8 +4,6 @@ matrix:
- os: osx
- os: linux
jdk: oraclejdk8
- os: linux
jdk: oraclejdk7

script: "mvn cobertura:cobertura"



+ 1
- 1
src/main/java/libsys/BookFactory.java View File

@@ -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;


+ 0
- 154
src/main/java/libsys/User.java View File

@@ -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<Integer> books = new ArrayList<Integer>();

/**
* 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<Integer> 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;
}
}

+ 3
- 3
src/main/kotlin/libsys/Book.kt View File

@@ -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


+ 114
- 0
src/main/kotlin/libsys/User.kt View File

@@ -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<Int>()

/**
* @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<Int>) {
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
}
}

Loading…
Cancel
Save