diff src/entai_app/entai_app.go @ 2:451c99c1d9de

implement intaractive mode.
author pyon@macmini
date Thu, 13 Oct 2016 06:05:06 +0900
parents 3dafd57af3b1
children ca866a38a6a0
line wrap: on
line diff
--- a/src/entai_app/entai_app.go	Thu Oct 06 00:55:26 2016 +0900
+++ b/src/entai_app/entai_app.go	Thu Oct 13 06:05:06 2016 +0900
@@ -1,6 +1,6 @@
 /****************************************************************************/
-/* Entai_app.go ( ver 0.2 )                                                 */
-/*                                    Last Change: 2016-10-06 Thu 00:49:10. */
+/* Entai_app.go ( ver 0.3 )                                                 */
+/*                                    Last Change: 2016-10-13 Thu 06:04:09. */
 /****************************************************************************/
 
 package main
@@ -17,9 +17,12 @@
     "entai"
 )
 
+var ver = "0.3"
+var e entai.Entai
+
 func main() {
 
-    var entai entai.Entai
+	e.Create()
 
     // arguments
     h := flag.Bool( "h", false, "help" )
@@ -35,14 +38,14 @@
     flag.Parse()
 
     if flag.NFlag() == 0 {
-        PrintTile()
+        printTile()
         flag.PrintDefaults()
         fmt.Fprint( os.Stderr, "\n" )
         os.Exit( 1 )
     }
 
     if *h {
-        PrintTile()
+        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" )
@@ -53,8 +56,8 @@
     }
 
 	if *r {
-        PrintTile()
-		fmt.Fprint( os.Stderr, entai.GetRate() )
+        printTile()
+		fmt.Fprint( os.Stderr, e.GetRate() )
         os.Exit( 0 )
 	}
 
@@ -65,65 +68,88 @@
     }
 
     if *d {
-        PrintTile()
-        //layout := "20060102"
-        //for {
-        //}
-        os.Exit( 0 )
+        printTile()
+		fmt.Print( e.GetRate() )
+        fmt.Print( "-----------\n\n" )
+
+        for {
+            fmt.Print( "Input Tax  > " )
+            input := bufio.NewScanner( os.Stdin )
+            input.Scan()
+            t := input.Text()
+
+            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 )
+            if err != nil {
+                fmt.Printf( "%v\n", err )
+            }
+            fmt.Printf( " = %d\n\n", result )
+
+        }
     }
 
     if *l {
-        layout := "20060102"
-
-        d, err := time.Parse( layout, flag.Arg(0) ); if err != nil {
-            fmt.Fprint( os.Stderr, "bad date format.\n" )
-            os.Exit( 1 )
-        }
-        p, err := time.Parse( layout, flag.Arg(1) ); if err != nil {
-            fmt.Fprint( os.Stderr, "bad date format.\n" )
-            os.Exit( 1 )
+        if *i {
+            fmt.Printf( "%s,%s,%s,", flag.Arg(0), flag.Arg(1), flag.Arg(2) )
         }
 
-        t, err := strconv.Atoi( flag.Arg(2) )
-		if err != nil || t < 0 {
-            fmt.Fprint( os.Stderr, "bad money format.\n" )
-            os.Exit( 1 )
-        }
-
-        if *i {
-            fmt.Printf( "%v,%v,%d,", d.Format( layout ), p.Format( layout ), t )
-        }
-
-		// MAIN
-		entai.Set( d, p, t )
-        result, detail := entai.Result()
-		//
-
+		result, detail, err := processEntai( flag.Arg(0), flag.Arg(1), flag.Arg(2) )
+        if err != nil {
+			fmt.Fprintf( os.Stderr, "%s : %v\n", detail, err )
+			os.Exit( 1 )
+		}
         fmt.Print( result )
 
         if *v {
             fmt.Print( ",", detail )
         }
         fmt.Print( "\n" )
+
         os.Exit( 0 )
     }
 
     if *b {
+
         files := flag.Args()
+
         if len( files ) == 0 {
             fmt.Fprint( os.Stderr, "no input file.\n" )
             os.Exit( 1 )
         } else {
+
             for _, file := range files {
                 f, err := os.Open( file ); if err != nil {
                     fmt.Fprintf( os.Stderr, "cannot open file.[%v]\n", err )
                     os.Exit( 1 )
                 }
+
                 input := bufio.NewScanner( f )
                 b := bufio.NewWriter( os.Stdout )
                 for input.Scan() {
                     s := strings.Split( input.Text(), "," )
-                    fmt.Fprint( b, s[0] )
+					if strings.HasPrefix( input.Text(), "#" ) {
+						if strings.Contains( input.Text(), "@@" ) {
+							buf := strings.Replace( input.Text(), "#", "", 1 )
+							buf  = strings.Replace( buf, "@@", "", 1 )
+							fmt.Fprintf( b, "%s\n", buf )
+						}
+						continue
+					}
+					result, detail, err := processEntai( s[0], s[1], s[2] )
+                    if err != nil {
+						fmt.Fprintf( os.Stderr, "%s : %v\n", detail, err )
+						os.Exit( 1 )
+					}
+					fmt.Fprintf( b, "%s,%s,%s,%d,%s\n", s[0], s[1], s[2], result, detail )
                 }
                 b.Flush()
                 f.Close()
@@ -133,9 +159,43 @@
     }
 }
 
-func PrintTile() {
-    fmt.Fprint( os.Stderr, "\n==================================================\n" )
-    fmt.Fprint( os.Stderr, "  Entai_app ver 0.2  ( 2016.10.09 ) - since 2016\n" )
-    fmt.Fprint( os.Stderr, "==================================================\n\n" )
+func processEntai( due, paid, tax string ) ( int, string, error ) {
+	d, msg, err := validDate( due ); if err != nil {
+		return -1, msg, err
+	}
+	p, msg, err := validDate( paid ); if err != nil {
+		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()
+
+	return result, detail, nil
 }
 
+func validDate( s string ) ( time.Time, string, error ) {
+	layout := "20060102"
+	t, err := time.Parse( layout, s ); if err != nil {
+		return t, "bad date format.", err
+	}
+    return t, "", nil
+}
+
+func validInt( s string ) ( int, string, error ) {
+	i, err := strconv.Atoi( s )
+	if err != nil || i < 0 {
+		return -1, "bad money format.", err
+	}
+    return i, "", nil
+}
+
+func printTile() {
+    fmt.Fprint(  os.Stderr, "\n==================================================\n" )
+    fmt.Fprintf( os.Stderr, "  Entai_app ver %s ( 2016.10.09 ) - since 2016\n", ver  )
+    fmt.Fprint(  os.Stderr, "==================================================\n\n" )
+}
+