Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
1.8 KiB

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. const (
  7. goodScore = 450
  8. lowScoreRation = 10
  9. goodScoreRatio = 20
  10. )
  11. var (
  12. ErrCreditScore = errors.New("invalid credit score")
  13. ErrIncome = errors.New("income invalid")
  14. ErrLoanAmount = errors.New("loan amount invalid")
  15. ErrLoanTerm = errors.New("loan term not a multiple of 12")
  16. )
  17. func checkLoan(creditScore int, income float64,
  18. loanAmount float64, loanTerm float64) error {
  19. interest := 20.0
  20. if creditScore >= goodScore {
  21. interest = 15
  22. }
  23. if creditScore < 1 {
  24. return ErrCreditScore
  25. }
  26. if income < 1 {
  27. return ErrIncome
  28. }
  29. if loanAmount < 1 {
  30. return ErrLoanAmount
  31. }
  32. if loanTerm < 1 || int(loanTerm)%12 != 0 {
  33. return ErrLoanTerm
  34. }
  35. rate := interest / 100
  36. payment := ((loanAmount * rate) / loanTerm) + (loanAmount / loanTerm)
  37. totalInterest := (payment * loanTerm) - loanAmount
  38. approved := false
  39. if income > payment {
  40. ratio := (payment / income) * 100
  41. if creditScore >= goodScore && ratio < goodScoreRatio {
  42. approved = true
  43. } else if ratio < lowScoreRation {
  44. approved = true
  45. }
  46. }
  47. fmt.Println("Credit Score :", creditScore)
  48. fmt.Println("Income :", income)
  49. fmt.Println("Loan Amount :", loanAmount)
  50. fmt.Println("Loan Term :", loanTerm)
  51. fmt.Println("Monthly Payment :", payment)
  52. fmt.Println("Rate :", interest)
  53. fmt.Println("Total Cost :", totalInterest)
  54. fmt.Println("Approved :", approved)
  55. fmt.Println("")
  56. return nil
  57. }
  58. func main() {
  59. // Approved
  60. fmt.Println("Applicant 1")
  61. fmt.Println("-----------")
  62. err := checkLoan(500, 1000, 1000, 24)
  63. if err != nil {
  64. fmt.Println("Error:", err)
  65. }
  66. // Denied
  67. fmt.Println("Applicant 2")
  68. fmt.Println("-----------")
  69. err = checkLoan(350, 1000, 10000, 12)
  70. if err != nil {
  71. fmt.Println("Error:", err)
  72. }
  73. }