+1 (315) 557-6473 

Program That Implements Hash Maps Using Java Programming Language Assignment Solution.


Instructions

Objective
Write a java assignment program to implement hash maps using programming language.

Requirements and Specifications

Implement hash maps using Java programming language

Source Code

package reports;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.MessageFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.*;

public class ReportsMain {

    private static final String INPUT_FOLDER = "Resources";

    private static final String OUTPUT_FOLDER = "Reports";

    private static final String INPUT_FILENAME = "TimeDataMedium";

    private static final String CLIENT_OUTPUT_PREFIX = "Client";

    private static final String EMPLOYEE_OUTPUT_PREFIX = "Employee";

    public static void main(String[] args) {

        Map employees = new HashMap<>();

        Map> clients = new HashMap<>();

        try (Scanner scanner = new Scanner(new File(INPUT_FOLDER + "/" + INPUT_FILENAME + ".csv"))) {

            scanner.nextLine();

            while (scanner.hasNextLine()) {

                String line = scanner.nextLine().trim();

                String[] parts = line.split(",");

                String employee = parts[0].trim();

                String info = parts[1].trim();

                String client = parts[3].trim();

                if (!client.isEmpty()) {

                    Set singleEmployee = new HashSet<>();

                    singleEmployee.add(employee);

                    clients.merge(client, singleEmployee, (a, b) -> {

                        a.addAll(b);

                        return a;

                    });

                }

                if (info.equals("Clock In/Out")) {

                    long minutes = getMinutes(parts[4], parts[5], parts[6], parts[7]);

                    employees.merge(employee, minutes, Long::sum);

                }

            }

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

        printClientReport(clients);

        printEmployeeReport(employees);

    }

    private static void printClientReport(Map> clients) {

        try (PrintWriter writer = new PrintWriter(OUTPUT_FOLDER + "/" + CLIENT_OUTPUT_PREFIX + INPUT_FILENAME + ".txt")) {

            List clientList = new ArrayList<>(clients.keySet());

            Collections.sort(clientList);

            for (String client : clientList) {

                List employees = new ArrayList<>(clients.get(client));

                Collections.sort(employees);

                writer.write(MessageFormat.format("CLIENT: {0} --- EMPLOYEES: [{1}]" + System.lineSeparator(),

                        client, String.join(", ", employees)));

            }

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

    }

    private static void printEmployeeReport(Map employees) {

        try (PrintWriter writer = new PrintWriter(OUTPUT_FOLDER + "/" + EMPLOYEE_OUTPUT_PREFIX + INPUT_FILENAME + ".txt")) {

            List employeeList = new ArrayList<>(employees.keySet());

            Collections.sort(employeeList);

            for (String employee : employeeList) {

                Long minutes = employees.get(employee);

                int hours = (int)(minutes/60);

                int mins = (int)(minutes%60);

                String formatted = String.format("%02d:%02d", hours, mins);

                writer.write(MessageFormat.format("EMPLOYEE: {0} --- TIME: {1}" + System.lineSeparator(),

                        employee, formatted));

            }

        } catch (IOException e) {

            throw new RuntimeException(e);

        }

    }

    private static long getMinutes(String startTime, String endTime, String startDate, String endDate) {

        SimpleDateFormat format = new SimpleDateFormat("hh:mm a dd/MM/yyyy");

        Date start = null;

        Date end = null;

        try {

            start = format.parse(startTime + " " + startDate);

            end = format.parse(endTime + " " + endDate);

        } catch (ParseException e) {

            e.printStackTrace();

            return 0;

        }

        long diff = end.getTime() - start.getTime();

        return diff / (60 * 1000);

    }

}