diff --git a/app/src/main/java/protect/card_locker/Companies.java b/app/src/main/java/protect/card_locker/Companies.java new file mode 100644 index 0000000000..2113cc6815 --- /dev/null +++ b/app/src/main/java/protect/card_locker/Companies.java @@ -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> companies = new HashMap<>(); + + public List getByISOCode(String isoCode) { + switch (isoCode) { + case "NL": + return NLCompanies(); + default: + throw new IllegalArgumentException(isoCode + " is not supported"); + } + } + + public List NLCompanies() { + List 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; + }; +} diff --git a/app/src/main/java/protect/card_locker/Company.java b/app/src/main/java/protect/card_locker/Company.java new file mode 100644 index 0000000000..b4bcbeaf01 --- /dev/null +++ b/app/src/main/java/protect/card_locker/Company.java @@ -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 barcodeFormats; + private final List 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 barcodeFormats = new ArrayList<>(); + private final List 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); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/translationRules/AddPrefixTranslationRule.java b/app/src/main/java/protect/card_locker/translationRules/AddPrefixTranslationRule.java new file mode 100644 index 0000000000..c4cc72d6af --- /dev/null +++ b/app/src/main/java/protect/card_locker/translationRules/AddPrefixTranslationRule.java @@ -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()); + } +} diff --git a/app/src/main/java/protect/card_locker/translationRules/TranslationRule.java b/app/src/main/java/protect/card_locker/translationRules/TranslationRule.java new file mode 100644 index 0000000000..ae3f6665d4 --- /dev/null +++ b/app/src/main/java/protect/card_locker/translationRules/TranslationRule.java @@ -0,0 +1,7 @@ +package protect.card_locker.translationRules; + +public interface TranslationRule { + String apply(String value); + String undo(String value); +} +