changeset 3:ca866a38a6a0

Implement intaractive-mode & setting from config-file.
author pyon@macmini
date Sun, 16 Oct 2016 09:54:33 +0900
parents 451c99c1d9de
children 4c6bfc90d75c
files src/entai/entai.go src/entai_app/entai.conf src/entai_app/entai_app.go
diffstat 3 files changed, 136 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/src/entai/entai.go	Thu Oct 13 06:05:06 2016 +0900
+++ b/src/entai/entai.go	Sun Oct 16 09:54:33 2016 +0900
@@ -1,12 +1,14 @@
 /****************************************************************************/
-/* Entai.go ( ver 0.2 )                                                     */
-/*                                    Last Change: 2016-10-07 Fri 18:32:08. */
+/* Entai.go ( ver 0.3 )                                                     */
+/*                                    Last Change: 2016-10-16 Sun 09:50:13. */
+/*                                    By T.Mutoh                            */
 /****************************************************************************/
 
 package entai
 
 import (
     "fmt"
+    "strconv"
     "time"
 )
 
@@ -25,6 +27,9 @@
 }
 
 func init() {
+}
+
+func ( e *Entai ) DefaultRates() {
     // 1カ月まで, 1カ月経過後, 元旦
 	createRates( 4.3, 14.6, "20120101" )	// H24
 	createRates( 4.3, 14.6, "20130101" )	// H25
@@ -32,6 +37,11 @@
 	createRates( 2.9,  9.2, "20150101" )	// H27
 	createRates( 2.8,  9.1, "20160101" )	// H28
 	createRates( 2.8,  9.1, "20170101" )	// H29 *****
+
+	for range nyds {
+		e.yd0 = append( e.yd0, 0 )
+		e.yd1 = append( e.yd1, 0 )
+	}
 }
 
 func createRates( r0, r1 float64, nyd string ) {
@@ -41,11 +51,24 @@
 	nyds   = append( nyds, t )
 }
 
+func ( e *Entai ) AddRates( nyd, r0, r1 string ) error {
+	rf0, err := strconv.ParseFloat( r0, 64 ); if err != nil {
+        return err
+    }
+	rf1, err := strconv.ParseFloat( r1, 64 ); if err != nil {
+        return err
+    }
+	createRates( rf0, rf1, nyd )
+    e.yd0 = append( e.yd0, 0 )
+    e.yd1 = append( e.yd1, 0 )
+    return nil
+}
+
 func ( e Entai ) String() string {
     return "Entai....."
 }
 
