Skip to content

Instantly share code, notes, and snippets.

@riftrsps
Created September 1, 2019 02:11
Show Gist options
  • Save riftrsps/222d1fe60af2d83612422b339c9a1ce3 to your computer and use it in GitHub Desktop.
Save riftrsps/222d1fe60af2d83612422b339c9a1ce3 to your computer and use it in GitHub Desktop.
Item Price Parser
class Item {
private final int id;
Item(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return id == item.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return Integer.toString(id);
}
}
class ItemPrice {
final Item item;
private final int price;
private final int other;
ItemPrice(Item item, int price, int other) {
this.item = item;
this.price = price;
this.other = other;
}
static ItemPrice parsePrice(String[] lineParts) {
Item item = new Item(Integer.parseInt(lineParts[0]));
int price = Integer.parseInt(lineParts[1]);
int other = Integer.parseInt(lineParts[2]);
return new ItemPrice(item, price, other);
}
@Override
public String toString() {
return item + " " + price + " " + 0;
}
}
public class Main {
public static void main(String[] args) throws IOException {
PriceRipper.rip("prices.txt", "itemdefs.txt", "newprices.txt");
}
}
class PriceRipper {
static void rip(String currentPriceFilePath, String newItemDefinitionPath, String outputPath) throws IOException {
List<ItemPrice> oldPrices = readPriceFile(currentPriceFilePath);
List<ItemPrice> newPrices = readDefinionFile(newItemDefinitionPath);
List<ItemPrice> resultPrices = filter(oldPrices, newPrices);
writeNewPriceFile(outputPath, resultPrices);
}
private static void writeNewPriceFile(String filePath, List<ItemPrice> resultPrices) throws IOException {
List<String> outputLines = resultPrices
.stream()
.map(Object::toString)
.collect(Collectors.toList());
Files.write(Paths.get(filePath), outputLines);
}
private static List<ItemPrice> filter(List<ItemPrice> oldPrices, List<ItemPrice> newPrices) {
Set<Item> supportedItems = oldPrices
.stream()
.map(oldPrice -> oldPrice.item)
.collect(Collectors.toSet());
List<ItemPrice> filteredNewPrices = new ArrayList<>(newPrices);
filteredNewPrices.removeIf(newPrice -> !supportedItems.contains(newPrice.item));
return filteredNewPrices;
}
private static List<ItemPrice> readPriceFile(String filePath) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
return parsePrices(reader.lines());
}
}
private static List<ItemPrice> parsePrices(Stream<String> lines){
return lines
.map(s -> s.split("\\s+"))
.map(ItemPrice::parsePrice)
.collect(Collectors.toList());
}
private static List<ItemPrice> readDefinionFile(String filePath) throws IOException {
return parseDefinitions(Files.readAllLines(Paths.get(filePath)));
}
private static List<ItemPrice> parseDefinitions(List<String> lines) throws IOException {
List<String> defLines = Files.readAllLines(Paths.get("itemdefs.txt"));
List<ItemPrice> defPrices = new ArrayList<>();
for(int i = 0; i < defLines.size(); i++) {
String idLine = defLines.get(i);
if(!idLine.startsWith("Id:"))
continue;
String priceLine = defLines.get(i + 2);
if(!priceLine.startsWith("Price:"))
throw new IllegalStateException("Price does not appear 2 lines down from ID "+idLine+" as expected.");
String idValue = idLine.substring(idLine.indexOf(' ') + 1);
String priceValue = priceLine.substring(priceLine.indexOf(' ') + 1);
Item item = new Item(Integer.parseInt(idValue));
int price = Integer.parseInt(priceValue);
defPrices.add(new ItemPrice(item, price, 0));
}
return defPrices;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment