DEV Community

Discussion on: Daily Challenge #121 - Who has the most money?

Collapse
 
dak425 profile image
Donald Feury • Edited

I put myself in here as a test case as a joke because I didn't get an allowance as a kid 😂

education.go

package education

type Trip struct {
    Students []Student
}

func (t Trip) Richest() string {
    sCount := len(t.Students)
    if sCount == 0 {
        return "none"
    }
    if sCount == 1 {
        return t.Students[0].Name
    }
    poorest, richest := t.Students[0], t.Students[0]
    for i := 1; i < sCount; i++ {
        if t.Students[i].Funds() < poorest.Funds() {
            poorest = t.Students[i]
        } else if t.Students[i].Funds() > richest.Funds() {
            richest = t.Students[i]
        }
    }
    if poorest.Funds() == richest.Funds() {
        return "all"
    }
    return richest.Name
}

type Student struct {
    Name     string
    Fives    int
    Tens     int
    Twenties int
}

func (s Student) Funds() int {
    return (s.Fives * 5) + (s.Tens * 10) + (s.Twenties * 20)
}

education_test.go

package education

import "testing"

type baseTestCase struct {
    description string
}

var bob = Student{"Bob", 4, 0, 0}
var mary = Student{"Mary", 0, 2, 0}
var john = Student{"John", 0, 0, 1}
var goat = Student{"Goat", 0, 0, 0}
var richie = Student{"Richie", 10, 20, 100}

func TestTrip_Richest(t *testing.T) {
    testCases := []struct {
        baseTestCase
        input    Trip
        expected string
    }{
        {
            baseTestCase{"no students"},
            Trip{},
            "none",
        },
        {
            baseTestCase{"one student"},
            Trip{[]Student{mary}},
            mary.Name,
        },
        {
            baseTestCase{"many students with same amount of funds"},
            Trip{[]Student{mary, bob, john}},
            "all",
        },
        {
            baseTestCase{"many students with one having more"},
            Trip{[]Student{mary, bob, goat, richie}},
            richie.Name,
        },
    }
    for _, test := range testCases {
        if result := test.input.Richest(); result != test.expected {
            t.Fatalf("FAIL: %s - %+v.Funds(): '%s' - expected: '%s'", test.baseTestCase.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.baseTestCase.description)
    }
}

func TestStudent_Funds(t *testing.T) {
    testCases := []struct {
        baseTestCase
        input    Student
        expected int
    }{
        {
            baseTestCase{"no moniez"},
            goat,
            0,
        },
        {
            baseTestCase{"only fives"},
            bob,
            20,
        },
        {
            baseTestCase{"only tens"},
            mary,
            20,
        },
        {
            baseTestCase{"only twenties"},
            john,
            20,
        },
        {
            baseTestCase{"all the moniez"},
            richie,
            2250,
        },
    }
    for _, test := range testCases {
        if result := test.input.Funds(); result != test.expected {
            t.Fatalf("FAIL: %s - %+v.Funds(): %d - expected: %d", test.baseTestCase.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.baseTestCase.description)
    }
}