Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Scanner;
- import datamodel.IResult;
- import datamodel.MeasurementRecord;
- import mainengine.IMainEngine;
- import mainengine.MainEngineFactory;
- public class Client {
- private static Scanner scanner;
- private static IMainEngine mainEngine;
- private static MainEngineFactory factory;
- private static ArrayList<MeasurementRecord> objCollection;
- private static IResult result;
- static private String inputFilename;
- static private String outputFilename;
- static private String delimeter;
- static private Boolean hasHeader;
- static private int numFields;
- private static String description;
- private static ArrayList<String> history = new ArrayList<String>();
- public static void main(String[] args) {
- scanner = new Scanner(System.in);
- factory = new MainEngineFactory();
- mainEngine = factory.createMainEngine("MainEngine");
- objCollection = new ArrayList<MeasurementRecord>();
- String input;
- do {
- System.out.println("LOAD <file path>");
- System.out.println("BUILD <aggregator type> <aggregator function>");
- System.out.println("REPORT <type> <file path>");
- System.out.println("HISTORY");
- System.out.println("EXIT");
- System.out.print("Select option: ");
- input = scanner.nextLine();
- if (input.toLowerCase().startsWith("load")) {
- if (input.contains(" ")) {
- inputFilename = input.replace(input.split(" ")[0]+" ", "");
- File file = new File(inputFilename);
- if (file.exists()) {
- System.out.print("Enter delimeter: ");
- delimeter = scanner.nextLine();
- System.out.print("Does file contain header line (true/false): ");
- hasHeader = scanner.nextBoolean();
- System.out.print("Fields number: ");
- numFields = scanner.nextInt();
- scanner.nextLine();
- mainEngine.loadData(inputFilename, delimeter, hasHeader, numFields, objCollection);
- } else {
- System.out.println("File doesnt exist");
- }
- } else {
- System.out.println("Missing input. Please enter path to load file");
- }
- } else if (input.toLowerCase().startsWith("build")) {
- if (input.contains(" ")) {
- String aggregatorType = input.split(" ")[1].toLowerCase();
- String aggregatorFunc = input.split(" ")[2].toLowerCase();
- if ((aggregatorType.equals("season") || aggregatorType.equals("month") || aggregatorType.equals("dayofweek") || aggregatorType.equals("period"))) {
- if ((aggregatorFunc.equals("avg") || aggregatorFunc.equals("sum"))) {
- System.out.print("Enter description: ");
- description = scanner.nextLine();
- result = mainEngine.aggregateByTimeUnit(objCollection, aggregatorType, aggregatorFunc, description);
- } else {
- System.out.println("Invalid input. Aggregator function should be avg or sum.");
- }
- } else {
- System.out.println("Invalid input. Aggregator type should be season, month, dayofweek or period.");
- }
- } else {
- System.out.println("Missing input. Please enter required fields");
- }
- } else if (input.toLowerCase().startsWith("report")) {
- if (input.contains(" ")) {
- String type = input.split(" ")[1].toLowerCase();
- if ((type.equals("md") || type.equals("txt") || type.equals("html"))) {
- outputFilename = input.replace(input.split(" ")[0]+" "+input.split(" ")[1]+" ", "");
- mainEngine.reportResultInFile(result, type, outputFilename);
- history.add(description+", "+outputFilename);
- } else {
- System.out.println("Invalid input. Please use md, html or txt for report type.");
- }
- } else {
- System.out.println("Missing input. Please enter required fields");
- }
- } else if (input.toLowerCase().equals("history")) {
- if (history.size()==0) {
- System.out.println("Histroy is empty");
- } else {
- for (String str : history) {
- System.out.println(str);
- }
- System.out.println("\nPress enter to continue...");
- scanner.nextLine();
- }
- } else if (input.toLowerCase().equals("exit")) {
- System.out.println("Exiting application.");
- } else if (!input.toLowerCase().equals("")) {
- System.out.println("Invalid command.");
- }
- System.out.println("\n");
- }
- while (!input.toLowerCase().equals("exit"));
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement