Browse Source

Add remaining code of chapter 03

master
T. Meissner 5 years ago
parent
commit
39586bdfc1
4 changed files with 162 additions and 0 deletions
  1. +21
    -0
      chapter_03/activity_3.01/main.go
  2. +94
    -0
      chapter_03/activity_3.02/main.go
  3. +19
    -0
      chapter_03/exercise_3.05/main.go
  4. +28
    -0
      chapter_03/rune/main.go

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

@ -0,0 +1,21 @@
package main
import "fmt"
func salesTax(cost float64, taxRate float64) float64 {
return cost * taxRate / 100
}
func main() {
taxTotal := 0.0
taxTotal = salesTax(0.99, 7.5)
taxTotal += salesTax(2.75, 1.5)
taxTotal += salesTax(0.87, 2.0)
fmt.Println("Sales Tax Total:", taxTotal)
}

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

@ -0,0 +1,94 @@
package main
import (
"errors"
"fmt"
)
const (
goodScore = 450
lowScoreRation = 10
goodScoreRatio = 20
)
var (
ErrCreditScore = errors.New("invalid credit score")
ErrIncome = errors.New("income invalid")
ErrLoanAmount = errors.New("loan amount invalid")
ErrLoanTerm = errors.New("loan term not a multiple of 12")
)
func checkLoan(creditScore int, income float64,
loanAmount float64, loanTerm float64) error {
interest := 20.0
if creditScore >= goodScore {
interest = 15
}
if creditScore < 1 {
return ErrCreditScore
}
if income < 1 {
return ErrIncome
}
if loanAmount < 1 {
return ErrLoanAmount
}
if loanTerm < 1 || int(loanTerm)%12 != 0 {
return ErrLoanTerm
}
rate := interest / 100
payment := ((loanAmount * rate) / loanTerm) + (loanAmount / loanTerm)
totalInterest := (payment * loanTerm) - loanAmount
approved := false
if income > payment {
ratio := (payment / income) * 100
if creditScore >= goodScore && ratio < goodScoreRatio {
approved = true
} else if ratio < lowScoreRation {
approved = true
}
}
fmt.Println("Credit Score :", creditScore)
fmt.Println("Income :", income)
fmt.Println("Loan Amount :", loanAmount)
fmt.Println("Loan Term :", loanTerm)
fmt.Println("Monthly Payment :", payment)
fmt.Println("Rate :", interest)
fmt.Println("Total Cost :", totalInterest)
fmt.Println("Approved :", approved)
fmt.Println("")
return nil
}
func main() {
// Approved
fmt.Println("Applicant 1")
fmt.Println("-----------")
err := checkLoan(500, 1000, 1000, 24)
if err != nil {
fmt.Println("Error:", err)
}
// Denied
fmt.Println("Applicant 2")
fmt.Println("-----------")
err = checkLoan(350, 1000, 10000, 12)
if err != nil {
fmt.Println("Error:", err)
}
}

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

@ -0,0 +1,19 @@
package main
import "fmt"
func main() {
logLevel := "デバッグ"
// For a string value, the range clause iterates over the
// Unicode code points in the string starting at byte index 0.
// On successive iterations, the index value will be the index
// of the first byte of successive UTF-8-encoded code points
// in the string, and the second value, of type rune,
// will be the value of the corresponding code point.
for index, runeVal := range logLevel {
fmt.Println(index, string(runeVal))
}
}

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

@ -0,0 +1,28 @@
package main
import "fmt"
func main() {
username := "Sir_King_Über"
// Print bytes directly
for i := 0; i < len(username); i++ {
fmt.Print(username[i], " ")
}
fmt.Printf("\n")
// Print bytes converted to string
for i := 0; i < len(username); i++ {
fmt.Print(string(username[i]), " ")
}
fmt.Printf("\n")
// Print bytes converted to runes
runes := []rune(username)
for _, value := range runes {
fmt.Print(string(value), " ")
}
fmt.Printf("\n")
}

Loading…
Cancel
Save