Commit 05f9d846 authored by noasakurajin's avatar noasakurajin
Browse files

progress on the input reading

parent 2777fd7e
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func getFormatVersionIndex() int{
fmt.Println("\n\nThe available format Versions are:")
for i := 0; i < len(supportedVersions); i++ {
fmt.Printf("[%d] -> {%d.%d.%d}\n",i, supportedVersions[i].Major,supportedVersions[i].Minor,supportedVersions[i].Patch)
}
fmt.Println("Inside of the brackets [] are the indices, which are needed to select the version.")
var input int = 0
scanner := bufio.NewScanner(os.Stdin)
var err error = nil
for ok := true; ok; {
fmt.Println("Please enter the index of the version you want to use:")
scanner.Scan()
input, err = strconv.Atoi(scanner.Text())
if err != nil || input < 0 || input >= len(supportedVersions) {
// handle invalid input
fmt.Println("please enter a valid value")
continue
}
ok = false
fmt.Printf("\n\n")
}
return input
}
func readTrainName() string{
fmt.Println("Enter the name of the train:")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
fmt.Printf("\n\n")
return scanner.Text()
}
func readTrainMass() 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:")
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 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)
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
}
ok = false
fmt.Printf("\n\n")
}
return input
}
\ No newline at end of file
package main
import "fmt"
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
)
var supportedVersions = [...]versionStruct { {0,4,0} }
func main(){
fmt.Println("Hello World!")
fmt.Println("Welcome to the train creator.")
fmt.Println("\nFirst you will have to select which format version you want to target.")
fmt.Println("The format version determines the features that can be used.")
fmt.Println("It is recommended to use the latest version if possible.")
selectedVersion := supportedVersions[getFormatVersionIndex()]
fmt.Printf("You have selected the format Version {%d.%d.%d}\n",selectedVersion.Major,selectedVersion.Minor,selectedVersion.Patch)
create(selectedVersion)
}
func create(version versionStruct){
var trainConfig trainProperties
switch version {
case versionStruct{0,4,0}:
tmp := createTrainProperties040()
trainConfig = tmp.cast()
default:
trainConfig = createTrainProperties()
}
filename := trainConfig.Name + ".json"
jOutput, err := json.MarshalIndent(trainConfig, "", "")
if err != nil {
log.Fatalln(err.Error())
return
}
err = ioutil.WriteFile(filename, jOutput, 0644)
if err != nil {
log.Fatalln(err.Error())
return
}
fmt.Println("Created the train file " + filename)
}
package main
type versionStruct struct {
Major int64
Minor int64
Patch int64
}
type trainProperties struct{
trainProperties040
}
type trainProperties040 struct {
Name string `json:"name"`
Mass float64 `json:"mass"`
MaxVelocity float64 `json:"maxVelocity"`
MaxAcceleration float64 `json:"maxAcceleration"`
FormatVersion string `json:"formatVersion"`
TrackDrag float64 `json:"trackDrag"`
AirDrag float64 `json:"airDrag"`
VelocityUnit string `json:"velocityUnit"`
}
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
}
func createTrainProperties() trainProperties{
tmp := createTrainProperties040()
return tmp.cast()
}
func createTrainProperties040() trainProperties040{
var retVal trainProperties040
retVal.FormatVersion = "0.4.0"
retVal.Name = readTrainName()
retVal.Mass = readTrainMass()
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