on 448bfaf help sign in
77 lines 1.7 kB history raw
1interface Display {
2 String asText()
3}
5trait Entity {
6 Integer id
7}
9class Product implements Entity, Display {
10 public String name
11 public Boolean available
12 public Float price
14 private String key
15 protected String data
17 /**
18 * Creates a new product instance.
19 * @param id Product ID.
20 * @param name Product name.
21 * @param available Product availability.
22 * @param price Product price.
23 * @param key Product key.
24 * @param data Product internal data.
25 */
26 Product(id, name, available, price, key = "key", data = "internal") {
27 this.id = id
28 this.name = name
29 this.available = available
30 this.price = price
31 this.key = key
32 this.data = data
33 }
35 /**@
36 * Returns product data as text.
37 * @return Data string.
38 */
39 String asText() {
40 return """ID [${id}] Name [${name}] Available [${available}] Price [${price}]"""
41 }
44/* Creates a new product instance */
45def product = new Product(1, "T-Shirt", true, 15.00)
47println(product.asText())
49product.available = false
50product.price = 0.0
52// Check values
53assert product.asText() == "ID [1] Name [T-Shirt] Available [false] Price [0.0]"
55def factorial(Integer value) {
56 if (value <= 1) {
57 return 1
58 } else {
59 return value * factorial(value - 1)
60 }
63assert factorial(5) == 120
65static String join(List<String> list, String separator) {
66 String data = ""
68 list.each { item ->
69 data += item + separator
70 }
72 data = data.substring(0, data.length() - 1)
74 return data
77assert join(["g", "r", "o", "o", "v", "y"], " ") == "g r o o v y"