Commit 426749be authored by noasakurajin's avatar noasakurajin
Browse files

finished the train creator for format version 0.4.0

parent 05f9d846
.idea .idea
build build
*.json
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
"strings"
) )
func getFormatVersionIndex() int{ func getFormatVersionIndex() int{
...@@ -48,13 +49,14 @@ func readTrainName() string{ ...@@ -48,13 +49,14 @@ func readTrainName() string{
return scanner.Text() return scanner.Text()
} }
func readTrainMass() float64{ func readPositiveFloat(message string) float64{
var input float64 = 0 var input float64 = 0
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
var err error = nil var err error = nil
for ok := true; ok; { for ok := true; ok; {
fmt.Println("Please enter the mass of the train:") fmt.Println(message)
fmt.Println()
scanner.Scan() scanner.Scan()
input, err = strconv.ParseFloat(scanner.Text(),64) input, err = strconv.ParseFloat(scanner.Text(),64)
...@@ -71,48 +73,20 @@ func readTrainMass() float64{ ...@@ -71,48 +73,20 @@ func readTrainMass() float64{
return input return input
} }
func readMaxVelocity() float64{ func readVelocityUnit() string{
var input float64 = 0 fmt.Println("The velocity unit can have the following units:")
scanner := bufio.NewScanner(os.Stdin) fmt.Println(" ms -> meter per second (default value)")
var err error = nil fmt.Println(" kmh -> kilometer per hour")
fmt.Println("Anything else will use the default unit.")
for ok := true; ok; { fmt.Println("\nEnter the velocity unit of the train:")
fmt.Println("Please enter the maximum velocity of the train:")
scanner.Scan()
input, err = strconv.ParseFloat(scanner.Text(),64)
if err != nil || input < 0 {
// handle invalid input
fmt.Println("please enter a valid value")
continue
}
ok = false
fmt.Printf("\n\n")
}
return input
}
func readMaxAcceleration() float64{
var input float64 = 0
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
var err error = nil scanner.Scan()
for ok := true; ok; {
fmt.Println("Please enter the maximum acceleration of the train:")
scanner.Scan()
input, err = strconv.ParseFloat(scanner.Text(),64)
if err != nil || input < 0 {
// handle invalid input
fmt.Println("please enter a valid value")
continue
}
ok = false fmt.Printf("\n\n")
fmt.Printf("\n\n") if strings.Contains(scanner.Text(), "kmh") {
return "kmh"
} }
return input return "ms"
} }
\ No newline at end of file
...@@ -22,16 +22,16 @@ type trainProperties040 struct { ...@@ -22,16 +22,16 @@ type trainProperties040 struct {
} }
func (b *trainProperties040) cast() trainProperties{ func (b *trainProperties040) cast() trainProperties{
var retval trainProperties var retVal trainProperties
retval.Name = b.Name retVal.Name = b.Name
retval.Mass = b.Mass retVal.Mass = b.Mass
retval.MaxVelocity = b.MaxVelocity retVal.MaxVelocity = b.MaxVelocity
retval.MaxAcceleration = b.MaxAcceleration retVal.MaxAcceleration = b.MaxAcceleration
retval.FormatVersion = b.FormatVersion retVal.FormatVersion = b.FormatVersion
retval.TrackDrag = b.TrackDrag retVal.TrackDrag = b.TrackDrag
retval.AirDrag = b.AirDrag retVal.AirDrag = b.AirDrag
retval.VelocityUnit = b.VelocityUnit retVal.VelocityUnit = b.VelocityUnit
return retval return retVal
} }
func createTrainProperties() trainProperties{ func createTrainProperties() trainProperties{
...@@ -43,7 +43,12 @@ func createTrainProperties040() trainProperties040{ ...@@ -43,7 +43,12 @@ func createTrainProperties040() trainProperties040{
var retVal trainProperties040 var retVal trainProperties040
retVal.FormatVersion = "0.4.0" retVal.FormatVersion = "0.4.0"
retVal.Name = readTrainName() retVal.Name = readTrainName()
retVal.Mass = readTrainMass() retVal.Mass = readPositiveFloat("Please enter the mass of the train:")
retVal.VelocityUnit = readVelocityUnit()
retVal.MaxVelocity = readPositiveFloat("Please enter the maximum velocity of the train:")
retVal.MaxAcceleration = readPositiveFloat("Please enter the maximum acceleration of the train:")
retVal.TrackDrag = readPositiveFloat("Please enter the drag coefficient of the train with the track:")
retVal.AirDrag = readPositiveFloat("Please enter the air drag coefficient of the train:")
return retVal return retVal
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment