Browse Source

Change book price data type to BigDecimal

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.1-alpha
Pekka Helenius 4 years ago
parent
commit
8171bd0b46
2 changed files with 10 additions and 7 deletions
  1. +4
    -2
      bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java
  2. +6
    -5
      bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java

+ 4
- 2
bookstore/src/main/java/com/fjordtek/bookstore/BookstoreApplication.java View File

@ -2,6 +2,8 @@
package com.fjordtek.bookstore; package com.fjordtek.bookstore;
import java.math.BigDecimal;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
@ -41,7 +43,7 @@ public class BookstoreApplication extends SpringBootServletInitializer {
"Angela Carter", "Angela Carter",
1979, 1979,
"1231231-12", "1231231-12",
18.00,
new BigDecimal("18.00"),
categoryRepository.findByName("Horror").get(0) categoryRepository.findByName("Horror").get(0)
)); ));
bookRepository.save(new Book( bookRepository.save(new Book(
@ -49,7 +51,7 @@ public class BookstoreApplication extends SpringBootServletInitializer {
"Andrzej Sapkowski", "Andrzej Sapkowski",
1999, 1999,
"3213221-3", "3213221-3",
19.99,
new BigDecimal("19.99"),
categoryRepository.findByName("Fantasy").get(0) categoryRepository.findByName("Fantasy").get(0)
)); ));


+ 6
- 5
bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java View File

@ -2,6 +2,8 @@
package com.fjordtek.bookstore.model; package com.fjordtek.bookstore.model;
import java.math.BigDecimal;
import javax.persistence.Column; import javax.persistence.Column;
//import java.sql.Timestamp; //import java.sql.Timestamp;
@ -149,8 +151,7 @@ public class Book {
@DecimalMax( @DecimalMax(
value = maxPrice, message = "Too high price value. Maximum allowed: " + maxPrice value = maxPrice, message = "Too high price value. Maximum allowed: " + maxPrice
) )
// TODO: Use BigDecimal to keep exact precision?
private double price;
private BigDecimal price;
@ManyToOne( @ManyToOne(
//fetch = FetchType.LAZY, //fetch = FetchType.LAZY,
@ -188,7 +189,7 @@ public class Book {
this.isbn = isbn; this.isbn = isbn;
} }
public void setPrice(double price) {
public void setPrice(BigDecimal price) {
this.price = price; this.price = price;
} }
@ -221,7 +222,7 @@ public class Book {
return isbn; return isbn;
} }
public double getPrice() {
public BigDecimal getPrice() {
return price; return price;
} }
@ -234,7 +235,7 @@ public class Book {
public Book() {} public Book() {}
public Book(String title, String author, int year, String isbn, double price, Category category) {
public Book(String title, String author, int year, String isbn, BigDecimal price, Category category) {
// super(); // super();
this.title = title; this.title = title;
this.author = author; this.author = author;


Loading…
Cancel
Save