Bladeren bron

Converted Book class to Kotlin

master
BinHong Lee 7 jaren geleden
bovenliggende
commit
00f9e5c388
5 gewijzigde bestanden met toevoegingen van 242 en 208 verwijderingen
  1. +74
    -9
      pom.xml
  2. +0
    -187
      src/main/java/libsys/Book.java
  3. +4
    -3
      src/main/java/libsys/BookFactory.java
  4. +2
    -9
      src/main/java/libsys/MainGUI.java
  5. +162
    -0
      src/main/kotlin/libsys/Book.kt

+ 74
- 9
pom.xml Bestand weergeven

@@ -7,6 +7,12 @@
<version>1.0</version>
<name>LibrarySystem</name>
<url>http://maven.apache.org</url>

<properties>
<kotlin.version>1.1.3-2</kotlin.version>
</properties>


<dependencies>
<dependency>
<groupId>junit</groupId>
@@ -19,6 +25,17 @@
<artifactId>org.json</artifactId>
<version>chargebee-1.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
@@ -30,15 +47,6 @@
<mainClass>libsys.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
@@ -64,6 +72,63 @@
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

+ 0
- 187
src/main/java/libsys/Book.java Bestand weergeven

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

+ 4
- 3
src/main/java/libsys/BookFactory.java Bestand weergeven

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


+ 2
- 9
src/main/java/libsys/MainGUI.java Bestand weergeven

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


+ 162
- 0
src/main/kotlin/libsys/Book.kt Bestand weergeven

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

Laden…
Annuleren
Opslaan