-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement calculation for all provider
- Loading branch information
1 parent
3dc4ade
commit 128d090
Showing
10 changed files
with
424 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package primitive | ||
|
||
import "errors" | ||
|
||
type Rate struct { | ||
PerKilogram int64 | ||
PerKilometer int64 | ||
PerCmCubic int64 | ||
KilometerPerHour int64 | ||
} | ||
|
||
func (rate Rate) Validate() error { | ||
if rate.PerKilogram < 0 { | ||
return errors.New("rate in kilogram lower than 0") | ||
} | ||
|
||
if rate.PerKilometer < 0 { | ||
return errors.New("rate in kilometer lower than 0") | ||
} | ||
|
||
if rate.PerCmCubic < 0 { | ||
return errors.New("rate cm cubic is lower than 0") | ||
} | ||
|
||
if rate.KilometerPerHour < 0 { | ||
return errors.New("kilometer per hour lower than 0") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package primitive_test | ||
|
||
import ( | ||
"mock-shipping-provider/primitive" | ||
"testing" | ||
) | ||
|
||
func TestRateInsuficient(t *testing.T) { | ||
var rate primitive.Rate | ||
|
||
err := rate.Validate() | ||
|
||
if err != nil { | ||
t.Error("get errorrrr") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package provider | ||
|
||
import ( | ||
"mock-shipping-provider/primitive" | ||
"mock-shipping-provider/repository" | ||
) | ||
|
||
type Anteraja struct { | ||
Rate primitive.Rate | ||
} | ||
|
||
func NewAnterajaCalculation(anterAjaRate *primitive.Rate) repository.ProviderCalculation { | ||
return &Anteraja{ | ||
Rate: primitive.Rate{ | ||
PerKilometer: anterAjaRate.PerKilometer, | ||
PerKilogram: anterAjaRate.PerKilogram, | ||
PerCmCubic: anterAjaRate.PerCmCubic, | ||
KilometerPerHour: anterAjaRate.KilometerPerHour, | ||
}, | ||
} | ||
} | ||
|
||
func (anterAja *Anteraja) CalculatePrice(distance float64, dimension primitive.Dimension, weight float64) int64 { | ||
volume := dimension.Width * dimension.Height * dimension.Depth | ||
|
||
distanceCost := distance * float64(anterAja.Rate.PerKilometer) | ||
|
||
weightCost := weight * float64(anterAja.Rate.PerKilogram) | ||
|
||
volumeCost := volume * float64(anterAja.Rate.PerCmCubic) | ||
|
||
return int64(distanceCost + weightCost + volumeCost) | ||
} | ||
|
||
func (anterAja *Anteraja) CalculateTimeOfArrival(distance float64) int64 { | ||
if distance < float64(anterAja.Rate.KilometerPerHour) { | ||
return 1 | ||
} | ||
|
||
return int64(distance / float64(anterAja.Rate.KilometerPerHour)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package provider_test | ||
|
||
import ( | ||
"mock-shipping-provider/primitive" | ||
"mock-shipping-provider/repository/provider" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func newRateAnterAja() primitive.Rate { | ||
return primitive.Rate{ | ||
PerKilogram: 700, | ||
PerKilometer: 500, | ||
PerCmCubic: 400, | ||
KilometerPerHour: 60, | ||
} | ||
} | ||
|
||
func createAnterAjaPrice(t *testing.T, distance float64, dimension primitive.Dimension, weight float64) int64 { | ||
anterAjaRate := newRateAnterAja() | ||
|
||
AnterAja := provider.NewAnterajaCalculation(&anterAjaRate) | ||
|
||
result := AnterAja.CalculatePrice(distance, dimension, weight) | ||
|
||
return result | ||
} | ||
|
||
func createAnterAjaTimeArrival(t *testing.T, distance float64) int64 { | ||
anterAjaRate := newRateAnterAja() | ||
|
||
AnterAja := provider.NewAnterajaCalculation(&anterAjaRate) | ||
|
||
result := AnterAja.CalculateTimeOfArrival(distance) | ||
|
||
return result | ||
} | ||
|
||
func TestAnterAjaCalculation(t *testing.T) { | ||
anterAjaRate := newRateAnterAja() | ||
|
||
AnterAja := provider.NewAnterajaCalculation(&anterAjaRate) | ||
|
||
testCases := []struct { | ||
distance float64 | ||
dimension primitive.Dimension | ||
weight float64 | ||
}{ | ||
{distance: 3400}, | ||
{dimension: primitive.Dimension{ | ||
Height: 40, | ||
Depth: 50, | ||
Width: 9, | ||
}}, | ||
{weight: 2}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run("calculate the price", func(t *testing.T) { | ||
result := AnterAja.CalculatePrice(testCase.distance, testCase.dimension, testCase.weight) | ||
|
||
expectedPrice := createAnterAjaPrice(t, testCase.distance, testCase.dimension, testCase.weight) | ||
|
||
if reflect.DeepEqual(result, expectedPrice) == false { | ||
t.Errorf("get %v and expected %v", result, expectedPrice) | ||
} | ||
}) | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run("calculate time an arrival", func(t *testing.T) { | ||
result := AnterAja.CalculateTimeOfArrival(testCase.distance) | ||
|
||
expectedTime := createAnterAjaTimeArrival(t, testCase.distance) | ||
|
||
if result != expectedTime { | ||
t.Errorf("error mean code err or paramter msiign argument get %v and expected %v", result, expectedTime) | ||
} | ||
|
||
}) | ||
} | ||
|
||
t.Run("when distance return an hour", func(t *testing.T) { | ||
result := AnterAja.CalculateTimeOfArrival(30) | ||
|
||
if result != 1 { | ||
t.Errorf("error time is must one hour get %v", result) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package provider | ||
|
||
import ( | ||
"mock-shipping-provider/primitive" | ||
"mock-shipping-provider/repository" | ||
) | ||
|
||
type JNT struct { | ||
Rate primitive.Rate | ||
} | ||
|
||
func NewJntCalculation(jntRate *primitive.Rate) repository.ProviderCalculation { | ||
return &JNT{ | ||
Rate: primitive.Rate{ | ||
PerKilogram: jntRate.PerKilogram, | ||
PerKilometer: jntRate.PerKilometer, | ||
PerCmCubic: jntRate.PerCmCubic, | ||
KilometerPerHour: jntRate.KilometerPerHour, | ||
}, | ||
} | ||
} | ||
|
||
func (jnt *JNT) CalculatePrice(distance float64, dimension primitive.Dimension, weight float64) int64 { | ||
volume := dimension.Width * dimension.Height * dimension.Depth | ||
|
||
distanceCost := distance * float64(jnt.Rate.PerKilometer) | ||
|
||
weightCost := weight * float64(jnt.Rate.PerKilogram) | ||
|
||
volumeCost := volume * float64(jnt.Rate.PerCmCubic) | ||
|
||
return int64(distanceCost + weightCost + volumeCost) | ||
} | ||
|
||
func (jnt *JNT) CalculateTimeOfArrival(distance float64) int64 { | ||
if distance < float64(jnt.Rate.KilometerPerHour) { | ||
return 1 | ||
} | ||
return int64(distance) / jnt.Rate.KilometerPerHour | ||
} |
Oops, something went wrong.