Files
goodcalc/theme.go
2021-01-03 08:08:10 +01:00

96 lines
3.2 KiB
Go

package goodcalc
import "fmt"
// Theme is the basic element of the matrix.
// No defines the id of the excel balance.
// A1 for Human dignity in the supply chain
type Theme struct {
No string `json:"no"`
Weight float32 `json:"weight"`
Aspects []Aspect `json:"aspects"`
NegativeAspects []NegativeAspect `json:"negative_aspects"`
Calc *ThemeCalc `json:"calculation"`
negPointsFactor int
}
func (t *Theme) String() string {
var s string
s += fmt.Sprintf(" No: %s\n", t.No)
s += fmt.Sprintf(" Weight: %.1f\n", t.Weight)
s += fmt.Sprintf(" Aspects: %v\n", t.Aspects)
s += fmt.Sprintf(" NegativeAspects: %v\n", t.NegativeAspects)
s += fmt.Sprintf(" NegPointsFactor: %v\n", t.negPointsFactor)
s += fmt.Sprintf(" Calc: %s\n", t.Calc)
return s
}
func (t *Theme) calcNrPosAspects() {
t.Calc.NrPositiveAspects = len(t.Aspects)
}
func (t *Theme) calcValPoints() {
t.Calc.ValuationPoints = 0
for _, a := range t.Aspects {
t.Calc.ValuationPoints += a.ValuationPoints * int(a.Weight)
}
for _, na := range t.NegativeAspects {
t.Calc.NegativeValuationPoints +=
na.ValuationPoints * int(na.Weight)
}
}
func (t *Theme) calcEstPercentage() {
t.Calc.EstPercentage = 1
if t.Calc.MaxValuationPoints != 0 {
t.Calc.EstPercentage =
float32(t.Calc.ValuationPoints) / float32(t.Calc.MaxValuationPoints)
}
}
func (t *Theme) calcBalancePoints() {
t.Calc.BalancePoints =
t.Calc.EstPercentage * t.Calc.MaxBalancePoints
t.Calc.NegativeBlancePoints =
float32(t.Calc.NegativeValuationPoints) * t.Calc.MaxBalancePoints / float32(t.negPointsFactor)
}
// ThemeCalc contains the different calculation
// steps.
type ThemeCalc struct {
CalcWeight float32 `json:"calc_weight"`
WeightFactor float32 `json:"weight_factor"`
MaxBalancePoints float32 `json:"max_points"`
NrPositiveAspects int `json:"nr_positive_aspects"`
ValuationPoints int `json:"valuation_points"`
MaxValuationPoints int `json:"max_valuation_points"`
EstPercentage float32 `json:"est_percentage"`
BalancePoints float32 `json:"balance_points"`
NegativeValuationPoints int `json:"negative_valuation_points"`
NegativeBlancePoints float32 `json:"negative_blance_points"`
}
func (tc *ThemeCalc) String() string {
if tc == nil {
return "<nil>"
}
var s string
s += fmt.Sprintf("\t\tCalcWeight: %.1f\n", tc.CalcWeight)
s += fmt.Sprintf("\t\tWeightFactor: %.1f\n", tc.WeightFactor)
s += fmt.Sprintf("\t\tMaxBalancePoints: %.1f\n", tc.MaxBalancePoints)
s += fmt.Sprintf("\t\tNrPositiveAspects: %d\n", tc.NrPositiveAspects)
s += fmt.Sprintf("\t\tValuationPoints: %d\n", tc.ValuationPoints)
s += fmt.Sprintf("\t\tMaxValuationPoints: %d\n", tc.MaxValuationPoints)
s += fmt.Sprintf("\t\tEstPercentage: %.1f\n", tc.EstPercentage)
s += fmt.Sprintf("\t\tBalancePoints: %.1f\n", tc.BalancePoints)
s += fmt.Sprintf("\t\tNegativeValuationPoints: %d\n", tc.NegativeValuationPoints)
s += fmt.Sprintf("\t\tNegativeBlancePoints: %.1f\n", tc.NegativeBlancePoints)
return s
}
// calcMaxPoints
// Stakeholder.calcWeight needs to run first
func (c *ThemeCalc) calcMaxPoints() {
c.MaxBalancePoints = c.CalcWeight * c.WeightFactor
}