Guest User

Untitled

a guest
Apr 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. package com.example.nguye.chngtrnhtnhchsbmi
  2.  
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.os.Message
  6. import android.widget.Button
  7. import android.widget.EditText
  8. import android.widget.Toast
  9. import kotlinx.android.synthetic.main.activity_main.*
  10. import java.math.RoundingMode
  11. import java.text.DecimalFormat
  12. import kotlin.math.pow
  13.  
  14. class MainActivity : AppCompatActivity() {
  15.  
  16. override fun onCreate(savedInstanceState: Bundle?) {
  17. super.onCreate(savedInstanceState)
  18. setContentView(R.layout.activity_main)
  19.  
  20. button_TinhBMI.setOnClickListener(){
  21. Tinh()
  22. }
  23. }
  24.  
  25. fun NgoaiLeTen(message: String): Nothing
  26. {
  27. throw Exception(message)
  28. }
  29.  
  30. fun Tinh()
  31. {
  32. try {
  33. val butChanDoan = findViewById<Button>(R.id.button_TinhBMI)
  34. val editTen = findViewById<EditText>(R.id.editText_ten)
  35. val editChieuCao = findViewById<EditText>(R.id.editText_ChieuCao)
  36. val editCanNang = findViewById<EditText>(R.id.editText_CanNang)
  37. val editBMI = findViewById<EditText>(R.id.editText_BMI)
  38. val editChanDoan = findViewById<EditText>(R.id.editText_ChanDoan)
  39.  
  40. val H: Double = editChieuCao.text.toString().toDouble()
  41. val W: Double = editCanNang.text.toString().toDouble()
  42. val BMI: Double = W / (H / 100.0).pow(2)
  43. var chuandoan = ""
  44.  
  45. val HoTen = editTen.text.toString().trim()
  46.  
  47. var isNum = true
  48. isNum = HoTen.matches("-?\\d+(\\.\\d+)?".toRegex())
  49.  
  50.  
  51. if (HoTen == "" || isNum)
  52. throw NgoaiLeTen("Vui lòng nhập lại tên")
  53.  
  54. if (BMI < 18)
  55. chuandoan = "Bạn gầy"
  56. else if (BMI <= 24.9)
  57. chuandoan = "Bạn bình thường"
  58. else if (BMI <= 29.9)
  59. chuandoan = "Bạn béo phì độ 1"
  60. else if (BMI <= 34.9)
  61. chuandoan = "Bạn béo phì độ 2"
  62. else
  63. chuandoan = "Khủng long"
  64. val df = DecimalFormat("#.0")
  65. df.roundingMode = RoundingMode.CEILING
  66. Toast.makeText(this@MainActivity, "Vui lòng chờ! Bạn đã Click vào nút" + butChanDoan.text.toString(), Toast.LENGTH_LONG).show()
  67. Toast.makeText(this@MainActivity, "Xin chào " + editTen.text.toString() + " đã có kết quả!", Toast.LENGTH_LONG).show()
  68.  
  69. editBMI.setText(df.format(BMI))
  70. editChanDoan.setText(chuandoan)
  71. }
  72. catch (ex: NullPointerException)
  73. {
  74. Toast.makeText(this@MainActivity, "Lỗi NullPointerException", Toast.LENGTH_LONG).show()
  75. }
  76. catch (ex: Exception)
  77. {
  78. Toast.makeText(this@MainActivity, "Không hợp lệ. " + ex.message, Toast.LENGTH_LONG).show()
  79. }
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment