|
1 | | -package com.anma.java.core.java21; |
2 | | - |
3 | | -import java.nio.file.Files; |
4 | | -import java.nio.file.Path; |
5 | | -import java.nio.file.Paths; |
6 | | -import java.util.Locale; |
7 | | - |
8 | | -import static java.lang.StringTemplate.STR; |
9 | | - |
10 | | -public class StrTemplating { |
11 | | - |
12 | | - record Rectangle(String name, double width, double height) { |
13 | | - double area() { |
14 | | - return width * height; |
15 | | - } |
16 | | - } |
17 | | - |
18 | | - Rectangle[] zone = new Rectangle[] { |
19 | | - new Rectangle("First", 17.8, 31.4), |
20 | | - new Rectangle("Second", 9.6, 12.2), |
21 | | - }; |
22 | | - |
23 | | - String table = STR.""" |
24 | | - Description\tWidth\tHeight\tArea |
25 | | - \{zone[0].name}\t\t\{zone[0].width}\t\{zone[0].height}\t\{zone[0].area()} |
26 | | - \{zone[1].name}\t\t\{zone[1].width}\t\{zone[1].height}\t\{zone[1].area()} |
27 | | - Total \{zone[0].area() + zone[1].area()} |
28 | | - """; |
29 | | - |
30 | | - System.out.println(table); |
31 | | - void templating() { |
32 | | - String time = STR."Today is \{java.time.LocalDate.now()}"; |
33 | | - System.out.println(time); |
34 | | - String canLang = STR."The language code of \{ |
35 | | - Locale.CANADA_FRENCH} is \{ |
36 | | - Locale.CANADA_FRENCH.getLanguage()}"; |
37 | | - |
38 | | - System.out.println(canLang); |
39 | | - } |
40 | | - |
41 | | - void templateTwo() { |
42 | | - Path filePath = Paths.get("Stemp.java"); |
43 | | - String msg = STR."The file \{filePath} \{ |
44 | | - // The Files class is in the package java.nio.file |
45 | | - Files.exists(filePath) ? "does" : "does not"} exist"; |
46 | | - System.out.println(msg); |
47 | | - |
48 | | - String currentTime = STR."The time is \{ |
49 | | - DateTimeFormatter |
50 | | - .ofPattern("HH:mm:ss") |
51 | | - .format(LocalTime.now()) |
52 | | - } right now"; |
53 | | - System.out.println(currentTime); |
54 | | - |
55 | | - } |
56 | | - |
57 | | - |
58 | | -} |
| 1 | +//package com.anma.java.core.java21; |
| 2 | +// |
| 3 | +//import java.nio.file.Files; |
| 4 | +//import java.nio.file.Path; |
| 5 | +//import java.nio.file.Paths; |
| 6 | +//import java.time.LocalTime; |
| 7 | +//import java.time.format.DateTimeFormatter; |
| 8 | +//import java.util.Locale; |
| 9 | +// |
| 10 | +//import static java.lang.StringTemplate.STR; |
| 11 | +// |
| 12 | +//public class StrTemplating { |
| 13 | +// |
| 14 | +// record Rectangle(String name, double width, double height) { |
| 15 | +// double area() { |
| 16 | +// return width * height; |
| 17 | +// } |
| 18 | +// } |
| 19 | +// |
| 20 | +// Rectangle[] zone = new Rectangle[] { |
| 21 | +// new Rectangle("First", 17.8, 31.4), |
| 22 | +// new Rectangle("Second", 9.6, 12.2), |
| 23 | +// }; |
| 24 | +// |
| 25 | +// String table = STR.""" |
| 26 | +// Description\tWidth\tHeight\tArea |
| 27 | +// \{zone[0].name}\t\t\{zone[0].width}\t\{zone[0].height}\t\{zone[0].area()} |
| 28 | +// \{zone[1].name}\t\t\{zone[1].width}\t\{zone[1].height}\t\{zone[1].area()} |
| 29 | +// Total \{zone[0].area() + zone[1].area()} |
| 30 | +// """; |
| 31 | +// |
| 32 | +// System.out.println(table); |
| 33 | +// void templating() { |
| 34 | +// String time = STR."Today is \{java.time.LocalDate.now()}"; |
| 35 | +// System.out.println(time); |
| 36 | +// String canLang = STR."The language code of \{ |
| 37 | +// Locale.CANADA_FRENCH} is \{ |
| 38 | +// Locale.CANADA_FRENCH.getLanguage()}"; |
| 39 | +// |
| 40 | +// System.out.println(canLang); |
| 41 | +// } |
| 42 | +// |
| 43 | +// void templateTwo() { |
| 44 | +// Path filePath = Paths.get("Stemp.java"); |
| 45 | +// String msg = STR."The file \{filePath} \{ |
| 46 | +// // The Files class is in the package java.nio.file |
| 47 | +// Files.exists(filePath) ? "does" : "does not"} exist"; |
| 48 | +// System.out.println(msg); |
| 49 | +// |
| 50 | +// String currentTime = STR."The time is \{ |
| 51 | +// DateTimeFormatter |
| 52 | +// .ofPattern("HH:mm:ss") |
| 53 | +// .format(LocalTime.now()) |
| 54 | +// } right now"; |
| 55 | +// System.out.println(currentTime); |
| 56 | +// |
| 57 | +// } |
| 58 | +// |
| 59 | +// |
| 60 | +//} |
0 commit comments