amount to float

master
bel 2021-07-31 23:54:10 -06:00
parent 43b282eee5
commit 33bfae1b8a
3 changed files with 15 additions and 12 deletions

View File

@ -19,7 +19,7 @@ func TestLedgerTransactions(t *testing.T) {
Description: "Qt needs pizza n kodiak", Description: "Qt needs pizza n kodiak",
Payee: "Withdrawal:Home:Grocery+Resturaunt:Target", Payee: "Withdrawal:Home:Grocery+Resturaunt:Target",
Payer: "Debts:Credit:ChaseAarpChaseVisa", Payer: "Debts:Credit:ChaseAarpChaseVisa",
Amount: "$37.86", Amount: 37.86,
}); transactions[0] != want { }); transactions[0] != want {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, transactions[0]) t.Fatalf("want \n\t%+v, got \n\t%+v", want, transactions[0])
} }
@ -28,7 +28,7 @@ func TestLedgerTransactions(t *testing.T) {
Description: "Testing detecting deposits via email alerts 5421705162", Description: "Testing detecting deposits via email alerts 5421705162",
Payer: "AssetAccount:Cash:Uccu", Payer: "AssetAccount:Cash:Uccu",
Payee: "Debts:Credit:ChaseAarpChaseVisa", Payee: "Debts:Credit:ChaseAarpChaseVisa",
Amount: "$100.00", Amount: 100.00,
}); transactions[1] != want { }); transactions[1] != want {
t.Fatalf("want \n\t%+v, got \n\t%+v", want, transactions[1]) t.Fatalf("want \n\t%+v, got \n\t%+v", want, transactions[1])
} }

View File

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"strconv"
"strings" "strings"
) )
@ -13,7 +14,7 @@ type Transaction struct {
Description string Description string
Payer string Payer string
Payee string Payee string
Amount string Amount float32
} }
func readTransaction(r io.Reader) (Transaction, error) { func readTransaction(r io.Reader) (Transaction, error) {
@ -83,29 +84,31 @@ func (transaction *Transaction) readDescription(lines [][]byte) error {
return nil return nil
} }
func (transaction *Transaction) readAmount(lines [][]byte) error {
amount := string(words(lines[1])[1])
f, err := strconv.ParseFloat(strings.Trim(amount, "$"), 32)
transaction.Amount = float32(f)
return err
}
func (transaction *Transaction) readPayerPayee(lines [][]byte) error { func (transaction *Transaction) readPayerPayee(lines [][]byte) error {
payer := string(words(lines[1])[0]) payer := string(words(lines[1])[0])
payee := string(words(lines[2])[0]) payee := string(words(lines[2])[0])
if !strings.Contains(transaction.Amount, "-") { if transaction.Amount >= 0 {
tmp := payer tmp := payer
payer = payee payer = payee
payee = tmp payee = tmp
} else { } else {
transaction.Amount = strings.ReplaceAll(transaction.Amount, "-", "") transaction.Amount *= -1
} }
transaction.Payer = payer transaction.Payer = payer
transaction.Payee = payee transaction.Payee = payee
return nil return nil
} }
func (transaction *Transaction) readAmount(lines [][]byte) error {
transaction.Amount = string(words(lines[1])[1])
return nil
}
func (transaction Transaction) Marshal() string { func (transaction Transaction) Marshal() string {
return fmt.Sprintf( return fmt.Sprintf(
"%-25s%s\n%25s%-50s%s\n%25s%s", "%-25s%s\n%25s%-50s%.2f\n%25s%s",
transaction.Date, transaction.Date,
transaction.Description, transaction.Description,
"", "",

View File

@ -73,7 +73,7 @@ func TestReadTransaction(t *testing.T) {
Date: "2021-07-01", Date: "2021-07-01",
Description: "description", Description: "description",
Payee: "payee", Payee: "payee",
Amount: "$1.00", Amount: 1.00,
Payer: "payer", Payer: "payer",
}, },
}, },