Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.metadata/
Binary file added OutputSS.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Qube/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions Qube/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions Qube/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Qube</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions Qube/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
14 changes: 14 additions & 0 deletions Qube/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
74 changes: 74 additions & 0 deletions Qube/src/com/challenge/qube/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.challenge.qube;
import java.util.*;
public class Config { // Config is used to store all regions for which a distributor has requested access to
Map<String, Boolean> isRegionAuthorized;
List<String> includeRegionsList;
List<String> excludeRegionsList;

Config() {
isRegionAuthorized = Initializer.getIsRegionAuthorizedMap();
includeRegionsList = new ArrayList<>();
excludeRegionsList = new ArrayList<>();
}

public void include(String region) { // INCLUDE regions
includeRegionsList.add(region);
}

public void exclude(String region) { //EXCLUDE regions
excludeRegionsList.add(region);
}

public void build() { // Config map is built here based on the include and exclude criteria
for(String region : includeRegionsList) {
String components[] = region.split(",");
if(components.length==1) {
authorize(components[0], true); // If just countryCode is provided then values of all regions(keys) with that countryCode get updated here
}
else if(components.length==2) {
authorize(components[0], components[1], true); // If just stateCode and countryCode are provided then values of all regions(keys) with that stateCode and countryCode get updated here
}
else {
authorize(components[0], components[1], components[2], true); // If cityCode, stateCode and countryCode are provided then value of that regions(key) with that cityCode, stateCode and countryCode gets updated here
}
}

for(String region : excludeRegionsList) {
String components[] = region.split(",");
if(components.length==1) {
authorize(components[0], false); // If just countryCode is provided then values of all regions(keys) with that countryCode get updated here
}
else if(components.length==2) {
authorize(components[0], components[1], false); // If just stateCode and countryCode are provided then values of all regions(keys) with that stateCode and countryCode get updated here
}
else {
authorize(components[0], components[1], components[2], false); // If cityCode, stateCode and countryCode are provided then value of that regions(key) with that cityCode, stateCode and countryCode gets updated here
}
}
}

private void authorize(String countryCode, boolean flag) { // If just countryCode is provided then values of all regions(keys) with that countryCode get updated here
for(String region : isRegionAuthorized.keySet()) {
if(region.endsWith(","+countryCode)) {
isRegionAuthorized.put(region, flag);
}
}
}

private void authorize(String stateCode, String countryCode, boolean flag) { // If just stateCode and countryCode are provided then values of all regions(keys) with that stateCode and countryCode get updated here
for(String region : isRegionAuthorized.keySet()) {
if(region.endsWith(","+stateCode+","+countryCode)) {
isRegionAuthorized.put(region, flag);
}
}
}

private void authorize(String cityCode, String stateCode, String countryCode, boolean flag) { // If cityCode, stateCode and countryCode are provided then value of that regions(key) with that cityCode, stateCode and countryCode gets updated here
for(String region : isRegionAuthorized.keySet()) {
if(region.endsWith(cityCode+","+stateCode+","+countryCode)) {
isRegionAuthorized.put(region, flag);
}
}
}

}
36 changes: 36 additions & 0 deletions Qube/src/com/challenge/qube/Distributor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.challenge.qube;
import java.util.*;

