Skip to content

Commit

Permalink
added the total ammount and the condition for the category to be must…
Browse files Browse the repository at this point in the history
… entered
  • Loading branch information
ibilalkayy committed Mar 18, 2024
1 parent 3338e51 commit 433b35b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ With a user-friendly CLI. It manages the finances and achieve greater financial

## Table of Contents

- [Documentation](#documentation)
- [What You Need?](#what-you-need)
- [Clonning](#clonning)
- [Installation](#installation)
Expand All @@ -15,6 +16,10 @@ With a user-friendly CLI. It manages the finances and achieve greater financial
- [License](#license)


## Documentation

To get to know about the application in detail, you can visit the [docs](https://github.com/ibilalkayy/flow/tree/main/docs) to understand the application in a better way.

## What You Need?

To get started in flow, you need to have two applications installed on your machine.
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.1.27"
const version = "v0.1.28"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
35 changes: 28 additions & 7 deletions internal/app/budget/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"strconv"

"github.com/ibilalkayy/flow/db/budget_db"
"github.com/ibilalkayy/flow/internal/structs"
Expand All @@ -23,14 +24,17 @@ func CreateBudget(bv *structs.BudgetVariables, basePath string) error {
if err != nil {
return err
}

defer insert.Close()

_, err = insert.Exec(bv.Category, bv.Amount)
if err != nil {
return err
if len(bv.Category) != 0 {
_, err = insert.Exec(bv.Category, bv.Amount)
if err != nil {
return err
}
fmt.Println("Budget data is successfully inserted!")
} else {
return errors.New("category can't be empty")
}
fmt.Println("Budget data is successfully inserted!")
return nil
}

Expand All @@ -49,6 +53,9 @@ func ViewBudget(category string) (string, error) {
tw := table.NewWriter()
tw.AppendHeader(table.Row{"Category", "Amount"})

// Initialize total amount
totalAmount := 0.0

// Query the database based on the provided category
var rows *sql.Rows
if len(category) != 0 {
Expand All @@ -68,11 +75,25 @@ func ViewBudget(category string) (string, error) {
if err := rows.Scan(&bv.Category, &bv.Amount); err != nil {
return "", err
}
tw.AppendRow([]interface{}{bv.Category, bv.Amount})
// Check if amount is empty
if bv.Amount != "" {
// Convert bv.Amount to float64
amount, err := strconv.ParseFloat(bv.Amount, 64)
if err != nil {
return "", err
}
tw.AppendRow([]interface{}{bv.Category, amount})
totalAmount += amount // Accumulate total amount
}
}

// Add total amount row to the table
tw.AppendFooter(table.Row{"Total Amount", totalAmount})

// Render the table
return "Budget Data\n" + tw.Render(), nil
tableRender := "Budget Data\n" + tw.Render()

return tableRender, nil
}

func RemoveBudget(category string) error {
Expand Down

0 comments on commit 433b35b

Please sign in to comment.