This commit is contained in:
2024-10-20 09:52:55 +00:00
parent 509df534d1
commit ac623d6d49
4 changed files with 57 additions and 10 deletions

View File

@@ -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
}