|
|
@ -8,15 +8,25 @@ import javax.validation.constraints.Size; |
|
|
|
import javax.validation.constraints.Pattern; |
|
|
|
import javax.validation.constraints.NotNull; |
|
|
|
import javax.validation.constraints.NotEmpty; |
|
|
|
import javax.validation.constraints.PastOrPresent; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.time.LocalDate; |
|
|
|
//import java.sql.Timestamp; |
|
|
|
//import javax.validation.constraints.PastOrPresent; |
|
|
|
|
|
|
|
import org.springframework.format.annotation.DateTimeFormat; |
|
|
|
import javax.persistence.Entity; |
|
|
|
import javax.persistence.GeneratedValue; |
|
|
|
import javax.persistence.GenerationType; |
|
|
|
import javax.persistence.Id; |
|
|
|
|
|
|
|
@Entity |
|
|
|
public class Book { |
|
|
|
|
|
|
|
//////////////////// |
|
|
|
// Primary key value in database |
|
|
|
|
|
|
|
@Id |
|
|
|
@GeneratedValue(strategy=GenerationType.AUTO) |
|
|
|
private long id; |
|
|
|
|
|
|
|
//////////////////// |
|
|
|
// Attributes with hard-coded constraints |
|
|
|
|
|
|
@ -33,9 +43,11 @@ public class Book { |
|
|
|
private String author; |
|
|
|
|
|
|
|
@NotNull |
|
|
|
@PastOrPresent |
|
|
|
@DateTimeFormat(pattern = "yyyy") |
|
|
|
private LocalDate year; |
|
|
|
//@PastOrPresent |
|
|
|
//@DateTimeFormat(pattern = "yyyy") |
|
|
|
// TODO: Prefer Timestamp data type |
|
|
|
// private Timestamp year; |
|
|
|
private int year; |
|
|
|
|
|
|
|
@NotNull |
|
|
|
@NotEmpty(message = "Please provide an ISBN code for the book") |
|
|
@ -46,14 +58,16 @@ public class Book { |
|
|
|
@NotNull |
|
|
|
@DecimalMin(value = "0.01", message = "Too low price value" ) |
|
|
|
@DecimalMax(value = "999.99", message = "Too high price value") |
|
|
|
// Keep precision |
|
|
|
private BigDecimal price; |
|
|
|
// TODO: Use BigDecimal to keep exact precision? |
|
|
|
private double price; |
|
|
|
|
|
|
|
//////////////////// |
|
|
|
// Attribute setters |
|
|
|
|
|
|
|
// TODO consider accessibility restrictions? |
|
|
|
|
|
|
|
// Id generated automatically |
|
|
|
|
|
|
|
public void setTitle(String title) { |
|
|
|
this.title = title; |
|
|
|
} |
|
|
@ -62,7 +76,7 @@ public class Book { |
|
|
|
this.author = author; |
|
|
|
} |
|
|
|
|
|
|
|
public void setYear(LocalDate year) { |
|
|
|
public void setYear(int year) { |
|
|
|
this.year = year; |
|
|
|
} |
|
|
|
|
|
|
@ -70,7 +84,7 @@ public class Book { |
|
|
|
this.isbn = isbn; |
|
|
|
} |
|
|
|
|
|
|
|
public void setPrice(BigDecimal price) { |
|
|
|
public void setPrice(double price) { |
|
|
|
this.price = price; |
|
|
|
} |
|
|
|
|
|
|
@ -79,6 +93,8 @@ public class Book { |
|
|
|
|
|
|
|
// TODO consider accessibility restrictions? |
|
|
|
|
|
|
|
// Id generated automatically |
|
|
|
|
|
|
|
public String getTitle() { |
|
|
|
return title; |
|
|
|
} |
|
|
@ -87,7 +103,7 @@ public class Book { |
|
|
|
return author; |
|
|
|
} |
|
|
|
|
|
|
|
public LocalDate getYear() { |
|
|
|
public int getYear() { |
|
|
|
return year; |
|
|
|
} |
|
|
|
|
|
|
@ -95,25 +111,30 @@ public class Book { |
|
|
|
return isbn; |
|
|
|
} |
|
|
|
|
|
|
|
public BigDecimal getPrice() { |
|
|
|
public double getPrice() { |
|
|
|
return price; |
|
|
|
} |
|
|
|
|
|
|
|
//////////////////// |
|
|
|
// Class constructors |
|
|
|
|
|
|
|
public Book() { |
|
|
|
public Book() {} |
|
|
|
|
|
|
|
public Book(String title, String author, int year, String isbn, double price) { |
|
|
|
// super(); |
|
|
|
this.title = title; |
|
|
|
this.author = author; |
|
|
|
this.year = year; |
|
|
|
this.isbn = isbn; |
|
|
|
this.price = price; |
|
|
|
} |
|
|
|
|
|
|
|
// ... |
|
|
|
|
|
|
|
//////////////////// |
|
|
|
// Class overrides |
|
|
|
|
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
// TODO |
|
|
|
return "N/A (placeholder)"; |
|
|
|
return this.id + ", " + this.title + ", " + this.author + ", " + this.year + ", " + this.isbn + ", " + this.price; |
|
|
|
} |
|
|
|
|
|
|
|
// ... |
|
|
|