Created
October 19, 2023 06:08
-
-
Save ipetrushin/1b1ff2aab68c36e9b49ae8aff57264e5 to your computer and use it in GitHub Desktop.
Using let operator to generate HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Класс Message описывает сообщение электронной почты. | |
(1) Дополните класс двумя полями по смыслу на ваше усмотрение. | |
(2) Модифицируйте код функции toHTML() таким образом, чтобы строки с пустыми (null) полями не вставлялись в HTML код. Для этого удобно использовать операторы let/also/with. | |
(3) Дополните HTML код оформлениям: цвета, шрифты, отступы. Чтобы записать строку с HTML-кодом в файл используйте примеры https://www.tutorialkart.com/kotlin/write-content-to-file-in-kotlin/ | |
Примеры использования этих операторов: https://www.digitalocean.com/community/tutorials/kotlin-let-run-also-apply-with | |
Документация: https://kotlinlang.org/docs/scope-functions.html | |
В качестве ответа приложите исходный код и снимки экрана с демонстрацией работы. | |
*/ | |
fun main() { | |
data class Message(val address: String?, val topic: String?) { | |
fun toHTML(): String { | |
if (address == null) { | |
} | |
var template = "<table> " | |
// ниже используется интерполяция строк (подстановка переменных как в PHP/Python) | |
address?.let { template += "<tr><td>address</td><td>$it</td></tr> \n" } | |
topic?.let { template += "<tr><td name=\"\">address</td><td>$it</td></tr> \n" } | |
template += "</table>" | |
return template | |
} | |
} | |
val m = Message("askbill@microsoft.com", null) | |
println(m.toHTML()) | |
var content = "Hello World. Welcome to Kotlin Tutorial by www.tutorialkart.com." | |
// using java class java.io.PrintWriter | |
val writer = PrintWriter("file.txt") | |
writer.append(m.toHTML()) | |
writer.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment