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

finished the train creator for format version 0.4.0

parent 05f9d846
......@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strconv"
"strings"
)
func getFormatVersionIndex() int{
......@@ -48,13 +49,14 @@ func readTrainName() string{
return scanner.Text()
}
func readTrainMass() float64{
func readPositiveFloat(message string) float64{
var input float64 = 0
scanner := bufio.NewScanner(os.Stdin)
var err error = nil
for ok := true; ok; {
fmt.Println("Please enter the mass of the train:")
fmt.Println(message)
fmt.Println()
scanner.Scan()
input, err = strconv.ParseFloat(scanner.Text(),64)
......@@ -71,48 +73,20 @@ func readTrainMass() float64{
return input
}
func readMaxVelocity() float64{
var input float64 = 0
scanner := bufio.NewScanner(os.Stdin)
var err error = nil
for ok := true; ok; {
fmt.Println("Please enter the maximum velocity of the train:")
scanner.Scan()
input, err = strconv.ParseFloat(scanner.Text(),64)
func readVelocityUnit() string{
fmt.Println("The velocity unit can have the following units:")
fmt.Println(" ms -> meter per second (default value)")
fmt.Println(" kmh -> kilometer per hour")
fmt.Println("Anything else will use the default unit.")
fmt.Println("\nEnter the velocity unit of the train:")
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)
var err error = nil
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
}
scanner.Scan()
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 {
}
func (b *trainProperties040) cast() trainProperties{
var retval trainProperties
retval.Name = b.Name
retval.Mass = b.Mass
retval.MaxVelocity = b.MaxVelocity
retval.MaxAcceleration = b.MaxAcceleration
retval.FormatVersion = b.FormatVersion
retval.TrackDrag = b.TrackDrag
retval.AirDrag = b.AirDrag
retval.VelocityUnit = b.VelocityUnit
return retval
var retVal trainProperties
retVal.Name = b.Name
retVal.Mass = b.Mass
retVal.MaxVelocity = b.MaxVelocity
retVal.MaxAcceleration = b.MaxAcceleration
retVal.FormatVersion = b.FormatVersion
retVal.TrackDrag = b.TrackDrag
retVal.AirDrag = b.AirDrag
retVal.VelocityUnit = b.VelocityUnit
return retVal
}
func createTrainProperties() trainProperties{
......@@ -43,7 +43,12 @@ func createTrainProperties040() trainProperties040{
var retVal trainProperties040
retVal.FormatVersion = "0.4.0"
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
}
Markdown is supported
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