Skip to content

Commit

Permalink
Initial companies list
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastProject committed Feb 1, 2021
1 parent 40d36ec commit 2e7ad5a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/src/main/java/protect/card_locker/Companies.java
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;
};
}
67 changes: 67 additions & 0 deletions app/src/main/java/protect/card_locker/Company.java
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);
}
}
}
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());
}
}
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);
}

0 comments on commit 2e7ad5a

Please sign in to comment.