-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40d36ec
commit 2e7ad5a
Showing
4 changed files
with
130 additions
and
0 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,33 @@ | ||
package protect.card_locker; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public class Companies { | ||
static HashMap<String, List<Company>> companies = new HashMap<>(); | ||
|
||
public List<Company> getByISOCode(String isoCode) { | ||
switch (isoCode) { | ||
case "NL": | ||
return NLCompanies(); | ||
default: | ||
throw new IllegalArgumentException(isoCode + " is not supported"); | ||
} | ||
} | ||
|
||
public List<Company> NLCompanies() { | ||
List<Company> comps = new ArrayList<>(); | ||
|
||
if (!companies.containsKey("NL")) { | ||
comps.add(new Company.Builder("Albert Heijn").addBarcodeFormat(BarcodeFormat.EAN_13).create()); | ||
comps.add(new Company.Builder("Air Miles").addBarcodeFormat(BarcodeFormat.EAN_13).addPrefix("470").create()); | ||
comps.add(new Company.Builder("HEMA").addBarcodeFormat(BarcodeFormat.QR_CODE).create()); | ||
companies.put("NL", comps); | ||
} | ||
|
||
return comps; | ||
}; | ||
} |
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,67 @@ | ||
package protect.card_locker; | ||
|
||
import com.google.zxing.BarcodeFormat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import protect.card_locker.translationRules.AddPrefixTranslationRule; | ||
import protect.card_locker.translationRules.TranslationRule; | ||
|
||
public class Company { | ||
private final String name; | ||
private final List<BarcodeFormat> barcodeFormats; | ||
private final List<TranslationRule> translationRuleList; | ||
|
||
private Company(final Builder builder) { | ||
name = builder.name; | ||
barcodeFormats = builder.barcodeFormats; | ||
translationRuleList = builder.translationRuleList; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String cardIDToBarcode(String cardID) { | ||
String barcode = cardID; | ||
for (int i = 0; i < translationRuleList.size(); i++) { | ||
barcode = translationRuleList.get(i).apply(barcode); | ||
} | ||
|
||
return barcode; | ||
} | ||
|
||
public String BarcodeToCardID(String barcode) { | ||
String cardID = barcode; | ||
for (int i = translationRuleList.size() - 1; i > 0; i--) { | ||
cardID = translationRuleList.get(i).undo(cardID); | ||
} | ||
|
||
return cardID; | ||
} | ||
|
||
static class Builder { | ||
private final String name; | ||
private final List<BarcodeFormat> barcodeFormats = new ArrayList<>(); | ||
private final List<TranslationRule> translationRuleList = new ArrayList<>(); | ||
|
||
public Builder(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Builder addBarcodeFormat(final BarcodeFormat barcodeFormat) { | ||
barcodeFormats.add(barcodeFormat); | ||
return this; | ||
} | ||
|
||
public Builder addPrefix(final String prefix) { | ||
translationRuleList.add(new AddPrefixTranslationRule(prefix)); | ||
return this; | ||
} | ||
|
||
public Company create() { | ||
return new Company(this); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/protect/card_locker/translationRules/AddPrefixTranslationRule.java
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,23 @@ | ||
package protect.card_locker.translationRules; | ||
|
||
public class AddPrefixTranslationRule implements TranslationRule { | ||
String prefix; | ||
|
||
public AddPrefixTranslationRule(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
@Override | ||
public String apply(String value) { | ||
return this.prefix + value; | ||
} | ||
|
||
@Override | ||
public String undo(String value) { | ||
if (!value.startsWith(this.prefix)) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return value.substring(this.prefix.length()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/protect/card_locker/translationRules/TranslationRule.java
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,7 @@ | ||
package protect.card_locker.translationRules; | ||
|
||
public interface TranslationRule { | ||
String apply(String value); | ||
String undo(String value); | ||
} | ||
|