Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge 2016 #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module challenge2016

go 1.23.2
40 changes: 40 additions & 0 deletions internal/distributor/distributor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package distributor

import "strings"

type Distributor struct {
Name string
Includes []string
Excludes []string
SubDistributor []*Distributor
}

func NewDistributor(name string, includes, excludes []string) *Distributor {
return &Distributor{
Name: name,
Includes: includes,
Excludes: excludes,
}
}

func (d *Distributor) AddSubDistributor(sub *Distributor) {
d.SubDistributor = append(d.SubDistributor, sub)
}

func (d *Distributor) HasPermission(region string) bool {
region = strings.ToUpper(region)

for _, exclude := range d.Excludes {
if strings.HasSuffix(region, exclude) {
return false
}
}

for _, include := range d.Includes {
if strings.HasSuffix(region, include) {
return true
}
}

return false
}
120 changes: 120 additions & 0 deletions internal/distributor/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package distributor

import (
"fmt"
"strings"
)

var Distributors []*Distributor

func GetDistributorByName(name string) *Distributor {
for _, distributor := range Distributors {
if name == distributor.Name {
return distributor
}
}
return nil
}

func AddDistributor(distributor *Distributor) {
if CheckDistributor(distributor) {
Distributors = append(Distributors, distributor)
} else {
fmt.Println("Location is already assigned to other distributor")
}
}

func AddDistributorAsSubDistributor(distributor *Distributor, parentDistribotorName string) {
var parentDistributor = GetDistributorByName(parentDistribotorName)
if parentDistributor == nil {
fmt.Println("No Distributor found with name ", parentDistribotorName)
return
}

for _, include := range distributor.Includes {
if !parentDistributor.HasPermission(include) {
fmt.Println("Perent Distributor has no permission for location ", include)
return
}
}

if !CheckSubDistributor(distributor, parentDistributor) {
fmt.Println("Location is already assigned to other sub distributor")
}
parentDistributor.AddSubDistributor(distributor)
Distributors = append(Distributors, distributor)
}

func CheckDistributor(newDistributor *Distributor) bool {
for _, distributor := range Distributors {
for _, newInclude := range newDistributor.Includes {
for _, exclude := range distributor.Excludes {
if strings.HasSuffix(newInclude, exclude) {
return true
}
}

for _, include := range distributor.Includes {
if strings.HasSuffix(newInclude, include) {
return false
}
}
}
}
return true
}

func CheckSubDistributor(newDistributor *Distributor, parentDistributor *Distributor) bool {
for _, distributor := range parentDistributor.SubDistributor {
for _, newInclude := range newDistributor.Includes {
for _, exclude := range distributor.Excludes {
if strings.HasSuffix(newInclude, exclude) {
fmt.Println(newInclude, exclude)
return true
}
}

for _, include := range distributor.Includes {
if strings.HasSuffix(newInclude, include) {
return false
}
}
}
}
return true
}

func CheckDistributorPermission(distributorName string, regionName string) {
var distributor = GetDistributorByName(distributorName)
fmt.Print("Distribution Permission: ")
if distributor.HasPermission(regionName) {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}

func PrintAllDistributor() {
fmt.Println("#################################################################################")
for _, distributor := range Distributors {
fmt.Println("Name: ", distributor.Name)

fmt.Print("Includes: ")
for _, include := range distributor.Includes {
fmt.Print(include, ", ")
}
fmt.Println()

fmt.Print("Excludes: ")
for _, exclude := range distributor.Excludes {
fmt.Print(exclude, ", ")
}
fmt.Println()
fmt.Print("Sub Distributor: ")
for _, distributor := range distributor.SubDistributor {
fmt.Print(distributor.Name, ", ")
}
fmt.Println()
fmt.Println("#################################################################################")
}
}
90 changes: 90 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"bufio"
"challenge2016/internal/distributor"
"fmt"
"os"
"strings"
)

func SplitRegion(input string) []string {
regions := strings.Split(input, " ")
for i, region := range regions {
regions[i] = strings.ToUpper(region)
}
return regions
}

func CreateNewDistributor() {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter Distributor Name")
var name string
fmt.Scanln(&name)
var isSubDistributor string

// var regionsIncludes string
fmt.Println("Enter Include region like region1 region2")
scanner.Scan()
var includes = SplitRegion(scanner.Text())

// var regionsExclude string
fmt.Println("Enter Exclude region like region1 region2")
scanner.Scan()
var excludes = SplitRegion(scanner.Text())

var d = distributor.NewDistributor(name, includes, excludes)

fmt.Println("Is Sub Distributor (enter yes or no)")
fmt.Scanln(&isSubDistributor)
if isSubDistributor == "yes" {
var subDistributorName string
fmt.Println("Enter Perent Distributor Name")
fmt.Scanln(&subDistributorName)
distributor.AddDistributorAsSubDistributor(d, subDistributorName)
} else {
distributor.AddDistributor(d)
}
}

func checkPermission() {
fmt.Println("Enter Distributor Name")
var name string
fmt.Scanln(&name)

var region string
fmt.Println("Enter region name")
fmt.Scanln(&region)

distributor.CheckDistributorPermission(name, region)

}

func main() {
// var d1 = distributor.NewDistributor("DISTRIBUTOR1", []string{"INDIA", "UNITEDSTATES"}, []string{"KARNATAKA-INDIA", "CHENNAI-TAMILNADU-INDIA"})
// var d2 = distributor.NewDistributor("DISTRIBUTOR2", []string{"INDIA"}, []string{"TAMILNADU-INDIA"})
// distributor.AddDistributor(d1)
// distributor.AddDistributorAsSubDistributor(d2, "DISTRIBUTOR1")
// distributor.PrintAllDistributor()
// distributor.CheckDistributorPermission("DISTRIBUTOR2", "CHICAGO-ILLINOIS-UNITEDSTATES")

var num int
for {
fmt.Println("To Create Distributor: 1")
fmt.Println("To Check Permission: 2")
fmt.Println("To Print All Distributor: 3")
fmt.Println("To Exit: 4")

fmt.Scanln(&num)

if num == 1 {
CreateNewDistributor()
} else if num == 2 {
checkPermission()
} else if num == 3 {
distributor.PrintAllDistributor()
} else {
break
}
}
}