go go go
This commit is contained in:
1
bank/balance.txt
Normal file
1
bank/balance.txt
Normal file
@@ -0,0 +1 @@
|
||||
1900
|
||||
22
bank/bank.go
22
bank/bank.go
@@ -1,21 +1,27 @@
|
||||
package main
|
||||
|
||||
import "errors"
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "strconv"
|
||||
|
||||
const accountBalanceFile = "balance.txt"
|
||||
|
||||
func getBalanceFromFile() float64 {
|
||||
func getBalanceFromFile() (float64, error) {
|
||||
data, err := os.ReadFile(accountBalanceFile, )
|
||||
|
||||
if err != nil {
|
||||
|
||||
return 1000, errors.New("Failed to find balance file.")
|
||||
}
|
||||
|
||||
balanceText := string(data)
|
||||
balance, _ :=strconv.ParseFloat(balanceText, 64)
|
||||
return balance
|
||||
balance, err :=strconv.ParseFloat(balanceText, 64)
|
||||
|
||||
if err != nil {
|
||||
return 1000, errors.New("Failed to parse stored balance value.")
|
||||
}
|
||||
|
||||
return balance, nil
|
||||
}
|
||||
|
||||
func writeBalanceToFile(balance float64) {
|
||||
@@ -24,7 +30,13 @@ func writeBalanceToFile(balance float64) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
var accountBalance = getBalanceFromFile()
|
||||
var accountBalance, err = getBalanceFromFile()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("ERROR")
|
||||
fmt.Println(err)
|
||||
fmt.Println("-------")
|
||||
}
|
||||
|
||||
fmt.Println("Welcome to Go Bank!")
|
||||
|
||||
|
||||
@@ -1,11 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "errors"
|
||||
import "os"
|
||||
|
||||
func main() {
|
||||
revenue := getUserInput("Revenue: ")
|
||||
expenses := getUserInput("Expenses: ")
|
||||
taxRate := getUserInput("Tax Rate: ")
|
||||
revenue, err := getUserInput("Revenue: ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
expenses, err := getUserInput("Expenses: ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
taxRate, err := getUserInput("Tax Rate: ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
ebt, profit, ratio := calculateFinancials(revenue, expenses, taxRate)
|
||||
|
||||
@@ -13,6 +32,13 @@ func main() {
|
||||
fmt.Printf("%.1f\n", ebt)
|
||||
fmt.Printf("%.1f\n", profit)
|
||||
fmt.Printf("%.3f", ratio)
|
||||
|
||||
storeResults(ebt, profit, ratio)
|
||||
}
|
||||
|
||||
func storeResults(ebt, profit, ratio float64) {
|
||||
results := fmt.Sprintf("EBT: %1f\nProfit: %.1f\nRatio: %.3f\n", ebt, profit, ratio)
|
||||
os.WriteFile("results.txt", []byte(results), 0644)
|
||||
}
|
||||
|
||||
func calculateFinancials(revenue, expenses, taxRate float64) (float64, float64, float64) {
|
||||
@@ -22,9 +48,14 @@ func calculateFinancials(revenue, expenses, taxRate float64) (float64, float64,
|
||||
return ebt, profit, ratio
|
||||
}
|
||||
|
||||
func getUserInput(infoText string) float64 {
|
||||
func getUserInput(infoText string) (float64, error) {
|
||||
var userInput float64
|
||||
fmt.Print(infoText)
|
||||
fmt.Scan(&userInput)
|
||||
return userInput
|
||||
|
||||
if userInput <= 0 {
|
||||
return 0, errors.New("Value must be a positive number")
|
||||
}
|
||||
|
||||
return userInput, nil
|
||||
}
|
||||
3
profitCalculator/results.txt
Normal file
3
profitCalculator/results.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
EBT: 50.000000
|
||||
Profit: 48.5
|
||||
Ratio: 1.031
|
||||
Reference in New Issue
Block a user