From 39586bdfc11c69806e78f1da1bc560d994234dd3 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Mon, 6 Jul 2020 23:18:46 +0200 Subject: [PATCH] Add remaining code of chapter 03 --- chapter_03/activity_3.01/main.go | 21 +++++++ chapter_03/activity_3.02/main.go | 94 ++++++++++++++++++++++++++++++++ chapter_03/exercise_3.05/main.go | 19 +++++++ chapter_03/rune/main.go | 28 ++++++++++ 4 files changed, 162 insertions(+) create mode 100644 chapter_03/activity_3.01/main.go create mode 100644 chapter_03/activity_3.02/main.go create mode 100644 chapter_03/exercise_3.05/main.go create mode 100644 chapter_03/rune/main.go diff --git a/chapter_03/activity_3.01/main.go b/chapter_03/activity_3.01/main.go new file mode 100644 index 0000000..a508a6a --- /dev/null +++ b/chapter_03/activity_3.01/main.go @@ -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) + +} diff --git a/chapter_03/activity_3.02/main.go b/chapter_03/activity_3.02/main.go new file mode 100644 index 0000000..1e284f2 --- /dev/null +++ b/chapter_03/activity_3.02/main.go @@ -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) + } + +} diff --git a/chapter_03/exercise_3.05/main.go b/chapter_03/exercise_3.05/main.go new file mode 100644 index 0000000..555cf7e --- /dev/null +++ b/chapter_03/exercise_3.05/main.go @@ -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)) + } + +} diff --git a/chapter_03/rune/main.go b/chapter_03/rune/main.go new file mode 100644 index 0000000..e9bb81d --- /dev/null +++ b/chapter_03/rune/main.go @@ -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") + +}