Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- import person.Author;
- import biblio.Bibliography;
- import biblio.Entry;
- import biblio.Query;
- import csv.CSV;
- public class Main {
- private static final String FILTER_TYPE = "filter_type";
- private static final String FILTER_VALUE = "filter_value";
- private static final String FORMAT = "format_string";
- private static final String AUTHOR_VALUE = "author";
- private static final String PUBLISHER_VALUE = "publisher";
- public static void main(String[] args) {
- final CSV csv = readCSV();
- final Bibliography bibliography = new Bibliography(csv);
- final Map<String, String> properties = loadProperties(args);
- final Query query = getQuery(properties);
- if (query != null) {
- bibliography.filter(query);
- }
- final String result = bibliography.show(parseFormat(properties
- .get(FORMAT)));
- printResult(result);
- }
- private static void printResult(String result) {
- try (final PrintWriter writer = new PrintWriter(System.out)) {
- writer.print(result);
- writer.flush();
- }
- }
- private static Query getQuery(Map<String, String> properties) {
- Query query;
- String filterType = properties.get(FILTER_TYPE);
- String filterValue = properties.get(FILTER_VALUE);
- if (PUBLISHER_VALUE.equals(filterType)) {
- return Query.byPublisher(filterValue);
- } else if (AUTHOR_VALUE.equals(filterType)) {
- final Author author = Author.make(filterValue);
- if (author != null) {
- return Query.byAuthor(author);
- }
- }
- return null;
- }
- private static CSV readCSV() {
- try (final Scanner scanner = new Scanner(System.in)) {
- return CSV.read(scanner);
- }
- }
- private static Map<String, String> loadProperties(String[] args) {
- final Map<String, String> properties = new HashMap<>();
- for (int i = 0; i < args.length; i = i + 2) {
- final String key = args[i];
- final String value = args[i + 1];
- if ("publisher=".equals(key)) {
- properties.put(FILTER_TYPE, PUBLISHER_VALUE);
- properties.put(FILTER_VALUE, value);
- } else if ("author=".equals(key)) {
- properties.put(FILTER_TYPE, AUTHOR_VALUE);
- properties.put(FILTER_VALUE, value);
- } else if ("format=".equals(key)) {
- properties.put(FORMAT, value);
- }
- }
- return properties;
- }
- private static int parseFormat(String formatString) {
- if (formatString == null) {
- return Entry.FORMAT_RAW;
- }
- switch (formatString) {
- case "authorYear":
- return Entry.FORMAT_AUTHOR_YEAR;
- case "authorYearCompact":
- return Entry.FORMAT_AUTHOR_YEAR_COMPACT;
- default:
- return Entry.FORMAT_RAW;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement