From 5aaae5d7face63cd7f3c335ea0c6dda0149365ff Mon Sep 17 00:00:00 2001 From: Abhinav Tiwari <57756500+abhi9tiwari@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:13:22 +0530 Subject: [PATCH 1/5] initial commit --- MovieDistribution.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 MovieDistribution.java diff --git a/MovieDistribution.java b/MovieDistribution.java new file mode 100644 index 000000000..ff8de1fc3 --- /dev/null +++ b/MovieDistribution.java @@ -0,0 +1,4 @@ +package PACKAGE_NAME; + +public class MovieDistribution { +} From 1de464ca373d182d840d652c95905057bfcd386c Mon Sep 17 00:00:00 2001 From: Abhinav Tiwari <57756500+abhi9tiwari@users.noreply.github.com> Date: Fri, 25 Oct 2024 01:42:07 +0530 Subject: [PATCH 2/5] Final code for QubeCinema Challenge --- MovieDistribution.java | 4 - QubeCinema/Distributor.java | 92 ++++++++++++++ QubeCinema/MovieDistribution.java | 191 ++++++++++++++++++++++++++++++ QubeCinema/Region.java | 27 +++++ 4 files changed, 310 insertions(+), 4 deletions(-) delete mode 100644 MovieDistribution.java create mode 100644 QubeCinema/Distributor.java create mode 100644 QubeCinema/MovieDistribution.java create mode 100644 QubeCinema/Region.java diff --git a/MovieDistribution.java b/MovieDistribution.java deleted file mode 100644 index ff8de1fc3..000000000 --- a/MovieDistribution.java +++ /dev/null @@ -1,4 +0,0 @@ -package PACKAGE_NAME; - -public class MovieDistribution { -} diff --git a/QubeCinema/Distributor.java b/QubeCinema/Distributor.java new file mode 100644 index 000000000..ed59c4081 --- /dev/null +++ b/QubeCinema/Distributor.java @@ -0,0 +1,92 @@ +package QubeCinema; + +import java.util.ArrayList; +import java.util.List; + +// Class representing a distributor with included and excluded regions +class Distributor { + String name; + List includedRegions; + List excludedRegions; + + public Distributor(String name) { + this.name = name; + this.includedRegions = new ArrayList<>(); + this.excludedRegions = new ArrayList<>(); + } + + // For adding regions we wnt to include in list. + public void addIncludeRegion(Region region) { + this.includedRegions.add(region); + } + + // For adding regions we want to exclude in list. + public void addExcludeRegion(Region region) { + this.excludedRegions.add(region); + } + + // Check if the distributor can distribute in the given region + public boolean canDistribute(Region queryRegion) { + // First checking if the region is excludded + for (Region excluded : excludedRegions) { + if (queryRegion.isSubsetOf(excluded)) return false; + } + // Then check if the region is included + for (Region included : includedRegions) { + if (queryRegion.isSubsetOf(included)) return true; + } + return false; + } + + // Assign part of this distributors permissions to another distributor + public void assignPermissions(Distributor otherDistributor, Region includeRegion, Region excludeRegion) { + // Check that the assigned regions are present in the parent disttributor. + boolean isValidInclude = false; + for (Region included : includedRegions) { + if (includeRegion.isSubsetOf(included)) { + isValidInclude = true; + break; + } + } + if (!isValidInclude) { + System.out.println("ERROR: Invalid inclusion. Cannot assign a region that is not part of " + this.name + "'s permissions."); + return; + } + + //same for excluding regions + boolean isValidExclude = false; + if (excludeRegion != null) { + for (Region excluded : excludedRegions) { + if (excludeRegion.isSubsetOf(excluded)) { + isValidExclude = true; + break; + } + } + if (!isValidExclude) { + System.out.println("ERROR: Invalid exclusion. Cannot assign an excluded region that is not part of " + this.name + "'s permissions."); + return; + } + } + + // If valid, assign the regions to the child distributor + otherDistributor.addIncludeRegion(includeRegion); + if (excludeRegion != null) { + otherDistributor.addExcludeRegion(excludeRegion); + } + System.out.println("Successfully assigned permissions from " + this.name + " to " + otherDistributor.name); + } + + // Print the distributor's regions for display purposes + public void printDistributorDetails() { + System.out.println("Distributor: " + name); + System.out.println("Included Regions:"); + for (Region included : includedRegions) { + System.out.println(" - " + included); + } + System.out.println("Excluded Regions:"); + for (Region excluded : excludedRegions) { + System.out.println(" - " + excluded); + } + System.out.println(); + } +} \ No newline at end of file diff --git a/QubeCinema/MovieDistribution.java b/QubeCinema/MovieDistribution.java new file mode 100644 index 000000000..812bb0c81 --- /dev/null +++ b/QubeCinema/MovieDistribution.java @@ -0,0 +1,191 @@ +package QubeCinema; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + +public class MovieDistribution { + // Map to store regions for quick lookup by their code (city-province-country) + private static Map regionMap = new HashMap<>(); + private static Map distributorMap = new HashMap<>(); + + public static void main(String[] args) { + // Load region data from the CSV file + loadCSVData(); + + // Set up default distributors (optional) + Distributor distributor1 = new Distributor("DISTRIBUTOR1"); + distributor1.addIncludeRegion(new Region(null, null, "IN")); // Include India + distributor1.addIncludeRegion(new Region(null, null, "US")); // Include United States + distributor1.addExcludeRegion(new Region(null, "KA", "IN")); // Exclude Karnataka, India + distributor1.addExcludeRegion(new Region("CHENNAI", "TN", "IN")); // Exclude Chennai, Tamil Nadu, India + distributorMap.put("DISTRIBUTOR1", distributor1); + + // Scanner for dynamic input + Scanner scanner = new Scanner(System.in); + + // Show distributors at the beginning + showDistributors(); + + while (true) { + System.out.println("\nEnter a command (1: Query Specific Distributor, 2: Assign Permissions, 3: Add Distributor, 4: Show Distributors, 5: List Distributors, 6: Exit):"); + int command = scanner.nextInt(); + scanner.nextLine(); // consume the newline + + if (command == 1) { + querySpecificDistributor(scanner); + } else if (command == 2) { + assignPermissions(scanner); + } else if (command == 3) { + addDistributor(scanner); + } else if (command == 4) { + showDistributors(); + } else if (command == 5) { + listDistributors(); + } else if (command == 6) { + break; + } else { + System.out.println("Invalid command."); + } + } + + scanner.close(); + } + + // Function to query for a specific distributor + private static void querySpecificDistributor(Scanner scanner) { + System.out.println("Enter the distributor's name:"); + String distributorName = scanner.nextLine().trim().toUpperCase(); + + Distributor distributor = distributorMap.get(distributorName); + if (distributor == null) { + System.out.println("Distributor not found."); + return; + } + + System.out.println("Enter the region code to check (format: CITY-PROVINCE-COUNTRY):"); + String regionCode = scanner.nextLine().trim().toUpperCase(); + Region queryRegion = regionMap.get(regionCode); + if (queryRegion == null) { + System.out.println("Invalid region code!"); + return; + } + + boolean canDistribute = distributor.canDistribute(queryRegion); + System.out.println("Can " + distributorName + " distribute in " + queryRegion + "? " + (canDistribute ? "YES" : "NO")); + } + + // Function to add a new distributor dynamically + private static void addDistributor(Scanner scanner) { + System.out.println("Enter the new distributor's name:"); + String distributorName = scanner.nextLine().trim().toUpperCase(); + + if (distributorMap.containsKey(distributorName)) { + System.out.println("Distributor already exists!"); + return; + } + + Distributor newDistributor = new Distributor(distributorName); + + System.out.println("Add regions to INCLUDE for " + distributorName + " (Enter regions in format CITY-PROVINCE-COUNTRY, type DONE when finished):"); + while (true) { + String regionCode = scanner.nextLine().trim().toUpperCase(); + if (regionCode.equals("DONE")) break; + + Region region = regionMap.get(regionCode); + if (region == null) { + System.out.println("Invalid region code! Try again."); + } else { + newDistributor.addIncludeRegion(region); + } + } + + System.out.println("Add regions to EXCLUDE for " + distributorName + "Enter regions in format CITY-PROVINCE-COUNTRY, type DONE when finished):"); + while (true) { + String regionCode = scanner.nextLine().trim().toUpperCase(); + if (regionCode.equals("DONE")) break; + + Region region = regionMap.get(regionCode); + if (region == null) { + System.out.println("Invalid region code! Try again."); + } else { + newDistributor.addExcludeRegion(region); + } + } + + distributorMap.put(distributorName, newDistributor); + System.out.println("Distributor " + distributorName + " added successfully."); + } + + // Function to assign permissions from one distributor to another + private static void assignPermissions(Scanner scanner) { + System.out.println("Enter the assigning distributor's name:"); + String assigningDistributorName = scanner.nextLine().trim().toUpperCase(); + Distributor assigningDistributor = distributorMap.get(assigningDistributorName); + + if (assigningDistributor == null) { + System.out.println("Distributor not found!"); + return; + } + + System.out.println("Enter the receiving distributor's name:"); + String receivingDistributorName = scanner.nextLine().trim().toUpperCase(); + Distributor receivingDistributor = distributorMap.get(receivingDistributorName); + + if (receivingDistributor == null) { + System.out.println("Distributor not found!"); + return; + } + + System.out.println("Enter the region to include (format: CITY-PROVINCE-COUNTRY):"); + String includeRegionCode = scanner.nextLine().trim().toUpperCase(); + Region includeRegion = regionMap.get(includeRegionCode); + + if (includeRegion == null) { + System.out.println("Invalid include region!"); + return; + } + + System.out.println("Enter the region to exclude (optional, format: CITY-PROVINCE-COUNTRY, or press ENTER to skip):"); + String excludeRegionCode = scanner.nextLine().trim().toUpperCase(); + Region excludeRegion = excludeRegionCode.isEmpty() ? null : regionMap.get(excludeRegionCode); + + assigningDistributor.assignPermissions(receivingDistributor, includeRegion, excludeRegion); + } + + // Function to display all distributors and their regions + private static void showDistributors() { + for (Distributor distributor : distributorMap.values()) { + distributor.printDistributorDetails(); + } + } + + // Function to display the list of distributor names only + private static void listDistributors() { + System.out.println("List of Distributors:"); + for (String distributorName : distributorMap.keySet()) { + System.out.println(" - " + distributorName); + } + } + + // Load CSV data and map regions + private static void loadCSVData() { + try (BufferedReader br = new BufferedReader(new FileReader("D:/cities.csv"))) { + String line; + while ((line = br.readLine()) != null) { + String[] values = line.split(","); + String cityCode = values[0].toUpperCase(); + String provinceCode = values[1].toUpperCase(); + String countryCode = values[2].toUpperCase(); + Region region = new Region(cityCode, provinceCode, countryCode); + regionMap.put(region.toString(), region); + } + System.out.println("Region data loaded successfully."); + } catch (IOException e) { + System.err.println("Error reading the CSV file: " + e.getMessage()); + } + } +} diff --git a/QubeCinema/Region.java b/QubeCinema/Region.java new file mode 100644 index 000000000..a8fc99776 --- /dev/null +++ b/QubeCinema/Region.java @@ -0,0 +1,27 @@ +package QubeCinema; + +// Class representing a region (city, state, country) +class Region { + String cityCode; + String provinceCode; + String countryCode; + + // Checks if the current region is a subset of another region + // When parent assigns child disttributor a region we will use to check if region comes under parent country. + public boolean isSubsetOf(Region other) { + if (!this.countryCode.equals(other.countryCode)) return false; + if (other.provinceCode != null && !this.provinceCode.equals(other.provinceCode)) return false; + return other.cityCode == null || this.cityCode.equals(other.cityCode); + } + + @Override + public String toString() { + return cityCode + "-" + provinceCode + "-" + countryCode; + } + + public Region(String cityCode, String provinceCode, String countryCode) { + this.cityCode = cityCode; + this.provinceCode = provinceCode; + this.countryCode = countryCode; + } +} From 5baf129285afc52522a0857b941f8430c7f20647 Mon Sep 17 00:00:00 2001 From: Abhinav Tiwari <57756500+abhi9tiwari@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:25:06 +0530 Subject: [PATCH 3/5] GO Changes --- QubeCinema/Distributor.java | 92 -------------- QubeCinema/MovieDistribution.java | 191 ---------------------------- QubeCinema/Region.java | 27 ---- QubeCinema/go.mod | 3 + QubeCinema/main.go | 18 +++ QubeCinema/models/distributor.go | 35 +++++ QubeCinema/repository/csv_loader.go | 35 +++++ QubeCinema/services/permissions.go | 32 +++++ QubeCinema/utils/cli.go | 67 ++++++++++ 9 files changed, 190 insertions(+), 310 deletions(-) delete mode 100644 QubeCinema/Distributor.java delete mode 100644 QubeCinema/MovieDistribution.java delete mode 100644 QubeCinema/Region.java create mode 100644 QubeCinema/go.mod create mode 100644 QubeCinema/main.go create mode 100644 QubeCinema/models/distributor.go create mode 100644 QubeCinema/repository/csv_loader.go create mode 100644 QubeCinema/services/permissions.go create mode 100644 QubeCinema/utils/cli.go diff --git a/QubeCinema/Distributor.java b/QubeCinema/Distributor.java deleted file mode 100644 index ed59c4081..000000000 --- a/QubeCinema/Distributor.java +++ /dev/null @@ -1,92 +0,0 @@ -package QubeCinema; - -import java.util.ArrayList; -import java.util.List; - -// Class representing a distributor with included and excluded regions -class Distributor { - String name; - List includedRegions; - List excludedRegions; - - public Distributor(String name) { - this.name = name; - this.includedRegions = new ArrayList<>(); - this.excludedRegions = new ArrayList<>(); - } - - // For adding regions we wnt to include in list. - public void addIncludeRegion(Region region) { - this.includedRegions.add(region); - } - - // For adding regions we want to exclude in list. - public void addExcludeRegion(Region region) { - this.excludedRegions.add(region); - } - - // Check if the distributor can distribute in the given region - public boolean canDistribute(Region queryRegion) { - // First checking if the region is excludded - for (Region excluded : excludedRegions) { - if (queryRegion.isSubsetOf(excluded)) return false; - } - // Then check if the region is included - for (Region included : includedRegions) { - if (queryRegion.isSubsetOf(included)) return true; - } - return false; - } - - // Assign part of this distributors permissions to another distributor - public void assignPermissions(Distributor otherDistributor, Region includeRegion, Region excludeRegion) { - // Check that the assigned regions are present in the parent disttributor. - boolean isValidInclude = false; - for (Region included : includedRegions) { - if (includeRegion.isSubsetOf(included)) { - isValidInclude = true; - break; - } - } - if (!isValidInclude) { - System.out.println("ERROR: Invalid inclusion. Cannot assign a region that is not part of " + this.name + "'s permissions."); - return; - } - - //same for excluding regions - boolean isValidExclude = false; - if (excludeRegion != null) { - for (Region excluded : excludedRegions) { - if (excludeRegion.isSubsetOf(excluded)) { - isValidExclude = true; - break; - } - } - if (!isValidExclude) { - System.out.println("ERROR: Invalid exclusion. Cannot assign an excluded region that is not part of " + this.name + "'s permissions."); - return; - } - } - - // If valid, assign the regions to the child distributor - otherDistributor.addIncludeRegion(includeRegion); - if (excludeRegion != null) { - otherDistributor.addExcludeRegion(excludeRegion); - } - System.out.println("Successfully assigned permissions from " + this.name + " to " + otherDistributor.name); - } - - // Print the distributor's regions for display purposes - public void printDistributorDetails() { - System.out.println("Distributor: " + name); - System.out.println("Included Regions:"); - for (Region included : includedRegions) { - System.out.println(" - " + included); - } - System.out.println("Excluded Regions:"); - for (Region excluded : excludedRegions) { - System.out.println(" - " + excluded); - } - System.out.println(); - } -} \ No newline at end of file diff --git a/QubeCinema/MovieDistribution.java b/QubeCinema/MovieDistribution.java deleted file mode 100644 index 812bb0c81..000000000 --- a/QubeCinema/MovieDistribution.java +++ /dev/null @@ -1,191 +0,0 @@ -package QubeCinema; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Scanner; - -public class MovieDistribution { - // Map to store regions for quick lookup by their code (city-province-country) - private static Map regionMap = new HashMap<>(); - private static Map distributorMap = new HashMap<>(); - - public static void main(String[] args) { - // Load region data from the CSV file - loadCSVData(); - - // Set up default distributors (optional) - Distributor distributor1 = new Distributor("DISTRIBUTOR1"); - distributor1.addIncludeRegion(new Region(null, null, "IN")); // Include India - distributor1.addIncludeRegion(new Region(null, null, "US")); // Include United States - distributor1.addExcludeRegion(new Region(null, "KA", "IN")); // Exclude Karnataka, India - distributor1.addExcludeRegion(new Region("CHENNAI", "TN", "IN")); // Exclude Chennai, Tamil Nadu, India - distributorMap.put("DISTRIBUTOR1", distributor1); - - // Scanner for dynamic input - Scanner scanner = new Scanner(System.in); - - // Show distributors at the beginning - showDistributors(); - - while (true) { - System.out.println("\nEnter a command (1: Query Specific Distributor, 2: Assign Permissions, 3: Add Distributor, 4: Show Distributors, 5: List Distributors, 6: Exit):"); - int command = scanner.nextInt(); - scanner.nextLine(); // consume the newline - - if (command == 1) { - querySpecificDistributor(scanner); - } else if (command == 2) { - assignPermissions(scanner); - } else if (command == 3) { - addDistributor(scanner); - } else if (command == 4) { - showDistributors(); - } else if (command == 5) { - listDistributors(); - } else if (command == 6) { - break; - } else { - System.out.println("Invalid command."); - } - } - - scanner.close(); - } - - // Function to query for a specific distributor - private static void querySpecificDistributor(Scanner scanner) { - System.out.println("Enter the distributor's name:"); - String distributorName = scanner.nextLine().trim().toUpperCase(); - - Distributor distributor = distributorMap.get(distributorName); - if (distributor == null) { - System.out.println("Distributor not found."); - return; - } - - System.out.println("Enter the region code to check (format: CITY-PROVINCE-COUNTRY):"); - String regionCode = scanner.nextLine().trim().toUpperCase(); - Region queryRegion = regionMap.get(regionCode); - if (queryRegion == null) { - System.out.println("Invalid region code!"); - return; - } - - boolean canDistribute = distributor.canDistribute(queryRegion); - System.out.println("Can " + distributorName + " distribute in " + queryRegion + "? " + (canDistribute ? "YES" : "NO")); - } - - // Function to add a new distributor dynamically - private static void addDistributor(Scanner scanner) { - System.out.println("Enter the new distributor's name:"); - String distributorName = scanner.nextLine().trim().toUpperCase(); - - if (distributorMap.containsKey(distributorName)) { - System.out.println("Distributor already exists!"); - return; - } - - Distributor newDistributor = new Distributor(distributorName); - - System.out.println("Add regions to INCLUDE for " + distributorName + " (Enter regions in format CITY-PROVINCE-COUNTRY, type DONE when finished):"); - while (true) { - String regionCode = scanner.nextLine().trim().toUpperCase(); - if (regionCode.equals("DONE")) break; - - Region region = regionMap.get(regionCode); - if (region == null) { - System.out.println("Invalid region code! Try again."); - } else { - newDistributor.addIncludeRegion(region); - } - } - - System.out.println("Add regions to EXCLUDE for " + distributorName + "Enter regions in format CITY-PROVINCE-COUNTRY, type DONE when finished):"); - while (true) { - String regionCode = scanner.nextLine().trim().toUpperCase(); - if (regionCode.equals("DONE")) break; - - Region region = regionMap.get(regionCode); - if (region == null) { - System.out.println("Invalid region code! Try again."); - } else { - newDistributor.addExcludeRegion(region); - } - } - - distributorMap.put(distributorName, newDistributor); - System.out.println("Distributor " + distributorName + " added successfully."); - } - - // Function to assign permissions from one distributor to another - private static void assignPermissions(Scanner scanner) { - System.out.println("Enter the assigning distributor's name:"); - String assigningDistributorName = scanner.nextLine().trim().toUpperCase(); - Distributor assigningDistributor = distributorMap.get(assigningDistributorName); - - if (assigningDistributor == null) { - System.out.println("Distributor not found!"); - return; - } - - System.out.println("Enter the receiving distributor's name:"); - String receivingDistributorName = scanner.nextLine().trim().toUpperCase(); - Distributor receivingDistributor = distributorMap.get(receivingDistributorName); - - if (receivingDistributor == null) { - System.out.println("Distributor not found!"); - return; - } - - System.out.println("Enter the region to include (format: CITY-PROVINCE-COUNTRY):"); - String includeRegionCode = scanner.nextLine().trim().toUpperCase(); - Region includeRegion = regionMap.get(includeRegionCode); - - if (includeRegion == null) { - System.out.println("Invalid include region!"); - return; - } - - System.out.println("Enter the region to exclude (optional, format: CITY-PROVINCE-COUNTRY, or press ENTER to skip):"); - String excludeRegionCode = scanner.nextLine().trim().toUpperCase(); - Region excludeRegion = excludeRegionCode.isEmpty() ? null : regionMap.get(excludeRegionCode); - - assigningDistributor.assignPermissions(receivingDistributor, includeRegion, excludeRegion); - } - - // Function to display all distributors and their regions - private static void showDistributors() { - for (Distributor distributor : distributorMap.values()) { - distributor.printDistributorDetails(); - } - } - - // Function to display the list of distributor names only - private static void listDistributors() { - System.out.println("List of Distributors:"); - for (String distributorName : distributorMap.keySet()) { - System.out.println(" - " + distributorName); - } - } - - // Load CSV data and map regions - private static void loadCSVData() { - try (BufferedReader br = new BufferedReader(new FileReader("D:/cities.csv"))) { - String line; - while ((line = br.readLine()) != null) { - String[] values = line.split(","); - String cityCode = values[0].toUpperCase(); - String provinceCode = values[1].toUpperCase(); - String countryCode = values[2].toUpperCase(); - Region region = new Region(cityCode, provinceCode, countryCode); - regionMap.put(region.toString(), region); - } - System.out.println("Region data loaded successfully."); - } catch (IOException e) { - System.err.println("Error reading the CSV file: " + e.getMessage()); - } - } -} diff --git a/QubeCinema/Region.java b/QubeCinema/Region.java deleted file mode 100644 index a8fc99776..000000000 --- a/QubeCinema/Region.java +++ /dev/null @@ -1,27 +0,0 @@ -package QubeCinema; - -// Class representing a region (city, state, country) -class Region { - String cityCode; - String provinceCode; - String countryCode; - - // Checks if the current region is a subset of another region - // When parent assigns child disttributor a region we will use to check if region comes under parent country. - public boolean isSubsetOf(Region other) { - if (!this.countryCode.equals(other.countryCode)) return false; - if (other.provinceCode != null && !this.provinceCode.equals(other.provinceCode)) return false; - return other.cityCode == null || this.cityCode.equals(other.cityCode); - } - - @Override - public String toString() { - return cityCode + "-" + provinceCode + "-" + countryCode; - } - - public Region(String cityCode, String provinceCode, String countryCode) { - this.cityCode = cityCode; - this.provinceCode = provinceCode; - this.countryCode = countryCode; - } -} diff --git a/QubeCinema/go.mod b/QubeCinema/go.mod new file mode 100644 index 000000000..89465f7db --- /dev/null +++ b/QubeCinema/go.mod @@ -0,0 +1,3 @@ +module QUBECINEMA + +go 1.23.2 diff --git a/QubeCinema/main.go b/QubeCinema/main.go new file mode 100644 index 000000000..bdbf64961 --- /dev/null +++ b/QubeCinema/main.go @@ -0,0 +1,18 @@ +// main.go +package main + +import ( + "QubeCinema/repository" + "QubeCinema/utils" + "fmt" +) + +func main() { + if err := repository.LoadCSV("path/to/your/cities.csv"); err != nil { + fmt.Println("Error loading locations:", err) + return + } + + utils.InitDistributors() + utils.DisplayMenu() +} diff --git a/QubeCinema/models/distributor.go b/QubeCinema/models/distributor.go new file mode 100644 index 000000000..7a9a14a53 --- /dev/null +++ b/QubeCinema/models/distributor.go @@ -0,0 +1,35 @@ +// models/distributor.go +package models + +type Distributor struct { + Name string + Parent *Distributor + Includes []string + Excludes []string + Children []*Distributor +} + +func (d *Distributor) AddInclude(location string) { + d.Includes = append(d.Includes, location) +} + +func (d *Distributor) AddExclude(location string) { + d.Excludes = append(d.Excludes, location) +} + +func (d *Distributor) HasPermission(location string) bool { + for _, excl := range d.Excludes { + if location == excl { + return false + } + } + for _, incl := range d.Includes { + if location == incl { + return true + } + } + if d.Parent != nil { + return d.Parent.HasPermission(location) + } + return false +} diff --git a/QubeCinema/repository/csv_loader.go b/QubeCinema/repository/csv_loader.go new file mode 100644 index 000000000..5bcfde696 --- /dev/null +++ b/QubeCinema/repository/csv_loader.go @@ -0,0 +1,35 @@ +// repository/csv_loader.go +package repository + +import ( + "encoding/csv" + "errors" + "os" + "strings" +) + +var ValidLocations = make(map[string]bool) + +func LoadCSV(filePath string) error { + file, err := os.Open(filePath) + if err != nil { + return errors.New("unable to open CSV file") + } + defer file.Close() + + reader := csv.NewReader(file) + records, err := reader.ReadAll() + if err != nil { + return errors.New("failed to read CSV data") + } + + for _, record := range records { + location := strings.Join(record, "-") + ValidLocations[location] = true + } + return nil +} + +func ValidateLocation(location string) bool { + return ValidLocations[location] +} diff --git a/QubeCinema/services/permissions.go b/QubeCinema/services/permissions.go new file mode 100644 index 000000000..3c44d79ef --- /dev/null +++ b/QubeCinema/services/permissions.go @@ -0,0 +1,32 @@ +// services/permissions.go +package services + +import ( + "QubeCinema/models" + "fmt" +) + +var Distributors = make(map[string]*models.Distributor) + +func AssignPermissions(distributor *models.Distributor, includes []string, excludes []string) { + for _, loc := range includes { + distributor.AddInclude(loc) + } + for _, loc := range excludes { + distributor.AddExclude(loc) + } + fmt.Printf("Permissions updated for %s\n", distributor.Name) +} + +func QueryDistributor(name, location string) { + dist, exists := Distributors[name] + if !exists { + fmt.Println("Distributor not found.") + return + } + if dist.HasPermission(location) { + fmt.Println("YES") + } else { + fmt.Println("NO") + } +} diff --git a/QubeCinema/utils/cli.go b/QubeCinema/utils/cli.go new file mode 100644 index 000000000..35b8b64bc --- /dev/null +++ b/QubeCinema/utils/cli.go @@ -0,0 +1,67 @@ +// utils/cli.go +package utils + +import ( + "QUBECINEMA/models" + "QUBECINEMA/services" + "fmt" + "strings" +) + +func InitDistributors() { + distributor1 := &models.Distributor{Name: "DISTRIBUTOR1"} + distributor2 := &models.Distributor{Name: "DISTRIBUTOR2", Parent: distributor1} + + distributor1.Includes = []string{"INDIA", "UNITEDSTATES"} + distributor1.Excludes = []string{"KARNATAKA-INDIA", "CHENNAI-TAMILNADU-INDIA"} + distributor2.Includes = []string{"INDIA"} + distributor2.Excludes = []string{"TAMILNADU-INDIA"} + + services.Distributors["DISTRIBUTOR1"] = distributor1 + services.Distributors["DISTRIBUTOR2"] = distributor2 +} + +func DisplayMenu() { + var choice int + for { + fmt.Println("\n1: Query Distributor") + fmt.Println("2: Assign Permissions") + fmt.Println("3: List Distributors") + fmt.Println("4: Exit") + fmt.Print("Choose an option: ") + fmt.Scan(&choice) + + switch choice { + case 1: + var name, location string + fmt.Print("Enter distributor name: ") + fmt.Scan(&name) + fmt.Print("Enter location: ") + fmt.Scan(&location) + services.QueryDistributor(name, location) + case 2: + var name, includes, excludes string + fmt.Print("Enter distributor name: ") + fmt.Scan(&name) + fmt.Print("Enter includes (comma-separated): ") + fmt.Scan(&includes) + fmt.Print("Enter excludes (comma-separated): ") + fmt.Scan(&excludes) + + if dist, ok := services.Distributors[name]; ok { + services.AssignPermissions(dist, strings.Split(includes, ","), strings.Split(excludes, ",")) + } else { + fmt.Println("Distributor not found.") + } + case 3: + for name := range services.Distributors { + fmt.Println(name) + } + case 4: + fmt.Println("Exiting.") + return + default: + fmt.Println("Invalid choice.") + } + } +} From 3004178cd02b732b1697648a950c31c68dc379bd Mon Sep 17 00:00:00 2001 From: Abhinav Tiwari <57756500+abhi9tiwari@users.noreply.github.com> Date: Fri, 1 Nov 2024 18:35:32 +0530 Subject: [PATCH 4/5] final --- QubeCinema/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/QubeCinema/main.go b/QubeCinema/main.go index bdbf64961..e9b8b7af7 100644 --- a/QubeCinema/main.go +++ b/QubeCinema/main.go @@ -2,13 +2,13 @@ package main import ( - "QubeCinema/repository" - "QubeCinema/utils" + "QUBECINEMA/repository" + "QUBECINEMA/utils" "fmt" ) func main() { - if err := repository.LoadCSV("path/to/your/cities.csv"); err != nil { + if err := repository.LoadCSV("D:/cities.csv"); err != nil { fmt.Println("Error loading locations:", err) return } From 664d7d10938aaffb0aa170160ab41a35dc927b8f Mon Sep 17 00:00:00 2001 From: Abhinav Tiwari <57756500+abhi9tiwari@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:30:07 +0530 Subject: [PATCH 5/5] Final Go Code for the challenge --- QubeCinema/main.go | 1 + QubeCinema/models/distributor.go | 3 +++ QubeCinema/services/permissions.go | 4 +++- QubeCinema/utils/cli.go | 3 ++- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/QubeCinema/main.go b/QubeCinema/main.go index e9b8b7af7..702489100 100644 --- a/QubeCinema/main.go +++ b/QubeCinema/main.go @@ -8,6 +8,7 @@ import ( ) func main() { + //loading csv file if err := repository.LoadCSV("D:/cities.csv"); err != nil { fmt.Println("Error loading locations:", err) return diff --git a/QubeCinema/models/distributor.go b/QubeCinema/models/distributor.go index 7a9a14a53..2aab44a1c 100644 --- a/QubeCinema/models/distributor.go +++ b/QubeCinema/models/distributor.go @@ -9,14 +9,17 @@ type Distributor struct { Children []*Distributor } +// func to add regions to include func (d *Distributor) AddInclude(location string) { d.Includes = append(d.Includes, location) } +//func to add regions to exclude func (d *Distributor) AddExclude(location string) { d.Excludes = append(d.Excludes, location) } +// checking for permission in a particular location func (d *Distributor) HasPermission(location string) bool { for _, excl := range d.Excludes { if location == excl { diff --git a/QubeCinema/services/permissions.go b/QubeCinema/services/permissions.go index 3c44d79ef..154f2361b 100644 --- a/QubeCinema/services/permissions.go +++ b/QubeCinema/services/permissions.go @@ -2,12 +2,13 @@ package services import ( - "QubeCinema/models" + "QUBECINEMA/models" "fmt" ) var Distributors = make(map[string]*models.Distributor) +// to assign permision from parent to child distributor func AssignPermissions(distributor *models.Distributor, includes []string, excludes []string) { for _, loc := range includes { distributor.AddInclude(loc) @@ -18,6 +19,7 @@ func AssignPermissions(distributor *models.Distributor, includes []string, exclu fmt.Printf("Permissions updated for %s\n", distributor.Name) } +// query for distributor func QueryDistributor(name, location string) { dist, exists := Distributors[name] if !exists { diff --git a/QubeCinema/utils/cli.go b/QubeCinema/utils/cli.go index 35b8b64bc..306c2764c 100644 --- a/QubeCinema/utils/cli.go +++ b/QubeCinema/utils/cli.go @@ -1,4 +1,3 @@ -// utils/cli.go package utils import ( @@ -9,6 +8,7 @@ import ( ) func InitDistributors() { + // adding pre loaded distributors distributor1 := &models.Distributor{Name: "DISTRIBUTOR1"} distributor2 := &models.Distributor{Name: "DISTRIBUTOR2", Parent: distributor1} @@ -24,6 +24,7 @@ func InitDistributors() { func DisplayMenu() { var choice int for { + //cli fmt.Println("\n1: Query Distributor") fmt.Println("2: Assign Permissions") fmt.Println("3: List Distributors")