Browse Source

Add remaining code of chapter 03

T. Meissner 3 months ago
parent
commit
39586bdfc1

+ 21
- 0
chapter_03/activity_3.01/main.go View File

@@ -0,0 +1,21 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func salesTax(cost float64, taxRate float64) float64 {
6
+
7
+  return cost * taxRate / 100
8
+
9
+}
10
+
11
+func main() {
12
+
13
+  taxTotal := 0.0
14
+
15
+  taxTotal = salesTax(0.99, 7.5)
16
+  taxTotal += salesTax(2.75, 1.5)
17
+  taxTotal += salesTax(0.87, 2.0)
18
+
19
+  fmt.Println("Sales Tax Total:", taxTotal)
20
+
21
+}

+ 94
- 0
chapter_03/activity_3.02/main.go View File

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

+ 19
- 0
chapter_03/exercise_3.05/main.go View File

@@ -0,0 +1,19 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  logLevel := "デバッグ"
8
+
9
+  // For a string value, the range clause iterates over the
10
+  // Unicode code points in the string starting at byte index 0.
11
+  // On successive iterations, the index value will be the index
12
+  // of the first byte of successive UTF-8-encoded code points
13
+  // in the string, and the second value, of type rune,
14
+  // will be the value of the corresponding code point.
15
+  for index, runeVal := range logLevel {
16
+    fmt.Println(index, string(runeVal))
17
+  }
18
+
19
+}

+ 28
- 0
chapter_03/rune/main.go View File

@@ -0,0 +1,28 @@
1
+package main
2
+
3
+import "fmt"
4
+
5
+func main() {
6
+
7
+  username := "Sir_King_Über"
8
+
9
+  // Print bytes directly
10
+  for i := 0; i < len(username); i++ {
11
+    fmt.Print(username[i], " ")
12
+  }
13
+  fmt.Printf("\n")
14
+
15
+  // Print bytes converted to string
16
+  for i := 0; i < len(username); i++ {
17
+    fmt.Print(string(username[i]), " ")
18
+  }
19
+  fmt.Printf("\n")
20
+
21
+  // Print bytes converted to runes
22
+  runes := []rune(username)
23
+  for _, value := range runes {
24
+    fmt.Print(string(value), " ")
25
+  }
26
+  fmt.Printf("\n")
27
+
28
+}