public class Distributor {
Map<String, Boolean> isAuthorized;

Distributor() {
isAuthorized = Initializer.getIsRegionAuthorizedMap(); // Get the default region map with all values set to false
}

public boolean isAuthorized(String region) {
return isAuthorized.get(region);
}

public void getAuthorizedByAdmin(Config config) { // Used to authorize a distributor by and Admin with all regions rights
Map<String, Boolean> configIsRegionAuthorizedMap = config.isRegionAuthorized;
for(String region : config.isRegionAuthorized.keySet()) {
if(configIsRegionAuthorizedMap.get(region)) { //If a region is set to true in config then set the value of region to true for this distributor
this.isAuthorized.put(region, true);
}
}
}

public void authorizeSubDistributor(Distributor subDistributor, Config config) { //Used to authorize regions for a sub distributor
Map<String, Boolean> configIsRegionAuthorizedMap = config.isRegionAuthorized;
Map<String, Boolean> subDistributorIsRegionAuthorizedMap = subDistributor.isAuthorized;

for(String region : config.isRegionAuthorized.keySet()) {
if(configIsRegionAuthorizedMap.get(region) && this.isAuthorized.get(region)) { //If a region is set to true in both config and this distributor then set the value of region to true for the sub distributor
subDistributorIsRegionAuthorizedMap.put(region, true);
}
}
}


}
26 changes: 26 additions & 0 deletions Qube/src/com/challenge/qube/Initializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.challenge.qube;
import java.util.*;
import java.io.*;
public class Initializer {

public static Map<String, Boolean> getIsRegionAuthorizedMap() {
Map<String, Boolean> isRegionAuthorized = new HashMap<>();

try (InputStream is = Initializer.class.getResourceAsStream("cities.csv");
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
line = br.readLine();
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length >= 3) {
String key = parts[0].trim() + "," + parts[1].trim() + "," + parts[2].trim(); // Key is generated by concatenating the cityCode, stateCode and countryCode
isRegionAuthorized.put(key, false); // By default, all regions in the map are marked false(unauthorized)
}
}
} catch (IOException e) {
e.printStackTrace();
}

return isRegionAuthorized;
}
}
49 changes: 49 additions & 0 deletions Qube/src/com/challenge/qube/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.challenge.qube;

public class Main {

public static void main(String[] args) {
Distributor DISTRIBUTOR1 = new Distributor();
Config config1 = new Config();
config1.include("IN"); //INCLUDE: INDIA
config1.include("US"); //INCLUDE: UNITEDSTATES
config1.exclude("KA,IN"); //EXCLUDE: KARNATAKA-INDIA
config1.exclude("CENAI,TN,IN"); //EXCLUDE: CHENNAI-TAMILNADU-INDIA
config1.build();
DISTRIBUTOR1.getAuthorizedByAdmin(config1);

System.out.println("For DISTRIBUTOR1 :");
System.out.println("Is Chicago(CHIAO,IL,US) authorized?");
System.out.println(DISTRIBUTOR1.isAuthorized("CHIAO,IL,US") ? "Yes" : "No"); // Returns Yes for CHICAGO-ILLINOIS-UNITEDSTATES
System.out.println("Is Chennai(CENAI,TN,IN) authorized?");
System.out.println(DISTRIBUTOR1.isAuthorized("CENAI,TN,IN") ? "Yes" : "No"); // Returns No for CENAI,TN,IN

System.out.println();
System.out.println("For DISTRIBUTOR2 :");
Distributor DISTRIBUTOR2 = new Distributor();
Config config2 = new Config();
config2.include("IN");
config2.exclude("TN,IN");
config2.build();
DISTRIBUTOR1.authorizeSubDistributor(DISTRIBUTOR2, config2);
System.out.println("Is Chicago(CHIAO,IL,US) authorized?");
System.out.println(DISTRIBUTOR2.isAuthorized("CHIAO,IL,US") ? "Yes" : "No"); // Returns No for CENAI,TN,IN
System.out.println("Is Chennai(CENAI,TN,IN) authorized?");
System.out.println(DISTRIBUTOR2.isAuthorized("CENAI,TN,IN") ? "Yes" : "No"); // Returns No for CENAI,TN,IN
System.out.println("Is Mumbai(MUBAI,MH,IN) authorized?");
System.out.println(DISTRIBUTOR2.isAuthorized("MUBAI,MH,IN") ? "Yes" : "No"); // Returns Yes for MUBAI,MH,IN

System.out.println();

Distributor DISTRIBUTOR3 = new Distributor();
Config config3 = new Config();
config3.include("HBALI,KA,IN");
config3.build();
DISTRIBUTOR2.authorizeSubDistributor(DISTRIBUTOR3, config3);
System.out.println("For DISTRIBUTOR3 :");
System.out.println("Is Hubli(HBALI,KA,IN) authorized?");
System.out.println(DISTRIBUTOR3.isAuthorized("HBALI,KA,IN") ? "Yes" : "No"); // Returns No for HBALI,KA,IN
}


}
Loading