From 3ca4e45455001723210ce05820e8a400c6ca2fa0 Mon Sep 17 00:00:00 2001 From: raj1944 Date: Thu, 24 Oct 2024 18:15:07 +0530 Subject: [PATCH] Challenge 2016 --- go.mod | 3 + internal/distributor/distributor.go | 40 ++++++++++ internal/distributor/service.go | 120 ++++++++++++++++++++++++++++ main.go | 90 +++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 go.mod create mode 100644 internal/distributor/distributor.go create mode 100644 internal/distributor/service.go create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 000000000..188b2ac91 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module challenge2016 + +go 1.23.2 diff --git a/internal/distributor/distributor.go b/internal/distributor/distributor.go new file mode 100644 index 000000000..e8e544ed2 --- /dev/null +++ b/internal/distributor/distributor.go @@ -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 +} diff --git a/internal/distributor/service.go b/internal/distributor/service.go new file mode 100644 index 000000000..435473745 --- /dev/null +++ b/internal/distributor/service.go @@ -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("#################################################################################") + } +} diff --git a/main.go b/main.go new file mode 100644 index 000000000..06086ccb0 --- /dev/null +++ b/main.go @@ -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(®ion) + + 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 + } + } +}