Skip to content

Commit

Permalink
worked on the putting the variables of calculate remaining in a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ibilalkayy committed May 29, 2024
1 parent 8bede71 commit 5a6bb43
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
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.113"
const version = "v0.1.114"

// rootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down
7 changes: 7 additions & 0 deletions entities/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ type BudgetVariables struct {
Spent int
Remaining int
}

type BudgetCalculateVariables struct {
BudgetAmount int
BudgetAmountInDB int
SpentAmountInDB int
RemainingAmountInDB int
}
14 changes: 9 additions & 5 deletions framework/db/budget_db/budget_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ func (h MyBudgetDB) UpdateBudget(bv *entities.BudgetVariables, new_category stri
totalBudgetAmount += amount
}

// bv := entities.BudgetVariables{Category: old}
expectional_budget_amount, err := h.Deps.ManageBudget.BudgetAmountWithException(bv)
if err != nil {
return err
Expand All @@ -228,10 +227,16 @@ func (h MyBudgetDB) UpdateBudget(bv *entities.BudgetVariables, new_category stri
return errors.New("unable to convert to int")
}

cr := entities.BudgetCalculateVariables{
BudgetAmount: bv.Amount,
BudgetAmountInDB: budgetAmountInDB,
SpentAmountInDB: spentAmountInDB,
RemainingAmountInDB: remainingAmountInDB,
}

if len(includedCategory) != 0 && fullTotalAmount != 0 && bv.Amount <= fullTotalAmount && totalBudgetAmount <= fullTotalAmount && expectional_budget_amount+bv.Amount <= fullTotalAmount {
if len(new_category) != 0 && bv.Amount != 0 {
details := [4]int{bv.Amount, budgetAmountInDB, spentAmountInDB, remainingAmountInDB}
data, err := h.Deps.ManageBudget.CalculateRemaining(details)
data, err := h.Deps.ManageBudget.CalculateRemaining(&cr)
if err != nil {
return err
}
Expand All @@ -241,8 +246,7 @@ func (h MyBudgetDB) UpdateBudget(bv *entities.BudgetVariables, new_category stri
query = "UPDATE Budget SET categories=$1 WHERE categories=$2"
params = []interface{}{new_category, bv.Category}
} else if bv.Amount != 0 {
details := [4]int{bv.Amount, budgetAmountInDB, spentAmountInDB, remainingAmountInDB}
data, err := h.Deps.ManageBudget.CalculateRemaining(details)
data, err := h.Deps.ManageBudget.CalculateRemaining(&cr)
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions framework/db/budget_db/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ func (h MyBudgetDB) BudgetAmountWithException(bv *entities.BudgetVariables) (int
return amounts, nil
}

func (MyBudgetDB) CalculateRemaining(details [4]int) ([2]int, error) {
if details[0] > details[1] {
updatedRemaining := details[0] - details[1]
details[3] += updatedRemaining
} else if details[0] < details[1] {
if details[2] <= details[0] {
details[3] = details[0] - details[2]
func (MyBudgetDB) CalculateRemaining(cr *entities.BudgetCalculateVariables) ([2]int, error) {
if cr.BudgetAmount > cr.BudgetAmountInDB {
updatedRemaining := cr.BudgetAmount - cr.BudgetAmountInDB
cr.RemainingAmountInDB += updatedRemaining
} else if cr.BudgetAmount < cr.BudgetAmountInDB {
if cr.SpentAmountInDB <= cr.BudgetAmount {
cr.RemainingAmountInDB = cr.BudgetAmount - cr.SpentAmountInDB
} else {
details[2] = 0
details[3] = 0
cr.SpentAmountInDB = 0
cr.RemainingAmountInDB = 0
}
} else {
return [2]int{}, errors.New("this amount is already present. enter a different amount")
}
result := [2]int{details[2], details[3]}
result := [2]int{cr.SpentAmountInDB, cr.RemainingAmountInDB}
return result, nil
}
2 changes: 1 addition & 1 deletion framework/db/total_amount_db/total_amount_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (h MyTotalAmountDB) UpdateTotalAmount(tv *entities.TotalAmountVariables) er
if spentAmountInDB <= tv.TotalAmount {
remainingAmountInDB = tv.TotalAmount - spentAmountInDB
} else {
spentAmountInDB = tv.TotalAmount
spentAmountInDB = 0
remainingAmountInDB = 0
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type ManageBudget interface {
RemoveBudget(category string) error
AddBudgetExpenditure(spent int, category string) error
GetBudgetData(filepath, filename string) error
CalculateRemaining(details [4]int) ([2]int, error)
CalculateRemaining(cr *entities.BudgetCalculateVariables) ([2]int, error)

InsertHistory(hv *entities.HistoryVariables) error
ViewHistory(category string) ([2]interface{}, error)
Expand Down

0 comments on commit 5a6bb43

Please sign in to comment.