-func ( e Entai ) GetRate() string {
+func ( e Entai ) GetRates() string {
 	var str string
 	for i, n := range nyds {
 		str += fmt.Sprintf( " (H%d) ", n.Year() - 1988 )
@@ -99,13 +122,6 @@
     }
 }
 
-func ( e *Entai ) Create() {
-	for range nyds {
-		e.yd0 = append( e.yd0, 0 )
-		e.yd1 = append( e.yd1, 0 )
-	}
-}
-
 func ( e *Entai ) Set( d, p time.Time, g int ) {
     e.due, e.paid = d, p
     e.tax = g
@@ -118,7 +134,7 @@
 	if e.tax < 2000 {
 		return 0, "tax < 2000"
 	}
-	e.tax = int( e.tax / 1000 * 1000 ) // 端数処理
+	e.tax = int( e.tax / 1000 ) * 1000 // 端数処理
 
 	/* 本計算 */
 	var tmp float64
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/entai_app/entai.conf	Sun Oct 16 09:54:33 2016 +0900
@@ -0,0 +1,9 @@
+#  元号, 元旦, 1カ月まで, 1カ月経過後
+H24, 20120101, 4.3, 14.6
+H25, 20130101, 4.3, 14.6
+H26, 20140101, 4.3, 14.6
+
+H27, 20150101, 2.9,  9.2
+H28, 20160101, 2.8,  9.1
+H29, 20170101, 2.8, 99.9
+
--- a/src/entai_app/entai_app.go	Thu Oct 13 06:05:06 2016 +0900
+++ b/src/entai_app/entai_app.go	Sun Oct 16 09:54:33 2016 +0900
@@ -1,14 +1,18 @@
 /****************************************************************************/
-/* Entai_app.go ( ver 0.3 )                                                 */
-/*                                    Last Change: 2016-10-13 Thu 06:04:09. */
+/* Entai_app.go                                                             */
+/*                                    Last Change: 2016-10-15 Sat 19:29:24. */
+/*                                    By T.Mutoh                            */
 /****************************************************************************/
 
 package main
 
 import (
     "bufio"
+    "encoding/csv"
     "flag"
     "fmt"
+    "io"
+    "log"
     "os"
     "strconv"
     "strings"
@@ -17,18 +21,17 @@
     "entai"
 )
 
-var ver = "0.3"
+var ver = "0.4"
 var e entai.Entai
 
 func main() {
 
-	e.Create()
-
     // arguments
     h := flag.Bool( "h", false, "help" )
+    c := flag.String( "c", "", "config csv file" )
     r := flag.Bool( "r", false, "print rate" )
+    b := flag.Bool( "b", false, "batch mode" )
     l := flag.Bool( "l", false, "1-liner mode" )
-    b := flag.Bool( "b", false, "batch mode" )
     v := flag.Bool( "v", false, "print verbose in 1-liner mode")
     i := flag.Bool( "i", false, "print input value in 1-liner mode")
     d := flag.Bool( "d", false, "intaracitve mode")
@@ -44,60 +47,91 @@
         os.Exit( 1 )
     }
 
-    if *h {
-        printTile()
-        fmt.Fprint( os.Stderr, "1-liner mode:\n" )
-        fmt.Fprint( os.Stderr, "> entai_app -l 20160731 20161224 30000\n\n" )
-        fmt.Fprint( os.Stderr, "> entai_app -i -l 20160731 20161224 30000\n\n" )
-        fmt.Fprint( os.Stderr, "> entai_app -i -v -l 20160731 20161224 30000\n\n" )
-        fmt.Fprint( os.Stderr, "server mode:\n" )
-        fmt.Fprint( os.Stderr, "> entai_app -s -p 3000\n" )
-        os.Exit( 0 )
-    }
+    // Setting rates
+    if *c == "" {
+        e.DefaultRates()
+    } else {
+        f, err := os.Open( *c ); if err != nil {
+            fmt.Fprintf( os.Stderr, "cannot open config-file : %v\n", err )
+            os.Exit( 1 )
+        }
 
-	if *r {
-        printTile()
-		fmt.Fprint( os.Stderr, e.GetRate() )
-        os.Exit( 0 )
-	}
+        r := csv.NewReader( f )
+        r.Comment = '#'
+        r.TrimLeadingSpace = true
+        for {
+            record, err := r.Read()
+            if err == io.EOF {
+                break
+            } else if err != nil {
+                log.Fatal( err )
+            }
+            e.AddRates( record[1], record[2], record[3] )
+        }
+        f.Close()
 
-    if *s {
-        *p = 8080
-        fmt.Fprint( os.Stderr, "server mode is not implemented.\n" )
-        os.Exit( 0 )
     }
 
-    if *d {
+    switch {
+
+    // Show examples
+    case *h :
         printTile()
-		fmt.Print( e.GetRate() )
+        fmt.Fprint( os.Stderr, "1-liner mode:\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -l 30000 20160731 20161224\n\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -i -l 29000 20160731 20161224\n\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -i -v -l 15000 20160731 20161224\n\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -c conf/entai.conf -l 15000 20160731 20161224\n\n" )
+        fmt.Fprint( os.Stderr, "batch mode:\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -b 1.csv 2.csv ...\n\n" )
+        fmt.Fprint( os.Stderr, "server mode:\n" )
+        fmt.Fprint( os.Stderr, "> entai_app -s -p 3000\n" )
+        os.Exit( 1 )
+
+    // Show rates
+    case *r :
+        printTile()
+		fmt.Fprint( os.Stderr, e.GetRates() )
+        os.Exit( 0 )
+
+    // Server-mode ( not implement )
+    case *s :
+        *p = 8080
+        fmt.Fprint( os.Stderr, "server mode is not implemented.\n" )
+        os.Exit( 1 )
+
+    // Intaractive-mode
+    case *d :
+        printTile()
+		fmt.Print( e.GetRates() )
         fmt.Print( "-----------\n\n" )
 
+        var t, d, p string
         for {
-            fmt.Print( "Input Tax  > " )
-            input := bufio.NewScanner( os.Stdin )
-            input.Scan()
-            t := input.Text()
+            t = getFromTerminal( "Input Tax ", t )
+            d = getFromTerminal( "Input Due ", d )
+            p = getFromTerminal( "Input Paid", p )
 
-            fmt.Print( "Input Due  > " )
-            input = bufio.NewScanner( os.Stdin )
-            input.Scan()
-            d := input.Text()
-
-            fmt.Print( "Input Paid > " )
-            input = bufio.NewScanner( os.Stdin )
-            input.Scan()
-            p := input.Text()
-
-            result, _, err := processEntai( d, p, t )
+            result, detail, err := processEntai( t, d, p )
             if err != nil {
                 fmt.Printf( "%v\n", err )
             }
-            fmt.Printf( " = %d\n\n", result )
+            fmt.Printf( " = %d", result )
 
+            fmt.Print( "\t\t\t... Continue [Enter] / Detail [1] / Quit [9] ? " )
+            input := bufio.NewScanner( os.Stdin )
+            input.Scan()
+            switch input.Text() {
+            case "1" :
+                fmt.Printf( " = %s\n\n", detail )
+            case "9" :
+                os.Exit( 0 )
+            default :
+            }
         }
-    }
 
-    if *l {
+    // 1-Liner-mode
+    case *l :
         if *i {
             fmt.Printf( "%s,%s,%s,", flag.Arg(0), flag.Arg(1), flag.Arg(2) )
         }
@@ -115,10 +149,9 @@
         fmt.Print( "\n" )
 
         os.Exit( 0 )
-    }
 
-    if *b {
-
+    // Batch-mode
+    case *b :
         files := flag.Args()
 
         if len( files ) == 0 {
@@ -159,7 +192,12 @@
     }
 }
 
-func processEntai( due, paid, tax string ) ( int, string, error ) {
+// Subroutines
+func processEntai( tax, due, paid string ) ( int, string, error ) {
+	t, msg, err := validInt( tax ); if err != nil {
+		return -1, msg, err
+	}
+
 	d, msg, err := validDate( due ); if err != nil {
 		return -1, msg, err
 	}
@@ -167,10 +205,6 @@
 		return -1, msg, err
 	}
 
-	t, msg, err := validInt( tax ); if err != nil {
-		return -1, msg, err
-	}
-
 	e.Set( d, p, t )
 	result, detail := e.Result()
 
@@ -193,9 +227,21 @@
     return i, "", nil
 }
 
+func getFromTerminal( prompt, last string ) ( string ) {
+    fmt.Printf( "%s > ", prompt )
+    input := bufio.NewScanner( os.Stdin )
+    input.Scan()
+    s := input.Text()
+    if s == "." {
+        s = last
+        fmt.Printf( "%s > %s\n", prompt, s )
+    }
+    return s
+}
+
 func printTile() {
     fmt.Fprint(  os.Stderr, "\n==================================================\n" )
-    fmt.Fprintf( os.Stderr, "  Entai_app ver %s ( 2016.10.09 ) - since 2016\n", ver  )
+    fmt.Fprintf( os.Stderr, "  Entai_app ver %s ( 2016.10.19 ) - since 2016\n", ver  )
     fmt.Fprint(  os.Stderr, "==================================================\n\n" )
 }