Filling in Date implementation

This commit is contained in:
Dan Buch 2012-12-21 22:31:33 -05:00
parent b7480b78a8
commit 209e074503
3 changed files with 64 additions and 1 deletions

View File

@ -7,7 +7,8 @@ TARGETS := \
$(REPO_BASE)/algs4-rolls \ $(REPO_BASE)/algs4-rolls \
$(REPO_BASE)/algs4-randomseq \ $(REPO_BASE)/algs4-randomseq \
$(REPO_BASE)/algs4-average \ $(REPO_BASE)/algs4-average \
$(REPO_BASE)/algs4-interval2d $(REPO_BASE)/algs4-interval2d \
$(REPO_BASE)/algs4-date
test: build test: build
go test -x $(TARGETS) go test -x $(TARGETS)

View File

@ -0,0 +1,39 @@
package main
import (
"errors"
"fmt"
"os"
"strconv"
"github.com/meatballhat/box-o-sand/algs4/src/go/algs4"
)
func die(err error) {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
func main() {
if len(os.Args) < 4 {
die(errors.New("We need 3 ints!"))
}
m, err := strconv.Atoi(os.Args[1])
if err != nil {
die(err)
}
d, err := strconv.Atoi(os.Args[2])
if err != nil {
die(err)
}
y, err := strconv.Atoi(os.Args[3])
if err != nil {
die(err)
}
date := algs4.NewDate(m, d, y)
fmt.Println(date)
}

View File

@ -0,0 +1,23 @@
package algs4
import (
"fmt"
)
type Date struct {
Month int
Day int
Year int
}
func NewDate(m, d, y int) *Date {
return &Date{
Month: m,
Day: d,
Year: y,
}
}
func (me *Date) String() string {
return fmt.Sprintf("%d/%d/%d", me.Month, me.Day, me.Year)
}