Workbook 2.11
Task 1
Create a POJO class for Record
objects. The class will have three fields:
private String item; private double revenue; private double cost;
Add the usual constructor, getters, and setters.
Task 2
Before returning the view, add the following objects to the model:
List<Record> records = Arrays.asList( new Record("Chair", 20.99, 5.99), new Record("Table", 40.99, 8.99), new Record("Couch", 100.99, 105.99), new Record("Fridge", 200.99, 59.99), new Record("Bed", 150.99, 205.99) );
Task 3
Under the header in records.html
, create a table similar to the following:
Item | Revenue | Cost | Profit |
---|---|---|---|
Data | Data | Data | Data |
Data | Data | Data | Data |
Data | Data | Data | Data |
Task 4
Using a Thymeleaf loop, your table must generate as many rows as there are objects in the records
list.
Hint: For the Profit
rows, Thymeleaf allows you to subtract using the -
operator.
Task 5
double
is not suitable for storing currency because it has a certain precision. BigDecimal
is an exact way of representing numbers.
1 Update your POJO class accordingly.
private String item; private BigDecimal revenue; private BigDecimal cost;
2 Use this List
instead.
List<Record> records = Arrays.asList( new Record("Chair", new BigDecimal("20.99"), new BigDecimal("5.99")), new Record("Table", new BigDecimal("40.99"), new BigDecimal("8.99")), new Record("Couch", new BigDecimal("100.99"), new BigDecimal("105.99")), new Record("Fridge", new BigDecimal("200.99"), new BigDecimal("59.99")), new Record("Bed", new BigDecimal("150.99"), new BigDecimal("205.99")) );
Expected Output:
Task 6
Common types from which Thymeleaf can leverage utility methods are:
#dates
#strings
#arrays
#lists
#numbers
Inside Thymeleaf Repo, the Numbers
class has a utility method that can format the model attribute into a currency.
Using this information, try to achieve the following output:
Task 7
You can use th:style
to format an HTML element.
th:style
must equalbackground: green
ifprofit
is greater than or equal to 0.- Otherwise, it must equal
background: red
.